Browse Source

Refactor tests (extract fixtures and/or use matchers)

master
Gintas Grigelionis 7 years ago
parent
commit
47f8c5d463
11 changed files with 964 additions and 852 deletions
  1. +350
    -257
      src/tests/junit/org/apache/tools/ant/IntrospectionHelperTest.java
  2. +34
    -55
      src/tests/junit/org/apache/tools/ant/types/PermissionsTest.java
  3. +47
    -41
      src/tests/junit/org/apache/tools/ant/types/selectors/ContainsSelectorTest.java
  4. +116
    -114
      src/tests/junit/org/apache/tools/ant/types/selectors/DateSelectorTest.java
  5. +36
    -50
      src/tests/junit/org/apache/tools/ant/types/selectors/DependSelectorTest.java
  6. +68
    -66
      src/tests/junit/org/apache/tools/ant/types/selectors/DepthSelectorTest.java
  7. +47
    -40
      src/tests/junit/org/apache/tools/ant/types/selectors/FilenameSelectorTest.java
  8. +65
    -64
      src/tests/junit/org/apache/tools/ant/types/selectors/PresentSelectorTest.java
  9. +101
    -95
      src/tests/junit/org/apache/tools/ant/types/selectors/SizeSelectorTest.java
  10. +25
    -20
      src/tests/junit/org/apache/tools/ant/types/selectors/TypeSelectorTest.java
  11. +75
    -50
      src/tests/junit/org/apache/tools/zip/AsiExtraFieldTest.java

+ 350
- 257
src/tests/junit/org/apache/tools/ant/IntrospectionHelperTest.java View File

@@ -34,13 +34,17 @@ import org.apache.tools.ant.taskdefs.condition.Os;
import org.junit.Before; import org.junit.Before;
import org.junit.ComparisonFailure; import org.junit.ComparisonFailure;
import org.junit.Ignore; import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test; 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.assertEquals;
import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;


