Skip to content

Commit 2e81b1d

Browse files
committed
Test that auto configurations can be added by init scripts
When an init command is run, it may add entries to the classpath. This commit adds a test that verifies that, if an entry that is added to the classpath contains a CompilerAutoConfiguration file in META-INF/services, then the CompilerAutoConfigurations declared in it are found by subsequent ServiceLoader.load calls.
1 parent 321ce3a commit 2e81b1d

File tree

4 files changed

+62
-0
lines changed

4 files changed

+62
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright 2012-2013 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package cli.command;
18+
19+
import groovy.lang.GroovyClassLoader;
20+
21+
import org.codehaus.groovy.ast.ClassNode;
22+
import org.codehaus.groovy.classgen.GeneratorContext;
23+
import org.codehaus.groovy.control.CompilationFailedException;
24+
import org.codehaus.groovy.control.SourceUnit;
25+
import org.springframework.boot.cli.compiler.CompilerAutoConfiguration;
26+
import org.springframework.boot.cli.compiler.GroovyCompilerConfiguration;
27+
28+
/**
29+
* @author Andy Wilkinson
30+
*/
31+
public class CustomCompilerAutoConfiguration extends CompilerAutoConfiguration {
32+
33+
@Override
34+
public void apply(GroovyClassLoader loader,
35+
GroovyCompilerConfiguration configuration, GeneratorContext generatorContext,
36+
SourceUnit source, ClassNode classNode) throws CompilationFailedException {
37+
System.out.println("Customising compiler configuration");
38+
}
39+
40+
}

spring-boot-cli/src/test/java/org/springframework/boot/cli/command/InitCommandTests.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,18 @@
1818

1919
import groovy.lang.GroovyClassLoader;
2020

21+
import java.util.ServiceLoader;
22+
2123
import org.junit.After;
2224
import org.junit.Before;
2325
import org.junit.Rule;
2426
import org.junit.Test;
2527
import org.springframework.boot.OutputCapture;
2628
import org.springframework.boot.cli.Command;
2729
import org.springframework.boot.cli.SpringCli;
30+
import org.springframework.boot.cli.compiler.CompilerAutoConfiguration;
31+
32+
import cli.command.CustomCompilerAutoConfiguration;
2833

2934
import static org.junit.Assert.assertTrue;
3035
import static org.mockito.Matchers.any;
@@ -67,6 +72,22 @@ public void initScript() throws Exception {
6772
this.command.run("src/test/resources/grab.groovy");
6873
verify(this.cli, times(this.defaultCount + 1)).register(any(Command.class));
6974
assertTrue(this.output.toString().contains("Hello Grab"));
75+
76+
Iterable<CompilerAutoConfiguration> autoConfigurations = ServiceLoader.load(
77+
CompilerAutoConfiguration.class, Thread.currentThread()
78+
.getContextClassLoader());
79+
80+
boolean foundCustomConfiguration = false;
81+
82+
for (CompilerAutoConfiguration autoConfiguration : autoConfigurations) {
83+
if (CustomCompilerAutoConfiguration.class.getName().equals(
84+
autoConfiguration.getClass().getName())) {
85+
foundCustomConfiguration = true;
86+
break;
87+
}
88+
}
89+
90+
assertTrue(foundCustomConfiguration);
7091
}
7192

7293
@Test
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
cli.command.CustomCompilerAutoConfiguration
Binary file not shown.

0 commit comments

Comments
 (0)