Skip to content

Commit 3177432

Browse files
authored
Merge pull request #112 from akkinoc/fix-issue-110
Fixed a bug that the file system path cannot be specified for the location of the configuration file
2 parents 0c69fb4 + 14199b3 commit 3177432

File tree

5 files changed

+128
-2
lines changed

5 files changed

+128
-2
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ logback.access:
175175
# Defaults to true.
176176
enabled: true
177177
# The location of the configuration file.
178+
# Specify a URL that starts with "classpath:" or "file:".
178179
# Auto-detected by default:
179180
# 1. "classpath:logback-access-test.xml"
180181
# 2. "classpath:logback-access.xml"

src/main/kotlin/dev/akkinoc/spring/boot/logback/access/LogbackAccessContext.kt

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import org.slf4j.Logger
1010
import org.slf4j.LoggerFactory.getLogger
1111
import org.springframework.core.env.Environment
1212
import org.springframework.core.io.ResourceLoader
13+
import org.springframework.util.ResourceUtils.getURL
1314

1415
/**
1516
* The Logback-access context.
@@ -30,7 +31,7 @@ class LogbackAccessContext(
3031
private val raw: AccessContext = AccessContext()
3132

3233
init {
33-
val (name, resource) = properties.config?.let { it to resourceLoader.getResource(it) }
34+
val (name, resource) = properties.config?.let { it to resourceLoader.getResource("${getURL(it)}") }
3435
?: DEFAULT_CONFIGS.asSequence()
3536
.map { it to resourceLoader.getResource(it) }
3637
.firstOrNull { (_, resource) -> resource.exists() }

src/main/kotlin/dev/akkinoc/spring/boot/logback/access/LogbackAccessProperties.kt

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import org.springframework.boot.context.properties.ConstructorBinding
1414
* Whether to enable auto-configuration.
1515
* @property config
1616
* The location of the configuration file.
17+
* Specify a URL that starts with "classpath:" or "file:".
1718
* Auto-detected by default:
1819
* 1. "classpath:logback-access-test.xml"
1920
* 2. "classpath:logback-access.xml"

src/main/resources/META-INF/spring-configuration-metadata.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
"name": "logback.access.config",
3838
"type": "java.lang.String",
3939
"sourceType": "dev.akkinoc.spring.boot.logback.access.LogbackAccessProperties",
40-
"description": "The location of the configuration file. Auto-detected by default: 1. \"classpath:logback-access-test.xml\" 2. \"classpath:logback-access.xml\" 3. \"classpath:logback-access-test-spring.xml\" 4. \"classpath:logback-access-spring.xml\" 5. \"classpath:dev/akkinoc/spring/boot/logback/access/logback-access-spring.xml\""
40+
"description": "The location of the configuration file. Specify a URL that starts with \"classpath:\" or \"file:\". Auto-detected by default: 1. \"classpath:logback-access-test.xml\" 2. \"classpath:logback-access.xml\" 3. \"classpath:logback-access-test-spring.xml\" 4. \"classpath:logback-access-spring.xml\" 5. \"classpath:dev/akkinoc/spring/boot/logback/access/logback-access-spring.xml\""
4141
},
4242
{
4343
"name": "logback.access.local-port-strategy",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
package dev.akkinoc.spring.boot.logback.access
2+
3+
import dev.akkinoc.spring.boot.logback.access.test.assertion.Assertions
4+
import dev.akkinoc.spring.boot.logback.access.test.extension.EventsCapture
5+
import dev.akkinoc.spring.boot.logback.access.test.extension.EventsCaptureExtension
6+
import dev.akkinoc.spring.boot.logback.access.test.type.JettyReactiveWebTest
7+
import dev.akkinoc.spring.boot.logback.access.test.type.JettyServletWebTest
8+
import dev.akkinoc.spring.boot.logback.access.test.type.TomcatReactiveWebTest
9+
import dev.akkinoc.spring.boot.logback.access.test.type.TomcatServletWebTest
10+
import dev.akkinoc.spring.boot.logback.access.test.type.UndertowReactiveWebTest
11+
import dev.akkinoc.spring.boot.logback.access.test.type.UndertowServletWebTest
12+
import io.kotest.matchers.collections.shouldBeSingleton
13+
import io.kotest.matchers.shouldBe
14+
import org.junit.jupiter.api.Test
15+
import org.junit.jupiter.api.extension.ExtendWith
16+
import org.springframework.beans.factory.annotation.Autowired
17+
import org.springframework.boot.test.web.client.TestRestTemplate
18+
import org.springframework.boot.test.web.client.exchange
19+
import org.springframework.http.RequestEntity
20+
import org.springframework.test.context.TestPropertySource
21+
22+
/**
23+
* Tests the case where the location of the configuration file is specified.
24+
*/
25+
@ExtendWith(EventsCaptureExtension::class)
26+
sealed class ConfigurationFileLocationTest {
27+
28+
@Test
29+
fun `Appends a Logback-access event according to the configuration file found`(
30+
@Autowired rest: TestRestTemplate,
31+
capture: EventsCapture,
32+
) {
33+
val request = RequestEntity.get("/mock-controller/text").build()
34+
val response = rest.exchange<String>(request)
35+
response.statusCodeValue.shouldBe(200)
36+
Assertions.assertLogbackAccessEventsEventually { capture.shouldBeSingleton() }
37+
}
38+
39+
}
40+
41+
/**
42+
* Tests the case where the location of the configuration file is specified as a file system path.
43+
*/
44+
@TestPropertySource(properties = ["logback.access.config=target/test-classes/logback-access-test.capture.xml"])
45+
sealed class ConfigurationFilePathTest : ConfigurationFileLocationTest()
46+
47+
/**
48+
* Tests the [ConfigurationFilePathTest] using the Tomcat servlet web server.
49+
*/
50+
@TomcatServletWebTest
51+
class TomcatServletWebConfigurationFilePathTest : ConfigurationFilePathTest()
52+
53+
/**
54+
* Tests the [ConfigurationFilePathTest] using the Tomcat reactive web server.
55+
*/
56+
@TomcatReactiveWebTest
57+
class TomcatReactiveWebConfigurationFilePathTest : ConfigurationFilePathTest()
58+
59+
/**
60+
* Tests the [ConfigurationFilePathTest] using the Jetty servlet web server.
61+
*/
62+
@JettyServletWebTest
63+
class JettyServletWebConfigurationFilePathTest : ConfigurationFilePathTest()
64+
65+
/**
66+
* Tests the [ConfigurationFilePathTest] using the Jetty reactive web server.
67+
*/
68+
@JettyReactiveWebTest
69+
class JettyReactiveWebConfigurationFilePathTest : ConfigurationFilePathTest()
70+
71+
/**
72+
* Tests the [ConfigurationFilePathTest] using the Undertow servlet web server.
73+
*/
74+
@UndertowServletWebTest
75+
class UndertowServletWebConfigurationFilePathTest : ConfigurationFilePathTest()
76+
77+
/**
78+
* Tests the [ConfigurationFilePathTest] using the Undertow reactive web server.
79+
*/
80+
@UndertowReactiveWebTest
81+
class UndertowReactiveWebConfigurationFilePathTest : ConfigurationFilePathTest()
82+
83+
/**
84+
* Tests the case where the location of the configuration file is specified as a file scheme URI.
85+
*/
86+
@TestPropertySource(properties = ["logback.access.config=file:target/test-classes/logback-access-test.capture.xml"])
87+
sealed class ConfigurationFileUriTest : ConfigurationFileLocationTest()
88+
89+
/**
90+
* Tests the [ConfigurationFileUriTest] using the Tomcat servlet web server.
91+
*/
92+
@TomcatServletWebTest
93+
class TomcatServletWebConfigurationFileUriTest : ConfigurationFileUriTest()
94+
95+
/**
96+
* Tests the [ConfigurationFileUriTest] using the Tomcat reactive web server.
97+
*/
98+
@TomcatReactiveWebTest
99+
class TomcatReactiveWebConfigurationFileUriTest : ConfigurationFileUriTest()
100+
101+
/**
102+
* Tests the [ConfigurationFileUriTest] using the Jetty servlet web server.
103+
*/
104+
@JettyServletWebTest
105+
class JettyServletWebConfigurationFileUriTest : ConfigurationFileUriTest()
106+
107+
/**
108+
* Tests the [ConfigurationFileUriTest] using the Jetty reactive web server.
109+
*/
110+
@JettyReactiveWebTest
111+
class JettyReactiveWebConfigurationFileUriTest : ConfigurationFileUriTest()
112+
113+
/**
114+
* Tests the [ConfigurationFileUriTest] using the Undertow servlet web server.
115+
*/
116+
@UndertowServletWebTest
117+
class UndertowServletWebConfigurationFileUriTest : ConfigurationFileUriTest()
118+
119+
/**
120+
* Tests the [ConfigurationFileUriTest] using the Undertow reactive web server.
121+
*/
122+
@UndertowReactiveWebTest
123+
class UndertowReactiveWebConfigurationFileUriTest : ConfigurationFileUriTest()

0 commit comments

Comments
 (0)