From a92e73743fb7a8082dcf84eedccb2ff2cab4c412 Mon Sep 17 00:00:00 2001 From: Gintas Grigelionis Date: Sun, 29 Apr 2018 22:35:37 +0200 Subject: [PATCH] Extract more fixtures --- .../apache/tools/ant/taskdefs/EchoTest.java | 43 ++++++++++--------- .../tools/ant/taskdefs/ParallelTest.java | 6 +-- .../ant/taskdefs/condition/EqualsTest.java | 10 ++++- .../tools/ant/types/CommandlineJavaTest.java | 7 ++- .../ant/types/XMLCatalogBuildFileTest.java | 10 ++--- 5 files changed, 41 insertions(+), 35 deletions(-) diff --git a/src/tests/junit/org/apache/tools/ant/taskdefs/EchoTest.java b/src/tests/junit/org/apache/tools/ant/taskdefs/EchoTest.java index 67715dc87..dbfed77ff 100644 --- a/src/tests/junit/org/apache/tools/ant/taskdefs/EchoTest.java +++ b/src/tests/junit/org/apache/tools/ant/taskdefs/EchoTest.java @@ -29,6 +29,7 @@ import org.apache.tools.ant.DefaultLogger; import org.apache.tools.ant.Project; import org.apache.tools.ant.util.FileUtils; import org.junit.After; +import org.junit.Before; import org.junit.Test; import static org.junit.Assert.assertEquals; @@ -38,16 +39,33 @@ import static org.junit.Assert.assertEquals; */ public class EchoTest { + private Project p; + + private EchoTestLogger logger; + + private Echo echo; + private File removeThis; - @Test - public void testLogBlankEcho() { - Project p = new Project(); + @Before + public void setUp() { + p = new Project(); p.init(); - EchoTestLogger logger = new EchoTestLogger(); + logger = new EchoTestLogger(); p.addBuildListener(logger); - Echo echo = new Echo(); + echo = new Echo(); echo.setProject(p); + } + + @After + public void tearDown() { + if (removeThis != null && removeThis.exists() && !removeThis.delete()) { + removeThis.deleteOnExit(); + } + } + + @Test + public void testLogBlankEcho() { echo.setTaskName("testLogBlankEcho"); echo.execute(); assertEquals("[testLogBlankEcho] ", logger.lastLoggedMessage); @@ -55,12 +73,6 @@ public class EchoTest { @Test public void testLogUTF8Echo() throws IOException { - Project p = new Project(); - p.init(); - EchoTestLogger logger = new EchoTestLogger(); - p.addBuildListener(logger); - Echo echo = new Echo(); - echo.setProject(p); echo.setTaskName("testLogUTF8Echo"); echo.setMessage("\u00e4\u00a9"); removeThis = new File("abc.txt"); @@ -71,15 +83,6 @@ public class EchoTest { assertEquals(x, "\u00e4\u00a9"); } - @After - public void tearDown() { - if (removeThis != null && removeThis.exists()) { - if (!removeThis.delete()) { - removeThis.deleteOnExit(); - } - } - } - private class EchoTestLogger extends DefaultLogger { String lastLoggedMessage; diff --git a/src/tests/junit/org/apache/tools/ant/taskdefs/ParallelTest.java b/src/tests/junit/org/apache/tools/ant/taskdefs/ParallelTest.java index dbf63255c..eba49c952 100644 --- a/src/tests/junit/org/apache/tools/ant/taskdefs/ParallelTest.java +++ b/src/tests/junit/org/apache/tools/ant/taskdefs/ParallelTest.java @@ -55,17 +55,18 @@ public class ParallelTest { /** the build file associated with this test */ public static final String TEST_BUILD_FILE = "src/etc/testcases/taskdefs/parallel.xml"; + private Project p; /** The JUnit setup method */ @Before public void setUp() { buildRule.configureProject(TEST_BUILD_FILE); + p = buildRule.getProject(); } /** tests basic operation of the parallel task */ @Test public void testBasic() { // should get no output at all - Project p = buildRule.getProject(); p.setUserProperty("test.direct", DIRECT_MESSAGE); p.setUserProperty("test.delayed", DELAYED_MESSAGE); buildRule.executeTarget("testBasic"); @@ -81,7 +82,6 @@ public class ParallelTest { @Test public void testThreadCount() { // should get no output at all - Project p = buildRule.getProject(); p.setUserProperty("test.direct", DIRECT_MESSAGE); p.setUserProperty("test.delayed", DELAYED_MESSAGE); buildRule.executeTarget("testThreadCount"); @@ -140,7 +140,6 @@ public class ParallelTest { // should get no output at all thrown.expect(BuildException.class); thrown.expectMessage(FAILURE_MESSAGE); - Project p = buildRule.getProject(); p.setUserProperty("test.failure", FAILURE_MESSAGE); p.setUserProperty("test.delayed", DELAYED_MESSAGE); buildRule.executeTarget("testFail"); @@ -149,7 +148,6 @@ public class ParallelTest { /** tests the demuxing of output streams in a multithreaded situation */ @Test public void testDemux() { - Project p = buildRule.getProject(); p.addTaskDefinition("demuxtest", DemuxOutputTask.class); synchronized (System.out) { PrintStream out = System.out; diff --git a/src/tests/junit/org/apache/tools/ant/taskdefs/condition/EqualsTest.java b/src/tests/junit/org/apache/tools/ant/taskdefs/condition/EqualsTest.java index b8c40b3c6..3b80747c7 100644 --- a/src/tests/junit/org/apache/tools/ant/taskdefs/condition/EqualsTest.java +++ b/src/tests/junit/org/apache/tools/ant/taskdefs/condition/EqualsTest.java @@ -18,6 +18,7 @@ package org.apache.tools.ant.taskdefs.condition; +import org.junit.Before; import org.junit.Test; import static org.junit.Assert.assertFalse; @@ -29,9 +30,15 @@ import static org.junit.Assert.assertTrue; */ public class EqualsTest { + private Equals eq; + + @Before + public void setUp() { + eq = new Equals(); + } + @Test public void testTrim() { - Equals eq = new Equals(); eq.setArg1("a"); eq.setArg2(" a"); assertFalse(eq.eval()); @@ -45,7 +52,6 @@ public class EqualsTest { @Test public void testCaseSensitive() { - Equals eq = new Equals(); eq.setArg1("a"); eq.setArg2("A"); assertFalse(eq.eval()); diff --git a/src/tests/junit/org/apache/tools/ant/types/CommandlineJavaTest.java b/src/tests/junit/org/apache/tools/ant/types/CommandlineJavaTest.java index 85f83f6d1..a96a96b3b 100644 --- a/src/tests/junit/org/apache/tools/ant/types/CommandlineJavaTest.java +++ b/src/tests/junit/org/apache/tools/ant/types/CommandlineJavaTest.java @@ -44,6 +44,8 @@ public class CommandlineJavaTest { private Project project; + private CommandlineJava c; + @Before public void setUp() { project = new Project(); @@ -55,6 +57,7 @@ public class CommandlineJavaTest { if (cloneVm != null) { System.setProperty("ant.build.clonevm", "false"); } + c = new CommandlineJava(); } @After @@ -72,7 +75,6 @@ public class CommandlineJavaTest { @Test public void testGetCommandline() throws CloneNotSupportedException { assertNotNull("Ant home not set", System.getProperty("ant.home")); - CommandlineJava c = new CommandlineJava(); c.createArgument().setValue("org.apache.tools.ant.CommandlineJavaTest"); c.setClassname("junit.textui.TestRunner"); c.createVmArgument().setValue("-Djava.compiler=NONE"); @@ -108,7 +110,6 @@ public class CommandlineJavaTest { @Test public void testJarOption() { - CommandlineJava c = new CommandlineJava(); c.createArgument().setValue("arg1"); c.setJar("myfile.jar"); c.createVmArgument().setValue("-classic"); @@ -126,7 +127,6 @@ public class CommandlineJavaTest { String currentClasspath = System.getProperty("java.class.path"); assertNotNull(currentClasspath); assertNull(System.getProperty("key")); - CommandlineJava c = new CommandlineJava(); Environment.Variable v = new Environment.Variable(); v.setKey("key"); v.setValue("value"); @@ -157,7 +157,6 @@ public class CommandlineJavaTest { @Test public void testAssertions() throws Exception { - CommandlineJava c = new CommandlineJava(); c.createArgument().setValue("org.apache.tools.ant.CommandlineJavaTest"); c.setClassname("junit.textui.TestRunner"); c.createVmArgument().setValue("-Djava.compiler=NONE"); diff --git a/src/tests/junit/org/apache/tools/ant/types/XMLCatalogBuildFileTest.java b/src/tests/junit/org/apache/tools/ant/types/XMLCatalogBuildFileTest.java index 96c0c5d25..6c656e017 100644 --- a/src/tests/junit/org/apache/tools/ant/types/XMLCatalogBuildFileTest.java +++ b/src/tests/junit/org/apache/tools/ant/types/XMLCatalogBuildFileTest.java @@ -19,6 +19,7 @@ package org.apache.tools.ant.types; import org.apache.tools.ant.BuildFileRule; +import org.junit.Before; import org.junit.Rule; import org.junit.Test; @@ -35,7 +36,10 @@ public class XMLCatalogBuildFileTest { @Rule public BuildFileRule buildRule = new BuildFileRule(); - + @Before + public void setUp() { + buildRule.configureProject("src/etc/testcases/types/xmlcatalog.xml"); + } // // Ensure that an external entity resolves as expected with NO @@ -46,7 +50,6 @@ public class XMLCatalogBuildFileTest { // @Test public void testEntityNoCatalog() { - buildRule.configureProject("src/etc/testcases/types/xmlcatalog.xml"); buildRule.executeTarget("testentitynocatalog"); assertEquals("A stitch in time saves nine", buildRule.getProject().getProperty("val1")); } @@ -61,7 +64,6 @@ public class XMLCatalogBuildFileTest { // @Test public void testEntityWithCatalog() { - buildRule.configureProject("src/etc/testcases/types/xmlcatalog.xml"); buildRule.executeTarget("testentitywithcatalog"); assertEquals("No news is good news", buildRule.getProject().getProperty("val2")); } @@ -76,7 +78,6 @@ public class XMLCatalogBuildFileTest { // @Test public void testDocumentNoCatalog() { - buildRule.configureProject("src/etc/testcases/types/xmlcatalog.xml"); buildRule.executeTarget("testdocumentnocatalog"); assertEquals("A stitch in time saves nine", buildRule.getProject().getProperty("val3")); } @@ -91,7 +92,6 @@ public class XMLCatalogBuildFileTest { // Stuff result into the property: val4 @Test public void testDocumentWithCatalog() { - buildRule.configureProject("src/etc/testcases/types/xmlcatalog.xml"); buildRule.executeTarget("testdocumentwithcatalog"); assertEquals("No news is good news", buildRule.getProject().getProperty("val4")); }