/** /**
* JUnit testcases for org.apache.tools.ant.IntrospectionHelper. * JUnit testcases for org.apache.tools.ant.IntrospectionHelper.
@@ -49,6 +53,9 @@ import static org.junit.Assert.fail;


public class IntrospectionHelperTest { public class IntrospectionHelperTest {


@Rule
public ExpectedException thrown = ExpectedException.none();

private Project p; private Project p;
private IntrospectionHelper ih; private IntrospectionHelper ih;
private static final String projectBasedir = File.separator; private static final String projectBasedir = File.separator;
@@ -71,42 +78,30 @@ public class IntrospectionHelperTest {
} }


@Test @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"); 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); 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") @Ignore("This silently ignores a build exception")
@Test(expected = BuildException.class)
public void testGetAddTextMethod() { public void testGetAddTextMethod() {
Method m = ih.getAddTextMethod(); Method m = ih.getAddTextMethod();
assertMethod(m, "addText", String.class, "test", "bing!"); 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 @Test
public void testSupportsCharacters() { public void testSupportsCharacters() {
assertTrue("IntrospectionHelperTest supports addText",
ih.supportsCharacters());

assertTrue("IntrospectionHelperTest supports addText", ih.supportsCharacters());
ih = IntrospectionHelper.getHelper(String.class); ih = IntrospectionHelper.getHelper(String.class);
assertFalse("String doesn\'t support addText", ih.supportsCharacters()); assertFalse("String doesn\'t support addText", ih.supportsCharacters());
} }
@@ -115,93 +110,135 @@ public class IntrospectionHelperTest {
assertEquals("test", text); 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 @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(String.class, ih.getElementType("six"));
assertEquals("test", ih.createElement(p, this, "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(StringBuffer.class, ih.getElementType("thirteen"));
assertEquals("test", ih.createElement(p, this, "thirteen").toString()); 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<String, Class<?>> getExpectedNestedElements() { private Map<String, Class<?>> getExpectedNestedElements() {
@@ -216,11 +253,11 @@ public class IntrospectionHelperTest {
@Test @Test
public void testGetNestedElements() { public void testGetNestedElements() {
Map<String, Class<?>> elemMap = getExpectedNestedElements(); Map<String, Class<?>> elemMap = getExpectedNestedElements();
Enumeration e = ih.getNestedElements();
Enumeration<String> e = ih.getNestedElements();
while (e.hasMoreElements()) { 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); expect);
assertEquals("Return type of " + name, expect, ih.getElementType(name)); assertEquals("Return type of " + name, expect, ih.getElementType(name));
elemMap.remove(name); elemMap.remove(name);
@@ -228,26 +265,22 @@ public class IntrospectionHelperTest {
assertTrue("Found all", elemMap.isEmpty()); assertTrue("Found all", elemMap.isEmpty());
} }


@Test
@Test(expected = UnsupportedOperationException.class)
public void testGetNestedElementMap() { public void testGetNestedElementMap() {
Map<String, Class<?>> elemMap = getExpectedNestedElements(); Map<String, Class<?>> elemMap = getExpectedNestedElements();
Map<String, Class<?>> actualMap = ih.getNestedElementMap(); Map<String, Class<?>> actualMap = ih.getNestedElementMap();
for (Map.Entry<String, Class<?>> entry : actualMap.entrySet()) { for (Map.Entry<String, Class<?>> entry : actualMap.entrySet()) {
String elemName = entry.getKey(); String elemName = entry.getKey();
Class<?> elemClass = elemMap.get(elemName); 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()); assertEquals("Type of " + elemName, elemClass, entry.getValue());
elemMap.remove(elemName); elemMap.remove(elemName);
} }
assertTrue("Found all", elemMap.isEmpty()); assertTrue("Found all", elemMap.isEmpty());


// Check it's a read-only map. // 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 @Test
@@ -262,7 +295,7 @@ public class IntrospectionHelperTest {
Class<?> returnType, Class<?> methodArg) { Class<?> returnType, Class<?> methodArg) {
Method m = ih.getElementMethod(elemName); Method m = ih.getElementMethod(elemName);
assertEquals("Method name", methodName, m.getName()); 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()); assertEquals("Return type", expectedReturnType, m.getReturnType());
Class<?>[] args = m.getParameterTypes(); Class<?>[] args = m.getParameterTypes();
if (methodArg != null) { if (methodArg != null) {
@@ -312,7 +345,7 @@ public class IntrospectionHelperTest {
public void addEleven(int i) { public void addEleven(int i) {
} }


public void addTwelve(Class c) {
public void addTwelve(Class<?> c) {
} }


public void addThirteen(StringBuffer sb) { public void addThirteen(StringBuffer sb) {
@@ -323,141 +356,211 @@ public class IntrospectionHelperTest {
throw new NullPointerException(); 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 @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"); 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"); 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"); 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"); 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"); 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"); 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"); 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"); ih.setAttribute(p, this, "fourteen", "2");
try {
ih.setAttribute(p, this, "fourteen", "on"); 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"); 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"); 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"); 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"); 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"); 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<String, Class<?>> getExpectedAttributes() { private Map<String, Class<?>> getExpectedAttributes() {
@@ -479,8 +582,7 @@ public class IntrospectionHelperTest {


/* /*
* JUnit 3.7 adds a getName method to TestCase - so we now * 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. * Simply add it here and remove it after the tests.
*/ */
@@ -512,20 +614,17 @@ public class IntrospectionHelperTest {
for (Map.Entry<String, Class<?>> entry : actualMap.entrySet()) { for (Map.Entry<String, Class<?>> entry : actualMap.entrySet()) {
String attrName = entry.getKey(); String attrName = entry.getKey();
Class<?> attrClass = attrMap.get(attrName); 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()); assertEquals("Type of " + attrName, attrClass, entry.getValue());
attrMap.remove(attrName); attrMap.remove(attrName);
} }
attrMap.remove("name"); attrMap.remove("name");
assertTrue("Found all", attrMap.isEmpty()); 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. // 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 @Test
@@ -559,12 +658,9 @@ public class IntrospectionHelperTest {
assertAttrMethod("twenty", "setTwenty", Path.class, assertAttrMethod("twenty", "setTwenty", Path.class,
new File(projectBasedir + 20).toPath(), Paths.get("toto")); 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, private void assertAttrMethod(String attrName, String methodName,
@@ -675,8 +771,7 @@ public class IntrospectionHelperTest {
// combinatorics are too hard to check. We really only want // combinatorics are too hard to check. We really only want
// to ensure that the more derived Hashtable can be found // to ensure that the more derived Hashtable can be found
// before Map. // 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) // addConfigured(Hashtable) should come before addConfigured(Map)
assertMethod(extensions.get(adders - 2), assertMethod(extensions.get(adders - 2),
@@ -703,12 +798,10 @@ public class IntrospectionHelperTest {


try { try {
m.invoke(this, badArg); m.invoke(this, badArg);
fail("Should have raised an assertion exception");
} catch (IllegalAccessException e) { } catch (IllegalAccessException e) {
throw new BuildException(e); throw new BuildException(e);
} catch (InvocationTargetException e) { } catch (InvocationTargetException e) {
Throwable t = e.getTargetException();
assertTrue(t.toString(), t instanceof AssertionError);
assertThat(e, hasProperty("cause", instanceOf(AssertionError.class)));
} }
} }




+ 34
- 55
src/tests/junit/org/apache/tools/ant/types/PermissionsTest.java View File

@@ -19,16 +19,23 @@
package org.apache.tools.ant.types; package org.apache.tools.ant.types;


import org.apache.tools.ant.ExitException; import org.apache.tools.ant.ExitException;
import org.junit.After;
import org.junit.Before; import org.junit.Before;
import org.junit.Rule;
import org.junit.Test; 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. * JUnit 4 testcases for org.apache.tools.ant.types.Permissions.
*/ */
public class PermissionsTest { public class PermissionsTest {


@Rule
public ExpectedException thrown = ExpectedException.none();

Permissions perms; Permissions perms;


@Before @Before
@@ -67,90 +74,62 @@ public class PermissionsTest {
perm.setName("os.*"); perm.setName("os.*");
perm.setClass("java.util.PropertyPermission"); perm.setClass("java.util.PropertyPermission");
perms.addConfiguredRevoke(perm); perms.addConfiguredRevoke(perm);

// Allow loading Hamcrest Matcher classes on demand
perm = new Permissions.Permission();
perm.setActions("read");
perm.setName("<<ALL FILES>>");
perm.setClass("java.io.FilePermission");
perms.addConfiguredGrant(perm);

perms.setSecurityManager();
}

@After
public void tearDown() {
perms.restoreSecurityManager();
} }


/** Tests a permission that is granted per default. */ /** Tests a permission that is granted per default. */
@Test @Test
public void testDefaultGranted() { 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. */ /** Tests a permission that has been granted later via wildcard. */
@Test @Test
public void testGranted() { 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. */ /** Tests a permission that has been granted and revoked later. */
@Test
@Test(expected = SecurityException.class)
public void testGrantedAndRevoked() { 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. */ /** Tests a permission that is granted as per default but revoked later via wildcard. */
@Test
@Test(expected = SecurityException.class)
public void testDefaultRevoked() { 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. */ /** Tests a permission that has not been granted or revoked. */
@Test
@Test(expected = SecurityException.class)
public void testOther() { 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. */ /** Tests an exit condition. */
@Test @Test
public void testExit() { public void testExit() {
perms.setSecurityManager();
thrown.expect(ExitException.class);
thrown.expect(hasProperty("status", equalTo(3)));
try { try {
System.out.println("If this is the last line on standard out the testExit f.a.i.l.e.d"); System.out.println("If this is the last line on standard out the testExit f.a.i.l.e.d");
System.exit(3); 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 { } finally {
perms.restoreSecurityManager();
System.out.println("testExit successful.");
} }
} }




+ 47
- 41
src/tests/junit/org/apache/tools/ant/types/selectors/ContainsSelectorTest.java View File

@@ -18,13 +18,14 @@


package org.apache.tools.ant.types.selectors; package org.apache.tools.ant.types.selectors;


import static org.junit.Assert.assertEquals;

import org.apache.tools.ant.BuildException; import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.types.Parameter; import org.apache.tools.ant.types.Parameter;
import org.junit.Before;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import org.junit.rules.ExpectedException;


/** /**
* Tests Contains Selectors. * Tests Contains Selectors.
@@ -35,76 +36,81 @@ public class ContainsSelectorTest {
@Rule @Rule
public final BaseSelectorRule selectorRule = new BaseSelectorRule(); 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 the code that validates the selector.
*/ */
@Test @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(); Parameter param = new Parameter();
param.setName("garbage in"); param.setName("garbage in");
param.setValue("garbage out"); param.setValue("garbage out");
Parameter[] params = {param}; Parameter[] params = {param};
s.setParameters(params); 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. * Tests to make sure that the selector is selecting files correctly.
*/ */
@Test @Test
public void testSelectionBehaviour() {
ContainsSelector s;
String results;

s = new ContainsSelector();
public void testSelectionBehaviourNoSuch() {
s.setText("no such string in test files"); 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"); 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.setText("apache ant");
s.setCasesensitive(true); 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.setText("apache ant");
s.setCasesensitive(false); 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.setText("ApacheAnt");
s.setIgnorewhitespace(true); 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.setText("A p a c h e A n t");
s.setIgnorewhitespace(true); s.setIgnorewhitespace(true);
results = selectorRule.selectionString(s);
assertEquals("TFFFTFFFFFFT", results);
assertEquals("TFFFTFFFFFFT", selectorRule.selectionString(s));
} }


} }

+ 116
- 114
src/tests/junit/org/apache/tools/ant/types/selectors/DateSelectorTest.java View File

@@ -20,8 +20,10 @@ package org.apache.tools.ant.types.selectors;


import org.apache.tools.ant.BuildException; import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.types.Parameter; import org.apache.tools.ant.types.Parameter;
import org.junit.Before;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.junit.rules.ExpectedException;


import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail; import static org.junit.Assert.fail;
@@ -36,159 +38,160 @@ public class DateSelectorTest {
@Rule @Rule
public final BaseSelectorRule selectorRule = new BaseSelectorRule(); 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 the code that validates the selector.
*/ */
@Test @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"); 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"); 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(); Parameter param = new Parameter();
param.setName("garbage in"); param.setName("garbage in");
param.setValue("garbage out"); param.setValue("garbage out");
Parameter[] params = new Parameter[1]; Parameter[] params = new Parameter[1];
params[0] = param; params[0] = param;
s.setParameters(params); 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.setName("millis");
param.setValue("garbage out"); param.setValue("garbage out");
Parameter[] params = new Parameter[1];
params[0] = param; params[0] = param;
s.setParameters(params); 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.setName("granularity");
param.setValue("garbage out"); param.setValue("garbage out");
Parameter[] params = new Parameter[1];
params[0] = param; params[0] = param;
s.setParameters(params); 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. * Tests to make sure that the selector is selecting files correctly.
*/ */
@Test @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.setDatetime("10/10/1999 1:45 PM");
s.setWhen(before); 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.setDatetime("10/10/1999 1:45 PM");
s.setWhen(before); s.setWhen(before);
s.setCheckdirs(true); 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.setDatetime("10/10/1999 1:45 PM");
s.setWhen(after); 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.setDatetime("11/21/2001 4:54 AM");
s.setWhen(before); 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"); s.setDatetime("11/21/2001 4:55 AM");

long milliseconds = s.getMillis();
s.setWhen(equal); 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); 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.setDatetime("11/21/2001 4:56 AM");
s.setWhen(after); 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 param1 = new Parameter();
Parameter param2 = new Parameter(); Parameter param2 = new Parameter();
param1.setName("datetime"); param1.setName("datetime");
@@ -197,28 +200,29 @@ public class DateSelectorTest {
param2.setValue("after"); param2.setValue("after");
Parameter[] params = {param1, param2}; Parameter[] params = {param1, param2};
s.setParameters(params); 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(); long testtime = selectorRule.getMirrorFiles()[5].lastModified();
s.setMillis(testtime); s.setMillis(testtime);
s.setWhen(after); s.setWhen(after);
s.setGranularity(2); 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++) { for (int i = 1; i <= 2; i++) {
assumeTrue("Cannot setup file times for test", assumeTrue("Cannot setup file times for test",
selectorRule.getMirrorFiles()[i].setLastModified(testtime - (3 * 60 * 60 * 100))); 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.setMillis(testtime);
s.setWhen(before); s.setWhen(before);
s.setGranularity(2); s.setGranularity(2);
@@ -227,9 +231,7 @@ public class DateSelectorTest {
selectorRule.getMirrorFiles()[i].setLastModified(testtime + (3 * 60 * 60 * 100))); selectorRule.getMirrorFiles()[i].setLastModified(testtime + (3 * 60 * 60 * 100)));
} }


results = selectorRule.mirrorSelectionString(s);
assertEquals("TTTTTTTFFFFT", results);

assertEquals("TTTTTTTFFFFT", selectorRule.mirrorSelectionString(s));
} }


} }

+ 36
- 50
src/tests/junit/org/apache/tools/ant/types/selectors/DependSelectorTest.java View File

@@ -18,15 +18,16 @@


package org.apache.tools.ant.types.selectors; 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.BuildException;
import org.apache.tools.ant.types.Mapper; import org.apache.tools.ant.types.Mapper;
import org.junit.Before;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; 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 { public class DependSelectorTest {


private DependSelector s;

@Rule @Rule
public final BaseSelectorRule selectorRule = new BaseSelectorRule(); public final BaseSelectorRule selectorRule = new BaseSelectorRule();


@Rule
public ExpectedException thrown = ExpectedException.none();

@Before
public void setUp() {
s = new DependSelector();
}

@Test @Test
public void testValidateSingleMapper() { 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 @Test
public void testValidateRequiredFields() { 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 @Test
public void testNoMapper() { public void testNoMapper() {
DependSelector s = new DependSelector();
s.setTargetdir(selectorRule.getBeddir()); s.setTargetdir(selectorRule.getBeddir());

String results = selectorRule.selectionString(s);
assertEquals("FFFFFFFFFFFF", results);
assertEquals("FFFFFFFFFFFF", selectorRule.selectionString(s));
} }


@Test @Test
public void testIdentityMapper() { public void testIdentityMapper() {
DependSelector s = new DependSelector();
s.setTargetdir(selectorRule.getBeddir()); s.setTargetdir(selectorRule.getBeddir());


Mapper.MapperType identity = new Mapper.MapperType(); Mapper.MapperType identity = new Mapper.MapperType();
@@ -85,13 +82,11 @@ public class DependSelectorTest {
Mapper m = s.createMapper(); Mapper m = s.createMapper();
m.setType(identity); m.setType(identity);


String results = selectorRule.selectionString(s);
assertEquals("FFFFFFFFFFFF", results);
assertEquals("FFFFFFFFFFFF", selectorRule.selectionString(s));
} }


@Test @Test
public void testMergeMapper() { public void testMergeMapper() {
DependSelector s = new DependSelector();
s.setTargetdir(selectorRule.getBeddir()); s.setTargetdir(selectorRule.getBeddir());


Mapper.MapperType merge = new Mapper.MapperType(); Mapper.MapperType merge = new Mapper.MapperType();
@@ -101,13 +96,11 @@ public class DependSelectorTest {
m.setType(merge); m.setType(merge);
m.setTo("asf-logo.gif.gz"); 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 @Test
public void testMergeMapper2() { public void testMergeMapper2() {
DependSelector s = new DependSelector();
s.setTargetdir(selectorRule.getBeddir()); s.setTargetdir(selectorRule.getBeddir());


Mapper.MapperType merge = new Mapper.MapperType(); Mapper.MapperType merge = new Mapper.MapperType();
@@ -116,13 +109,12 @@ public class DependSelectorTest {
Mapper m = s.createMapper(); Mapper m = s.createMapper();
m.setType(merge); m.setType(merge);
m.setTo("asf-logo.gif.bz2"); m.setTo("asf-logo.gif.bz2");
String results = selectorRule.selectionString(s);
assertEquals("TTFTTTTTTTTT", results);
assertEquals("TTFTTTTTTTTT", selectorRule.selectionString(s));
} }


@Test @Test
public void testGlobMapperRelativePath() { public void testGlobMapperRelativePath() {
DependSelector s = new DependSelector();
File subdir = new File("selectortest/tar/bz2"); File subdir = new File("selectortest/tar/bz2");
s.setTargetdir(subdir); s.setTargetdir(subdir);


@@ -134,13 +126,11 @@ public class DependSelectorTest {
m.setFrom("*.bz2"); m.setFrom("*.bz2");
m.setTo("*.tar.bz2"); m.setTo("*.tar.bz2");


String results = selectorRule.selectionString(s);
assertEquals("FFTFFFFFFTTF", results);
assertEquals("FFTFFFFFFTTF", selectorRule.selectionString(s));
} }


@Test @Test
public void testRestrictedGlobMapper() { public void testRestrictedGlobMapper() {
DependSelector s = new DependSelector();
File subdir = new File(selectorRule.getBeddir(), "tar/bz2"); File subdir = new File(selectorRule.getBeddir(), "tar/bz2");
s.setTargetdir(subdir); s.setTargetdir(subdir);


@@ -151,22 +141,19 @@ public class DependSelectorTest {
m.setType(glob); m.setType(glob);
m.setFrom("*.bz2"); m.setFrom("*.bz2");
m.setTo("*.tar.bz2"); m.setTo("*.tar.bz2");
String results = selectorRule.selectionString(s);
assertEquals("FFFFFFFFFTTF", results);
assertEquals("FFFFFFFFFTTF", selectorRule.selectionString(s));
} }


@Test @Test
public void testSelectionNoMapper() { public void testSelectionNoMapper() {
DependSelector s = new DependSelector();
s.setTargetdir(new File(selectorRule.getOutputDir(), "selectortest2")); s.setTargetdir(new File(selectorRule.getOutputDir(), "selectortest2"));
String results = selectorRule.selectionString(s);
assertEquals("FFFTTFFFFFFF", results);
assertEquals("FFFTTFFFFFFF", selectorRule.selectionString(s));
} }




@Test @Test
public void testMirroredSelection() { public void testMirroredSelection() {
DependSelector s = new DependSelector();
s.setTargetdir(new File(selectorRule.getOutputDir(), "selectortest2/tar/bz2")); s.setTargetdir(new File(selectorRule.getOutputDir(), "selectortest2/tar/bz2"));


Mapper.MapperType glob = new Mapper.MapperType(); Mapper.MapperType glob = new Mapper.MapperType();
@@ -176,10 +163,9 @@ public class DependSelectorTest {
m.setType(glob); m.setType(glob);
m.setFrom("*.bz2"); m.setFrom("*.bz2");
m.setTo("*.tar.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));
} }


} }

+ 68
- 66
src/tests/junit/org/apache/tools/ant/types/selectors/DepthSelectorTest.java View File

@@ -18,13 +18,14 @@


package org.apache.tools.ant.types.selectors; package org.apache.tools.ant.types.selectors;


import static org.junit.Assert.assertEquals;

import org.apache.tools.ant.BuildException; import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.types.Parameter; import org.apache.tools.ant.types.Parameter;
import org.junit.Before;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import org.junit.rules.ExpectedException;


/** /**
* Tests Depth Selectors * Tests Depth Selectors
@@ -35,111 +36,112 @@ public class DepthSelectorTest {
@Rule @Rule
public final BaseSelectorRule selectorRule = new BaseSelectorRule(); 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 the code that validates the selector.
*/ */
@Test @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.setMin(5);
s.setMax(2); 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(); Parameter param = new Parameter();
param.setName("garbage in"); param.setName("garbage in");
param.setValue("garbage out"); param.setValue("garbage out");
Parameter[] params = new Parameter[1]; Parameter[] params = new Parameter[1];
params[0] = param; params[0] = param;
s.setParameters(params); 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.setName("min");
param.setValue("garbage out"); param.setValue("garbage out");
Parameter[] params = new Parameter[1];
params[0] = param; params[0] = param;
s.setParameters(params); 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.setName("max");
param.setValue("garbage out"); param.setValue("garbage out");
Parameter[] params = new Parameter[1];
params[0] = param; params[0] = param;
s.setParameters(params); 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. * Tests to make sure that the selector is selecting files correctly.
*/ */
@Test @Test
public void testSelectionBehaviour() {
DepthSelector s;
String results;

s = new DepthSelector();
public void testSelectionBehaviourMinMax() {
s.setMin(20); s.setMin(20);
s.setMax(25); 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); 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); 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); 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.setMin(1);
s.setMax(1); s.setMax(1);
results = selectorRule.selectionString(s);
assertEquals("FFFFFTTTFFFT", results);
assertEquals("FFFFFTTTFFFT", selectorRule.selectionString(s));
} }


} }

+ 47
- 40
src/tests/junit/org/apache/tools/ant/types/selectors/FilenameSelectorTest.java View File

@@ -18,13 +18,14 @@


package org.apache.tools.ant.types.selectors; package org.apache.tools.ant.types.selectors;


import static org.junit.Assert.assertEquals;

import org.apache.tools.ant.BuildException; import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.types.Parameter; import org.apache.tools.ant.types.Parameter;
import org.junit.Before;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import org.junit.rules.ExpectedException;


/** /**
* Tests Filename Selectors * Tests Filename Selectors
@@ -35,32 +36,41 @@ public class FilenameSelectorTest {
@Rule @Rule
public final BaseSelectorRule selectorRule = new BaseSelectorRule(); 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 @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(); Parameter param = new Parameter();
param.setName("garbage in"); param.setName("garbage in");
param.setValue("garbage out"); param.setValue("garbage out");
Parameter[] params = {param}; Parameter[] params = {param};
s.setParameters(params); 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 @Test
public void testSelectionBehaviour() { public void testSelectionBehaviour() {
FilenameSelector s;
String results;

s = new FilenameSelector();
s.setName("no match possible"); 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 // This is turned off temporarily. There appears to be a bug
// in SelectorUtils.matchPattern() where it is recursive on // in SelectorUtils.matchPattern() where it is recursive on
// Windows even if no ** is in pattern. // Windows even if no ** is in pattern.
//assertEquals("FFFTFFFFFFFF", results); // Unix
// s.setName("*.gz");
// String results = selectorRule.selectionString(s);
// assertEquals("FFFTFFFFFFFF", results); // Unix
// vs // vs
//assertEquals("FFFTFFFFTFFF", results); // Windows
// assertEquals("FFFTFFFFTFFF", results); // Windows


s = new FilenameSelector();
@Test
public void testSelectionBehaviourNegate() {
s.setName("**/*.gz"); s.setName("**/*.gz");
s.setNegate(true); 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.setName("**/*.GZ");
s.setCasesensitive(false); 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(); Parameter param1 = new Parameter();
param1.setName("name"); param1.setName("name");
param1.setValue("**/*.bz2"); param1.setValue("**/*.bz2");
Parameter[] params = {param1}; Parameter[] params = {param1};
s.setParameters(params); s.setParameters(params);
results = selectorRule.selectionString(s);
assertEquals("FFTFFFFFFTTF", results);
assertEquals("FFTFFFFFFTTF", selectorRule.selectionString(s));
} }


} }

+ 65
- 64
src/tests/junit/org/apache/tools/ant/types/selectors/PresentSelectorTest.java View File

@@ -18,16 +18,16 @@


package org.apache.tools.ant.types.selectors; 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.BuildException;
import org.apache.tools.ant.types.Mapper; import org.apache.tools.ant.types.Mapper;
import org.junit.Before;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; 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 * Tests Present Selectors
@@ -35,34 +35,39 @@ import static org.junit.Assert.fail;
*/ */
public class PresentSelectorTest { public class PresentSelectorTest {


private PresentSelector s;

private File beddir;


@Rule @Rule
public final BaseSelectorRule selectorRule = new BaseSelectorRule(); 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 the code that validates the selector.
*/ */
@Test @Test
public void testValidate() { 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 @Test
public void testSelectionBehaviour() { 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); 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); s.setTargetdir(beddir);
m = s.createMapper();
Mapper m = s.createMapper();
Mapper.MapperType identity = new Mapper.MapperType();
identity.setValue("identity");
m.setType(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"); File subdir = new File(System.getProperty("root"), "src/etc/testcases/taskdefs/expected");
s.setTargetdir(subdir); s.setTargetdir(subdir);
m = s.createMapper();
Mapper m = s.createMapper();
Mapper.MapperType flatten = new Mapper.MapperType();
flatten.setValue("flatten");
m.setType(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); s.setTargetdir(beddir);
m = s.createMapper();
Mapper m = s.createMapper();
Mapper.MapperType merge = new Mapper.MapperType();
merge.setValue("merge");
m.setType(merge); m.setType(merge);
m.setTo("asf-logo.gif.gz"); 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); s.setTargetdir(subdir);
m = s.createMapper();
Mapper m = s.createMapper();
Mapper.MapperType glob = new Mapper.MapperType();
glob.setValue("glob");
m.setType(glob); m.setType(glob);
m.setFrom("*.bz2"); m.setFrom("*.bz2");
m.setTo("*.tar.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); 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));
} }


} }

+ 101
- 95
src/tests/junit/org/apache/tools/ant/types/selectors/SizeSelectorTest.java View File

@@ -18,15 +18,16 @@


package org.apache.tools.ant.types.selectors; package org.apache.tools.ant.types.selectors;


import static org.junit.Assert.assertEquals;

import java.util.Locale; import java.util.Locale;


import org.apache.tools.ant.BuildException; import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.types.Parameter; import org.apache.tools.ant.types.Parameter;
import org.junit.Before;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import org.junit.rules.ExpectedException;


/** /**
* Tests Size Selectors * Tests Size Selectors
@@ -37,140 +38,151 @@ public class SizeSelectorTest {
@Rule @Rule
public final BaseSelectorRule selectorRule = new BaseSelectorRule(); 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 the code that validates the selector.
*/ */
@Test @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); 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(); Parameter param = new Parameter();
param.setName("garbage in"); param.setName("garbage in");
param.setValue("garbage out"); param.setValue("garbage out");
Parameter[] params = {param}; Parameter[] params = {param};
s.setParameters(params); 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.setName("value");
param.setValue("garbage out"); param.setValue("garbage out");
params[0] = param;
Parameter[] params = {param};
s.setParameters(params); 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 param1 = new Parameter();
Parameter param2 = new Parameter(); Parameter param2 = new Parameter();
param1.setName("value"); param1.setName("value");
param1.setValue("5"); param1.setValue("5");
param2.setName("units"); param2.setName("units");
param2.setValue("garbage out"); param2.setValue("garbage out");
params = new Parameter[2];
Parameter[] params = new Parameter[2];
params[0] = param1; params[0] = param1;
params[1] = param2; 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. * Tests to make sure that the selector is selecting files correctly.
*/ */
@Test @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.setValue(10);
s.setWhen(less); 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.setValue(10);
s.setWhen(more); 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.setValue(32);
s.setWhen(equal); 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.setValue(7);
s.setWhen(more); s.setWhen(more);
s.setUnits(kilo); 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.setValue(7);
s.setWhen(more); s.setWhen(more);
s.setUnits(kibi); 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.setValue(99999);
s.setWhen(more); s.setWhen(more);
s.setUnits(tibi); 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 param1 = new Parameter();
Parameter param2 = new Parameter(); Parameter param2 = new Parameter();
Parameter param3 = new Parameter(); Parameter param3 = new Parameter();
@@ -182,8 +194,7 @@ public class SizeSelectorTest {
param3.setValue("more"); param3.setValue("more");
Parameter[] params = {param1, param2, param3}; Parameter[] params = {param1, param2, param3};
s.setParameters(params); s.setParameters(params);
results = selectorRule.selectionString(s);
assertEquals("TFFFFFFTFFTT", results);
assertEquals("TFFFFFFTFFTT", selectorRule.selectionString(s));
} }


@Test @Test
@@ -219,16 +230,11 @@ public class SizeSelectorTest {
} }


private void testCaseInsensitiveParameterParsing(String name) { 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(); Parameter p = new Parameter();
p.setName(name); p.setName(name);
p.setValue("foo"); 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);
} }
} }

