From 2e81b1d0d0924f1a24c3c0dd2d686218cd701f06 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Tue, 7 Jan 2014 11:42:58 +0000 Subject: [PATCH] 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. --- .../CustomCompilerAutoConfiguration.java | 40 ++++++++++++++++++ .../boot/cli/command/InitCommandTests.java | 21 +++++++++ ...oot.cli.compiler.CompilerAutoConfiguration | 1 + .../custom/custom/0.0.1/custom-0.0.1.jar | Bin 2710 -> 3123 bytes 4 files changed, 62 insertions(+) create mode 100644 spring-boot-cli/src/test/java/cli/command/CustomCompilerAutoConfiguration.java create mode 100644 spring-boot-cli/src/test/plugins/custom/META-INF/services/org.springframework.boot.cli.compiler.CompilerAutoConfiguration diff --git a/spring-boot-cli/src/test/java/cli/command/CustomCompilerAutoConfiguration.java b/spring-boot-cli/src/test/java/cli/command/CustomCompilerAutoConfiguration.java new file mode 100644 index 0000000000..7da36c57c0 --- /dev/null +++ b/spring-boot-cli/src/test/java/cli/command/CustomCompilerAutoConfiguration.java @@ -0,0 +1,40 @@ +/* + * Copyright 2012-2013 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package cli.command; + +import groovy.lang.GroovyClassLoader; + +import org.codehaus.groovy.ast.ClassNode; +import org.codehaus.groovy.classgen.GeneratorContext; +import org.codehaus.groovy.control.CompilationFailedException; +import org.codehaus.groovy.control.SourceUnit; +import org.springframework.boot.cli.compiler.CompilerAutoConfiguration; +import org.springframework.boot.cli.compiler.GroovyCompilerConfiguration; + +/** + * @author Andy Wilkinson + */ +public class CustomCompilerAutoConfiguration extends CompilerAutoConfiguration { + + @Override + public void apply(GroovyClassLoader loader, + GroovyCompilerConfiguration configuration, GeneratorContext generatorContext, + SourceUnit source, ClassNode classNode) throws CompilationFailedException { + System.out.println("Customising compiler configuration"); + } + +} diff --git a/spring-boot-cli/src/test/java/org/springframework/boot/cli/command/InitCommandTests.java b/spring-boot-cli/src/test/java/org/springframework/boot/cli/command/InitCommandTests.java index f6020ef3b9..b913bf0ade 100644 --- a/spring-boot-cli/src/test/java/org/springframework/boot/cli/command/InitCommandTests.java +++ b/spring-boot-cli/src/test/java/org/springframework/boot/cli/command/InitCommandTests.java @@ -18,6 +18,8 @@ package org.springframework.boot.cli.command; import groovy.lang.GroovyClassLoader; +import java.util.ServiceLoader; + import org.junit.After; import org.junit.Before; import org.junit.Rule; @@ -25,6 +27,9 @@ import org.junit.Test; import org.springframework.boot.OutputCapture; import org.springframework.boot.cli.Command; import org.springframework.boot.cli.SpringCli; +import org.springframework.boot.cli.compiler.CompilerAutoConfiguration; + +import cli.command.CustomCompilerAutoConfiguration; import static org.junit.Assert.assertTrue; import static org.mockito.Matchers.any; @@ -67,6 +72,22 @@ public class InitCommandTests { this.command.run("src/test/resources/grab.groovy"); verify(this.cli, times(this.defaultCount + 1)).register(any(Command.class)); assertTrue(this.output.toString().contains("Hello Grab")); + + Iterable autoConfigurations = ServiceLoader.load( + CompilerAutoConfiguration.class, Thread.currentThread() + .getContextClassLoader()); + + boolean foundCustomConfiguration = false; + + for (CompilerAutoConfiguration autoConfiguration : autoConfigurations) { + if (CustomCompilerAutoConfiguration.class.getName().equals( + autoConfiguration.getClass().getName())) { + foundCustomConfiguration = true; + break; + } + } + + assertTrue(foundCustomConfiguration); } @Test diff --git a/spring-boot-cli/src/test/plugins/custom/META-INF/services/org.springframework.boot.cli.compiler.CompilerAutoConfiguration b/spring-boot-cli/src/test/plugins/custom/META-INF/services/org.springframework.boot.cli.compiler.CompilerAutoConfiguration new file mode 100644 index 0000000000..c59f47ea9e --- /dev/null +++ b/spring-boot-cli/src/test/plugins/custom/META-INF/services/org.springframework.boot.cli.compiler.CompilerAutoConfiguration @@ -0,0 +1 @@ +cli.command.CustomCompilerAutoConfiguration \ No newline at end of file diff --git a/spring-boot-cli/src/test/plugins/custom/custom/0.0.1/custom-0.0.1.jar b/spring-boot-cli/src/test/plugins/custom/custom/0.0.1/custom-0.0.1.jar index 6623163c9317783856e79ad492f9da94a11c282c..062524811f1debb1be3f9e66a33f10aaf39583fe 100644 GIT binary patch literal 3123 zcmb7`eLT~98^?b&gh(DD$BQ+O-DC6A-SLpZSgo1R)QUs1SQCxSaEP4lI}+8QJXKRm zzUb!l#b7P5$ERSNizf!{z7vnc zplL{2I9xo(r*cjgCXbWBXguE6ca zbk+>(m}CQ42Zgj(v}GSf6jWmwDDf7cU{HiI0EmUHlR1Hgv#0*MNw%#VtJe%ERWB{ zbzr;bNCc9@XCzPdYn;If)8My13V*mhAkaX(4Qx^PLg~$AgnNIx5E3;!f)W-?0~LK- zxGm~ps$Vr6j(F7tJ`^}V zZD2G$vp@CbSXSO?J;8o;iVDjQyI%k9zQlI8&wi9nwc>Mb_PN?dvJLKL3L?z$)*N;^ zh;`ag0J9sZ?mJ5!=k*0nTQ++9sLHh06dq2A3kiCQ_w~C|ej(oEg5QA1J6~RuUEWd6 z?n3c29*GhM6rSkJK+Ri~pBngGQp-sR4CY_|iGZpXK^zEVx12fcx+!=Kgm>0=V#h7* z{@L_NYR%7oRl#clRKv!%zeGmo9k(hD6(ueQM+mBR$GZAu05B(>Ba*LC_A;1$?J_KW za0_>$&7>zUfXR#*tr-&qSjJ)p~vogH=J_V<5y2N{6G8NgK@Rb;wc>pn}q+ECtF=9zNi zKohByTbS}4TWCMu0BOEqJhQ=Gr()WREa)~>623%-aCWnIMzXlo`!3&9-QM}$N@W_7 zhly|Xno#px8;VMRAI5iuHLwlYKE}3@{#P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