|
1 | 1 | package io.github.dmitrysulman.logback.access.reactor.netty |
2 | 2 |
|
3 | 3 | import ch.qos.logback.access.common.joran.JoranConfigurator |
| 4 | +import ch.qos.logback.core.joran.spi.JoranException |
4 | 5 | import ch.qos.logback.core.status.OnConsoleStatusListener |
| 6 | +import ch.qos.logback.core.status.Status |
5 | 7 | import io.github.dmitrysulman.logback.access.reactor.netty.ReactorNettyAccessLogFactory.Companion.CONFIG_FILE_NAME_PROPERTY |
| 8 | +import io.github.dmitrysulman.logback.access.reactor.netty.ReactorNettyAccessLogFactory.Companion.DEFAULT_CONFIG_FILE_NAME |
6 | 9 | import io.github.dmitrysulman.logback.access.reactor.netty.integration.EventCaptureAppender |
7 | 10 | import io.kotest.assertions.throwables.shouldThrowExactly |
8 | | -import io.kotest.matchers.booleans.shouldBeFalse |
9 | 11 | import io.kotest.matchers.booleans.shouldBeTrue |
| 12 | +import io.kotest.matchers.nulls.shouldBeNull |
10 | 13 | import io.kotest.matchers.nulls.shouldNotBeNull |
11 | 14 | import io.kotest.matchers.types.shouldBeTypeOf |
| 15 | +import io.mockk.every |
| 16 | +import io.mockk.mockk |
| 17 | +import io.mockk.spyk |
| 18 | +import io.mockk.verify |
12 | 19 | import org.junit.jupiter.api.Test |
13 | 20 | import java.io.FileNotFoundException |
| 21 | +import java.net.URL |
14 | 22 |
|
15 | 23 | class ReactorNettyAccessLogFactoryTests { |
16 | 24 | @Test |
@@ -96,18 +104,53 @@ class ReactorNettyAccessLogFactoryTests { |
96 | 104 | }.shouldBeTrue() |
97 | 105 | } |
98 | 106 |
|
| 107 | + @Test |
| 108 | + fun `test not existing file url from constructor parameter`() { |
| 109 | + shouldThrowExactly<JoranException> { ReactorNettyAccessLogFactory(URL("file:logback-access-not-exist.xml")) } |
| 110 | + } |
| 111 | + |
99 | 112 | @Test |
100 | 113 | fun `test not existing filename from configuration property`() { |
101 | 114 | System.setProperty(CONFIG_FILE_NAME_PROPERTY, "logback-access-not-exist.xml") |
102 | | - val reactorNettyAccessLogFactory = ReactorNettyAccessLogFactory() |
103 | | - reactorNettyAccessLogFactory.accessContext |
104 | | - .iteratorForAppenders() |
105 | | - .hasNext() |
106 | | - .shouldBeFalse() |
| 115 | + shouldThrowExactly<FileNotFoundException> { ReactorNettyAccessLogFactory() } |
107 | 116 | } |
108 | 117 |
|
109 | 118 | @Test |
110 | 119 | fun `test not existing filename from constructor parameter`() { |
111 | 120 | shouldThrowExactly<FileNotFoundException> { ReactorNettyAccessLogFactory("logback-access-not-exist.xml") } |
112 | 121 | } |
| 122 | + |
| 123 | + @Test |
| 124 | + fun `test not existing default config file`() { |
| 125 | + val reactorNettyAccessLogFactory = spyk<ReactorNettyAccessLogFactory>(recordPrivateCalls = true) |
| 126 | + every { reactorNettyAccessLogFactory["getConfigFromFileName"](DEFAULT_CONFIG_FILE_NAME) } throws FileNotFoundException() |
| 127 | + val getDefaultConfigMethod = reactorNettyAccessLogFactory::class.java.getDeclaredMethod("getDefaultConfig") |
| 128 | + getDefaultConfigMethod.trySetAccessible() |
| 129 | + val defaultConfigUrl = getDefaultConfigMethod.invoke(reactorNettyAccessLogFactory) as URL? |
| 130 | + defaultConfigUrl.shouldBeNull() |
| 131 | + } |
| 132 | + |
| 133 | + @Test |
| 134 | + fun `test initialization without config`() { |
| 135 | + val reactorNettyAccessLogFactory = ReactorNettyAccessLogFactory() |
| 136 | + val joranConfigurator = mockk<JoranConfigurator>(relaxed = true) |
| 137 | + val initializeMethod = |
| 138 | + reactorNettyAccessLogFactory::class.java.getDeclaredMethod( |
| 139 | + "initialize", |
| 140 | + URL::class.java, |
| 141 | + JoranConfigurator::class.java, |
| 142 | + Boolean::class.java, |
| 143 | + ) |
| 144 | + initializeMethod.trySetAccessible() |
| 145 | + initializeMethod.invoke(reactorNettyAccessLogFactory, null, joranConfigurator, false) |
| 146 | + |
| 147 | + verify(exactly = 0) { joranConfigurator.context } |
| 148 | + verify(exactly = 0) { joranConfigurator.doConfigure(any<URL>()) } |
| 149 | + |
| 150 | + reactorNettyAccessLogFactory.accessContext.statusManager.copyOfStatusList |
| 151 | + .any { |
| 152 | + it.effectiveLevel == Status.WARN && |
| 153 | + it.message == "No configuration file provided, skipping configuration" |
| 154 | + }.shouldBeTrue() |
| 155 | + } |
113 | 156 | } |
0 commit comments