From b5f0f97110caf4e01de6e5a795072e4694bf5725 Mon Sep 17 00:00:00 2001 From: Dave Syer Date: Tue, 8 Oct 2013 15:39:37 -0400 Subject: [PATCH] Clean up TestCommand paraphenalia --- spring-boot-cli/pom.xml | 12 +- .../{testscripts => samples}/book.groovy | 0 .../book_and_tests.groovy | 0 .../{testscripts => samples}/empty.groovy | 0 .../{testscripts => samples}/spock.groovy | 0 .../{testscripts => samples}/test.groovy | 0 .../main/groovy}/testers/junit.groovy | 0 .../main/groovy}/testers/spock.groovy | 0 .../main/groovy}/testers/tester.groovy | 0 .../boot/cli/command/TestCommand.java | 394 +++++++++--------- .../resources/META-INF/springcli.properties | 14 - .../boot/cli/TestCommandIntegrationTests.java | 159 +++---- .../test/resources/commands/handler.groovy | 5 +- 13 files changed, 283 insertions(+), 301 deletions(-) rename spring-boot-cli/{testscripts => samples}/book.groovy (100%) rename spring-boot-cli/{testscripts => samples}/book_and_tests.groovy (100%) rename spring-boot-cli/{testscripts => samples}/empty.groovy (100%) rename spring-boot-cli/{testscripts => samples}/spock.groovy (100%) rename spring-boot-cli/{testscripts => samples}/test.groovy (100%) rename spring-boot-cli/{ => src/main/groovy}/testers/junit.groovy (100%) rename spring-boot-cli/{ => src/main/groovy}/testers/spock.groovy (100%) rename spring-boot-cli/{ => src/main/groovy}/testers/tester.groovy (100%) delete mode 100644 spring-boot-cli/src/main/resources/META-INF/springcli.properties diff --git a/spring-boot-cli/pom.xml b/spring-boot-cli/pom.xml index 02e73a71a06..a54deb24b3d 100644 --- a/spring-boot-cli/pom.xml +++ b/spring-boot-cli/pom.xml @@ -33,10 +33,6 @@ org.codehaus.groovy groovy - - org.springframework - spring-core - org.codehaus.groovy @@ -48,11 +44,6 @@ junit test - - org.spockframework - spock-core - test - org.springframework.integration @@ -87,8 +78,7 @@ true - testers - testers + src/main/groovy diff --git a/spring-boot-cli/testscripts/book.groovy b/spring-boot-cli/samples/book.groovy similarity index 100% rename from spring-boot-cli/testscripts/book.groovy rename to spring-boot-cli/samples/book.groovy diff --git a/spring-boot-cli/testscripts/book_and_tests.groovy b/spring-boot-cli/samples/book_and_tests.groovy similarity index 100% rename from spring-boot-cli/testscripts/book_and_tests.groovy rename to spring-boot-cli/samples/book_and_tests.groovy diff --git a/spring-boot-cli/testscripts/empty.groovy b/spring-boot-cli/samples/empty.groovy similarity index 100% rename from spring-boot-cli/testscripts/empty.groovy rename to spring-boot-cli/samples/empty.groovy diff --git a/spring-boot-cli/testscripts/spock.groovy b/spring-boot-cli/samples/spock.groovy similarity index 100% rename from spring-boot-cli/testscripts/spock.groovy rename to spring-boot-cli/samples/spock.groovy diff --git a/spring-boot-cli/testscripts/test.groovy b/spring-boot-cli/samples/test.groovy similarity index 100% rename from spring-boot-cli/testscripts/test.groovy rename to spring-boot-cli/samples/test.groovy diff --git a/spring-boot-cli/testers/junit.groovy b/spring-boot-cli/src/main/groovy/testers/junit.groovy similarity index 100% rename from spring-boot-cli/testers/junit.groovy rename to spring-boot-cli/src/main/groovy/testers/junit.groovy diff --git a/spring-boot-cli/testers/spock.groovy b/spring-boot-cli/src/main/groovy/testers/spock.groovy similarity index 100% rename from spring-boot-cli/testers/spock.groovy rename to spring-boot-cli/src/main/groovy/testers/spock.groovy diff --git a/spring-boot-cli/testers/tester.groovy b/spring-boot-cli/src/main/groovy/testers/tester.groovy similarity index 100% rename from spring-boot-cli/testers/tester.groovy rename to spring-boot-cli/src/main/groovy/testers/tester.groovy diff --git a/spring-boot-cli/src/main/java/org/springframework/boot/cli/command/TestCommand.java b/spring-boot-cli/src/main/java/org/springframework/boot/cli/command/TestCommand.java index f1a2b4ff3b7..042e74b0d9f 100644 --- a/spring-boot-cli/src/main/java/org/springframework/boot/cli/command/TestCommand.java +++ b/spring-boot-cli/src/main/java/org/springframework/boot/cli/command/TestCommand.java @@ -17,7 +17,20 @@ package org.springframework.boot.cli.command; import groovy.lang.GroovyObject; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.PrintWriter; +import java.net.URL; +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; +import java.util.logging.Level; + import joptsimple.OptionSet; + import org.apache.ivy.util.FileUtil; import org.springframework.boot.cli.Log; import org.springframework.boot.cli.command.tester.Failure; @@ -25,240 +38,233 @@ import org.springframework.boot.cli.command.tester.TestResults; import org.springframework.boot.cli.compiler.GroovyCompiler; import org.springframework.boot.cli.compiler.GroovyCompilerConfiguration; -import java.io.File; -import java.io.FileNotFoundException; -import java.io.IOException; -import java.io.InputStream; -import java.io.PrintWriter; -import java.net.URL; -import java.util.ArrayList; -import java.util.HashSet; -import java.util.List; -import java.util.Scanner; -import java.util.Set; -import java.util.logging.Level; - /** * Invokes testing for autocompiled scripts - * + * * @author Greg Turnquist */ public class TestCommand extends OptionParsingCommand { - private TestOptionHandler testOptionHandler; + private TestOptionHandler testOptionHandler; - public TestCommand() { - super("test", "Test a groovy script", new TestOptionHandler()); - this.testOptionHandler = (TestOptionHandler)this.getHandler(); - } + public TestCommand() { + super("test", "Test a groovy script", new TestOptionHandler()); + this.testOptionHandler = (TestOptionHandler) this.getHandler(); + } - @Override - public String getUsageHelp() { - return "[options] "; - } + @Override + public String getUsageHelp() { + return "[options] "; + } - public TestResults getResults() { - return testOptionHandler.results; - } + public TestResults getResults() { + return this.testOptionHandler.results; + } - private static class TestGroovyCompilerConfiguration implements GroovyCompilerConfiguration { - @Override - public boolean isGuessImports() { - return true; - } + private static class TestGroovyCompilerConfiguration implements + GroovyCompilerConfiguration { + @Override + public boolean isGuessImports() { + return true; + } - @Override - public boolean isGuessDependencies() { - return true; - } + @Override + public boolean isGuessDependencies() { + return true; + } - @Override - public String getClasspath() { - return ""; - } + @Override + public String getClasspath() { + return ""; + } - public Level getLogLevel() { - return Level.INFO; - } - } + public Level getLogLevel() { + return Level.INFO; + } + } - private static class TestOptionHandler extends OptionHandler { + private static class TestOptionHandler extends OptionHandler { - private final GroovyCompiler compiler; + private final GroovyCompiler compiler; - private TestResults results; + private TestResults results; - public TestOptionHandler() { - TestGroovyCompilerConfiguration configuration = new TestGroovyCompilerConfiguration(); - this.compiler = new GroovyCompiler(configuration); - if (configuration.getLogLevel().intValue() <= Level.FINE.intValue()) { - System.setProperty("groovy.grape.report.downloads", "true"); - } - } + public TestOptionHandler() { + TestGroovyCompilerConfiguration configuration = new TestGroovyCompilerConfiguration(); + this.compiler = new GroovyCompiler(configuration); + if (configuration.getLogLevel().intValue() <= Level.FINE.intValue()) { + System.setProperty("groovy.grape.report.downloads", "true"); + } + } - @Override - protected void run(OptionSet options) throws Exception { - List nonOptionArguments = options.nonOptionArguments(); + @Override + protected void run(OptionSet options) throws Exception { + List nonOptionArguments = options.nonOptionArguments(); - Set testerFiles = new HashSet(); - File[] files = getFileArguments(nonOptionArguments, testerFiles); + Set testerFiles = new HashSet(); + File[] files = getFileArguments(nonOptionArguments, testerFiles); - /* - * Need to compile the code twice: The first time automatically - * pulls in autoconfigured libraries including test tools. Then - * the compiled code can be scanned to see what libraries were - * activated. Then it can be recompiled, with appropriate tester - * groovy scripts included in the same classloading context. Then - * the testers can be fetched and invoked through reflection against - * the composite AST. - */ - // Compile - Pass 1 - Object[] sources = this.compiler.sources(files); + /* + * Need to compile the code twice: The first time automatically pulls in + * autoconfigured libraries including test tools. Then the compiled code can + * be scanned to see what libraries were activated. Then it can be recompiled, + * with appropriate tester groovy scripts included in the same classloading + * context. Then the testers can be fetched and invoked through reflection + * against the composite AST. + */ + // Compile - Pass 1 + Object[] sources = this.compiler.sources(files); - boolean testing = false; - try { - check("org.junit.Test", sources); - testerFiles.add(locateSourceFromUrl("junit", "testers/junit.groovy")); - testing = true; - } catch (ClassNotFoundException e) { - } + boolean testing = false; + try { + check("org.junit.Test", sources); + testerFiles.add(locateSourceFromUrl("junit", "testers/junit.groovy")); + testing = true; + } + catch (ClassNotFoundException e) { + } - try { - check("spock.lang.Specification", sources); - testerFiles.add(locateSourceFromUrl("spock", "testers/spock.groovy")); - testing = true; - } catch (ClassNotFoundException e) { - } + try { + check("spock.lang.Specification", sources); + testerFiles.add(locateSourceFromUrl("spock", "testers/spock.groovy")); + testing = true; + } + catch (ClassNotFoundException e) { + } - if (testing) { - testerFiles.add(locateSourceFromUrl("tester", "testers/tester.groovy")); - } + if (testing) { + testerFiles.add(locateSourceFromUrl("tester", "testers/tester.groovy")); + } - // Compile - Pass 2 - with appropriate testers added in - files = getFileArguments(nonOptionArguments, testerFiles); - sources = this.compiler.sources(files); + // Compile - Pass 2 - with appropriate testers added in + files = getFileArguments(nonOptionArguments, testerFiles); + sources = this.compiler.sources(files); + if (sources.length == 0) { + throw new RuntimeException("No classes found in '" + files + "'"); + } - if (sources.length == 0) { - throw new RuntimeException("No classes found in '" + files + "'"); - } + List> testers = new ArrayList>(); - List> testers = new ArrayList>(); + // Extract list of compiled classes + List> compiled = new ArrayList>(); + for (Object source : sources) { + if (source.getClass() == Class.class) { + Class sourceClass = (Class) source; + if (sourceClass.getSuperclass().getName().equals("AbstractTester")) { + testers.add(sourceClass); + } + else { + compiled.add((Class) source); + } + } + } - // Extract list of compiled classes - List> compiled = new ArrayList>(); - for (Object source : sources) { - if (source.getClass() == Class.class) { - Class sourceClass = (Class)source; - if (sourceClass.getSuperclass().getName().equals("AbstractTester")) { - testers.add(sourceClass); - } else { - compiled.add((Class)source); - } - } - } + this.results = new TestResults(); - this.results = new TestResults(); + for (Class tester : testers) { + GroovyObject obj = (GroovyObject) tester.newInstance(); + this.results.add((TestResults) obj.invokeMethod("findAndTest", compiled)); + } - for (Class tester : testers) { - GroovyObject obj = (GroovyObject)tester.newInstance(); - this.results.add((TestResults)obj.invokeMethod("findAndTest", compiled)); - } + printReport(this.results); + } - printReport(this.results); - } + private File locateSourceFromUrl(String name, String path) { + try { + File file = File.createTempFile(name, ".groovy"); + file.deleteOnExit(); + FileUtil.copy(getClass().getClassLoader().getResourceAsStream(path), + file, null); + return file; + } + catch (IOException ex) { + throw new IllegalStateException("Could not create temp file for source: " + + name); + } + } - private File locateSourceFromUrl(String name, String path) { - try { - File file = File.createTempFile(name, ".groovy"); - file.deleteOnExit(); - FileUtil.copy(getClass().getClassLoader().getResourceAsStream(path), file, null); - return file; - } - catch (IOException ex) { - throw new IllegalStateException("Could not create temp file for source: " - + name); - } - } + private Class check(String className, Object[] sources) + throws ClassNotFoundException { + Class classToReturn = null; + ClassNotFoundException classNotFoundException = null; + for (Object source : sources) { + try { + classToReturn = ((Class) source).getClassLoader().loadClass( + className); + } + catch (ClassNotFoundException e) { + classNotFoundException = e; + } + } + if (classToReturn != null) { + return classToReturn; + } + throw classNotFoundException; + } - private Class check(String className, Object[] sources) throws ClassNotFoundException { - Class classToReturn = null; - ClassNotFoundException classNotFoundException = null; - for (Object source : sources) { - try { - classToReturn = ((Class)source).getClassLoader().loadClass(className); - } catch (ClassNotFoundException e) { - classNotFoundException = e; - } - } - if (classToReturn != null) { - return classToReturn; - } - throw classNotFoundException; - } + private File[] getFileArguments(List nonOptionArguments, Set testerFiles) { + List files = new ArrayList(); + for (Object option : nonOptionArguments) { + if (option instanceof String) { + String filename = (String) option; + if ("--".equals(filename)) { + break; + } + if (filename.endsWith(".groovy") || filename.endsWith(".java")) { + File file = new File(filename); + if (file.isFile() && file.canRead()) { + files.add(file); + } + else { + URL url = getClass().getClassLoader().getResource(filename); + if (url != null) { + if (url.toString().startsWith("file:")) { + files.add(new File(url.toString().substring( + "file:".length()))); + } + } + else { + throw new RuntimeException("Can't find " + filename); + } + } + } + } + } + if (files.size() == 0) { + throw new RuntimeException("Please specify a file to run"); + } + for (File testerFile : testerFiles) { + files.add(testerFile); + } - private File[] getFileArguments(List nonOptionArguments, Set testerFiles) { - List files = new ArrayList(); - for (Object option : nonOptionArguments) { - if (option instanceof String) { - String filename = (String) option; - if ("--".equals(filename)) { - break; - } - if (filename.endsWith(".groovy") || filename.endsWith(".java")) { - File file = new File(filename); - if (file.isFile() && file.canRead()) { - files.add(file); - } - else { - URL url = getClass().getClassLoader().getResource(filename); - if (url != null) { - if (url.toString().startsWith("file:")) { - files.add(new File(url.toString().substring("file:".length()))); - } - } - else { - throw new RuntimeException("Can't find " + filename); - } - } - } - } - } - if (files.size() == 0) { - throw new RuntimeException("Please specify a file to run"); - } + return files.toArray(new File[files.size()]); + } - for (File testerFile : testerFiles) { - files.add(testerFile); - } + private void printReport(TestResults results) throws FileNotFoundException { + PrintWriter writer = new PrintWriter("results.txt"); - return files.toArray(new File[files.size()]); - } + String header = "Total: " + results.getRunCount() + ", Success: " + + (results.getRunCount() - results.getFailureCount()) + + ", : Failures: " + results.getFailureCount() + "\n" + "Passed? " + + results.wasSuccessful(); - private void printReport(TestResults results) throws FileNotFoundException { - PrintWriter writer = new PrintWriter("results.txt"); + String trailer = ""; + String trace = ""; + for (Failure failure : results.getFailures()) { + trailer += failure.getDescription().toString(); + trace += failure.getTrace() + "\n"; + } - String header = "Total: " + results.getRunCount() + - ", Success: " + (results.getRunCount()-results.getFailureCount()) + - ", : Failures: " + results.getFailureCount() + "\n" + - "Passed? " + results.wasSuccessful(); + writer.println(header); + writer.println(trace); + writer.close(); - String trailer = ""; - String trace = ""; - for (Failure failure : results.getFailures()) { - trailer += failure.getDescription().toString(); - trace += failure.getTrace() + "\n"; - } + Log.info(header); + Log.info(trailer); + } - writer.println(header); - writer.println(trace); - writer.close(); - - Log.info(header); - Log.info(trailer); - } - - } + } } diff --git a/spring-boot-cli/src/main/resources/META-INF/springcli.properties b/spring-boot-cli/src/main/resources/META-INF/springcli.properties deleted file mode 100644 index d3c6869afd0..00000000000 --- a/spring-boot-cli/src/main/resources/META-INF/springcli.properties +++ /dev/null @@ -1,14 +0,0 @@ -groovy.version: ${groovy.version} -hamcrest.version: ${hamcrest.version} -jetty.version: ${jetty.version} -junit.version: ${junit.version} -reactor.version: ${reactor.version} -spock.version: ${spock.version} -spring.version: ${spring.version} -spring-batch.version: ${spring-batch.version} -spring-boot.version: ${project.version} -spring-rabbit.version: ${spring-rabbit.version} -spring-security.version: ${spring-security.version} -spring-integration.version: ${spring-integration.version} -spring-integration-groovydsl.version: ${spring-integration-groovydsl.version} -tomcat.version: ${tomcat.version} diff --git a/spring-boot-cli/src/test/java/org/springframework/boot/cli/TestCommandIntegrationTests.java b/spring-boot-cli/src/test/java/org/springframework/boot/cli/TestCommandIntegrationTests.java index ebe2c0010a2..f3c446703de 100644 --- a/spring-boot-cli/src/test/java/org/springframework/boot/cli/TestCommandIntegrationTests.java +++ b/spring-boot-cli/src/test/java/org/springframework/boot/cli/TestCommandIntegrationTests.java @@ -29,97 +29,98 @@ import static org.junit.Assert.assertTrue; /** * Integration tests to exercise the CLI's test command. - * + * * @author Greg Turnquist */ public class TestCommandIntegrationTests { - @BeforeClass - public static void cleanGrapes() throws Exception { - GrapesCleaner.cleanIfNecessary(); - // System.setProperty("ivy.message.logger.level", "3"); - } + @BeforeClass + public static void cleanGrapes() throws Exception { + GrapesCleaner.cleanIfNecessary(); + // System.setProperty("ivy.message.logger.level", "3"); + } - @Before - public void setup() throws Exception { - System.setProperty("disableSpringSnapshotRepos", "true"); - new CleanCommand().run("org.springframework"); - } + @Before + public void setup() throws Exception { + System.setProperty("disableSpringSnapshotRepos", "true"); + new CleanCommand().run("org.springframework"); + } - @After - public void teardown() { - System.clearProperty("disableSpringSnapshotRepos"); - } + @After + public void teardown() { + System.clearProperty("disableSpringSnapshotRepos"); + } - @Test - public void noTests() throws Throwable { - TestCommand command = new TestCommand(); - command.run("testscripts/book.groovy"); - TestResults results = command.getResults(); - assertEquals(0, results.getRunCount()); - assertEquals(0, results.getFailureCount()); - assertTrue(results.wasSuccessful()); - } + @Test + public void noTests() throws Throwable { + TestCommand command = new TestCommand(); + command.run("samples/book.groovy"); + TestResults results = command.getResults(); + assertEquals(0, results.getRunCount()); + assertEquals(0, results.getFailureCount()); + assertTrue(results.wasSuccessful()); + } - @Test - public void empty() throws Exception { - TestCommand command = new TestCommand(); - command.run("testscripts/empty.groovy"); - TestResults results = command.getResults(); - assertEquals(0, results.getRunCount()); - assertEquals(0, results.getFailureCount()); - assertTrue(results.wasSuccessful()); - } + @Test + public void empty() throws Exception { + TestCommand command = new TestCommand(); + command.run("samples/empty.groovy"); + TestResults results = command.getResults(); + assertEquals(0, results.getRunCount()); + assertEquals(0, results.getFailureCount()); + assertTrue(results.wasSuccessful()); + } - @Test(expected = RuntimeException.class) - public void noFile() throws Exception { - try { - TestCommand command = new TestCommand(); - command.run("testscripts/nothing.groovy"); - } catch (RuntimeException e) { - assertEquals("Can't find testscripts/nothing.groovy", e.getMessage()); - throw e; - } - } + @Test(expected = RuntimeException.class) + public void noFile() throws Exception { + try { + TestCommand command = new TestCommand(); + command.run("samples/nothing.groovy"); + } + catch (RuntimeException e) { + assertEquals("Can't find samples/nothing.groovy", e.getMessage()); + throw e; + } + } - @Test - public void appAndTestsInOneFile() throws Exception { - TestCommand command = new TestCommand(); - command.run("testscripts/book_and_tests.groovy"); - TestResults results = command.getResults(); - assertEquals(1, results.getRunCount()); - assertEquals(0, results.getFailureCount()); - assertTrue(results.wasSuccessful()); - } + @Test + public void appAndTestsInOneFile() throws Exception { + TestCommand command = new TestCommand(); + command.run("samples/book_and_tests.groovy"); + TestResults results = command.getResults(); + assertEquals(1, results.getRunCount()); + assertEquals(0, results.getFailureCount()); + assertTrue(results.wasSuccessful()); + } - @Test - public void appInOneFileTestsInAnotherFile() throws Exception { - TestCommand command = new TestCommand(); - command.run("testscripts/book.groovy", "testscripts/test.groovy"); - TestResults results = command.getResults(); - assertEquals(1, results.getRunCount()); - assertEquals(0, results.getFailureCount()); - assertTrue(results.wasSuccessful()); - } + @Test + public void appInOneFileTestsInAnotherFile() throws Exception { + TestCommand command = new TestCommand(); + command.run("samples/book.groovy", "samples/test.groovy"); + TestResults results = command.getResults(); + assertEquals(1, results.getRunCount()); + assertEquals(0, results.getFailureCount()); + assertTrue(results.wasSuccessful()); + } - @Test - public void spockTester() throws Exception { - TestCommand command = new TestCommand(); - command.run("testscripts/spock.groovy"); - TestResults results = command.getResults(); - assertEquals(1, results.getRunCount()); - assertEquals(0, results.getFailureCount()); - assertTrue(results.wasSuccessful()); - } + @Test + public void spockTester() throws Exception { + TestCommand command = new TestCommand(); + command.run("samples/spock.groovy"); + TestResults results = command.getResults(); + assertEquals(1, results.getRunCount()); + assertEquals(0, results.getFailureCount()); + assertTrue(results.wasSuccessful()); + } - @Test - public void spockAndJunitTester() throws Exception { - TestCommand command = new TestCommand(); - command.run("testscripts/spock.groovy", "testscripts/book_and_tests.groovy"); - TestResults results = command.getResults(); - assertEquals(2, results.getRunCount()); - assertEquals(0, results.getFailureCount()); - assertTrue(results.wasSuccessful()); - } + @Test + public void spockAndJunitTester() throws Exception { + TestCommand command = new TestCommand(); + command.run("samples/spock.groovy", "samples/book_and_tests.groovy"); + TestResults results = command.getResults(); + assertEquals(2, results.getRunCount()); + assertEquals(0, results.getFailureCount()); + assertTrue(results.wasSuccessful()); + } } diff --git a/spring-boot-cli/src/test/resources/commands/handler.groovy b/spring-boot-cli/src/test/resources/commands/handler.groovy index 2c3eef136fa..b9353d124ef 100644 --- a/spring-boot-cli/src/test/resources/commands/handler.groovy +++ b/spring-boot-cli/src/test/resources/commands/handler.groovy @@ -1,11 +1,10 @@ package org.test.command +import joptsimple.OptionSet + @Grab("org.eclipse.jgit:org.eclipse.jgit:2.3.1.201302201838-r") import org.eclipse.jgit.api.Git -@Grab("org.eclipse.jgit:org.eclipse.jgit:2.3.1.201302201838-r") @Grab("org.eclipse.jgit:org.eclipse.jgit:2.3.1.201302201838-r") @Grab("org.eclipse.jgit:org.eclipse.jgit:2.3.1.201302201838-r") @Grab("org.eclipse.jgit:org.eclipse.jgit:2.3.1.201302201838-r") @Grab("org.eclipse.jgit:org.eclipse.jgit:2.3.1.201302201838-r") -import java.lang.Object - class TestCommand extends OptionHandler { void options() {