+ 25
- 20
src/tests/junit/org/apache/tools/ant/types/selectors/TypeSelectorTest.java View File

@@ -19,8 +19,10 @@
package org.apache.tools.ant.types.selectors; package org.apache.tools.ant.types.selectors;


import org.apache.tools.ant.BuildException; import org.apache.tools.ant.BuildException;
import org.junit.Before;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.junit.rules.ExpectedException;


import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail; import static org.junit.Assert.fail;
@@ -34,43 +36,46 @@ public class TypeSelectorTest {
@Rule @Rule
public BaseSelectorRule selectorRule = new BaseSelectorRule(); 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 the code that validates the selector.
*/ */
@Test @Test
public void testValidate() { 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. * Tests to make sure that the selector is selecting files correctly.
*/ */
@Test @Test
public void testSelectionBehaviour() {
TypeSelector s;
String results;

public void testSelectionBehaviourDir() {
TypeSelector.FileType directory = new TypeSelector.FileType(); TypeSelector.FileType directory = new TypeSelector.FileType();
directory.setValue("dir"); directory.setValue("dir");
TypeSelector.FileType file = new TypeSelector.FileType();
file.setValue("file");


s = new TypeSelector();
s.setType(directory); 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); s.setType(file);
results = selectorRule.selectionString(s);
assertEquals("FTTTTTTTTTTF", results);
assertEquals("FTTTTTTTTTTF", selectorRule.selectionString(s));
} }


} }

