Skip to content

Commit 300b01f

Browse files
committed
test: align on using log appender in tests and some cleanup
1 parent 7220404 commit 300b01f

4 files changed

Lines changed: 207 additions & 166 deletions

File tree

grails-core/build.gradle

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -95,21 +95,20 @@ dependencies {
9595
api 'org.slf4j:jcl-over-slf4j'
9696

9797
// Testing
98-
testImplementation 'ch.qos.logback:logback-classic'
98+
testImplementation 'ch.qos.logback:logback-classic' // Used for checking log output in tests
9999
testImplementation 'org.spockframework:spock-core'
100100

101101
testRuntimeOnly 'net.bytebuddy:byte-buddy' // Required by Spock's mocking support
102102
testRuntimeOnly 'org.objenesis:objenesis' // Required by Spock for mocking classes without default constructor
103103
}
104104

105-
TaskProvider<WriteProperties> writeProps = tasks.register('writeGrailsProperties', WriteProperties)
106-
writeProps.configure { WriteProperties it ->
107-
it.destinationFile = project.layout.buildDirectory.file('grails.build.properties')
108-
it.property('grails.version', project.version)
105+
def writeProps= tasks.register('writeGrailsProperties', WriteProperties) {
106+
destinationFile = project.layout.buildDirectory.file('grails.build.properties')
107+
property('grails.version', project.version)
109108
}
110109

111-
tasks.named('processResources', ProcessResources).configure { ProcessResources it ->
112-
it.from writeProps
110+
tasks.named('processResources', ProcessResources) {
111+
from(writeProps)
113112
}
114113

115114
apply {

grails-core/src/test/groovy/grails/plugins/DefaultGrailsPluginManagerSpec.groovy

Lines changed: 81 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,22 @@
1818
*/
1919
package grails.plugins
2020

21+
import ch.qos.logback.classic.Logger
22+
import ch.qos.logback.classic.spi.ILoggingEvent
23+
import ch.qos.logback.core.read.ListAppender
24+
import org.slf4j.LoggerFactory
25+
import spock.lang.Specification
26+
import spock.lang.Unroll
27+
28+
import org.springframework.context.support.GenericApplicationContext
29+
import org.springframework.core.env.StandardEnvironment
30+
2131
import grails.core.DefaultGrailsApplication
2232
import grails.core.GrailsApplication
2333
import org.apache.grails.core.plugins.DefaultPluginDiscovery
24-
import org.apache.grails.core.plugins.PluginInfo
2534
import org.apache.grails.core.plugins.PluginDiscovery
35+
import org.apache.grails.core.plugins.PluginInfo
2636
import org.apache.grails.core.plugins.PluginUtils
27-
import org.springframework.context.support.GenericApplicationContext
28-
import org.springframework.core.env.StandardEnvironment
29-
import spock.lang.Specification
30-
import spock.lang.Unroll
3137

3238
/**
3339
* Test suite for DefaultGrailsPluginManager
@@ -61,7 +67,7 @@ class DefaultGrailsPluginManagerSpec extends Specification {
6167
@Unroll
6268
def "should return #pluginGrailsVersion as plugin grails version"() {
6369
given:
64-
PluginInfo plugin = stubPluginWithGrailsVersion(pluginGrailsVersion)
70+
def plugin = stubPluginWithGrailsVersion(pluginGrailsVersion)
6571

6672
when:
6773
def version = plugin.getGrailsVersionRange()
@@ -71,13 +77,13 @@ class DefaultGrailsPluginManagerSpec extends Specification {
7177

7278
where:
7379
pluginGrailsVersion | _
74-
"3.3.10 > *" | _
80+
'3.3.10 > *' | _
7581
}
7682

7783
@Unroll
7884
def "it should check that plugin with grailsVersion=#pluginGrailsVersion is compatible with grails #grailsVersion"() {
7985
given:
80-
PluginInfo plugin = stubPluginWithGrailsVersion(pluginGrailsVersion)
86+
def plugin = stubPluginWithGrailsVersion(pluginGrailsVersion)
8187

8288
when:
8389
def compatible = plugin.isGrailsVersionCompatible(grailsVersion)
@@ -87,83 +93,96 @@ class DefaultGrailsPluginManagerSpec extends Specification {
8793

8894
where:
8995
grailsVersion | pluginGrailsVersion || expectedCompatible
90-
"1.0" | "3.3.1 > *" || false
91-
"2.5" | "3.0.1" || false
92-
"3.0.0" | "3.3.10 > *" || false
93-
"3.3.10" | "4.0.0 > *" || false
94-
"4.0.1" | "3.0.0.BUILD-SNAPSHOT > *" || true
95-
"4.0.1" | "4.0.1" || true
96-
"4.0.1" | "3.0.1" || false
97-
"4.0.1" | "3.3.1 > *" || true
98-
"4.0.1" | "3.3.10 > *" || true
96+
'1.0' | '3.3.1 > *' || false
97+
'2.5' | '3.0.1' || false
98+
'3.0.0' | '3.3.10 > *' || false
99+
'3.3.10' | '4.0.0 > *' || false
100+
'4.0.1' | '3.0.0.BUILD-SNAPSHOT > *' || true
101+
'4.0.1' | '4.0.1' || true
102+
'4.0.1' | '3.0.1' || false
103+
'4.0.1' | '3.3.1 > *' || true
104+
'4.0.1' | '3.3.10 > *' || true
99105

100106
// Milestone, release candidate and snapshot versions on both the application and the plugin (#14058)
101-
"7.0.0-M2" | "7.0.0-M1 > *" || true
102-
"7.0.0-M1" | "7.0.0-M2 > *" || false
103-
"7.0.0-RC1" | "7.0.0-M1 > *" || true
104-
"7.0.0-M1" | "7.0.0-RC1 > *" || false
105-
"7.0.0" | "7.0.0-RC1 > *" || true
106-
"7.0.0-RC1" | "7.0.0 > *" || false
107-
"7.0.0-SNAPSHOT" | "7.0.0-SNAPSHOT > *" || true
108-
"7.0.5-M1" | "7.0.3 > *" || true
109-
"7.0.0-M1" | "7.0.0-M1" || true
110-
"7.0.0-M2" | "7.0.0-M1" || false
107+
'7.0.0-M2' | '7.0.0-M1 > *' || true
108+
'7.0.0-M1' | '7.0.0-M2 > *' || false
109+
'7.0.0-RC1' | '7.0.0-M1 > *' || true
110+
'7.0.0-M1' | '7.0.0-RC1 > *' || false
111+
'7.0.0' | '7.0.0-RC1 > *' || true
112+
'7.0.0-RC1' | '7.0.0 > *' || false
113+
'7.0.0-SNAPSHOT' | '7.0.0-SNAPSHOT > *' || true
114+
'7.0.5-M1' | '7.0.3 > *' || true
115+
'7.0.0-M1' | '7.0.0-M1' || true
116+
'7.0.0-M2' | '7.0.0-M1' || false
111117
}
112118

113119
def "per-plugin loaded messages are DEBUG and a single INFO summary reports the load order"() {
114120
given: 'a discovery bean with two plugins registered in reverse of their load order'
115121
def gcl = new GroovyClassLoader()
116122
def alphaClass = gcl.parseClass('''
117-
class AlphaProbeGrailsPlugin {
118-
def version = "1.0.0"
119-
}
120-
''')
123+
class AlphaProbeGrailsPlugin {
124+
def version = '1.0.0'
125+
}
126+
''')
121127
def betaClass = gcl.parseClass('''
122-
class BetaProbeGrailsPlugin {
123-
def version = "2.0.0"
124-
def loadAfter = ['alphaProbe']
125-
}
126-
''')
127-
def application = new DefaultGrailsApplication()
128-
application.mainContext = new GenericApplicationContext()
129-
def discovery = new DefaultPluginDiscovery(new Class<?>[]{betaClass, alphaClass})
130-
discovery.loadPluginsFromClasspath = false
131-
132-
and: 'standard error is captured to observe slf4j-simple output'
133-
def originalErr = System.err
134-
def captured = new ByteArrayOutputStream()
135-
System.setErr(new PrintStream(captured, true))
128+
class BetaProbeGrailsPlugin {
129+
def version = '2.0.0'
130+
def loadAfter = ['alphaProbe']
131+
}
132+
''')
133+
def application = new DefaultGrailsApplication(mainContext: new GenericApplicationContext())
134+
def discovery = new DefaultPluginDiscovery([betaClass, alphaClass] as Class<?>[]).tap {
135+
loadPluginsFromClasspath = false
136+
}
137+
138+
and: 'configure a logback appender to capture log messages'
139+
def logger = LoggerFactory.getLogger(DefaultGrailsPluginManager) as Logger
140+
def appender = new ListAppender<ILoggingEvent>().tap { start() }
141+
logger.addAppender(appender)
136142

137143
when:
138144
discovery.init(new StandardEnvironment())
139-
def manager = new DefaultGrailsPluginManager(application, discovery)
140-
manager.loadPlugins()
145+
def manager = new DefaultGrailsPluginManager(application, discovery).tap {
146+
loadPlugins()
147+
}
141148

142149
then: 'both plugins are loaded'
143-
manager.getGrailsPlugin('alphaProbe') != null
144-
manager.getGrailsPlugin('betaProbe') != null
150+
with(manager) {
151+
getGrailsPlugin('alphaProbe') != null
152+
getGrailsPlugin('betaProbe') != null
153+
}
145154

146155
and: 'the per-plugin loaded-successfully messages are not emitted at INFO'
147-
captured.toString().readLines()
148-
.findAll { it.contains('loaded successfully') }
149-
.every { !it.contains('INFO') }
156+
appender.list
157+
.findAll { it.formattedMessage.contains('loaded successfully') }
158+
.every { it.level.toString() != 'INFO' }
150159

151160
and: 'a single INFO summary line reports the plugins in load order'
152-
def summaryLines = captured.toString().readLines()
153-
.findAll { it.contains('Grails plugins in load order') }
161+
def summaryLines = appender.list.findAll {
162+
it.formattedMessage.contains('Grails plugins in load order')
163+
}
154164
summaryLines.size() == 1
155-
summaryLines[0].contains('INFO')
156-
summaryLines[0].contains('Loaded 2 Grails plugins in load order: [alphaProbe (1.0.0), betaProbe (2.0.0)]')
165+
with(summaryLines[0]) {
166+
level.toString() == 'INFO'
167+
formattedMessage.contains('Loaded 2 Grails plugins in load order: [alphaProbe (1.0.0), betaProbe (2.0.0)]')
168+
}
157169

158170
cleanup:
159-
System.setErr(originalErr)
171+
logger.detachAppender(appender)
172+
appender.stop()
160173
}
161174

162-
PluginInfo stubPluginWithGrailsVersion(String grailsVersion) {
175+
private static PluginInfo stubPluginWithGrailsVersion(String grailsVersion) {
163176
def gcl = new GroovyClassLoader()
164-
return PluginUtils.createPluginInfo(gcl.parseClass("class ACustomGrailsPlugin {\n" +
165-
"def version = \"1.0.0\"\n" +
166-
"def grailsVersion = \"$grailsVersion\"\n" +
167-
"}"), null, true)
177+
PluginUtils.createPluginInfo(
178+
gcl.parseClass("""
179+
class ACustomGrailsPlugin {
180+
def version = '1.0.0'
181+
def grailsVersion = "$grailsVersion"
182+
}
183+
"""),
184+
null,
185+
true
186+
)
168187
}
169188
}

0 commit comments

Comments
 (0)