diff --git a/src/tests/junit/org/apache/tools/ant/types/PathTest.java b/src/tests/junit/org/apache/tools/ant/types/PathTest.java index 46ac37e3c..57a9ea0d9 100644 --- a/src/tests/junit/org/apache/tools/ant/types/PathTest.java +++ b/src/tests/junit/org/apache/tools/ant/types/PathTest.java @@ -25,13 +25,14 @@ import org.apache.tools.ant.BuildException; import org.apache.tools.ant.Project; import org.apache.tools.ant.taskdefs.condition.Os; import org.junit.Before; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; import static org.hamcrest.Matchers.containsString; +import static org.hamcrest.Matchers.endsWith; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertThat; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; /** * JUnit testcases for org.apache.tools.ant.types.Path @@ -44,17 +45,24 @@ public class PathTest { public static boolean isNetWare = Os.isFamily("netware"); private Project project; + private Path p; + + @Rule + public ExpectedException thrown = ExpectedException.none(); @Before public void setUp() { project = new Project(); - project.setBasedir(System.getProperty("root")); + if (System.getProperty("root") != null) { + project.setBasedir(System.getProperty("root")); + } + p = new Path(project); } // actually tests constructor as well as setPath @Test public void testConstructorUnixStyle() { - Path p = new Path(project, "/a:/b"); + p = new Path(project, "/a:/b"); String[] l = p.list(); assertEquals("two items, Unix style", 2, l.length); if (isUnixStyle) { @@ -72,31 +80,31 @@ public class PathTest { @Test public void testRelativePathUnixStyle() { - project.setBasedir(new File(System.getProperty("root"), "src/etc").getAbsolutePath()); - Path p = new Path(project, "..:testcases"); + project.setBasedir(new File(project.getBaseDir(), "src/etc").getAbsolutePath()); + p = new Path(project, "..:testcases"); String[] l = p.list(); assertEquals("two items, Unix style", 2, l.length); if (isUnixStyle) { - assertTrue("test resolved relative to src/etc", - l[0].endsWith("/src")); - assertTrue("test resolved relative to src/etc", - l[1].endsWith("/src/etc/testcases")); + assertThat("test resolved relative to src/etc", + l[0], endsWith("/src")); + assertThat("test resolved relative to src/etc", + l[1], endsWith("/src/etc/testcases")); } else if (isNetWare) { - assertTrue("test resolved relative to src/etc", - l[0].endsWith("\\src")); - assertTrue("test resolved relative to src/etc", - l[1].endsWith("\\src\\etc\\testcases")); + assertThat("test resolved relative to src/etc", + l[0], endsWith("\\src")); + assertThat("test resolved relative to src/etc", + l[1], endsWith("\\src\\etc\\testcases")); } else { - assertTrue("test resolved relative to src/etc", - l[0].endsWith("\\src")); - assertTrue("test resolved relative to src/etc", - l[1].endsWith("\\src\\etc\\testcases")); + assertThat("test resolved relative to src/etc", + l[0], endsWith("\\src")); + assertThat("test resolved relative to src/etc", + l[1], endsWith("\\src\\etc\\testcases")); } } @Test - public void testConstructorWindowsStyle() { - Path p = new Path(project, "\\a;\\b"); + public void testConstructorWindowsStyleTwoItemsNoDrive() { + p = new Path(project, "\\a;\\b"); String[] l = p.list(); assertEquals("two items, DOS style", 2, l.length); if (isUnixStyle) { @@ -110,13 +118,16 @@ public class PathTest { assertEquals(base + "a", l[0]); assertEquals(base + "b", l[1]); } + } + @Test + public void testConstructorWindowsStyle() { p = new Path(project, "c:\\test"); - l = p.list(); + String[] l = p.list(); if (isUnixStyle) { assertEquals("no drives on Unix", 2, l.length); - assertTrue("c resolved relative to project\'s basedir", - l[0].endsWith("/c")); + assertThat("c resolved relative to project\'s basedir", + l[0], endsWith("/c")); assertEquals("/test", l[1]); } else if (isNetWare) { assertEquals("volumes on NetWare", 1, l.length); @@ -125,16 +136,19 @@ public class PathTest { assertEquals("drives on DOS", 1, l.length); assertEquals("c:\\test", l[0].toLowerCase(Locale.US)); } + } + @Test + public void testConstructorWindowsStyleTwoItems() { p = new Path(project, "c:\\test;d:\\programs"); - l = p.list(); + String[] l = p.list(); if (isUnixStyle) { assertEquals("no drives on Unix", 4, l.length); - assertTrue("c resolved relative to project\'s basedir", - l[0].endsWith("/c")); + assertThat("c resolved relative to project\'s basedir", + l[0], endsWith("/c")); assertEquals("/test", l[1]); - assertTrue("d resolved relative to project\'s basedir", - l[2].endsWith("/d")); + assertThat("d resolved relative to project\'s basedir", + l[2], endsWith("/d")); assertEquals("/programs", l[3]); } else if (isNetWare) { assertEquals("volumes on NetWare", 2, l.length); @@ -145,13 +159,16 @@ public class PathTest { assertEquals("c:\\test", l[0].toLowerCase(Locale.US)); assertEquals("d:\\programs", l[1].toLowerCase(Locale.US)); } + } + @Test + public void testConstructorWindowsStyleUnixFS() { p = new Path(project, "c:/test"); - l = p.list(); + String[] l = p.list(); if (isUnixStyle) { assertEquals("no drives on Unix", 2, l.length); - assertTrue("c resolved relative to project\'s basedir", - l[0].endsWith("/c")); + assertThat("c resolved relative to project\'s basedir", + l[0], endsWith("/c")); assertEquals("/test", l[1]); } else if (isNetWare) { assertEquals("volumes on NetWare", 1, l.length); @@ -160,16 +177,19 @@ public class PathTest { assertEquals("drives on DOS", 1, l.length); assertEquals("c:\\test", l[0].toLowerCase(Locale.US)); } + } + @Test + public void testConstructorWindowsStyleUnixFSTwoItems() { p = new Path(project, "c:/test;d:/programs"); - l = p.list(); + String[] l = p.list(); if (isUnixStyle) { assertEquals("no drives on Unix", 4, l.length); - assertTrue("c resolved relative to project\'s basedir", - l[0].endsWith("/c")); + assertThat("c resolved relative to project\'s basedir", + l[0], endsWith("/c")); assertEquals("/test", l[1]); - assertTrue("d resolved relative to project\'s basedir", - l[2].endsWith("/d")); + assertThat("d resolved relative to project\'s basedir", + l[2], endsWith("/d")); assertEquals("/programs", l[3]); } else if (isNetWare) { assertEquals("volumes on NetWare", 2, l.length); @@ -182,37 +202,40 @@ public class PathTest { } } + // try a netware-volume length path, see how it is handled @Test public void testConstructorNetWareStyle() { - // try a netware-volume length path, see how it is handled - Path p = new Path(project, "sys:\\test"); + p = new Path(project, "sys:\\test"); String[] l = p.list(); if (isUnixStyle) { assertEquals("no drives on Unix", 2, l.length); - assertTrue("sys resolved relative to project\'s basedir", - l[0].endsWith("/sys")); + assertThat("sys resolved relative to project\'s basedir", + l[0], endsWith("/sys")); assertEquals("/test", l[1]); } else if (isNetWare) { assertEquals("sys:\\test", l[0].toLowerCase(Locale.US)); assertEquals("volumes on NetWare", 1, l.length); } else { assertEquals("no multiple character-length volumes on Windows", 2, l.length); - assertTrue("sys resolved relative to project\'s basedir", - l[0].endsWith("\\sys")); - assertTrue("test resolved relative to project\'s basedir", - l[1].endsWith("\\test")); + assertThat("sys resolved relative to project\'s basedir", + l[0], endsWith("\\sys")); + assertThat("test resolved relative to project\'s basedir", + l[1], endsWith("\\test")); } + } - // try a multi-part netware-volume length path, see how it is handled + // try a multi-part netware-volume length path, see how it is handled + @Test + public void testConstructorNetWareStyleTwoItems() { p = new Path(project, "sys:\\test;dev:\\temp"); - l = p.list(); + String[] l = p.list(); if (isUnixStyle) { assertEquals("no drives on Unix", 4, l.length); - assertTrue("sys resolved relative to project\'s basedir", - l[0].endsWith("/sys")); + assertThat("sys resolved relative to project\'s basedir", + l[0], endsWith("/sys")); assertEquals("/test", l[1]); - assertTrue("dev resolved relative to project\'s basedir", - l[2].endsWith("/dev")); + assertThat("dev resolved relative to project\'s basedir", + l[2], endsWith("/dev")); assertEquals("/temp", l[3]); } else if (isNetWare) { assertEquals("volumes on NetWare", 2, l.length); @@ -220,45 +243,51 @@ public class PathTest { assertEquals("dev:\\temp", l[1].toLowerCase(Locale.US)); } else { assertEquals("no multiple character-length volumes on Windows", 4, l.length); - assertTrue("sys resolved relative to project\'s basedir", - l[0].endsWith("\\sys")); - assertTrue("test resolved relative to project\'s basedir", - l[1].endsWith("\\test")); - assertTrue("dev resolved relative to project\'s basedir", - l[2].endsWith("\\dev")); - assertTrue("temp resolved relative to project\'s basedir", - l[3].endsWith("\\temp")); + assertThat("sys resolved relative to project\'s basedir", + l[0], endsWith("\\sys")); + assertThat("test resolved relative to project\'s basedir", + l[1], endsWith("\\test")); + assertThat("dev resolved relative to project\'s basedir", + l[2], endsWith("\\dev")); + assertThat("temp resolved relative to project\'s basedir", + l[3], endsWith("\\temp")); } + } - // try a netware-volume length path w/forward slash, see how it is handled + // try a netware-volume length path w/forward slash, see how it is handled + @Test + public void testConstructorNetWareStyleUnixFS() { p = new Path(project, "sys:/test"); - l = p.list(); + String[] l = p.list(); if (isUnixStyle) { assertEquals("no drives on Unix", 2, l.length); - assertTrue("sys resolved relative to project\'s basedir", - l[0].endsWith("/sys")); + assertThat("sys resolved relative to project\'s basedir", + l[0], endsWith("/sys")); assertEquals("/test", l[1]); } else if (isNetWare) { assertEquals("volumes on NetWare", 1, l.length); assertEquals("sys:\\test", l[0].toLowerCase(Locale.US)); } else { assertEquals("no multiple character-length volumes on Windows", 2, l.length); - assertTrue("sys resolved relative to project\'s basedir", - l[0].endsWith("\\sys")); - assertTrue("test resolved relative to project\'s basedir", - l[1].endsWith("\\test")); + assertThat("sys resolved relative to project\'s basedir", + l[0], endsWith("\\sys")); + assertThat("test resolved relative to project\'s basedir", + l[1], endsWith("\\test")); } + } - // try a multi-part netware-volume length path w/forward slash, see how it is handled + // try a multi-part netware-volume length path w/forward slash, see how it is handled + @Test + public void testConstructorNetWareStyleUnixFSTwoItems() { p = new Path(project, "sys:/test;dev:/temp"); - l = p.list(); + String[] l = p.list(); if (isUnixStyle) { assertEquals("no drives on Unix", 4, l.length); - assertTrue("sys resolved relative to project\'s basedir", - l[0].endsWith("/sys")); + assertThat("sys resolved relative to project\'s basedir", + l[0], endsWith("/sys")); assertEquals("/test", l[1]); - assertTrue("dev resolved relative to project\'s basedir", - l[2].endsWith("/dev")); + assertThat("dev resolved relative to project\'s basedir", + l[2], endsWith("/dev")); assertEquals("/temp", l[3]); } else if (isNetWare) { assertEquals("volumes on NetWare", 2, l.length); @@ -266,26 +295,28 @@ public class PathTest { assertEquals("dev:\\temp", l[1].toLowerCase(Locale.US)); } else { assertEquals("no multiple character-length volumes on Windows", 4, l.length); - assertTrue("sys resolved relative to project\'s basedir", - l[0].endsWith("\\sys")); - assertTrue("test resolved relative to project\'s basedir", - l[1].endsWith("\\test")); - assertTrue("dev resolved relative to project\'s basedir", - l[2].endsWith("\\dev")); - assertTrue("temp resolved relative to project\'s basedir", - l[3].endsWith("\\temp")); + assertThat("sys resolved relative to project\'s basedir", + l[0], endsWith("\\sys")); + assertThat("test resolved relative to project\'s basedir", + l[1], endsWith("\\test")); + assertThat("dev resolved relative to project\'s basedir", + l[2], endsWith("\\dev")); + assertThat("temp resolved relative to project\'s basedir", + l[3], endsWith("\\temp")); } + } - // try a multi-part netware-volume length path with UNIX - // separator (this testcase if from an actual bug that was - // found, in AvailableTest, which uses PathTokenizer) - p = new Path(project, - "SYS:\\JAVA/lib/rt.jar:SYS:\\JAVA/lib/classes.zip"); - l = p.list(); + // try a multi-part netware-volume length path with UNIX + // separator (this testcase if from an actual bug that was + // found, in AvailableTest, which uses PathTokenizer) + @Test + public void testConstructorNetWareStyleUnixPS() { + p = new Path(project, "SYS:\\JAVA/lib/rt.jar:SYS:\\JAVA/lib/classes.zip"); + String[] l = p.list(); if (isUnixStyle) { assertEquals("no drives on Unix", 3, l.length); - assertTrue("sys resolved relative to project\'s basedir", - l[0].endsWith("/SYS")); + assertThat("sys resolved relative to project\'s basedir", + l[0], endsWith("/SYS")); assertEquals("/JAVA/lib/rt.jar", l[1]); assertEquals("/JAVA/lib/classes.zip", l[2]); } else if (isNetWare) { @@ -294,18 +325,18 @@ public class PathTest { assertEquals("sys:\\java\\lib\\classes.zip", l[1].toLowerCase(Locale.US)); } else { assertEquals("no multiple character-length volumes on Windows", 3, l.length); - assertTrue("sys resolved relative to project\'s basedir", - l[0].endsWith("\\SYS")); - assertTrue("java/lib/rt.jar resolved relative to project\'s basedir", - l[1].endsWith("\\JAVA\\lib\\rt.jar")); - assertTrue("java/lib/classes.zip resolved relative to project\'s basedir", - l[2].endsWith("\\JAVA\\lib\\classes.zip")); + assertThat("sys resolved relative to project\'s basedir", + l[0], endsWith("\\SYS")); + assertThat("java/lib/rt.jar resolved relative to project\'s basedir", + l[1], endsWith("\\JAVA\\lib\\rt.jar")); + assertThat("java/lib/classes.zip resolved relative to project\'s basedir", + l[2], endsWith("\\JAVA\\lib\\classes.zip")); } } @Test public void testConstructorMixedStyle() { - Path p = new Path(project, "\\a;\\b:/c"); + p = new Path(project, "\\a;\\b:/c"); String[] l = p.list(); assertEquals("three items, mixed style", 3, l.length); if (isUnixStyle) { @@ -326,7 +357,6 @@ public class PathTest { @Test public void testSetLocation() { - Path p = new Path(project); p.setLocation(new File(File.separatorChar + "a")); String[] l = p.list(); if (isUnixStyle) { @@ -343,7 +373,7 @@ public class PathTest { @Test public void testAppending() { - Path p = new Path(project, "/a:/b"); + p = new Path(project, "/a:/b"); String[] l = p.list(); assertEquals("2 after construction", 2, l.length); p.setLocation(new File("/c")); @@ -362,7 +392,7 @@ public class PathTest { @Test public void testEmptyPath() { - Path p = new Path(project, ""); + p = new Path(project, ""); String[] l = p.list(); assertEquals("0 after construction", 0, l.length); p.setPath(""); @@ -378,7 +408,7 @@ public class PathTest { @Test public void testUnique() { - Path p = new Path(project, "/a:/a"); + p = new Path(project, "/a:/a"); String[] l = p.list(); assertEquals("1 after construction", 1, l.length); String base = new File(File.separator).getAbsolutePath(); @@ -397,125 +427,112 @@ public class PathTest { } @Test - public void testEmptyElementIfIsReference() { - Path p = new Path(project, "/a:/a"); - try { - p.setRefid(new Reference(project, "dummyref")); - fail("Can add reference to Path with elements from constructor"); - } catch (BuildException be) { - assertEquals("You must not specify more than one attribute when using refid", - be.getMessage()); - } + public void testEmptyElementSetRefid() { + thrown.expect(BuildException.class); + thrown.expectMessage("You must not specify more than one attribute when using refid"); + p = new Path(project, "/a:/a"); + p.setRefid(new Reference(project, "dummyref")); + } - p = new Path(project); + @Test + public void testEmptyElementSetLocationThenRefid() { + thrown.expect(BuildException.class); + thrown.expectMessage("You must not specify more than one attribute when using refid"); p.setLocation(new File("/a")); - try { - p.setRefid(new Reference(project, "dummyref")); - fail("Can add reference to Path with elements from setLocation"); - } catch (BuildException be) { - assertEquals("You must not specify more than one attribute when using refid", - be.getMessage()); - } + p.setRefid(new Reference(project, "dummyref")); + } + @Test + public void testUseExistingRefid() { + thrown.expect(BuildException.class); + thrown.expectMessage("You must not specify more than one attribute when using refid"); Path another = new Path(project, "/a:/a"); project.addReference("dummyref", another); - p = new Path(project); p.setRefid(new Reference(project, "dummyref")); - try { - p.setLocation(new File("/a")); - fail("Can set location in Path that is a reference."); - } catch (BuildException be) { - assertEquals("You must not specify more than one attribute when using refid", - be.getMessage()); - } + p.setLocation(new File("/a")); + } - try { - p.setPath("/a;\\a"); - fail("Can set path in Path that is a reference."); - } catch (BuildException be) { - assertEquals("You must not specify more than one attribute when using refid", - be.getMessage()); - } + @Test + public void testEmptyElementIfIsReferenceAttr() { + thrown.expect(BuildException.class); + thrown.expectMessage("You must not specify more than one attribute when using refid"); + p.setRefid(new Reference(project, "dummyref")); + p.setPath("/a;\\a"); + } - try { - p.createPath(); - fail("Can create nested Path in Path that is a reference."); - } catch (BuildException be) { - assertEquals("You must not specify nested elements when using refid", - be.getMessage()); - } + @Test + public void testEmptyElementCreatePath() { + thrown.expect(BuildException.class); + thrown.expectMessage("You must not specify nested elements when using refid"); + p.setRefid(new Reference(project, "dummyref")); + p.createPath(); + } - try { - p.createPathElement(); - fail("Can create nested PathElement in Path that is a reference."); - } catch (BuildException be) { - assertEquals("You must not specify nested elements when using refid", - be.getMessage()); - } + @Test + public void testEmptyElementCreatePathElement() { + thrown.expect(BuildException.class); + thrown.expectMessage("You must not specify nested elements when using refid"); + p.setRefid(new Reference(project, "dummyref")); + p.createPathElement(); + } - try { - p.addFileset(new FileSet()); - fail("Can add nested FileSet in Path that is a reference."); - } catch (BuildException be) { - assertEquals("You must not specify nested elements when using refid", - be.getMessage()); - } + @Test + public void testEmptyElementAddFileset() { + thrown.expect(BuildException.class); + thrown.expectMessage("You must not specify nested elements when using refid"); + p.setRefid(new Reference(project, "dummyref")); + p.addFileset(new FileSet()); + } - try { - p.addFilelist(new FileList()); - fail("Can add nested FileList in Path that is a reference."); - } catch (BuildException be) { - assertEquals("You must not specify nested elements when using refid", - be.getMessage()); - } + @Test + public void testEmptyElementAddFilelist() { + thrown.expect(BuildException.class); + thrown.expectMessage("You must not specify nested elements when using refid"); + p.setRefid(new Reference(project, "dummyref")); + p.addFilelist(new FileList()); + } - try { - p.addDirset(new DirSet()); - fail("Can add nested Dirset in Path that is a reference."); - } catch (BuildException be) { - assertEquals("You must not specify nested elements when using refid", - be.getMessage()); - } + @Test + public void testEmptyElementAddDirset() { + thrown.expect(BuildException.class); + thrown.expectMessage("You must not specify nested elements when using refid"); + p.setRefid(new Reference(project, "dummyref")); + p.addDirset(new DirSet()); } @Test public void testCircularReferenceCheck() { - Path p = new Path(project); + thrown.expect(BuildException.class); + thrown.expectMessage("This data type contains a circular reference."); project.addReference("dummy", p); p.setRefid(new Reference(project, "dummy")); - try { - p.list(); - fail("Can make Path a Reference to itself."); - } catch (BuildException be) { - assertEquals("This data type contains a circular reference.", - be.getMessage()); - } + p.list(); + } + @Test + public void testLoopReferenceCheck() { // dummy1 --> dummy2 --> dummy3 --> dummy1 - Path p1 = new Path(project); - project.addReference("dummy1", p1); - Path p2 = p1.createPath(); - project.addReference("dummy2", p2); - Path p3 = p2.createPath(); - project.addReference("dummy3", p3); - p3.setRefid(new Reference(project, "dummy1")); - try { - p1.list(); - fail("Can make circular reference."); - } catch (BuildException be) { - assertEquals("This data type contains a circular reference.", - be.getMessage()); - } + thrown.expect(BuildException.class); + thrown.expectMessage("This data type contains a circular reference."); + project.addReference("dummy1", p); + Path pa = p.createPath(); + project.addReference("dummy2", pa); + Path pb = pa.createPath(); + project.addReference("dummy3", pb); + pb.setRefid(new Reference(project, "dummy1")); + p.list(); + } + @Test + public void testLoopReferenceCheckWithLocation() { // dummy1 --> dummy2 --> dummy3 (with Path "/a") - p1 = new Path(project); - project.addReference("dummy1", p1); - p2 = p1.createPath(); - project.addReference("dummy2", p2); - p3 = p2.createPath(); - project.addReference("dummy3", p3); - p3.setLocation(new File("/a")); - String[] l = p1.list(); + project.addReference("dummy1", p); + Path pa = p.createPath(); + project.addReference("dummy2", pa); + Path pb = pa.createPath(); + project.addReference("dummy3", pb); + pb.setLocation(new File("/a")); + String[] l = p.list(); assertEquals("One element buried deep inside a nested path structure", 1, l.length); if (isUnixStyle) { @@ -529,7 +546,6 @@ public class PathTest { @Test public void testFileList() { - Path p = new Path(project); FileList f = new FileList(); f.setProject(project); f.setDir(project.resolveFile(".")); @@ -542,7 +558,6 @@ public class PathTest { @Test public void testFileSet() { - Path p = new Path(project); FileSet f = new FileSet(); f.setProject(project); f.setDir(project.resolveFile(".")); @@ -555,7 +570,6 @@ public class PathTest { @Test public void testDirSet() { - Path p = new Path(project); DirSet d = new DirSet(); d.setProject(project); d.setDir(project.resolveFile(".")); @@ -568,13 +582,12 @@ public class PathTest { @Test public void testRecursion() { - Path p = new Path(project); + thrown.expect(BuildException.class); + thrown.expectMessage("circular"); try { p.append(p); + } finally { assertEquals(0, p.list().length); - } catch (BuildException x) { - String m = x.toString(); - assertThat(m, m, containsString("circular")); } } diff --git a/src/tests/junit/org/apache/tools/ant/types/XMLCatalogTest.java b/src/tests/junit/org/apache/tools/ant/types/XMLCatalogTest.java index 45a08414a..5b607e445 100644 --- a/src/tests/junit/org/apache/tools/ant/types/XMLCatalogTest.java +++ b/src/tests/junit/org/apache/tools/ant/types/XMLCatalogTest.java @@ -31,15 +31,17 @@ import org.apache.tools.ant.BuildException; import org.apache.tools.ant.Project; import org.apache.tools.ant.util.JAXPUtils; import org.junit.Before; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; import org.xml.sax.InputSource; import org.xml.sax.SAXException; +import static org.hamcrest.Matchers.endsWith; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertThat; /** * JUnit testcases for org.apache.tools.ant.types.XMLCatalog @@ -60,11 +62,15 @@ public class XMLCatalogTest { return JAXPUtils.getSystemId(file); } + @Rule + public ExpectedException thrown = ExpectedException.none(); + @Before public void setUp() { project = new Project(); - project.setBasedir(System.getProperty("root")); - + if (System.getProperty("root") != null) { + project.setBasedir(System.getProperty("root")); + } // This causes XMLCatalog to print out detailed logging // messages for debugging // @@ -78,26 +84,19 @@ public class XMLCatalogTest { } @Test - public void testEmptyCatalog() { - try { - InputSource result = catalog.resolveEntity("PUBLIC ID ONE", - "i/dont/exist.dtd"); - assertNull("Empty catalog should return null", result); - } catch (Exception e) { - fail("resolveEntity() failed!" + e.toString()); - } + public void testEmptyCatalogResolveEntity() throws IOException, SAXException { + InputSource result = catalog.resolveEntity("PUBLIC ID ONE", "i/dont/exist.dtd"); + assertNull("Empty catalog should return null", result); + } - try { - Source result = catalog.resolve("i/dont/exist.dtd", null); - String expected = toURLString(new File(project.getBaseDir() + - "/i/dont/exist.dtd")); - String resultStr = fileURLPartWithoutLeadingSlashes((SAXSource) result); - assertTrue("Empty catalog should return input with a system ID like " - + expected + " but was " + resultStr, - expected.endsWith(resultStr)); - } catch (Exception e) { - fail("resolve() failed!" + e.toString()); - } + @Test + public void testEmptyCatalogResolve() throws TransformerException, MalformedURLException { + String expected = toURLString(new File(project.getBaseDir() + + "/i/dont/exist.dtd")); + Source result = catalog.resolve("i/dont/exist.dtd", null); + String resultStr = fileURLPartWithoutLeadingSlashes((SAXSource) result); + assertThat("Empty catalog should return input with a system ID like " + + expected + " but was " + resultStr, expected, endsWith(resultStr)); } private static String fileURLPartWithoutLeadingSlashes(SAXSource result) @@ -131,76 +130,58 @@ public class XMLCatalogTest { String expected = toURLString(new File(project.getBaseDir().toURL() + "/i/dont/exist.dtd")); String resultStr = fileURLPartWithoutLeadingSlashes((SAXSource) result); - assertTrue("Nonexistent Catalog entry return input with a system ID like " + assertThat("Nonexistent Catalog entry return input with a system ID like " + expected + " but was " + resultStr, - expected.endsWith(resultStr)); + expected, endsWith(resultStr)); } @Test - public void testEmptyElementIfIsReference() { + public void testEmptyElementIfIsReferenceAttr() { + thrown.expect(BuildException.class); + thrown.expectMessage("You must not specify more than one attribute when using refid"); ResourceLocation dtd = new ResourceLocation(); dtd.setPublicId("PUBLIC ID ONE"); dtd.setLocation("i/dont/exist.dtd"); catalog.addDTD(dtd); project.addReference("catalog", catalog); + catalog.setRefid(new Reference(project, "dummyref")); + } - try { - catalog.setRefid(new Reference(project, "dummyref")); - fail("Can add reference to nonexistent XMLCatalog"); - } catch (BuildException be) { - assertEquals("You must not specify more than one " - + "attribute when using refid", be.getMessage()); - } - - XMLCatalog catalog2 = newCatalog(); - catalog2.setRefid(new Reference(project, "catalog")); - - try { - catalog2.addConfiguredXMLCatalog(catalog); - fail("Can add nested XMLCatalog to XMLCatalog that is a reference"); - } catch (BuildException be) { - assertEquals("You must not specify nested elements when using refid", - be.getMessage()); - } + @Test + public void testEmptyElementIfIsReferenceElem() { + thrown.expect(BuildException.class); + thrown.expectMessage("You must not specify nested elements when using refid"); + XMLCatalog catalogA = newCatalog(); + catalogA.setRefid(new Reference(project, "catalog")); + catalogA.addConfiguredXMLCatalog(catalog); } @Test public void testCircularReferenceCheck() throws IOException, SAXException { - + thrown.expect(BuildException.class); + thrown.expectMessage("This data type contains a circular reference."); // catalog <--> catalog project.addReference("catalog", catalog); catalog.setRefid(new Reference(project, "catalog")); + catalog.resolveEntity("PUBLIC ID ONE", "i/dont/exist.dtd"); + } - try { - @SuppressWarnings("unused") - InputSource result = catalog.resolveEntity("PUBLIC ID ONE", "i/dont/exist.dtd"); - fail("Can make XMLCatalog a Reference to itself."); - } catch (BuildException be) { - assertEquals("This data type contains a circular reference.", - be.getMessage()); - } catch (Exception e) { - fail("resolveEntity() failed!" + e.toString()); - } + @Test + public void testLoopReferenceCheck() throws IOException, SAXException { + thrown.expect(BuildException.class); + thrown.expectMessage("This data type contains a circular reference."); + // catalog --> catalogA --> catalogB --> catalog + project.addReference("catalog", catalog); + XMLCatalog catalogA = newCatalog(); + project.addReference("catalogA", catalogA); + XMLCatalog catalogB = newCatalog(); + project.addReference("catalogB", catalogB); - // catalog1 --> catalog2 --> catalog3 --> catalog1 - XMLCatalog catalog1 = newCatalog(); - project.addReference("catalog1", catalog1); - XMLCatalog catalog2 = newCatalog(); - project.addReference("catalog2", catalog2); - XMLCatalog catalog3 = newCatalog(); - project.addReference("catalog3", catalog3); - - catalog3.setRefid(new Reference(project, "catalog1")); - catalog2.setRefid(new Reference(project, "catalog3")); - catalog1.setRefid(new Reference(project, "catalog2")); - - try { - catalog1.resolveEntity("PUBLIC ID ONE", "i/dont/exist.dtd"); - fail("Can make circular reference"); - } catch (BuildException be) { - assertEquals("This data type contains a circular reference.", - be.getMessage()); - } + catalogB.setRefid(new Reference(project, "catalog")); + catalogA.setRefid(new Reference(project, "catalogB")); + catalog.setRefid(new Reference(project, "catalogA")); + + catalog.resolveEntity("PUBLIC ID ONE", "i/dont/exist.dtd"); } // inspired by Bugzilla Report 23913 @@ -211,14 +192,16 @@ public class XMLCatalogTest { ResourceLocation dtd = new ResourceLocation(); dtd.setPublicId("-//stevo//DTD doc 1.0//EN"); - String sysid = System.getProperty("root") + File.separator + "src/etc/testcases/taskdefs/optional/xml/doc.dtd"; + String sysid = project.resolveFile("src/etc/testcases/taskdefs/optional/xml/doc.dtd") + .getAbsolutePath(); dtd.setLocation(sysid); catalog.addDTD(dtd); - File dtdFile = project.resolveFile(sysid); InputSource result = catalog.resolveEntity("-//stevo//DTD doc 1.0//EN", "nap:chemical+brothers"); assertNotNull(result); + + File dtdFile = project.resolveFile(sysid); assertEquals(toURLString(dtdFile), result.getSystemId()); } @@ -229,13 +212,13 @@ public class XMLCatalogTest { String sysid = "src/etc/testcases/taskdefs/optional/xml/doc.dtd"; dtd.setLocation(sysid); catalog.addDTD(dtd); - File dtdFile = project.resolveFile(sysid); InputSource result = catalog.resolveEntity("-//stevo//DTD doc 1.0//EN", "nap:chemical+brothers"); assertNotNull(result); - assertEquals(toURLString(dtdFile), result.getSystemId()); + File dtdFile = project.resolveFile(sysid); + assertEquals(toURLString(dtdFile), result.getSystemId()); } @Test @@ -243,12 +226,11 @@ public class XMLCatalogTest { String publicId = "-//stevo//DTD doc 1.0//EN"; String sysid = "src/etc/testcases/taskdefs/optional/xml/doc.dtd"; - // catalog2 --> catalog1 --> catalog + // catalogB --> catalogA --> catalog ResourceLocation dtd = new ResourceLocation(); dtd.setPublicId(publicId); dtd.setLocation(sysid); catalog.addDTD(dtd); - File dtdFile = project.resolveFile(sysid); String uri = "http://foo.com/bar/blah.xml"; String uriLoc = "src/etc/testcases/taskdefs/optional/xml/about.xml"; @@ -257,26 +239,28 @@ public class XMLCatalogTest { entity.setPublicId(uri); entity.setLocation(uriLoc); catalog.addEntity(entity); - File xmlFile = project.resolveFile(uriLoc); project.addReference("catalog", catalog); - XMLCatalog catalog1 = newCatalog(); - project.addReference("catalog1", catalog1); - XMLCatalog catalog2 = newCatalog(); - project.addReference("catalog2", catalog1); + XMLCatalog catalogA = newCatalog(); + project.addReference("catalogA", catalogA); + XMLCatalog catalogB = newCatalog(); + project.addReference("catalogB", catalogB); - catalog1.setRefid(new Reference(project, "catalog")); - catalog2.setRefid(new Reference(project, "catalog1")); - - InputSource isResult = catalog2.resolveEntity(publicId, "nap:chemical+brothers"); + catalogA.setRefid(new Reference(project, "catalog")); + catalogB.setRefid(new Reference(project, "catalogA")); + InputSource isResult = catalogB.resolveEntity(publicId, "nap:chemical+brothers"); assertNotNull(isResult); + + File dtdFile = project.resolveFile(sysid); assertEquals(toURLString(dtdFile), isResult.getSystemId()); - Source result = catalog.resolve(uri, null); - assertNotNull(result); - assertEquals(toURLString(xmlFile), result.getSystemId()); + Source result = catalog.resolve(uri, null); + assertNotNull(result); + + File xmlFile = project.resolveFile(uriLoc); + assertEquals(toURLString(xmlFile), result.getSystemId()); } @Test @@ -288,7 +272,6 @@ public class XMLCatalogTest { dtd.setPublicId(publicId); dtd.setLocation(dtdLoc); catalog.addDTD(dtd); - File dtdFile = project.resolveFile(dtdLoc); String uri = "http://foo.com/bar/blah.xml"; String uriLoc = "src/etc/testcases/taskdefs/optional/xml/about.xml"; @@ -297,17 +280,19 @@ public class XMLCatalogTest { entity.setPublicId(uri); entity.setLocation(uriLoc); catalog.addEntity(entity); - File xmlFile = project.resolveFile(uriLoc); - XMLCatalog catalog1 = newCatalog(); - catalog1.addConfiguredXMLCatalog(catalog); - - InputSource isResult = catalog1.resolveEntity(publicId, "nap:chemical+brothers"); + XMLCatalog catalogA = newCatalog(); + catalogA.addConfiguredXMLCatalog(catalog); + InputSource isResult = catalogA.resolveEntity(publicId, "nap:chemical+brothers"); assertNotNull(isResult); + + File dtdFile = project.resolveFile(dtdLoc); assertEquals(toURLString(dtdFile), isResult.getSystemId()); Source result = catalog.resolve(uri, null); assertNotNull(result); + + File xmlFile = project.resolveFile(uriLoc); assertEquals(toURLString(xmlFile), result.getSystemId()); } @@ -321,10 +306,11 @@ public class XMLCatalogTest { entity.setPublicId(uri); entity.setLocation(uriLoc); catalog.addEntity(entity); - File xmlFile = project.resolveFile("src/" + uriLoc); Source result = catalog.resolve(uri, base); assertNotNull(result); + + File xmlFile = project.resolveFile("src/" + uriLoc); assertEquals(toURLString(xmlFile), result.getSystemId()); } @@ -338,7 +324,6 @@ public class XMLCatalogTest { dtd.setPublicId(publicId); dtd.setLocation(dtdLoc); catalog.addDTD(dtd); - File dtdFile = project.resolveFile("src/etc/" + dtdLoc); String uri = "http://foo.com/bar/blah.xml"; String uriLoc = "etc/testcases/taskdefs/optional/xml/about.xml"; @@ -348,7 +333,6 @@ public class XMLCatalogTest { entity.setPublicId(uri); entity.setLocation(uriLoc); catalog.addEntity(entity); - File xmlFile = project.resolveFile("src/" + uriLoc); Path aPath = new Path(project, path1); aPath.append(new Path(project, path2)); @@ -356,12 +340,16 @@ public class XMLCatalogTest { InputSource isResult = catalog.resolveEntity(publicId, "nap:chemical+brothers"); assertNotNull(isResult); + String resultStr1 = new URL(isResult.getSystemId()).getFile(); - assertTrue(toURLString(dtdFile).endsWith(resultStr1)); + File dtdFile = project.resolveFile("src/etc/" + dtdLoc); + assertThat(toURLString(dtdFile), endsWith(resultStr1)); Source result = catalog.resolve(uri, null); assertNotNull(result); + + File xmlFile = project.resolveFile("src/" + uriLoc); String resultStr = new URL(result.getSystemId()).getFile(); - assertTrue(toURLString(xmlFile).endsWith(resultStr)); + assertThat(toURLString(xmlFile), endsWith(resultStr)); } }