+ 75
- 50
src/tests/junit/org/apache/tools/zip/AsiExtraFieldTest.java View File

@@ -18,13 +18,17 @@


package org.apache.tools.zip; package org.apache.tools.zip;


import org.junit.Test;

import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotSame; import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertTrue; 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. * JUnit testcases for org.apache.tools.zip.AsiExtraField.
@@ -32,12 +36,21 @@ import static org.junit.Assert.fail;
*/ */
public class AsiExtraFieldTest implements UnixStat { 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 file mode magic.
*/ */
@Test @Test
public void testModes() { public void testModes() {
AsiExtraField a = new AsiExtraField();
a.setMode(0123); a.setMode(0123);
assertEquals("plain file", 0100123, a.getMode()); assertEquals("plain file", 0100123, a.getMode());
a.setDirectory(true); a.setDirectory(true);
@@ -51,7 +64,6 @@ public class AsiExtraFieldTest implements UnixStat {
*/ */
@Test @Test
public void testContent() { public void testContent() {
AsiExtraField a = new AsiExtraField();
a.setMode(0123); a.setMode(0123);
a.setUserId(5); a.setUserId(5);
a.setGroupId(6); a.setGroupId(6);
@@ -82,83 +94,96 @@ public class AsiExtraFieldTest implements UnixStat {
} }


/** /**
* Test reparse
* Test reparse file
*/ */
@Test @Test
public void testReparse() throws Exception {
AsiExtraField a = new AsiExtraField();
public void testReparseFile() throws Exception {
// CRC manually calculated, sorry // CRC manually calculated, sorry
byte[] data = {(byte) 0xC6, 0x02, 0x78, (byte) 0xB6, // CRC 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); a.parseFromLocalFileData(data, 0, data.length);
assertEquals("length plain file", 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 link", a.isLink());
assertFalse("plain file, no dir", a.isDirectory()); assertFalse("plain file, no dir", a.isDirectory());
assertEquals("mode plain file", FILE_FLAG | 0123, a.getMode()); assertEquals("mode plain file", FILE_FLAG | 0123, a.getMode());
assertEquals("uid plain file", 5, a.getUserId()); assertEquals("uid plain file", 5, a.getUserId());
assertEquals("gid plain file", 6, a.getGroupId()); 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); a.parseFromLocalFileData(data, 0, data.length);
assertEquals("length link", data.length, assertEquals("length link", data.length,
a.getLocalFileDataLength().getValue());
a.getLocalFileDataLength().getValue());
assertTrue("link, is link", a.isLink()); assertTrue("link, is link", a.isLink());
assertFalse("link, no dir", a.isDirectory()); assertFalse("link, no dir", a.isDirectory());
assertEquals("mode link", LINK_FLAG | 0123, a.getMode()); assertEquals("mode link", LINK_FLAG | 0123, a.getMode());
assertEquals("uid link", 5, a.getUserId()); assertEquals("uid link", 5, a.getUserId());
assertEquals("gid link", 6, a.getGroupId()); assertEquals("gid link", 6, a.getGroupId());
assertEquals("test", a.getLinkedFile()); 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); a.parseFromLocalFileData(data, 0, data.length);
assertEquals("length dir", data.length, assertEquals("length dir", data.length,
a.getLocalFileDataLength().getValue());
a.getLocalFileDataLength().getValue());
assertFalse("dir, no link", a.isLink()); assertFalse("dir, no link", a.isLink());
assertTrue("dir, is dir", a.isDirectory()); assertTrue("dir, is dir", a.isDirectory());
assertEquals("mode dir", DIR_FLAG | 0123, a.getMode()); assertEquals("mode dir", DIR_FLAG | 0123, a.getMode());
assertEquals("uid dir", 5, a.getUserId()); assertEquals("uid dir", 5, a.getUserId());
assertEquals("gid dir", 6, a.getGroupId()); 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 @Test
public void testClone() { 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());
} }
} }

Loading…
Cancel
Save