diff --git a/src/tests/junit/org/apache/tools/ant/IntrospectionHelperTest.java b/src/tests/junit/org/apache/tools/ant/IntrospectionHelperTest.java index 5a20f38b4..262dc67f0 100644 --- a/src/tests/junit/org/apache/tools/ant/IntrospectionHelperTest.java +++ b/src/tests/junit/org/apache/tools/ant/IntrospectionHelperTest.java @@ -34,13 +34,17 @@ import org.apache.tools.ant.taskdefs.condition.Os; import org.junit.Before; import org.junit.ComparisonFailure; import org.junit.Ignore; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; +import static org.hamcrest.Matchers.hasProperty; +import static org.hamcrest.Matchers.instanceOf; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertThat; import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; /** * JUnit testcases for org.apache.tools.ant.IntrospectionHelper. @@ -49,6 +53,9 @@ import static org.junit.Assert.fail; public class IntrospectionHelperTest { + @Rule + public ExpectedException thrown = ExpectedException.none(); + private Project p; private IntrospectionHelper ih; private static final String projectBasedir = File.separator; @@ -71,42 +78,30 @@ public class IntrospectionHelperTest { } @Test - public void testAddText() throws BuildException { + public void testAddText() { + thrown.expect(BuildException.class); + thrown.expect(hasProperty("cause", instanceOf(ComparisonFailure.class))); ih.addText(p, this, "test"); - try { - ih.addText(p, this, "test2"); - fail("test2 shouldn\'t be equal to test"); - } catch (BuildException be) { - assertTrue(be.getCause() instanceof ComparisonFailure); - } + ih.addText(p, this, "test2"); + } + @Test(expected = BuildException.class) + public void testAddTextToString() throws BuildException { ih = IntrospectionHelper.getHelper(String.class); - try { - ih.addText(p, "", "test"); - fail("String doesn\'t support addText"); - } catch (BuildException be) { - //TODO the value should be asserted - } + ih.addText(p, "", "test"); } - @Test @Ignore("This silently ignores a build exception") + @Test(expected = BuildException.class) public void testGetAddTextMethod() { Method m = ih.getAddTextMethod(); assertMethod(m, "addText", String.class, "test", "bing!"); - - ih = IntrospectionHelper.getHelper(String.class); - try { - m = ih.getAddTextMethod(); - } catch (BuildException e) { - } + IntrospectionHelper.getHelper(String.class).getAddTextMethod(); } @Test public void testSupportsCharacters() { - assertTrue("IntrospectionHelperTest supports addText", - ih.supportsCharacters()); - + assertTrue("IntrospectionHelperTest supports addText", ih.supportsCharacters()); ih = IntrospectionHelper.getHelper(String.class); assertFalse("String doesn\'t support addText", ih.supportsCharacters()); } @@ -115,93 +110,135 @@ public class IntrospectionHelperTest { assertEquals("test", text); } + /** + * Fail: don't have element type one + */ + @Test(expected = BuildException.class) + public void testElementCreatorOne() { + ih.getElementType("one"); + // TODO we should be asserting a value in here + } + + /** + * Fail: createTwo takes arguments + */ + @Test(expected = BuildException.class) + public void testElementCreatorTwo() { + ih.getElementType("two"); + // TODO we should be asserting a value in here + } + + /** + * Fail: createThree returns void + */ + @Test(expected = BuildException.class) + public void testElementCreatorThree() { + ih.getElementType("three"); + // TODO we should be asserting a value in here + } + + /** + * Fail: createFour returns array + */ + @Test(expected = BuildException.class) + public void testElementCreatorFour() { + ih.getElementType("four"); + // TODO we should be asserting a value in here + } + + /** + * Fail: createFive returns primitive type + */ + @Test(expected = BuildException.class) + public void testElementCreatorFive() { + ih.getElementType("five"); + // TODO we should be asserting a value in here + } + @Test - public void testElementCreators() throws BuildException { - try { - ih.getElementType("one"); - fail("don't have element type one"); - } catch (BuildException be) { - //TODO we should be asserting a value in here - } - try { - ih.getElementType("two"); - fail("createTwo takes arguments"); - } catch (BuildException be) { - //TODO we should be asserting a value in here - } - try { - ih.getElementType("three"); - fail("createThree returns void"); - } catch (BuildException be) { - //TODO we should be asserting a value in here - } - try { - ih.getElementType("four"); - fail("createFour returns array"); - } catch (BuildException be) { - //TODO we should be asserting a value in here - } - try { - ih.getElementType("five"); - fail("createFive returns primitive type"); - } catch (BuildException be) { - //TODO we should be asserting a value in here - } + public void testElementCreatorSix() throws BuildException { assertEquals(String.class, ih.getElementType("six")); assertEquals("test", ih.createElement(p, this, "six")); + } - try { - ih.getElementType("seven"); - fail("addSeven takes two arguments"); - } catch (BuildException be) { - //TODO we should be asserting a value in here - } - try { - ih.getElementType("eight"); - fail("addEight takes no arguments"); - } catch (BuildException be) { - //TODO we should be asserting a value in here - } - try { - ih.getElementType("nine"); - fail("nine return non void"); - } catch (BuildException be) { - //TODO we should be asserting a value in here - } - try { - ih.getElementType("ten"); - fail("addTen takes array argument"); - } catch (BuildException be) { - //TODO we should be asserting a value in here - } - try { - ih.getElementType("eleven"); - fail("addEleven takes primitive argument"); - } catch (BuildException be) { - //TODO we should be asserting a value in here - } - try { - ih.getElementType("twelve"); - fail("no primitive constructor for java.lang.Class"); - } catch (BuildException be) { - //TODO we should be asserting a value in here - } + /** + * Fail: addSeven takes two arguments + */ + @Test(expected = BuildException.class) + public void testElementCreatorSeven() { + ih.getElementType("seven"); + // TODO we should be asserting a value in here + } + + /** + * Fail: addEight takes no arguments + */ + @Test(expected = BuildException.class) + public void testElementCreatorEight() { + ih.getElementType("eight"); + // TODO we should be asserting a value in here + } + + /** + * Fail: addNine returns non void + */ + @Test(expected = BuildException.class) + public void testElementCreatorNine() { + ih.getElementType("nine"); + // TODO we should be asserting a value in here + } + + /** + * Fail: addTen takes array argument + */ + @Test(expected = BuildException.class) + public void testElementCreatorTen() { + ih.getElementType("ten"); + // TODO we should be asserting a value in here + } + + /** + * addEleven takes primitive argument + */ + @Test(expected = BuildException.class) + public void testElementCreatorEleven() { + ih.getElementType("eleven"); + // TODO we should be asserting a value in here + } + + /** + * Fail: no primitive constructor for java.lang.Class + */ + @Test(expected = BuildException.class) + public void testElementCreatorTwelve() throws BuildException { + ih.getElementType("twelve"); + // TODO we should be asserting a value in here + } + + @Test + public void testElementCreatorThirteen() throws BuildException { assertEquals(StringBuffer.class, ih.getElementType("thirteen")); assertEquals("test", ih.createElement(p, this, "thirteen").toString()); + } - try { - ih.createElement(p, this, "fourteen"); - fail("fourteen throws NullPointerException"); - } catch (BuildException be) { - assertTrue(be.getCause() instanceof NullPointerException); - } + /** + * Fail: fourteen throws NullPointerException + */ + @Test + public void testElementCreatorFourteen() { + thrown.expect(BuildException.class); + thrown.expect(hasProperty("cause", instanceOf(NullPointerException.class))); + ih.createElement(p, this, "fourteen"); + } - try { - ih.createElement(p, this, "fourteen"); - fail("fifteen throws NullPointerException"); - } catch (BuildException be) { - assertTrue(be.getCause() instanceof NullPointerException); - } + /** + * Fail: fifteen throws NullPointerException + */ + @Test + public void testElementCreatorFifteen() { + thrown.expect(BuildException.class); + thrown.expect(hasProperty("cause", instanceOf(NullPointerException.class))); + ih.createElement(p, this, "fifteen"); } private Map> getExpectedNestedElements() { @@ -216,11 +253,11 @@ public class IntrospectionHelperTest { @Test public void testGetNestedElements() { Map> elemMap = getExpectedNestedElements(); - Enumeration e = ih.getNestedElements(); + Enumeration e = ih.getNestedElements(); while (e.hasMoreElements()) { - String name = (String) e.nextElement(); - Class expect = elemMap.get(name); - assertNotNull("Support for "+name+" in IntrospectioNHelperTest?", + String name = e.nextElement(); + Class expect = elemMap.get(name); + assertNotNull("Support for " + name + " in IntrospectioNHelperTest?", expect); assertEquals("Return type of " + name, expect, ih.getElementType(name)); elemMap.remove(name); @@ -228,26 +265,22 @@ public class IntrospectionHelperTest { assertTrue("Found all", elemMap.isEmpty()); } - @Test + @Test(expected = UnsupportedOperationException.class) public void testGetNestedElementMap() { Map> elemMap = getExpectedNestedElements(); Map> actualMap = ih.getNestedElementMap(); for (Map.Entry> entry : actualMap.entrySet()) { String elemName = entry.getKey(); Class elemClass = elemMap.get(elemName); - assertNotNull("Support for " + elemName + - " in IntrospectionHelperTest?", elemClass); + assertNotNull("Support for " + elemName + " in IntrospectionHelperTest?", elemClass); assertEquals("Type of " + elemName, elemClass, entry.getValue()); elemMap.remove(elemName); } assertTrue("Found all", elemMap.isEmpty()); // Check it's a read-only map. - try { - actualMap.clear(); - //TODO we should be asserting a value somewhere in here - } catch (UnsupportedOperationException e) { - } + actualMap.clear(); + // TODO we should be asserting a value somewhere in here } @Test @@ -262,7 +295,7 @@ public class IntrospectionHelperTest { Class returnType, Class methodArg) { Method m = ih.getElementMethod(elemName); assertEquals("Method name", methodName, m.getName()); - Class expectedReturnType = (returnType == null)? Void.TYPE: returnType; + Class expectedReturnType = (returnType == null) ? Void.TYPE : returnType; assertEquals("Return type", expectedReturnType, m.getReturnType()); Class[] args = m.getParameterTypes(); if (methodArg != null) { @@ -312,7 +345,7 @@ public class IntrospectionHelperTest { public void addEleven(int i) { } - public void addTwelve(Class c) { + public void addTwelve(Class c) { } public void addThirteen(StringBuffer sb) { @@ -323,141 +356,211 @@ public class IntrospectionHelperTest { throw new NullPointerException(); } + /** + * Fail: setOne doesn't exist + */ + @Test(expected = BuildException.class) + public void testAttributeSetterOne() { + ih.setAttribute(p, this, "one", "test"); + // TODO we should be asserting a value in here + } + + /** + * Fail: setTwo returns non void + */ + @Test(expected = BuildException.class) + public void testAttributeSetterTwo() { + ih.setAttribute(p, this, "two", "test"); + // TODO we should be asserting a value in here + } + + /** + * Fail: setThree takes no args + */ + @Test(expected = BuildException.class) + public void testAttributeSetterThree() { + ih.setAttribute(p, this, "three", "test"); + // TODO we should be asserting a value in here + } + + /** + * Fail: setFour takes two args + */ + @Test(expected = BuildException.class) + public void testAttributeSetterFour() { + ih.setAttribute(p, this, "four", "test"); + //TODO we should be asserting a value in here + } + + /** + * Fail: setFive takes array arg + */ + @Test(expected = BuildException.class) + public void testAttributeSetterFive() { + ih.setAttribute(p, this, "five", "test"); + // TODO we should be asserting a value in here + } + + /** + * Fail: Project doesn't have a String constructor + */ + @Test(expected = BuildException.class) + public void testAttributeSetterSix() { + ih.setAttribute(p, this, "six", "test"); + // TODO we should be asserting a value in here + } + + /** + * Fail: 2 shouldn't be equal to three + */ @Test - public void testAttributeSetters() throws BuildException { - try { - ih.setAttribute(p, this, "one", "test"); - fail("setOne doesn't exist"); - } catch (BuildException be) { - //TODO we should be asserting a value in here - } - try { - ih.setAttribute(p, this, "two", "test"); - fail("setTwo returns non void"); - } catch (BuildException be) { - //TODO we should be asserting a value in here - } - try { - ih.setAttribute(p, this, "three", "test"); - fail("setThree takes no args"); - } catch (BuildException be) { - //TODO we should be asserting a value in here - } - try { - ih.setAttribute(p, this, "four", "test"); - fail("setFour takes two args"); - } catch (BuildException be) { - //TODO we should be asserting a value in here - } - try { - ih.setAttribute(p, this, "five", "test"); - fail("setFive takes array arg"); - } catch (BuildException be) { - //TODO we should be asserting a value in here - } - try { - ih.setAttribute(p, this, "six", "test"); - fail("Project doesn't have a String constructor"); - } catch (BuildException be) { - //TODO we should be asserting a value in here - } + public void testAttributeSetterSeven() { + thrown.expect(BuildException.class); + thrown.expect(hasProperty("cause", instanceOf(ComparisonFailure.class))); ih.setAttribute(p, this, "seven", "2"); - try { - ih.setAttribute(p, this, "seven", "3"); - fail("2 shouldn't be equals to three"); - } catch (BuildException be) { - assertTrue(be.getCause() instanceof ComparisonFailure); - } + ih.setAttribute(p, this, "seven", "3"); + } + + /** + * Fail: 2 shouldn't be equal to three as int + */ + @Test + public void testAttributeSetterEight() { + thrown.expect(BuildException.class); + thrown.expect(hasProperty("cause", instanceOf(AssertionError.class))); ih.setAttribute(p, this, "eight", "2"); - try { - ih.setAttribute(p, this, "eight", "3"); - fail("2 shouldn't be equals to three - as int"); - } catch (BuildException be) { - assertTrue("Cause of error: " + be.toString(), be.getCause() instanceof AssertionError); - } + ih.setAttribute(p, this, "eight", "3"); + } + + /** + * Fail: 2 shouldn't be equal to three as Integer + */ + @Test + public void testAttributeSetterNine() { + thrown.expect(BuildException.class); + thrown.expect(hasProperty("cause", instanceOf(AssertionError.class))); ih.setAttribute(p, this, "nine", "2"); - try { - ih.setAttribute(p, this, "nine", "3"); - fail("2 shouldn't be equals to three - as Integer"); - } catch (BuildException be) { - assertTrue(be.getCause() instanceof AssertionError); - } + ih.setAttribute(p, this, "nine", "3"); + } + + /** + * Fail: string + 2 shouldn't be equal to string + 3 + */ + @Test + public void testAttributeSetterTen() { + thrown.expect(BuildException.class); + thrown.expect(hasProperty("cause", instanceOf(AssertionError.class))); ih.setAttribute(p, this, "ten", "2"); - try { - ih.setAttribute(p, this, "ten", "3"); - fail(projectBasedir + "2 shouldn't be equals to " + projectBasedir + "3"); - } catch (BuildException be) { - assertTrue(be.getCause() instanceof AssertionError); - } + ih.setAttribute(p, this, "ten", "3"); + } + + /** + * Fail: on shouldn't be false + */ + @Test + public void testAttributeSetterEleven() { + thrown.expect(BuildException.class); + thrown.expect(hasProperty("cause", instanceOf(AssertionError.class))); ih.setAttribute(p, this, "eleven", "2"); - try { - ih.setAttribute(p, this, "eleven", "on"); - fail("on shouldn't be false"); - } catch (BuildException be) { - assertTrue(be.getCause() instanceof AssertionError); - } + ih.setAttribute(p, this, "eleven", "on"); + } + + /** + * Fail: on shouldn't be false + */ + @Test + public void testAttributeSetterTwelve() { + thrown.expect(BuildException.class); + thrown.expect(hasProperty("cause", instanceOf(AssertionError.class))); ih.setAttribute(p, this, "twelve", "2"); - try { - ih.setAttribute(p, this, "twelve", "on"); - fail("on shouldn't be false"); - } catch (BuildException be) { - assertTrue(be.getCause() instanceof AssertionError); - } + ih.setAttribute(p, this, "twelve", "on"); + } + + /** + * Fail: org.apache.tools.ant.Project shouldn't be equal to org.apache.tools.ant.ProjectHelper + */ + @Test + public void testAttributeSetterThirteen() { + thrown.expect(BuildException.class); + thrown.expect(hasProperty("cause", instanceOf(AssertionError.class))); ih.setAttribute(p, this, "thirteen", "org.apache.tools.ant.Project"); - try { - ih.setAttribute(p, this, "thirteen", "org.apache.tools.ant.ProjectHelper"); - fail("org.apache.tools.ant.Project shouldn't be equal to org.apache.tools.ant.ProjectHelper"); - } catch (BuildException be) { - assertTrue(be.getCause() instanceof AssertionError); - } - try { - ih.setAttribute(p, this, "thirteen", "org.apache.tools.ant.Project2"); - fail("org.apache.tools.ant.Project2 doesn't exist"); - } catch (BuildException be) { - assertTrue(be.getCause() instanceof ClassNotFoundException); - } + ih.setAttribute(p, this, "thirteen", "org.apache.tools.ant.ProjectHelper"); + } + + /** + * Fail: org.apache.tools.ant.Project2 doesn't exist + */ + @Test + public void testAttributeSetterThirteenNonExistentClass() { + thrown.expect(BuildException.class); + thrown.expect(hasProperty("cause", instanceOf(ClassNotFoundException.class))); + ih.setAttribute(p, this, "thirteen", "org.apache.tools.ant.Project2"); + } + + /** + * Fail: 2 shouldn't be equal to three as StringBuffer + */ + @Test + public void testAttributeSetterFourteen() { + thrown.expect(BuildException.class); + thrown.expect(hasProperty("cause", instanceOf(ComparisonFailure.class))); ih.setAttribute(p, this, "fourteen", "2"); - try { ih.setAttribute(p, this, "fourteen", "on"); - fail("2 shouldn't be equals to three - as StringBuffer"); - } catch (BuildException be) { - assertTrue(be.getCause() instanceof ComparisonFailure); - } + } + + /** + * Fail: o shouldn't be equal to a + */ + @Test + public void testAttributeSetterFifteen() { + thrown.expect(BuildException.class); + thrown.expect(hasProperty("cause", instanceOf(AssertionError.class))); ih.setAttribute(p, this, "fifteen", "abcd"); - try { - ih.setAttribute(p, this, "fifteen", "on"); - fail("o shouldn't be equal to a"); - } catch (BuildException be) { - assertTrue(be.getCause() instanceof AssertionError); - } + ih.setAttribute(p, this, "fifteen", "on"); + } + + /** + * Fail: o shouldn't be equal to a + */ + @Test + public void testAttributeSetterSixteen() { + thrown.expect(BuildException.class); + thrown.expect(hasProperty("cause", instanceOf(AssertionError.class))); ih.setAttribute(p, this, "sixteen", "abcd"); - try { - ih.setAttribute(p, this, "sixteen", "on"); - fail("o shouldn't be equal to a"); - } catch (BuildException be) { - assertTrue(be.getCause() instanceof AssertionError); - } + ih.setAttribute(p, this, "sixteen", "on"); + } + + /** + * Fail: 17 shouldn't be equals to three + */ + @Test + public void testAttributeSetterSeventeen() { + thrown.expect(BuildException.class); + thrown.expect(hasProperty("cause", instanceOf(AssertionError.class))); ih.setAttribute(p, this, "seventeen", "17"); - try { - ih.setAttribute(p, this, "seventeen", "3"); - fail("17 shouldn't be equals to three"); - } catch (BuildException be) { - assertTrue(be.getCause() instanceof AssertionError); - } + ih.setAttribute(p, this, "seventeen", "3"); + } + + /** + * Fail: 18 shouldn't be equals to three + */ + @Test + public void testAttributeSetterEighteen() { + thrown.expect(BuildException.class); + thrown.expect(hasProperty("cause", instanceOf(AssertionError.class))); ih.setAttribute(p, this, "eightteen", "18"); - try { - ih.setAttribute(p, this, "eightteen", "3"); - fail("18 shouldn't be equals to three"); - } catch (BuildException be) { - assertTrue(be.getCause() instanceof AssertionError); - } + ih.setAttribute(p, this, "eightteen", "3"); + } + + /** + * Fail: 19 shouldn't be equals to three + */ + @Test + public void testAttributeSetterNineteen() throws BuildException { + thrown.expect(BuildException.class); + thrown.expect(hasProperty("cause", instanceOf(AssertionError.class))); ih.setAttribute(p, this, "nineteen", "19"); - try { - ih.setAttribute(p, this, "nineteen", "3"); - fail("19 shouldn't be equals to three"); - } catch (BuildException be) { - assertTrue(be.getCause() instanceof AssertionError); - } + ih.setAttribute(p, this, "nineteen", "3"); } private Map> getExpectedAttributes() { @@ -479,8 +582,7 @@ public class IntrospectionHelperTest { /* * JUnit 3.7 adds a getName method to TestCase - so we now - * have a name attribute in IntrospectionHelperTest if we run - * under JUnit 3.7 but not in earlier versions. + * have a name attribute in IntrospectionHelperTest. * * Simply add it here and remove it after the tests. */ @@ -512,20 +614,17 @@ public class IntrospectionHelperTest { for (Map.Entry> entry : actualMap.entrySet()) { String attrName = entry.getKey(); Class attrClass = attrMap.get(attrName); - assertNotNull("Support for " + attrName + - " in IntrospectionHelperTest?", attrClass); + assertNotNull("Support for " + attrName + " in IntrospectionHelperTest?", attrClass); assertEquals("Type of " + attrName, attrClass, entry.getValue()); attrMap.remove(attrName); } attrMap.remove("name"); assertTrue("Found all", attrMap.isEmpty()); + thrown.expect(UnsupportedOperationException.class); + // TODO we should be asserting a value somewhere in here // Check it's a read-only map. - try { - actualMap.clear(); - //TODO we should be asserting a value somewhere in here - } catch (UnsupportedOperationException e) { - } + actualMap.clear(); } @Test @@ -559,12 +658,9 @@ public class IntrospectionHelperTest { assertAttrMethod("twenty", "setTwenty", Path.class, new File(projectBasedir + 20).toPath(), Paths.get("toto")); - try { - assertAttrMethod("onehundred", null, null, null, null); - fail("Should have raised a BuildException!"); - } catch (BuildException e) { - //TODO we should be asserting a value in here - } + thrown.expect(BuildException.class); + thrown.expectMessage("doesn't support the \"onehundred\" attribute."); + assertAttrMethod("onehundred", null, null, null, null); } private void assertAttrMethod(String attrName, String methodName, @@ -675,8 +771,7 @@ public class IntrospectionHelperTest { // combinatorics are too hard to check. We really only want // to ensure that the more derived Hashtable can be found // before Map. -// assertMethod(extensions.get(0), "add", Number.class, -// new Integer(2), new Integer(3)); + // assertMethod(extensions.get(0), "add", Number.class, new Integer(2), new Integer(3)); // addConfigured(Hashtable) should come before addConfigured(Map) assertMethod(extensions.get(adders - 2), @@ -703,12 +798,10 @@ public class IntrospectionHelperTest { try { m.invoke(this, badArg); - fail("Should have raised an assertion exception"); } catch (IllegalAccessException e) { throw new BuildException(e); } catch (InvocationTargetException e) { - Throwable t = e.getTargetException(); - assertTrue(t.toString(), t instanceof AssertionError); + assertThat(e, hasProperty("cause", instanceOf(AssertionError.class))); } } diff --git a/src/tests/junit/org/apache/tools/ant/types/PermissionsTest.java b/src/tests/junit/org/apache/tools/ant/types/PermissionsTest.java index 091258879..f90fe91d3 100644 --- a/src/tests/junit/org/apache/tools/ant/types/PermissionsTest.java +++ b/src/tests/junit/org/apache/tools/ant/types/PermissionsTest.java @@ -19,16 +19,23 @@ package org.apache.tools.ant.types; import org.apache.tools.ant.ExitException; +import org.junit.After; import org.junit.Before; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; -import static org.junit.Assert.fail; +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.hasProperty; /** * JUnit 4 testcases for org.apache.tools.ant.types.Permissions. */ public class PermissionsTest { + @Rule + public ExpectedException thrown = ExpectedException.none(); + Permissions perms; @Before @@ -67,90 +74,62 @@ public class PermissionsTest { perm.setName("os.*"); perm.setClass("java.util.PropertyPermission"); perms.addConfiguredRevoke(perm); + + // Allow loading Hamcrest Matcher classes on demand + perm = new Permissions.Permission(); + perm.setActions("read"); + perm.setName("<>"); + perm.setClass("java.io.FilePermission"); + perms.addConfiguredGrant(perm); + + perms.setSecurityManager(); + } + + @After + public void tearDown() { + perms.restoreSecurityManager(); } /** Tests a permission that is granted per default. */ @Test public void testDefaultGranted() { - perms.setSecurityManager(); - try { - System.getProperty("line.separator"); - } finally { - perms.restoreSecurityManager(); - } + System.getProperty("line.separator"); } /** Tests a permission that has been granted later via wildcard. */ @Test public void testGranted() { - perms.setSecurityManager(); - try { - String s = System.getProperty("user.name"); - System.setProperty("user.name", s); - } finally { - perms.restoreSecurityManager(); - } + System.setProperty("user.name", System.getProperty("user.name")); } /** Tests a permission that has been granted and revoked later. */ - @Test + @Test(expected = SecurityException.class) public void testGrantedAndRevoked() { - perms.setSecurityManager(); - try { - String s = System.getProperty("user.home"); - System.setProperty("user.home", s); - fail("Could perform an action that should have been forbidden."); - } catch (SecurityException e) { - // Was expected, test passes - } finally { - perms.restoreSecurityManager(); - } + System.setProperty("user.home", System.getProperty("user.home")); } /** Tests a permission that is granted as per default but revoked later via wildcard. */ - @Test + @Test(expected = SecurityException.class) public void testDefaultRevoked() { - perms.setSecurityManager(); - try { - System.getProperty("os.name"); - fail("Could perform an action that should have been forbidden."); - } catch (SecurityException e) { - // Was expected, test passes - } finally { - perms.restoreSecurityManager(); - } + System.getProperty("os.name"); } + /** Tests a permission that has not been granted or revoked. */ - @Test + @Test(expected = SecurityException.class) public void testOther() { - String ls = System.getProperty("line.separator"); - perms.setSecurityManager(); - try { - System.setProperty("line.separator",ls); - fail("Could perform an action that should have been forbidden."); - } catch (SecurityException e) { - //TODO assert exception message - // Was expected, test passes - } finally { - perms.restoreSecurityManager(); - } + System.setProperty("line.separator", System.lineSeparator()); } /** Tests an exit condition. */ @Test public void testExit() { - perms.setSecurityManager(); + thrown.expect(ExitException.class); + thrown.expect(hasProperty("status", equalTo(3))); try { System.out.println("If this is the last line on standard out the testExit f.a.i.l.e.d"); System.exit(3); - fail("Totally impossible that this fail is ever executed. Please let me know if it is!"); - } catch (ExitException e) { - if (e.getStatus() != 3) { - fail("Received wrong exit status in Exit Exception."); - } - System.out.println("testExit successful."); } finally { - perms.restoreSecurityManager(); + System.out.println("testExit successful."); } } diff --git a/src/tests/junit/org/apache/tools/ant/types/selectors/ContainsSelectorTest.java b/src/tests/junit/org/apache/tools/ant/types/selectors/ContainsSelectorTest.java index 1b19c053d..3522ad743 100644 --- a/src/tests/junit/org/apache/tools/ant/types/selectors/ContainsSelectorTest.java +++ b/src/tests/junit/org/apache/tools/ant/types/selectors/ContainsSelectorTest.java @@ -18,13 +18,14 @@ package org.apache.tools.ant.types.selectors; +import static org.junit.Assert.assertEquals; + import org.apache.tools.ant.BuildException; import org.apache.tools.ant.types.Parameter; +import org.junit.Before; import org.junit.Rule; import org.junit.Test; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.fail; +import org.junit.rules.ExpectedException; /** * Tests Contains Selectors. @@ -35,76 +36,81 @@ public class ContainsSelectorTest { @Rule public final BaseSelectorRule selectorRule = new BaseSelectorRule(); + @Rule + public ExpectedException thrown = ExpectedException.none(); + + private ContainsSelector s; + + @Before + public void setUp() { + s = new ContainsSelector(); + } /** * Test the code that validates the selector. */ @Test - public void testValidate() { - ContainsSelector s = new ContainsSelector(); - try { - s.isSelected(selectorRule.getProject().getBaseDir(),selectorRule.getFilenames()[0],selectorRule.getFiles()[0]); - fail("ContainsSelector did not check for required field 'text'"); - } catch (BuildException be1) { - assertEquals("The text attribute is required", be1.getMessage()); - } + public void testValidateMissingParameter() { + thrown.expect(BuildException.class); + thrown.expectMessage("The text attribute is required"); + s.isSelected(selectorRule.getProject().getBaseDir(), selectorRule.getFilenames()[0], + selectorRule.getFiles()[0]); + } - s = new ContainsSelector(); + @Test + public void testValidateInvalidParameter() { + thrown.expect(BuildException.class); + thrown.expectMessage("Invalid parameter garbage in"); Parameter param = new Parameter(); param.setName("garbage in"); param.setValue("garbage out"); Parameter[] params = {param}; s.setParameters(params); - try { - s.isSelected(selectorRule.getProject().getBaseDir(),selectorRule.getFilenames()[0],selectorRule.getFiles()[0]); - fail("ContainsSelector did not check for valid parameter element"); - } catch (BuildException be2) { - assertEquals("Invalid parameter garbage in", be2.getMessage()); - } - + s.isSelected(selectorRule.getProject().getBaseDir(), selectorRule.getFilenames()[0], + selectorRule.getFiles()[0]); } /** * Tests to make sure that the selector is selecting files correctly. */ @Test - public void testSelectionBehaviour() { - ContainsSelector s; - String results; - - s = new ContainsSelector(); + public void testSelectionBehaviourNoSuch() { s.setText("no such string in test files"); - results = selectorRule.selectionString(s); - assertEquals("TFFFFFFFFFFT", results); + assertEquals("TFFFFFFFFFFT", selectorRule.selectionString(s)); + } - s = new ContainsSelector(); + @Test + public void testSelectionBehaviourCase() { s.setText("Apache Ant"); - results = selectorRule.selectionString(s); - assertEquals("TFFFTFFFFFFT", results); + assertEquals("TFFFTFFFFFFT", selectorRule.selectionString(s)); + } - s = new ContainsSelector(); + @Test + public void testSelectionBehaviourCaseSensitive() { s.setText("apache ant"); s.setCasesensitive(true); - results = selectorRule.selectionString(s); - assertEquals("TFFFFFFFFFFT", results); + assertEquals("TFFFFFFFFFFT", selectorRule.selectionString(s)); + } - s = new ContainsSelector(); + @Test + public void testSelectionBehaviourCaseInsensitive() { s.setText("apache ant"); s.setCasesensitive(false); - results = selectorRule.selectionString(s); - assertEquals("TFFFTFFFFFFT", results); + assertEquals("TFFFTFFFFFFT", selectorRule.selectionString(s)); + } - s = new ContainsSelector(); + @Test + public void testSelectionBehaviourNoWhitespace() { s.setText("ApacheAnt"); s.setIgnorewhitespace(true); - results = selectorRule.selectionString(s); - assertEquals("TFFFTFFFFFFT", results); + assertEquals("TFFFTFFFFFFT", selectorRule.selectionString(s)); + } - s = new ContainsSelector(); + @Test + public void testSelectionBehaviourIgnoreWhitespace() { s.setText("A p a c h e A n t"); s.setIgnorewhitespace(true); - results = selectorRule.selectionString(s); - assertEquals("TFFFTFFFFFFT", results); + assertEquals("TFFFTFFFFFFT", selectorRule.selectionString(s)); } } diff --git a/src/tests/junit/org/apache/tools/ant/types/selectors/DateSelectorTest.java b/src/tests/junit/org/apache/tools/ant/types/selectors/DateSelectorTest.java index 54bd5e430..4b9341302 100644 --- a/src/tests/junit/org/apache/tools/ant/types/selectors/DateSelectorTest.java +++ b/src/tests/junit/org/apache/tools/ant/types/selectors/DateSelectorTest.java @@ -20,8 +20,10 @@ package org.apache.tools.ant.types.selectors; import org.apache.tools.ant.BuildException; import org.apache.tools.ant.types.Parameter; +import org.junit.Before; import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; import static org.junit.Assert.assertEquals; import static org.junit.Assert.fail; @@ -36,159 +38,160 @@ public class DateSelectorTest { @Rule public final BaseSelectorRule selectorRule = new BaseSelectorRule(); + @Rule + public ExpectedException thrown = ExpectedException.none(); + + private DateSelector s; + + private DateSelector.TimeComparisons before; + + private DateSelector.TimeComparisons equal; + + private DateSelector.TimeComparisons after; + + @Before + public void setUp() { + s = new DateSelector(); + + before = new DateSelector.TimeComparisons(); + before.setValue("before"); + + equal = new DateSelector.TimeComparisons(); + equal.setValue("equal"); + + after = new DateSelector.TimeComparisons(); + after.setValue("after"); + } + /** * Test the code that validates the selector. */ @Test - public void testValidate() { - DateSelector s = new DateSelector(); - try { - s.isSelected(selectorRule.getProject().getBaseDir(), - selectorRule.getFilenames()[0], selectorRule.getFiles()[0]); - fail("DateSelector did not check for required fields"); - } catch (BuildException be1) { - assertEquals("You must provide a datetime or the number of " - + "milliseconds.", be1.getMessage()); - } + public void testValidateFields() { + thrown.expect(BuildException.class); + thrown.expectMessage("You must provide a datetime or the number of milliseconds."); + s.isSelected(selectorRule.getProject().getBaseDir(), + selectorRule.getFilenames()[0], selectorRule.getFiles()[0]); + } - s = new DateSelector(); + @Test + public void testValidateRange() { + thrown.expect(BuildException.class); + thrown.expectMessage("Date of 01/01/1969 01:01 AM results in negative milliseconds value" + + " relative to epoch (January 1, 1970, 00:00:00 GMT)."); s.setDatetime("01/01/1969 01:01 AM"); - try { - s.isSelected(selectorRule.getProject().getBaseDir(), - selectorRule.getFilenames()[0], selectorRule.getFiles()[0]); - fail("DateSelector did not check for Datetime being in the " - + "allowable range"); - } catch (BuildException be2) { - assertEquals("Date of 01/01/1969 01:01 AM results in negative " - + "milliseconds value relative to epoch (January 1, " - + "1970, 00:00:00 GMT).", be2.getMessage()); - } + s.isSelected(selectorRule.getProject().getBaseDir(), + selectorRule.getFilenames()[0], selectorRule.getFiles()[0]); + } - s = new DateSelector(); + @Test + public void testValidateFormat() { + thrown.expect(BuildException.class); + thrown.expectMessage("Date of this is not a date Cannot be parsed correctly." + + " It should be in 'MM/dd/yyyy hh:mm a' format."); s.setDatetime("this is not a date"); - try { - s.isSelected(selectorRule.getProject().getBaseDir(), - selectorRule.getFilenames()[0], selectorRule.getFiles()[0]); - fail("DateSelector did not check for Datetime being in a " - + "valid format"); - } catch (BuildException be3) { - assertEquals("Date of this is not a date" - + " Cannot be parsed correctly. It should be in" - + " 'MM/dd/yyyy hh:mm a' format.", be3.getMessage()); - } + s.isSelected(selectorRule.getProject().getBaseDir(), + selectorRule.getFilenames()[0], selectorRule.getFiles()[0]); + } - s = new DateSelector(); + @Test + public void testValidateParameterIn() { + thrown.expect(BuildException.class); + thrown.expectMessage("Invalid parameter garbage in"); Parameter param = new Parameter(); param.setName("garbage in"); param.setValue("garbage out"); Parameter[] params = new Parameter[1]; params[0] = param; s.setParameters(params); - try { - s.isSelected(selectorRule.getProject().getBaseDir(), - selectorRule.getFilenames()[0], selectorRule.getFiles()[0]); - fail("DateSelector did not check for valid parameter element"); - } catch (BuildException be4) { - assertEquals("Invalid parameter garbage in", be4.getMessage()); - } + s.isSelected(selectorRule.getProject().getBaseDir(), + selectorRule.getFilenames()[0], selectorRule.getFiles()[0]); + } - s = new DateSelector(); - param = new Parameter(); + @Test + public void testValidateParameterMillis() { + thrown.expect(BuildException.class); + thrown.expectMessage("Invalid millisecond setting garbage out"); + Parameter param = new Parameter(); param.setName("millis"); param.setValue("garbage out"); + Parameter[] params = new Parameter[1]; params[0] = param; s.setParameters(params); - try { - s.isSelected(selectorRule.getProject().getBaseDir(), - selectorRule.getFilenames()[0], selectorRule.getFiles()[0]); - fail("DateSelector did not check for valid millis parameter"); - } catch (BuildException be5) { - assertEquals("Invalid millisecond setting garbage out", - be5.getMessage()); - } + s.isSelected(selectorRule.getProject().getBaseDir(), + selectorRule.getFilenames()[0], selectorRule.getFiles()[0]); + } - s = new DateSelector(); - param = new Parameter(); + @Test + public void testValidateParameterGranularity() { + thrown.expect(BuildException.class); + thrown.expectMessage("Invalid granularity setting garbage out"); + Parameter param = new Parameter(); param.setName("granularity"); param.setValue("garbage out"); + Parameter[] params = new Parameter[1]; params[0] = param; s.setParameters(params); - try { - s.isSelected(selectorRule.getProject().getBaseDir(), - selectorRule.getFilenames()[0], selectorRule.getFiles()[0]); - fail("DateSelector did not check for valid granularity parameter"); - } catch (BuildException be6) { - assertEquals("Invalid granularity setting garbage out", - be6.getMessage()); - } - + s.isSelected(selectorRule.getProject().getBaseDir(), + selectorRule.getFilenames()[0], selectorRule.getFiles()[0]); } /** * Tests to make sure that the selector is selecting files correctly. */ @Test - public void testSelectionBehaviour() { - DateSelector s; - String results; - - DateSelector.TimeComparisons before = new - DateSelector.TimeComparisons(); - before.setValue("before"); - DateSelector.TimeComparisons equal = new - DateSelector.TimeComparisons(); - equal.setValue("equal"); - DateSelector.TimeComparisons after = new - DateSelector.TimeComparisons(); - after.setValue("after"); - - - s = new DateSelector(); + public void testSelectionBehaviourBefore() { s.setDatetime("10/10/1999 1:45 PM"); s.setWhen(before); - results = selectorRule.selectionString(s); - assertEquals("TFFFFFFFFFFT", results); + assertEquals("TFFFFFFFFFFT", selectorRule.selectionString(s)); + } - s = new DateSelector(); + @Test + public void testSelectionBehaviourBeforeCheckdirs() { s.setDatetime("10/10/1999 1:45 PM"); s.setWhen(before); s.setCheckdirs(true); - results = selectorRule.selectionString(s); - assertEquals("FFFFFFFFFFFF", results); + assertEquals("FFFFFFFFFFFF", selectorRule.selectionString(s)); + } - s = new DateSelector(); + @Test + public void testSelectionBehaviourAfter() { s.setDatetime("10/10/1999 1:45 PM"); s.setWhen(after); - results = selectorRule.selectionString(s); - assertEquals("TTTTTTTTTTTT", results); + assertEquals("TTTTTTTTTTTT", selectorRule.selectionString(s)); + } - s = new DateSelector(); + @Test + public void testSelectionBehaviourBefore2K() { s.setDatetime("11/21/2001 4:54 AM"); s.setWhen(before); - results = selectorRule.selectionString(s); - assertEquals("TFTFFFFFFFFT", results); + assertEquals("TFTFFFFFFFFT", selectorRule.selectionString(s)); + } - s = new DateSelector(); + @Test + public void testSelectionBehaviourEqual() { s.setDatetime("11/21/2001 4:55 AM"); - - long milliseconds = s.getMillis(); s.setWhen(equal); - results = selectorRule.selectionString(s); - assertEquals("TTFFTFFFTTTT", results); + assertEquals("TTFFTFFFTTTT", selectorRule.selectionString(s)); + } - s = new DateSelector(); - s.setMillis(milliseconds); + @Test + public void testSelectionBehaviourEqualMillis() { + s.setDatetime("11/21/2001 4:55 AM"); + s.setMillis(s.getMillis()); s.setWhen(equal); - results = selectorRule.selectionString(s); - assertEquals("TTFFTFFFTTTT", results); + assertEquals("TTFFTFFFTTTT", selectorRule.selectionString(s)); + } - s = new DateSelector(); + @Test + public void testSelectionBehaviourAfter2K() { s.setDatetime("11/21/2001 4:56 AM"); s.setWhen(after); - results = selectorRule.selectionString(s); - assertEquals("TFFTFTTTFFFT", results); + assertEquals("TFFTFTTTFFFT", selectorRule.selectionString(s)); + } - s = new DateSelector(); + @Test + public void testSelectionBehaviourAfterNamed() { Parameter param1 = new Parameter(); Parameter param2 = new Parameter(); param1.setName("datetime"); @@ -197,28 +200,29 @@ public class DateSelectorTest { param2.setValue("after"); Parameter[] params = {param1, param2}; s.setParameters(params); - results = selectorRule.selectionString(s); - assertEquals("TFFTFTTTFFFT", results); + assertEquals("TFFTFTTTFFFT", selectorRule.selectionString(s)); + } - s = new DateSelector(); + @Test + public void testSelectionBehaviourAfterWithGranularity() { long testtime = selectorRule.getMirrorFiles()[5].lastModified(); s.setMillis(testtime); s.setWhen(after); s.setGranularity(2); - // setup the modified timestamp to match what the test needs, although be aware that the 3rd and 4th - // files don't exist so can't be changed, so don't try and loop over them + // setup the modified timestamp to match what the test needs, although be aware that + // the 3rd and 4th files don't exist so can't be changed, so don't try and loop over them for (int i = 1; i <= 2; i++) { assumeTrue("Cannot setup file times for test", selectorRule.getMirrorFiles()[i].setLastModified(testtime - (3 * 60 * 60 * 100))); } + assertEquals("TFFFFTTTTTTT", selectorRule.mirrorSelectionString(s)); + } - results = selectorRule.mirrorSelectionString(s); - assertEquals("TFFFFTTTTTTT", results); - - s = new DateSelector(); - testtime = selectorRule.getMirrorFiles()[6].lastModified(); + @Test + public void testSelectionBehaviourBeforeWithGranularity() { + long testtime = selectorRule.getMirrorFiles()[6].lastModified(); s.setMillis(testtime); s.setWhen(before); s.setGranularity(2); @@ -227,9 +231,7 @@ public class DateSelectorTest { selectorRule.getMirrorFiles()[i].setLastModified(testtime + (3 * 60 * 60 * 100))); } - results = selectorRule.mirrorSelectionString(s); - assertEquals("TTTTTTTFFFFT", results); - + assertEquals("TTTTTTTFFFFT", selectorRule.mirrorSelectionString(s)); } } diff --git a/src/tests/junit/org/apache/tools/ant/types/selectors/DependSelectorTest.java b/src/tests/junit/org/apache/tools/ant/types/selectors/DependSelectorTest.java index ec95878a8..006ef0d25 100644 --- a/src/tests/junit/org/apache/tools/ant/types/selectors/DependSelectorTest.java +++ b/src/tests/junit/org/apache/tools/ant/types/selectors/DependSelectorTest.java @@ -18,15 +18,16 @@ package org.apache.tools.ant.types.selectors; +import static org.junit.Assert.assertEquals; + +import java.io.File; + import org.apache.tools.ant.BuildException; import org.apache.tools.ant.types.Mapper; +import org.junit.Before; import org.junit.Rule; import org.junit.Test; - -import java.io.File; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.fail; +import org.junit.rules.ExpectedException; /** @@ -35,48 +36,44 @@ import static org.junit.Assert.fail; */ public class DependSelectorTest { + private DependSelector s; + @Rule public final BaseSelectorRule selectorRule = new BaseSelectorRule(); + @Rule + public ExpectedException thrown = ExpectedException.none(); + + @Before + public void setUp() { + s = new DependSelector(); + } + @Test public void testValidateSingleMapper() { - try { - DependSelector s = new DependSelector(); - s.createMapper(); - s.createMapper(); - fail("DependSelector allowed more than one nested mapper."); - } catch (BuildException be1) { - assertEquals("Cannot define more than one mapper", - be1.getMessage()); - } + thrown.expect(BuildException.class); + thrown.expectMessage("Cannot define more than one mapper"); + s.createMapper(); + s.createMapper(); } @Test public void testValidateRequiredFields() { - try { - DependSelector s = new DependSelector(); - s.isSelected(selectorRule.getProject().getBaseDir(), selectorRule.getFilenames()[0], selectorRule.getFiles()[0]); - fail("DependSelector did not check for required fields"); - } catch (BuildException be2) { - assertEquals("The targetdir attribute is required.", - be2.getMessage()); - } - + thrown.expect(BuildException.class); + thrown.expectMessage("The targetdir attribute is required."); + s.isSelected(selectorRule.getProject().getBaseDir(), selectorRule.getFilenames()[0], + selectorRule.getFiles()[0]); } @Test public void testNoMapper() { - DependSelector s = new DependSelector(); s.setTargetdir(selectorRule.getBeddir()); - - String results = selectorRule.selectionString(s); - assertEquals("FFFFFFFFFFFF", results); + assertEquals("FFFFFFFFFFFF", selectorRule.selectionString(s)); } @Test public void testIdentityMapper() { - DependSelector s = new DependSelector(); s.setTargetdir(selectorRule.getBeddir()); Mapper.MapperType identity = new Mapper.MapperType(); @@ -85,13 +82,11 @@ public class DependSelectorTest { Mapper m = s.createMapper(); m.setType(identity); - String results = selectorRule.selectionString(s); - assertEquals("FFFFFFFFFFFF", results); + assertEquals("FFFFFFFFFFFF", selectorRule.selectionString(s)); } @Test public void testMergeMapper() { - DependSelector s = new DependSelector(); s.setTargetdir(selectorRule.getBeddir()); Mapper.MapperType merge = new Mapper.MapperType(); @@ -101,13 +96,11 @@ public class DependSelectorTest { m.setType(merge); m.setTo("asf-logo.gif.gz"); - String results = selectorRule.selectionString(s); - assertEquals("TFFFFTTTFFF", results.substring(0,11)); + assertEquals("TFFFFTTTFFF", selectorRule.selectionString(s).substring(0, 11)); } @Test public void testMergeMapper2() { - DependSelector s = new DependSelector(); s.setTargetdir(selectorRule.getBeddir()); Mapper.MapperType merge = new Mapper.MapperType(); @@ -116,13 +109,12 @@ public class DependSelectorTest { Mapper m = s.createMapper(); m.setType(merge); m.setTo("asf-logo.gif.bz2"); - String results = selectorRule.selectionString(s); - assertEquals("TTFTTTTTTTTT", results); + + assertEquals("TTFTTTTTTTTT", selectorRule.selectionString(s)); } @Test public void testGlobMapperRelativePath() { - DependSelector s = new DependSelector(); File subdir = new File("selectortest/tar/bz2"); s.setTargetdir(subdir); @@ -134,13 +126,11 @@ public class DependSelectorTest { m.setFrom("*.bz2"); m.setTo("*.tar.bz2"); - String results = selectorRule.selectionString(s); - assertEquals("FFTFFFFFFTTF", results); + assertEquals("FFTFFFFFFTTF", selectorRule.selectionString(s)); } @Test public void testRestrictedGlobMapper() { - DependSelector s = new DependSelector(); File subdir = new File(selectorRule.getBeddir(), "tar/bz2"); s.setTargetdir(subdir); @@ -151,22 +141,19 @@ public class DependSelectorTest { m.setType(glob); m.setFrom("*.bz2"); m.setTo("*.tar.bz2"); - String results = selectorRule.selectionString(s); - assertEquals("FFFFFFFFFTTF", results); + + assertEquals("FFFFFFFFFTTF", selectorRule.selectionString(s)); } @Test public void testSelectionNoMapper() { - DependSelector s = new DependSelector(); s.setTargetdir(new File(selectorRule.getOutputDir(), "selectortest2")); - String results = selectorRule.selectionString(s); - assertEquals("FFFTTFFFFFFF", results); + assertEquals("FFFTTFFFFFFF", selectorRule.selectionString(s)); } @Test public void testMirroredSelection() { - DependSelector s = new DependSelector(); s.setTargetdir(new File(selectorRule.getOutputDir(), "selectortest2/tar/bz2")); Mapper.MapperType glob = new Mapper.MapperType(); @@ -176,10 +163,9 @@ public class DependSelectorTest { m.setType(glob); m.setFrom("*.bz2"); m.setTo("*.tar.bz2"); - String results = selectorRule.mirrorSelectionString(s); - assertEquals("FFFFFFFFFTTF", results); - results = selectorRule.selectionString(s); - assertEquals("FFFFFFFFFTTF", results); + + assertEquals("FFFFFFFFFTTF", selectorRule.mirrorSelectionString(s)); + assertEquals("FFFFFFFFFTTF", selectorRule.selectionString(s)); } } diff --git a/src/tests/junit/org/apache/tools/ant/types/selectors/DepthSelectorTest.java b/src/tests/junit/org/apache/tools/ant/types/selectors/DepthSelectorTest.java index 3c7e429f0..ad02cda6f 100644 --- a/src/tests/junit/org/apache/tools/ant/types/selectors/DepthSelectorTest.java +++ b/src/tests/junit/org/apache/tools/ant/types/selectors/DepthSelectorTest.java @@ -18,13 +18,14 @@ package org.apache.tools.ant.types.selectors; +import static org.junit.Assert.assertEquals; + import org.apache.tools.ant.BuildException; import org.apache.tools.ant.types.Parameter; +import org.junit.Before; import org.junit.Rule; import org.junit.Test; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.fail; +import org.junit.rules.ExpectedException; /** * Tests Depth Selectors @@ -35,111 +36,112 @@ public class DepthSelectorTest { @Rule public final BaseSelectorRule selectorRule = new BaseSelectorRule(); + @Rule + public ExpectedException thrown = ExpectedException.none(); + + private DepthSelector s; + + @Before + public void setUp() { + s = new DepthSelector(); + } /** * Test the code that validates the selector. */ @Test - public void testValidate() { - DepthSelector s = new DepthSelector(); - try { - s.isSelected(selectorRule.getProject().getBaseDir(),selectorRule.getFilenames()[0],selectorRule.getFiles()[0]); - fail("DepthSelector did not check for required fields"); - } catch (BuildException be1) { - assertEquals("You must set at least one of the min or the " + - "max levels.", be1.getMessage()); - } + public void testValidateRequiredFields() { + thrown.expect(BuildException.class); + thrown.expectMessage("You must set at least one of the min or the max levels."); + s.isSelected(selectorRule.getProject().getBaseDir(), selectorRule.getFilenames()[0], + selectorRule.getFiles()[0]); + } - s = new DepthSelector(); + @Test + public void testValidateDepth() { + thrown.expect(BuildException.class); + thrown.expectMessage("The maximum depth is lower than the minimum."); s.setMin(5); s.setMax(2); - try { - s.isSelected(selectorRule.getProject().getBaseDir(),selectorRule.getFilenames()[0],selectorRule.getFiles()[0]); - fail("DepthSelector did not check for maximum being higher " - + "than minimum"); - } catch (BuildException be2) { - assertEquals("The maximum depth is lower than the minimum.", - be2.getMessage()); - } + s.isSelected(selectorRule.getProject().getBaseDir(), selectorRule.getFilenames()[0], + selectorRule.getFiles()[0]); + } - s = new DepthSelector(); + @Test + public void testValidateParameterName() { + thrown.expect(BuildException.class); + thrown.expectMessage("Invalid parameter garbage in"); Parameter param = new Parameter(); param.setName("garbage in"); param.setValue("garbage out"); Parameter[] params = new Parameter[1]; params[0] = param; s.setParameters(params); - try { - s.isSelected(selectorRule.getProject().getBaseDir(),selectorRule.getFilenames()[0],selectorRule.getFiles()[0]); - fail("DepthSelector did not check for valid parameter element"); - } catch (BuildException be3) { - assertEquals("Invalid parameter garbage in", be3.getMessage()); - } + s.isSelected(selectorRule.getProject().getBaseDir(), selectorRule.getFilenames()[0], + selectorRule.getFiles()[0]); + } - s = new DepthSelector(); - param = new Parameter(); + @Test + public void testValidateMinValue() { + thrown.expect(BuildException.class); + thrown.expectMessage("Invalid minimum value garbage out"); + Parameter param = new Parameter(); param.setName("min"); param.setValue("garbage out"); + Parameter[] params = new Parameter[1]; params[0] = param; s.setParameters(params); - try { - s.isSelected(selectorRule.getProject().getBaseDir(),selectorRule.getFilenames()[0],selectorRule.getFiles()[0]); - fail("DepthSelector accepted bad minimum as parameter"); - } catch (BuildException be4) { - assertEquals("Invalid minimum value garbage out", - be4.getMessage()); - } + s.isSelected(selectorRule.getProject().getBaseDir(), selectorRule.getFilenames()[0], + selectorRule.getFiles()[0]); + } - s = new DepthSelector(); - param = new Parameter(); + @Test + public void testValidateMaxValue() { + thrown.expect(BuildException.class); + thrown.expectMessage("Invalid maximum value garbage out"); + Parameter param = new Parameter(); param.setName("max"); param.setValue("garbage out"); + Parameter[] params = new Parameter[1]; params[0] = param; s.setParameters(params); - try { - s.isSelected(selectorRule.getProject().getBaseDir(),selectorRule.getFilenames()[0],selectorRule.getFiles()[0]); - fail("DepthSelector accepted bad maximum as parameter"); - } catch (BuildException be5) { - assertEquals("Invalid maximum value garbage out", - be5.getMessage()); - } - + s.isSelected(selectorRule.getProject().getBaseDir(), selectorRule.getFilenames()[0], + selectorRule.getFiles()[0]); } /** * Tests to make sure that the selector is selecting files correctly. */ @Test - public void testSelectionBehaviour() { - DepthSelector s; - String results; - - s = new DepthSelector(); + public void testSelectionBehaviourMinMax() { s.setMin(20); s.setMax(25); - results = selectorRule.selectionString(s); - assertEquals("FFFFFFFFFFFF", results); + assertEquals("FFFFFFFFFFFF", selectorRule.selectionString(s)); + } - s = new DepthSelector(); + @Test + public void testSelectionBehaviourMin0() { s.setMin(0); - results = selectorRule.selectionString(s); - assertEquals("TTTTTTTTTTTT", results); + assertEquals("TTTTTTTTTTTT", selectorRule.selectionString(s)); + } - s = new DepthSelector(); + @Test + public void testSelectionBehaviourMin1() { s.setMin(1); - results = selectorRule.selectionString(s); - assertEquals("FFFFFTTTTTTT", results); + assertEquals("FFFFFTTTTTTT", selectorRule.selectionString(s)); + } - s = new DepthSelector(); + @Test + public void testSelectionBehaviourMax0() { s.setMax(0); - results = selectorRule.selectionString(s); - assertEquals("TTTTTFFFFFFF", results); + assertEquals("TTTTTFFFFFFF", selectorRule.selectionString(s)); + } - s = new DepthSelector(); + @Test + public void testSelectionBehaviourMin1Max1() { s.setMin(1); s.setMax(1); - results = selectorRule.selectionString(s); - assertEquals("FFFFFTTTFFFT", results); + assertEquals("FFFFFTTTFFFT", selectorRule.selectionString(s)); } } diff --git a/src/tests/junit/org/apache/tools/ant/types/selectors/FilenameSelectorTest.java b/src/tests/junit/org/apache/tools/ant/types/selectors/FilenameSelectorTest.java index 09483bfa2..09a5086ac 100644 --- a/src/tests/junit/org/apache/tools/ant/types/selectors/FilenameSelectorTest.java +++ b/src/tests/junit/org/apache/tools/ant/types/selectors/FilenameSelectorTest.java @@ -18,13 +18,14 @@ package org.apache.tools.ant.types.selectors; +import static org.junit.Assert.assertEquals; + import org.apache.tools.ant.BuildException; import org.apache.tools.ant.types.Parameter; +import org.junit.Before; import org.junit.Rule; import org.junit.Test; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.fail; +import org.junit.rules.ExpectedException; /** * Tests Filename Selectors @@ -35,32 +36,41 @@ public class FilenameSelectorTest { @Rule public final BaseSelectorRule selectorRule = new BaseSelectorRule(); + @Rule + public ExpectedException thrown = ExpectedException.none(); + + private FilenameSelector s; + + @Before + public void setUp() { + s = new FilenameSelector(); + } + /** - * Test the code that validates the selector. + * Test the code that validates the selector: required attribute. */ @Test - public void testValidate() { - FilenameSelector s = new FilenameSelector(); - try { - s.isSelected(selectorRule.getProject().getBaseDir(),selectorRule.getFilenames()[0],selectorRule.getFiles()[0]); - fail("FilenameSelector did not check for required fields"); - } catch (BuildException be1) { - assertEquals("The name or regex attribute is required", be1.getMessage()); - } + public void testRequired() { + thrown.expect(BuildException.class); + thrown.expectMessage("The name or regex attribute is required"); + s.isSelected(selectorRule.getProject().getBaseDir(), selectorRule.getFilenames()[0], + selectorRule.getFiles()[0]); + } - s = new FilenameSelector(); + /** + * Test the code that validates the selector: invalid parameter. + */ + @Test + public void testValidate() { + thrown.expect(BuildException.class); + thrown.expectMessage("Invalid parameter garbage in"); Parameter param = new Parameter(); param.setName("garbage in"); param.setValue("garbage out"); Parameter[] params = {param}; s.setParameters(params); - try { - s.isSelected(selectorRule.getProject().getBaseDir(),selectorRule.getFilenames()[0],selectorRule.getFiles()[0]); - fail("FilenameSelector did not check for valid parameter element"); - } catch (BuildException be2) { - assertEquals("Invalid parameter garbage in", be2.getMessage()); - } - + s.isSelected(selectorRule.getProject().getBaseDir(), selectorRule.getFilenames()[0], + selectorRule.getFiles()[0]); } /** @@ -68,44 +78,41 @@ public class FilenameSelectorTest { */ @Test public void testSelectionBehaviour() { - FilenameSelector s; - String results; - - s = new FilenameSelector(); s.setName("no match possible"); - results = selectorRule.selectionString(s); - assertEquals("FFFFFFFFFFFF", results); + assertEquals("FFFFFFFFFFFF", selectorRule.selectionString(s)); + } - s = new FilenameSelector(); - s.setName("*.gz"); - results = selectorRule.selectionString(s); // This is turned off temporarily. There appears to be a bug // in SelectorUtils.matchPattern() where it is recursive on // Windows even if no ** is in pattern. - //assertEquals("FFFTFFFFFFFF", results); // Unix + // s.setName("*.gz"); + // String results = selectorRule.selectionString(s); + // assertEquals("FFFTFFFFFFFF", results); // Unix // vs - //assertEquals("FFFTFFFFTFFF", results); // Windows + // assertEquals("FFFTFFFFTFFF", results); // Windows - s = new FilenameSelector(); + @Test + public void testSelectionBehaviourNegate() { s.setName("**/*.gz"); s.setNegate(true); - results = selectorRule.selectionString(s); - assertEquals("TTTFTTTFFTTT", results); + assertEquals("TTTFTTTFFTTT", selectorRule.selectionString(s)); + } - s = new FilenameSelector(); + @Test + public void testSelectionBehaviourCaseInsensitive() { s.setName("**/*.GZ"); s.setCasesensitive(false); - results = selectorRule.selectionString(s); - assertEquals("FFFTFFFTTFFF", results); + assertEquals("FFFTFFFTTFFF", selectorRule.selectionString(s)); + } - s = new FilenameSelector(); + @Test + public void testSelectionBehaviourNamedParameter() { Parameter param1 = new Parameter(); param1.setName("name"); param1.setValue("**/*.bz2"); Parameter[] params = {param1}; s.setParameters(params); - results = selectorRule.selectionString(s); - assertEquals("FFTFFFFFFTTF", results); + assertEquals("FFTFFFFFFTTF", selectorRule.selectionString(s)); } } diff --git a/src/tests/junit/org/apache/tools/ant/types/selectors/PresentSelectorTest.java b/src/tests/junit/org/apache/tools/ant/types/selectors/PresentSelectorTest.java index 07b664d57..3d6d95f62 100644 --- a/src/tests/junit/org/apache/tools/ant/types/selectors/PresentSelectorTest.java +++ b/src/tests/junit/org/apache/tools/ant/types/selectors/PresentSelectorTest.java @@ -18,16 +18,16 @@ package org.apache.tools.ant.types.selectors; +import static org.junit.Assert.assertEquals; + +import java.io.File; + import org.apache.tools.ant.BuildException; import org.apache.tools.ant.types.Mapper; +import org.junit.Before; import org.junit.Rule; import org.junit.Test; - -import java.io.File; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.fail; - +import org.junit.rules.ExpectedException; /** * Tests Present Selectors @@ -35,34 +35,39 @@ import static org.junit.Assert.fail; */ public class PresentSelectorTest { + private PresentSelector s; + + private File beddir; @Rule public final BaseSelectorRule selectorRule = new BaseSelectorRule(); + @Rule + public ExpectedException thrown = ExpectedException.none(); + + @Before + public void setUp() { + s = new PresentSelector(); + beddir = selectorRule.getBeddir(); + } + /** * Test the code that validates the selector. */ @Test public void testValidate() { - PresentSelector s = new PresentSelector(); - try { - s.createMapper(); - s.createMapper(); - fail("PresentSelector allowed more than one nested mapper."); - } catch (BuildException be1) { - assertEquals("Cannot define more than one mapper", - be1.getMessage()); - } - - s = new PresentSelector(); - try { - s.isSelected(selectorRule.getProject().getBaseDir(),selectorRule.getFilenames()[0],selectorRule.getFiles()[0]); - fail("PresentSelector did not check for required fields"); - } catch (BuildException be2) { - assertEquals("The targetdir attribute is required.", - be2.getMessage()); - } + thrown.expect(BuildException.class); + thrown.expectMessage("Cannot define more than one mapper"); + s.createMapper(); + s.createMapper(); + } + @Test + public void testValidateAttributes() { + thrown.expect(BuildException.class); + thrown.expectMessage("The targetdir attribute is required."); + s.isSelected(selectorRule.getProject().getBaseDir(), selectorRule.getFilenames()[0], + selectorRule.getFiles()[0]); } /** @@ -70,65 +75,61 @@ public class PresentSelectorTest { */ @Test public void testSelectionBehaviour() { - PresentSelector s; - String results; - Mapper m; - Mapper.MapperType identity = new Mapper.MapperType(); - identity.setValue("identity"); - Mapper.MapperType glob = new Mapper.MapperType(); - glob.setValue("glob"); - Mapper.MapperType merge = new Mapper.MapperType(); - merge.setValue("merge"); - Mapper.MapperType flatten = new Mapper.MapperType(); - flatten.setValue("flatten"); - - File beddir = selectorRule.getBeddir(); - - s = new PresentSelector(); s.setTargetdir(beddir); - results = selectorRule.selectionString(s); - assertEquals("TTTTTTTTTTTT", results); + assertEquals("TTTTTTTTTTTT", selectorRule.selectionString(s)); + } - s = new PresentSelector(); + @Test + public void testSelectionBehaviour1() { s.setTargetdir(beddir); - m = s.createMapper(); + Mapper m = s.createMapper(); + Mapper.MapperType identity = new Mapper.MapperType(); + identity.setValue("identity"); m.setType(identity); - results = selectorRule.selectionString(s); - assertEquals("TTTTTTTTTTTT", results); + assertEquals("TTTTTTTTTTTT", selectorRule.selectionString(s)); + } - s = new PresentSelector(); + @Test + public void testSelectionBehaviour2() { File subdir = new File(System.getProperty("root"), "src/etc/testcases/taskdefs/expected"); s.setTargetdir(subdir); - m = s.createMapper(); + Mapper m = s.createMapper(); + Mapper.MapperType flatten = new Mapper.MapperType(); + flatten.setValue("flatten"); m.setType(flatten); - results = selectorRule.selectionString(s); - assertEquals("TTTTTTTTTTTF", results); + assertEquals("TTTTTTTTTTTF", selectorRule.selectionString(s)); + } - s = new PresentSelector(); + @Test + public void testSelectionBehaviour3() { s.setTargetdir(beddir); - m = s.createMapper(); + Mapper m = s.createMapper(); + Mapper.MapperType merge = new Mapper.MapperType(); + merge.setValue("merge"); m.setType(merge); m.setTo("asf-logo.gif.gz"); - results = selectorRule.selectionString(s); - assertEquals("TTTTTTTTTTTT", results); + assertEquals("TTTTTTTTTTTT", selectorRule.selectionString(s)); + } - s = new PresentSelector(); - subdir = new File(beddir, "tar/bz2"); + @Test + public void testSelectionBehaviour4() { + File subdir = new File(beddir, "tar/bz2"); s.setTargetdir(subdir); - m = s.createMapper(); + Mapper m = s.createMapper(); + Mapper.MapperType glob = new Mapper.MapperType(); + glob.setValue("glob"); m.setType(glob); m.setFrom("*.bz2"); m.setTo("*.tar.bz2"); - results = selectorRule.selectionString(s); - assertEquals("FFTFFFFFFFFF", results); + assertEquals("FFTFFFFFFFFF", selectorRule.selectionString(s)); + } - s = new PresentSelector(); - subdir = new File(selectorRule.getOutputDir(), "selectortest2"); + @Test + public void testSelectionBehaviour5() { + File subdir = new File(selectorRule.getOutputDir(), "selectortest2"); s.setTargetdir(subdir); - results = selectorRule.selectionString(s); - assertEquals("TTTFFTTTTTTT", results); - results = selectorRule.selectionString(s); - assertEquals("TTTFFTTTTTTT", results); + assertEquals("TTTFFTTTTTTT", selectorRule.selectionString(s)); + assertEquals("TTTFFTTTTTTT", selectorRule.selectionString(s)); } } diff --git a/src/tests/junit/org/apache/tools/ant/types/selectors/SizeSelectorTest.java b/src/tests/junit/org/apache/tools/ant/types/selectors/SizeSelectorTest.java index e3be005bd..3128feeec 100644 --- a/src/tests/junit/org/apache/tools/ant/types/selectors/SizeSelectorTest.java +++ b/src/tests/junit/org/apache/tools/ant/types/selectors/SizeSelectorTest.java @@ -18,15 +18,16 @@ package org.apache.tools.ant.types.selectors; +import static org.junit.Assert.assertEquals; + import java.util.Locale; import org.apache.tools.ant.BuildException; import org.apache.tools.ant.types.Parameter; +import org.junit.Before; import org.junit.Rule; import org.junit.Test; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.fail; +import org.junit.rules.ExpectedException; /** * Tests Size Selectors @@ -37,140 +38,151 @@ public class SizeSelectorTest { @Rule public final BaseSelectorRule selectorRule = new BaseSelectorRule(); + @Rule + public ExpectedException thrown = ExpectedException.none(); + + private SizeSelector s; + + private SizeSelector.SizeComparisons less; + + private SizeSelector.SizeComparisons equal; + + private SizeSelector.SizeComparisons more; + + @Before + public void setUp() { + s = new SizeSelector(); + + less = new SizeSelector.SizeComparisons(); + less.setValue("less"); + + equal = new SizeSelector.SizeComparisons(); + equal.setValue("equal"); + + more = new SizeSelector.SizeComparisons(); + more.setValue("more"); + } + /** * Test the code that validates the selector. */ @Test - public void testValidate() { - SizeSelector s = new SizeSelector(); - try { - s.isSelected(selectorRule.getProject().getBaseDir(), selectorRule.getFilenames()[0],selectorRule.getFiles()[0]); - fail("SizeSelector did not check for required fields"); - } catch (BuildException be1) { - assertEquals("The value attribute is required, and must " - + "be positive", be1.getMessage()); - } + public void testValidateAttribute() { + thrown.expect(BuildException.class); + thrown.expectMessage("The value attribute is required, and must be positive"); + s.isSelected(selectorRule.getProject().getBaseDir(), selectorRule.getFilenames()[0], + selectorRule.getFiles()[0]); + } - s = new SizeSelector(); + @Test + public void testValidateValue() { + thrown.expect(BuildException.class); + thrown.expectMessage("The value attribute is required, and must be positive"); s.setValue(-10); - try { - s.isSelected(selectorRule.getProject().getBaseDir(), selectorRule.getFilenames()[0],selectorRule.getFiles()[0]); - fail("SizeSelector did not check for value being in the " - + "allowable range"); - } catch (BuildException be2) { - assertEquals("The value attribute is required, and must " - + "be positive", be2.getMessage()); - } + s.isSelected(selectorRule.getProject().getBaseDir(), selectorRule.getFilenames()[0], + selectorRule.getFiles()[0]); + } - s = new SizeSelector(); + @Test + public void testValidateAttributeName() { + thrown.expect(BuildException.class); + thrown.expectMessage("Invalid parameter garbage in"); Parameter param = new Parameter(); param.setName("garbage in"); param.setValue("garbage out"); Parameter[] params = {param}; s.setParameters(params); - try { - s.isSelected(selectorRule.getProject().getBaseDir(), selectorRule.getFilenames()[0],selectorRule.getFiles()[0]); - fail("SizeSelector did not check for valid parameter element"); - } catch (BuildException be3) { - assertEquals("Invalid parameter garbage in", be3.getMessage()); - } + s.isSelected(selectorRule.getProject().getBaseDir(), selectorRule.getFilenames()[0], + selectorRule.getFiles()[0]); + } - s = new SizeSelector(); - param = new Parameter(); + @Test + public void testValidateAttributeValue() { + thrown.expect(BuildException.class); + thrown.expectMessage("Invalid size setting garbage out"); + Parameter param = new Parameter(); param.setName("value"); param.setValue("garbage out"); - params[0] = param; + Parameter[] params = {param}; s.setParameters(params); - try { - s.isSelected(selectorRule.getProject().getBaseDir(), selectorRule.getFilenames()[0],selectorRule.getFiles()[0]); - fail("SizeSelector accepted bad value as parameter"); - } catch (BuildException be4) { - assertEquals("Invalid size setting garbage out", - be4.getMessage()); - } + s.isSelected(selectorRule.getProject().getBaseDir(), selectorRule.getFilenames()[0], + selectorRule.getFiles()[0]); + } - s = new SizeSelector(); + @Test + public void testValidateAttributeUnits() { + thrown.expect(BuildException.class); + thrown.expectMessage("garbage out is not a legal value for this attribute"); Parameter param1 = new Parameter(); Parameter param2 = new Parameter(); param1.setName("value"); param1.setValue("5"); param2.setName("units"); param2.setValue("garbage out"); - params = new Parameter[2]; + Parameter[] params = new Parameter[2]; params[0] = param1; params[1] = param2; - try { - s.setParameters(params); - s.isSelected(selectorRule.getProject().getBaseDir(), selectorRule.getFilenames()[0],selectorRule.getFiles()[0]); - fail("SizeSelector accepted bad units as parameter"); - } catch (BuildException be5) { - assertEquals("garbage out is not a legal value for this attribute", - be5.getMessage()); - } - + s.setParameters(params); + s.isSelected(selectorRule.getProject().getBaseDir(), selectorRule.getFilenames()[0], + selectorRule.getFiles()[0]); } /** * Tests to make sure that the selector is selecting files correctly. */ @Test - public void testSelectionBehaviour() { - SizeSelector s; - String results; - - SizeSelector.ByteUnits kilo = new SizeSelector.ByteUnits(); - kilo.setValue("K"); - SizeSelector.ByteUnits kibi = new SizeSelector.ByteUnits(); - kibi.setValue("Ki"); - SizeSelector.ByteUnits tibi = new SizeSelector.ByteUnits(); - tibi.setValue("Ti"); - SizeSelector.SizeComparisons less = new SizeSelector.SizeComparisons(); - less.setValue("less"); - SizeSelector.SizeComparisons equal = new SizeSelector.SizeComparisons(); - equal.setValue("equal"); - SizeSelector.SizeComparisons more = new SizeSelector.SizeComparisons(); - more.setValue("more"); - - s = new SizeSelector(); + public void testSelectionBehaviourLess() { s.setValue(10); s.setWhen(less); - results = selectorRule.selectionString(s); - assertEquals("TFFFFFFFFFFT", results); + assertEquals("TFFFFFFFFFFT", selectorRule.selectionString(s)); + } - s = new SizeSelector(); + @Test + public void testSelectionBehaviourMore() { s.setValue(10); s.setWhen(more); - results = selectorRule.selectionString(s); - assertEquals("TTTTTTTTTTTT", results); + assertEquals("TTTTTTTTTTTT", selectorRule.selectionString(s)); + } - s = new SizeSelector(); + @Test + public void testSelectionBehaviourEqual() { s.setValue(32); s.setWhen(equal); - results = selectorRule.selectionString(s); - assertEquals("TFFFTFFFFFFT", results); + assertEquals("TFFFTFFFFFFT", selectorRule.selectionString(s)); + } - s = new SizeSelector(); + @Test + public void testSelectionBehaviourKilo() { + SizeSelector.ByteUnits kilo = new SizeSelector.ByteUnits(); + kilo.setValue("K"); s.setValue(7); s.setWhen(more); s.setUnits(kilo); - results = selectorRule.selectionString(s); - assertEquals("TFTFFTTTTTTT", results); + assertEquals("TFTFFTTTTTTT", selectorRule.selectionString(s)); + } - s = new SizeSelector(); + @Test + public void testSelectionBehaviourKibi() { + SizeSelector.ByteUnits kibi = new SizeSelector.ByteUnits(); + kibi.setValue("Ki"); s.setValue(7); s.setWhen(more); s.setUnits(kibi); - results = selectorRule.selectionString(s); - assertEquals("TFTFFFTTFTTT", results); + assertEquals("TFTFFFTTFTTT", selectorRule.selectionString(s)); + } - s = new SizeSelector(); + @Test + public void testSelectionBehaviourTibi() { + SizeSelector.ByteUnits tibi = new SizeSelector.ByteUnits(); + tibi.setValue("Ti"); s.setValue(99999); s.setWhen(more); s.setUnits(tibi); - results = selectorRule.selectionString(s); - assertEquals("TFFFFFFFFFFT", results); + assertEquals("TFFFFFFFFFFT", selectorRule.selectionString(s)); + } - s = new SizeSelector(); + @Test + public void testSelectionBehaviour() { Parameter param1 = new Parameter(); Parameter param2 = new Parameter(); Parameter param3 = new Parameter(); @@ -182,8 +194,7 @@ public class SizeSelectorTest { param3.setValue("more"); Parameter[] params = {param1, param2, param3}; s.setParameters(params); - results = selectorRule.selectionString(s); - assertEquals("TFFFFFFTFFTT", results); + assertEquals("TFFFFFFTFFTT", selectorRule.selectionString(s)); } @Test @@ -219,16 +230,11 @@ public class SizeSelectorTest { } private void testCaseInsensitiveParameterParsing(String name) { - SizeSelector s = new SizeSelector(); + thrown.expect(BuildException.class); + thrown.expectMessage("foo is not a legal value for this attribute"); Parameter p = new Parameter(); p.setName(name); p.setValue("foo"); - try { - s.setParameters(p); - fail("should have caused an exception"); - } catch (BuildException be) { - assertEquals("foo is not a legal value for this attribute", - be.getMessage()); - } + s.setParameters(p); } } diff --git a/src/tests/junit/org/apache/tools/ant/types/selectors/TypeSelectorTest.java b/src/tests/junit/org/apache/tools/ant/types/selectors/TypeSelectorTest.java index f6be24055..881411d22 100644 --- a/src/tests/junit/org/apache/tools/ant/types/selectors/TypeSelectorTest.java +++ b/src/tests/junit/org/apache/tools/ant/types/selectors/TypeSelectorTest.java @@ -19,8 +19,10 @@ package org.apache.tools.ant.types.selectors; import org.apache.tools.ant.BuildException; +import org.junit.Before; import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; import static org.junit.Assert.assertEquals; import static org.junit.Assert.fail; @@ -34,43 +36,46 @@ public class TypeSelectorTest { @Rule public BaseSelectorRule selectorRule = new BaseSelectorRule(); + @Rule + public ExpectedException thrown = ExpectedException.none(); + + private TypeSelector s; + + @Before + public void setUp() { + s = new TypeSelector(); + } + /** * Test the code that validates the selector. */ @Test public void testValidate() { - TypeSelector s = new TypeSelector(); - try { - s.isSelected(selectorRule.getProject().getBaseDir(), - selectorRule.getFilenames()[0], selectorRule.getFiles()[0]); - fail("TypeSelector did not check for required fields"); - } catch (BuildException be1) { - assertEquals("The type attribute is required", be1.getMessage()); - } + thrown.expect(BuildException.class); + thrown.expectMessage("The type attribute is required"); + s.isSelected(selectorRule.getProject().getBaseDir(), + selectorRule.getFilenames()[0], selectorRule.getFiles()[0]); } /** * Tests to make sure that the selector is selecting files correctly. */ @Test - public void testSelectionBehaviour() { - TypeSelector s; - String results; - + public void testSelectionBehaviourDir() { TypeSelector.FileType directory = new TypeSelector.FileType(); directory.setValue("dir"); - TypeSelector.FileType file = new TypeSelector.FileType(); - file.setValue("file"); - s = new TypeSelector(); s.setType(directory); - results = selectorRule.selectionString(s); - assertEquals("TFFFFFFFFFFT", results); + assertEquals("TFFFFFFFFFFT", selectorRule.selectionString(s)); + } + + @Test + public void testSelectionBehaviourFile() { + TypeSelector.FileType file = new TypeSelector.FileType(); + file.setValue("file"); - s = new TypeSelector(); s.setType(file); - results = selectorRule.selectionString(s); - assertEquals("FTTTTTTTTTTF", results); + assertEquals("FTTTTTTTTTTF", selectorRule.selectionString(s)); } } diff --git a/src/tests/junit/org/apache/tools/zip/AsiExtraFieldTest.java b/src/tests/junit/org/apache/tools/zip/AsiExtraFieldTest.java index 9a9abddea..cdb9cf070 100644 --- a/src/tests/junit/org/apache/tools/zip/AsiExtraFieldTest.java +++ b/src/tests/junit/org/apache/tools/zip/AsiExtraFieldTest.java @@ -18,13 +18,17 @@ package org.apache.tools.zip; -import org.junit.Test; - import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotSame; import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; + +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; + +import java.util.zip.ZipException; /** * JUnit testcases for org.apache.tools.zip.AsiExtraField. @@ -32,12 +36,21 @@ import static org.junit.Assert.fail; */ public class AsiExtraFieldTest implements UnixStat { + private AsiExtraField a; + + @Rule + public ExpectedException thrown = ExpectedException.none(); + + @Before + public void setUp() { + a = new AsiExtraField(); + } + /** * Test file mode magic. */ @Test public void testModes() { - AsiExtraField a = new AsiExtraField(); a.setMode(0123); assertEquals("plain file", 0100123, a.getMode()); a.setDirectory(true); @@ -51,7 +64,6 @@ public class AsiExtraFieldTest implements UnixStat { */ @Test public void testContent() { - AsiExtraField a = new AsiExtraField(); a.setMode(0123); a.setUserId(5); a.setGroupId(6); @@ -82,83 +94,96 @@ public class AsiExtraFieldTest implements UnixStat { } /** - * Test reparse + * Test reparse file */ @Test - public void testReparse() throws Exception { - AsiExtraField a = new AsiExtraField(); + public void testReparseFile() throws Exception { // CRC manually calculated, sorry byte[] data = {(byte) 0xC6, 0x02, 0x78, (byte) 0xB6, // CRC - 0123, (byte) 0x80, // mode - 0, 0, 0, 0, // link length - 5, 0, 6, 0}; // uid, gid + 0123, (byte) 0x80, // mode + 0, 0, 0, 0, // link length + 5, 0, 6, 0}; // uid, gid + a.parseFromLocalFileData(data, 0, data.length); assertEquals("length plain file", data.length, - a.getLocalFileDataLength().getValue()); + a.getLocalFileDataLength().getValue()); assertFalse("plain file, no link", a.isLink()); assertFalse("plain file, no dir", a.isDirectory()); assertEquals("mode plain file", FILE_FLAG | 0123, a.getMode()); assertEquals("uid plain file", 5, a.getUserId()); assertEquals("gid plain file", 6, a.getGroupId()); + } + + /** + * Test reparse link + */ + @Test + public void testReparseLink() throws Exception { + byte[] data = {0x75, (byte) 0x8E, 0x41, (byte) 0xFD, // CRC + 0123, (byte) 0xA0, // mode + 4, 0, 0, 0, // link length + 5, 0, 6, 0, // uid, gid + (byte) 't', (byte) 'e', (byte) 's', (byte) 't'}; - data = new byte[] {0x75, (byte) 0x8E, 0x41, (byte) 0xFD, // CRC - 0123, (byte) 0xA0, // mode - 4, 0, 0, 0, // link length - 5, 0, 6, 0, // uid, gid - (byte) 't', (byte) 'e', (byte) 's', (byte) 't'}; - a = new AsiExtraField(); a.parseFromLocalFileData(data, 0, data.length); assertEquals("length link", data.length, - a.getLocalFileDataLength().getValue()); + a.getLocalFileDataLength().getValue()); assertTrue("link, is link", a.isLink()); assertFalse("link, no dir", a.isDirectory()); assertEquals("mode link", LINK_FLAG | 0123, a.getMode()); assertEquals("uid link", 5, a.getUserId()); assertEquals("gid link", 6, a.getGroupId()); assertEquals("test", a.getLinkedFile()); + } + + /** + * Test reparse directory + */ + @Test + public void testReparseDirectory() throws Exception { + byte[] data ={(byte)0x8E, 0x01, (byte)0xBF, (byte)0x0E, // CRC + 0123, (byte)0x40, // mode + 0, 0, 0, 0, // link + 5, 0, 6, 0}; // uid, gid - data = new byte[] {(byte) 0x8E, 0x01, (byte) 0xBF, (byte) 0x0E, // CRC - 0123, (byte) 0x40, // mode - 0, 0, 0, 0, // link - 5, 0, 6, 0}; // uid, gid - a = new AsiExtraField(); a.parseFromLocalFileData(data, 0, data.length); assertEquals("length dir", data.length, - a.getLocalFileDataLength().getValue()); + a.getLocalFileDataLength().getValue()); assertFalse("dir, no link", a.isLink()); assertTrue("dir, is dir", a.isDirectory()); assertEquals("mode dir", DIR_FLAG | 0123, a.getMode()); assertEquals("uid dir", 5, a.getUserId()); assertEquals("gid dir", 6, a.getGroupId()); + } - data = new byte[] {0, 0, 0, 0, // bad CRC - 0123, (byte) 0x40, // mode - 0, 0, 0, 0, // link - 5, 0, 6, 0}; // uid, gid - a = new AsiExtraField(); - try { - a.parseFromLocalFileData(data, 0, data.length); - fail("should raise bad CRC exception"); - } catch (Exception e) { - assertEquals("bad CRC checksum 0 instead of ebf018e", - e.getMessage()); - } + /** + * Test reparse bad CRC + */ + @Test + public void testReparseBadCRC() throws Exception { + thrown.expect(ZipException.class); + thrown.expectMessage("bad CRC checksum 0 instead of ebf018e"); + byte[] data = {0, 0, 0, 0, // bad CRC + 0123, (byte)0x40, // mode + 0, 0, 0, 0, // link + 5, 0, 6, 0}; // uid, gid + + a.parseFromLocalFileData(data, 0, data.length); } @Test public void testClone() { - AsiExtraField s1 = new AsiExtraField(); - s1.setUserId(42); - s1.setGroupId(12); - s1.setLinkedFile("foo"); - s1.setMode(0644); - s1.setDirectory(true); - AsiExtraField s2 = (AsiExtraField) s1.clone(); - assertNotSame(s1, s2); - assertEquals(s1.getUserId(), s2.getUserId()); - assertEquals(s1.getGroupId(), s2.getGroupId()); - assertEquals(s1.getLinkedFile(), s2.getLinkedFile()); - assertEquals(s1.getMode(), s2.getMode()); - assertEquals(s1.isDirectory(), s2.isDirectory()); + a.setUserId(42); + a.setGroupId(12); + a.setLinkedFile("foo"); + a.setMode(0644); + a.setDirectory(true); + AsiExtraField s2 = (AsiExtraField) a.clone(); + assertNotSame(a, s2); + assertEquals(a.getUserId(), s2.getUserId()); + assertEquals(a.getGroupId(), s2.getGroupId()); + assertEquals(a.getLinkedFile(), s2.getLinkedFile()); + assertEquals(a.getMode(), s2.getMode()); + assertEquals(a.isDirectory(), s2.isDirectory()); } }