http://git-wip-us.apache.org/repos/asf/ant/blob/1ae68097/src/tests/junit/org/apache/tools/ant/types/PermissionsTest.java ---------------------------------------------------------------------- diff --git a/src/tests/junit/org/apache/tools/ant/types/PermissionsTest.java b/src/tests/junit/org/apache/tools/ant/types/PermissionsTest.java index 1e43fe1..37e643d 100644 --- a/src/tests/junit/org/apache/tools/ant/types/PermissionsTest.java +++ b/src/tests/junit/org/apache/tools/ant/types/PermissionsTest.java @@ -1,158 +1,158 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -package org.apache.tools.ant.types; - -import org.apache.tools.ant.ExitException; -import org.junit.Before; -import org.junit.Test; - -import static org.junit.Assert.fail; - -/** - * JUnit 3 testcases for org.apache.tools.ant.types.Permissions. - * - */ -public class PermissionsTest { - - Permissions perms; - - @Before - public void setUp() { - perms = new Permissions(); - Permissions.Permission perm = new Permissions.Permission(); - // Grant extra permissions to read and write the user.* properties and read to the - // java.home property - perm.setActions("read, write"); - perm.setName("user.*"); - perm.setClass("java.util.PropertyPermission"); - perms.addConfiguredGrant(perm); - - perm = new Permissions.Permission(); - perm.setActions("read"); - perm.setName("java.home"); - perm.setClass("java.util.PropertyPermission"); - perms.addConfiguredGrant(perm); - - perm = new Permissions.Permission(); - perm.setActions("read"); - perm.setName("file.encoding"); - perm.setClass("java.util.PropertyPermission"); - perms.addConfiguredGrant(perm); - - // Revoke permission to write user.home (granted above via user.*), still able to read though. - // and the default granted permission to read os.name. - perm = new Permissions.Permission(); - perm.setActions("write"); - perm.setName("user.home"); - perm.setClass("java.util.PropertyPermission"); - perms.addConfiguredRevoke(perm); - - perm = new Permissions.Permission(); - perm.setActions("read"); - perm.setName("os.*"); - perm.setClass("java.util.PropertyPermission"); - perms.addConfiguredRevoke(perm); - } - - /** Tests a permission that is granted per default. */ - @Test - public void testDefaultGranted() { - perms.setSecurityManager(); - try { - System.getProperty("line.separator"); - } finally { - perms.restoreSecurityManager(); - } - } - - /** Tests a permission that has been granted later via wildcard. */ - @Test - public void testGranted() { - perms.setSecurityManager(); - try { - String s = System.getProperty("user.name"); - System.setProperty("user.name", s); - } finally { - perms.restoreSecurityManager(); - } - } - - /** Tests a permission that has been granted and revoked later. */ - @Test - 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(); - } - } - - /** Tests a permission that is granted as per default but revoked later via wildcard. */ - @Test - 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(); - } - } - /** Tests a permission that has not been granted or revoked. */ - @Test - 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(); - } - } - - /** Tests an exit condition. */ - @Test - public void testExit() { - perms.setSecurityManager(); - try { - System.out.println("If this is the last line on standard out the testExit f.a.i.l.e.d"); - System.exit(3); - fail("Totaly impossible that this fail is ever executed. Please let me know if it is!"); - } catch (ExitException e) { - if (e.getStatus() != 3) { - fail("Received wrong exit status in Exit Exception."); - } - System.out.println("testExit successful."); - } finally { - perms.restoreSecurityManager(); - } - } - -} +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.tools.ant.types; + +import org.apache.tools.ant.ExitException; +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.fail; + +/** + * JUnit 3 testcases for org.apache.tools.ant.types.Permissions. + * + */ +public class PermissionsTest { + + Permissions perms; + + @Before + public void setUp() { + perms = new Permissions(); + Permissions.Permission perm = new Permissions.Permission(); + // Grant extra permissions to read and write the user.* properties and read to the + // java.home property + perm.setActions("read, write"); + perm.setName("user.*"); + perm.setClass("java.util.PropertyPermission"); + perms.addConfiguredGrant(perm); + + perm = new Permissions.Permission(); + perm.setActions("read"); + perm.setName("java.home"); + perm.setClass("java.util.PropertyPermission"); + perms.addConfiguredGrant(perm); + + perm = new Permissions.Permission(); + perm.setActions("read"); + perm.setName("file.encoding"); + perm.setClass("java.util.PropertyPermission"); + perms.addConfiguredGrant(perm); + + // Revoke permission to write user.home (granted above via user.*), still able to read though. + // and the default granted permission to read os.name. + perm = new Permissions.Permission(); + perm.setActions("write"); + perm.setName("user.home"); + perm.setClass("java.util.PropertyPermission"); + perms.addConfiguredRevoke(perm); + + perm = new Permissions.Permission(); + perm.setActions("read"); + perm.setName("os.*"); + perm.setClass("java.util.PropertyPermission"); + perms.addConfiguredRevoke(perm); + } + + /** Tests a permission that is granted per default. */ + @Test + public void testDefaultGranted() { + perms.setSecurityManager(); + try { + System.getProperty("line.separator"); + } finally { + perms.restoreSecurityManager(); + } + } + + /** Tests a permission that has been granted later via wildcard. */ + @Test + public void testGranted() { + perms.setSecurityManager(); + try { + String s = System.getProperty("user.name"); + System.setProperty("user.name", s); + } finally { + perms.restoreSecurityManager(); + } + } + + /** Tests a permission that has been granted and revoked later. */ + @Test + 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(); + } + } + + /** Tests a permission that is granted as per default but revoked later via wildcard. */ + @Test + 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(); + } + } + /** Tests a permission that has not been granted or revoked. */ + @Test + 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(); + } + } + + /** Tests an exit condition. */ + @Test + public void testExit() { + perms.setSecurityManager(); + try { + System.out.println("If this is the last line on standard out the testExit f.a.i.l.e.d"); + System.exit(3); + fail("Totaly impossible that this fail is ever executed. Please let me know if it is!"); + } catch (ExitException e) { + if (e.getStatus() != 3) { + fail("Received wrong exit status in Exit Exception."); + } + System.out.println("testExit successful."); + } finally { + perms.restoreSecurityManager(); + } + } + +}
http://git-wip-us.apache.org/repos/asf/ant/blob/1ae68097/src/tests/junit/org/apache/tools/ant/types/PolyTest.java ---------------------------------------------------------------------- diff --git a/src/tests/junit/org/apache/tools/ant/types/PolyTest.java b/src/tests/junit/org/apache/tools/ant/types/PolyTest.java index 0d5e2ce..89f6268 100644 --- a/src/tests/junit/org/apache/tools/ant/types/PolyTest.java +++ b/src/tests/junit/org/apache/tools/ant/types/PolyTest.java @@ -1,79 +1,79 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -package org.apache.tools.ant.types; - -import org.apache.tools.ant.AntAssert; -import org.apache.tools.ant.BuildFileRule; -import org.apache.tools.ant.Project; -import org.apache.tools.ant.Task; -import org.junit.Before; -import org.junit.Rule; -import org.junit.Test; - -public class PolyTest { - - @Rule - public BuildFileRule buildRule = new BuildFileRule(); - - @Before - public void setUp() { - buildRule.configureProject("src/etc/testcases/types/poly.xml"); - } - - @Test - public void testFileSet() { - buildRule.executeTarget("fileset"); - AntAssert.assertContains( "types.FileSet", buildRule.getLog()); - } - - @Test - public void testFileSetAntType() { - buildRule.executeTarget("fileset-ant-type"); - AntAssert.assertContains("types.PolyTest$MyFileSet", buildRule.getLog()); - } - - @Test - public void testPath() { - buildRule.executeTarget("path"); - AntAssert.assertContains( "types.Path", buildRule.getLog()); - } - - @Test - public void testPathAntType() { - buildRule.executeTarget("path-ant-type"); - AntAssert.assertContains( "types.PolyTest$MyPath", buildRule.getLog()); - } - - public static class MyFileSet extends FileSet {} - - public static class MyPath extends Path { - public MyPath(Project project) { - super(project); - } - } - - public static class MyTask extends Task { - public void addPath(Path path) { - log("class of path is " + path.getClass()); - } - public void addFileset(FileSet fileset) { - log("class of fileset is " + fileset.getClass()); - } - } -} +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.tools.ant.types; + +import org.apache.tools.ant.AntAssert; +import org.apache.tools.ant.BuildFileRule; +import org.apache.tools.ant.Project; +import org.apache.tools.ant.Task; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; + +public class PolyTest { + + @Rule + public BuildFileRule buildRule = new BuildFileRule(); + + @Before + public void setUp() { + buildRule.configureProject("src/etc/testcases/types/poly.xml"); + } + + @Test + public void testFileSet() { + buildRule.executeTarget("fileset"); + AntAssert.assertContains( "types.FileSet", buildRule.getLog()); + } + + @Test + public void testFileSetAntType() { + buildRule.executeTarget("fileset-ant-type"); + AntAssert.assertContains("types.PolyTest$MyFileSet", buildRule.getLog()); + } + + @Test + public void testPath() { + buildRule.executeTarget("path"); + AntAssert.assertContains( "types.Path", buildRule.getLog()); + } + + @Test + public void testPathAntType() { + buildRule.executeTarget("path-ant-type"); + AntAssert.assertContains( "types.PolyTest$MyPath", buildRule.getLog()); + } + + public static class MyFileSet extends FileSet {} + + public static class MyPath extends Path { + public MyPath(Project project) { + super(project); + } + } + + public static class MyTask extends Task { + public void addPath(Path path) { + log("class of path is " + path.getClass()); + } + public void addFileset(FileSet fileset) { + log("class of fileset is " + fileset.getClass()); + } + } +} http://git-wip-us.apache.org/repos/asf/ant/blob/1ae68097/src/tests/junit/org/apache/tools/ant/types/RedirectorElementTest.java ---------------------------------------------------------------------- diff --git a/src/tests/junit/org/apache/tools/ant/types/RedirectorElementTest.java b/src/tests/junit/org/apache/tools/ant/types/RedirectorElementTest.java index f4c2abe..62c76f0 100644 --- a/src/tests/junit/org/apache/tools/ant/types/RedirectorElementTest.java +++ b/src/tests/junit/org/apache/tools/ant/types/RedirectorElementTest.java @@ -1,86 +1,86 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ -package org.apache.tools.ant.types; - -import org.apache.tools.ant.AntAssert; -import org.apache.tools.ant.BuildException; -import org.apache.tools.ant.BuildFileRule; -import org.apache.tools.ant.Project; -import org.junit.Before; -import org.junit.Rule; -import org.junit.Test; - -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; - -public class RedirectorElementTest { - - @Rule - public BuildFileRule buildRule = new BuildFileRule(); - - @Before - public void setUp() { - buildRule.configureProject("src/etc/testcases/types/redirector.xml", Project.MSG_VERBOSE); - } - - @Test - public void test1() { - buildRule.executeTarget("test1"); - assertTrue((buildRule.getProject().<Object> getReference("test1") - instanceof RedirectorElement)); - } - - @Test - public void test2() { - try { - buildRule.executeTarget("test2"); - fail("You must not specify more than one attribute when using refid"); - } catch (BuildException ex) { - //TODO assert exception message - } - } - - @Test - public void test3() { - try { - buildRule.executeTarget("test3"); - fail("You must not specify nested elements when using refid"); - } catch (BuildException ex) { - //TODO assert exception message - } - } - - @Test - public void test4() { - buildRule.executeTarget("test4"); - } - - @Test - public void testLogInputString() { - buildRule.executeTarget("testLogInputString"); - if (buildRule.getLog().indexOf("testLogInputString can-cat") >=0 ) { - AntAssert.assertContains("Using input string", buildRule.getFullLog()); - } - } - - @Test - public void testRefid() { - buildRule.executeTarget("testRefid"); - } - -} +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ +package org.apache.tools.ant.types; + +import org.apache.tools.ant.AntAssert; +import org.apache.tools.ant.BuildException; +import org.apache.tools.ant.BuildFileRule; +import org.apache.tools.ant.Project; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; + +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +public class RedirectorElementTest { + + @Rule + public BuildFileRule buildRule = new BuildFileRule(); + + @Before + public void setUp() { + buildRule.configureProject("src/etc/testcases/types/redirector.xml", Project.MSG_VERBOSE); + } + + @Test + public void test1() { + buildRule.executeTarget("test1"); + assertTrue((buildRule.getProject().<Object> getReference("test1") + instanceof RedirectorElement)); + } + + @Test + public void test2() { + try { + buildRule.executeTarget("test2"); + fail("You must not specify more than one attribute when using refid"); + } catch (BuildException ex) { + //TODO assert exception message + } + } + + @Test + public void test3() { + try { + buildRule.executeTarget("test3"); + fail("You must not specify nested elements when using refid"); + } catch (BuildException ex) { + //TODO assert exception message + } + } + + @Test + public void test4() { + buildRule.executeTarget("test4"); + } + + @Test + public void testLogInputString() { + buildRule.executeTarget("testLogInputString"); + if (buildRule.getLog().indexOf("testLogInputString can-cat") >=0 ) { + AntAssert.assertContains("Using input string", buildRule.getFullLog()); + } + } + + @Test + public void testRefid() { + buildRule.executeTarget("testRefid"); + } + +} http://git-wip-us.apache.org/repos/asf/ant/blob/1ae68097/src/tests/junit/org/apache/tools/ant/types/ResourceOutputTest.java ---------------------------------------------------------------------- diff --git a/src/tests/junit/org/apache/tools/ant/types/ResourceOutputTest.java b/src/tests/junit/org/apache/tools/ant/types/ResourceOutputTest.java index b66335a..0a48a42 100644 --- a/src/tests/junit/org/apache/tools/ant/types/ResourceOutputTest.java +++ b/src/tests/junit/org/apache/tools/ant/types/ResourceOutputTest.java @@ -1,160 +1,160 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -package org.apache.tools.ant.types; - -import java.io.File; -import java.io.IOException; -import java.net.UnknownServiceException; - -import org.apache.tools.ant.BuildException; -import org.apache.tools.ant.Project; -import org.apache.tools.ant.taskdefs.Zip; -import org.apache.tools.ant.types.resources.ImmutableResourceException; -import org.apache.tools.ant.types.resources.PropertyResource; -import org.apache.tools.ant.types.resources.StringResource; -import org.apache.tools.ant.types.resources.URLResource; -import org.apache.tools.ant.types.resources.ZipResource; -import org.apache.tools.ant.util.FileUtils; -import org.apache.tools.ant.util.ResourceUtils; -import org.junit.Before; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.fail; - -public class ResourceOutputTest { - - private static final FileUtils FILE_UTILS = FileUtils.getFileUtils(); - private static final File basedir = new File(System.getProperty("root"), - "src/etc/testcases/types/resources"); - - private Project project; - - @Before - public void setUp() { - project = new Project(); - project.init(); - project.setUserProperty("basedir" , basedir.getAbsolutePath()); - } - - @Test - public void testresourceoutput() { - try { - testoutputbe(new Resource("foo")); - fail("should have caught UnsupportedOperationException"); - } catch (UnsupportedOperationException e) { - //TODO assert exception message - } - } - - @Test - public void teststringoutput1() { - StringResource r = new StringResource(); - testoutputbe(r); - assertEquals("foo", r.getValue()); - } - - @Test - public void teststringoutput2() throws IOException { - StringResource r = new StringResource("bar"); - try { - testoutput(r); - fail("should have caught ImmutableResourceException"); - } catch (ImmutableResourceException e) { - //TODO assert exception message - } - assertEquals("bar", r.getValue()); - } - - @Test - public void testpropertyoutput1() { - PropertyResource r = new PropertyResource(project, "bar"); - testoutputbe(r); - assertEquals("foo", project.getProperty("bar")); - } - - @Test - public void testpropertyoutput2() throws IOException { - project.setNewProperty("bar", "bar"); - PropertyResource r = new PropertyResource(project, "bar"); - try { - testoutput(r); - fail("should have caught ImmutableResourceException"); - } catch (ImmutableResourceException e) { - //TODO assert exception message - } - assertEquals("bar", project.getProperty("bar")); - } - - @Test - public void testurloutput() throws IOException { - File f = project.resolveFile("testurloutput"); - try { - FILE_UTILS.createNewFile(f); - testoutput(new URLResource(f)); - fail("should have caught UnknownServiceException"); - } catch (UnknownServiceException e) { - //TODO assert exception message - } finally { - if (!f.delete()) { - f.deleteOnExit(); - } - } - } - - @Test - public void testzipentryoutput() { - Zip z = new Zip(); - z.setProject(project); - Zip.WhenEmpty create = new Zip.WhenEmpty(); - create.setValue("create"); - z.setWhenempty(create); - z.setBasedir(basedir); - z.setExcludes("**/*"); - File f = project.resolveFile("foo"); - z.setDestFile(f); - z.execute(); - ZipResource r = new ZipResource(); - r.setZipfile(f); - r.setName("foo"); - try { - testoutputbe(r); - fail("should have caught UnsupportedOperationException"); - } catch (UnsupportedOperationException e) { - //TODO assert exception message - } finally { - if (!f.delete()) { - f.deleteOnExit(); - } - } - } - - private void testoutputbe(Resource dest) { - try { - testoutput(dest); - } catch (IOException e) { - throw new BuildException(e); - } - } - - private void testoutput(Resource dest) throws IOException { - ResourceUtils.copyResource(new StringResource("foo"), dest, null); - } - -} +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.tools.ant.types; + +import java.io.File; +import java.io.IOException; +import java.net.UnknownServiceException; + +import org.apache.tools.ant.BuildException; +import org.apache.tools.ant.Project; +import org.apache.tools.ant.taskdefs.Zip; +import org.apache.tools.ant.types.resources.ImmutableResourceException; +import org.apache.tools.ant.types.resources.PropertyResource; +import org.apache.tools.ant.types.resources.StringResource; +import org.apache.tools.ant.types.resources.URLResource; +import org.apache.tools.ant.types.resources.ZipResource; +import org.apache.tools.ant.util.FileUtils; +import org.apache.tools.ant.util.ResourceUtils; +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; + +public class ResourceOutputTest { + + private static final FileUtils FILE_UTILS = FileUtils.getFileUtils(); + private static final File basedir = new File(System.getProperty("root"), + "src/etc/testcases/types/resources"); + + private Project project; + + @Before + public void setUp() { + project = new Project(); + project.init(); + project.setUserProperty("basedir" , basedir.getAbsolutePath()); + } + + @Test + public void testresourceoutput() { + try { + testoutputbe(new Resource("foo")); + fail("should have caught UnsupportedOperationException"); + } catch (UnsupportedOperationException e) { + //TODO assert exception message + } + } + + @Test + public void teststringoutput1() { + StringResource r = new StringResource(); + testoutputbe(r); + assertEquals("foo", r.getValue()); + } + + @Test + public void teststringoutput2() throws IOException { + StringResource r = new StringResource("bar"); + try { + testoutput(r); + fail("should have caught ImmutableResourceException"); + } catch (ImmutableResourceException e) { + //TODO assert exception message + } + assertEquals("bar", r.getValue()); + } + + @Test + public void testpropertyoutput1() { + PropertyResource r = new PropertyResource(project, "bar"); + testoutputbe(r); + assertEquals("foo", project.getProperty("bar")); + } + + @Test + public void testpropertyoutput2() throws IOException { + project.setNewProperty("bar", "bar"); + PropertyResource r = new PropertyResource(project, "bar"); + try { + testoutput(r); + fail("should have caught ImmutableResourceException"); + } catch (ImmutableResourceException e) { + //TODO assert exception message + } + assertEquals("bar", project.getProperty("bar")); + } + + @Test + public void testurloutput() throws IOException { + File f = project.resolveFile("testurloutput"); + try { + FILE_UTILS.createNewFile(f); + testoutput(new URLResource(f)); + fail("should have caught UnknownServiceException"); + } catch (UnknownServiceException e) { + //TODO assert exception message + } finally { + if (!f.delete()) { + f.deleteOnExit(); + } + } + } + + @Test + public void testzipentryoutput() { + Zip z = new Zip(); + z.setProject(project); + Zip.WhenEmpty create = new Zip.WhenEmpty(); + create.setValue("create"); + z.setWhenempty(create); + z.setBasedir(basedir); + z.setExcludes("**/*"); + File f = project.resolveFile("foo"); + z.setDestFile(f); + z.execute(); + ZipResource r = new ZipResource(); + r.setZipfile(f); + r.setName("foo"); + try { + testoutputbe(r); + fail("should have caught UnsupportedOperationException"); + } catch (UnsupportedOperationException e) { + //TODO assert exception message + } finally { + if (!f.delete()) { + f.deleteOnExit(); + } + } + } + + private void testoutputbe(Resource dest) { + try { + testoutput(dest); + } catch (IOException e) { + throw new BuildException(e); + } + } + + private void testoutput(Resource dest) throws IOException { + ResourceUtils.copyResource(new StringResource("foo"), dest, null); + } + +} http://git-wip-us.apache.org/repos/asf/ant/blob/1ae68097/src/tests/junit/org/apache/tools/ant/types/TarFileSetTest.java ---------------------------------------------------------------------- diff --git a/src/tests/junit/org/apache/tools/ant/types/TarFileSetTest.java b/src/tests/junit/org/apache/tools/ant/types/TarFileSetTest.java index b30168a..3b186f9 100644 --- a/src/tests/junit/org/apache/tools/ant/types/TarFileSetTest.java +++ b/src/tests/junit/org/apache/tools/ant/types/TarFileSetTest.java @@ -1,119 +1,119 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -package org.apache.tools.ant.types; - -import java.io.File; - -import org.apache.tools.ant.BuildException; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; - -/** - * JUnit 3 testcases for org.apache.tools.ant.types.TarFileSet. - * - * <p>This doesn't actually test much, mainly reference handling. - * - */ - -public class TarFileSetTest extends AbstractFileSetTest { - - - protected AbstractFileSet getInstance() { - return new TarFileSet(); - } - - @Test - public final void testAttributes() { - TarFileSet f = (TarFileSet)getInstance(); - //check that dir and src are incompatible - f.setSrc(new File("example.tar")); - try { - f.setDir(new File("examples")); - fail("can add dir to " - + f.getDataTypeName() - + " when a src is already present"); - } catch (BuildException be) { - assertEquals("Cannot set both dir and src attributes",be.getMessage()); - } - f = (TarFileSet)getInstance(); - //check that dir and src are incompatible - f.setDir(new File("examples")); - try { - f.setSrc(new File("example.tar")); - fail("can add src to " - + f.getDataTypeName() - + " when a dir is already present"); - } catch (BuildException be) { - assertEquals("Cannot set both dir and src attributes",be.getMessage()); - } - //check that fullpath and prefix are incompatible - f = (TarFileSet)getInstance(); - f.setSrc(new File("example.tar")); - f.setPrefix("/examples"); - try { - f.setFullpath("/doc/manual/index.html"); - fail("Can add fullpath to " - + f.getDataTypeName() - + " when a prefix is already present"); - } catch (BuildException be) { - assertEquals("Cannot set both fullpath and prefix attributes", be.getMessage()); - } - f = (TarFileSet)getInstance(); - f.setSrc(new File("example.tar")); - f.setFullpath("/doc/manual/index.html"); - try { - f.setPrefix("/examples"); - fail("Can add prefix to " - + f.getDataTypeName() - + " when a fullpath is already present"); - } catch (BuildException be) { - assertEquals("Cannot set both fullpath and prefix attributes", be.getMessage()); - } - // check that reference tarfilesets cannot have specific attributes - f = (TarFileSet)getInstance(); - f.setRefid(new Reference(getProject(), "test")); - try { - f.setSrc(new File("example.tar")); - fail("Can add src to " - + f.getDataTypeName() - + " when a refid is already present"); - } catch (BuildException be) { - assertEquals("You must not specify more than one " - + "attribute when using refid", be.getMessage()); - } - // check that a reference tarfileset gets the same attributes as the original - f = (TarFileSet)getInstance(); - f.setSrc(new File("example.tar")); - f.setPrefix("/examples"); - f.setFileMode("600"); - f.setDirMode("530"); - getProject().addReference("test",f); - TarFileSet zid=(TarFileSet)getInstance(); - zid.setRefid(new Reference(getProject(), "test")); - assertTrue("src attribute copied by copy constructor",zid.getSrc(getProject()).equals(f.getSrc(getProject()))); - assertTrue("prefix attribute copied by copy constructor",f.getPrefix(getProject()).equals(zid.getPrefix(getProject()))); - assertTrue("file mode attribute copied by copy constructor",f.getFileMode(getProject())==zid.getFileMode(getProject())); - assertTrue("dir mode attribute copied by copy constructor",f.getDirMode(getProject())==zid.getDirMode(getProject())); - } - - -} +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.tools.ant.types; + +import java.io.File; + +import org.apache.tools.ant.BuildException; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +/** + * JUnit 3 testcases for org.apache.tools.ant.types.TarFileSet. + * + * <p>This doesn't actually test much, mainly reference handling. + * + */ + +public class TarFileSetTest extends AbstractFileSetTest { + + + protected AbstractFileSet getInstance() { + return new TarFileSet(); + } + + @Test + public final void testAttributes() { + TarFileSet f = (TarFileSet)getInstance(); + //check that dir and src are incompatible + f.setSrc(new File("example.tar")); + try { + f.setDir(new File("examples")); + fail("can add dir to " + + f.getDataTypeName() + + " when a src is already present"); + } catch (BuildException be) { + assertEquals("Cannot set both dir and src attributes",be.getMessage()); + } + f = (TarFileSet)getInstance(); + //check that dir and src are incompatible + f.setDir(new File("examples")); + try { + f.setSrc(new File("example.tar")); + fail("can add src to " + + f.getDataTypeName() + + " when a dir is already present"); + } catch (BuildException be) { + assertEquals("Cannot set both dir and src attributes",be.getMessage()); + } + //check that fullpath and prefix are incompatible + f = (TarFileSet)getInstance(); + f.setSrc(new File("example.tar")); + f.setPrefix("/examples"); + try { + f.setFullpath("/doc/manual/index.html"); + fail("Can add fullpath to " + + f.getDataTypeName() + + " when a prefix is already present"); + } catch (BuildException be) { + assertEquals("Cannot set both fullpath and prefix attributes", be.getMessage()); + } + f = (TarFileSet)getInstance(); + f.setSrc(new File("example.tar")); + f.setFullpath("/doc/manual/index.html"); + try { + f.setPrefix("/examples"); + fail("Can add prefix to " + + f.getDataTypeName() + + " when a fullpath is already present"); + } catch (BuildException be) { + assertEquals("Cannot set both fullpath and prefix attributes", be.getMessage()); + } + // check that reference tarfilesets cannot have specific attributes + f = (TarFileSet)getInstance(); + f.setRefid(new Reference(getProject(), "test")); + try { + f.setSrc(new File("example.tar")); + fail("Can add src to " + + f.getDataTypeName() + + " when a refid is already present"); + } catch (BuildException be) { + assertEquals("You must not specify more than one " + + "attribute when using refid", be.getMessage()); + } + // check that a reference tarfileset gets the same attributes as the original + f = (TarFileSet)getInstance(); + f.setSrc(new File("example.tar")); + f.setPrefix("/examples"); + f.setFileMode("600"); + f.setDirMode("530"); + getProject().addReference("test",f); + TarFileSet zid=(TarFileSet)getInstance(); + zid.setRefid(new Reference(getProject(), "test")); + assertTrue("src attribute copied by copy constructor",zid.getSrc(getProject()).equals(f.getSrc(getProject()))); + assertTrue("prefix attribute copied by copy constructor",f.getPrefix(getProject()).equals(zid.getPrefix(getProject()))); + assertTrue("file mode attribute copied by copy constructor",f.getFileMode(getProject())==zid.getFileMode(getProject())); + assertTrue("dir mode attribute copied by copy constructor",f.getDirMode(getProject())==zid.getDirMode(getProject())); + } + + +} http://git-wip-us.apache.org/repos/asf/ant/blob/1ae68097/src/tests/junit/org/apache/tools/ant/types/XMLCatalogBuildFileTest.java ---------------------------------------------------------------------- diff --git a/src/tests/junit/org/apache/tools/ant/types/XMLCatalogBuildFileTest.java b/src/tests/junit/org/apache/tools/ant/types/XMLCatalogBuildFileTest.java index 96c0c5d..ce796b1 100644 --- a/src/tests/junit/org/apache/tools/ant/types/XMLCatalogBuildFileTest.java +++ b/src/tests/junit/org/apache/tools/ant/types/XMLCatalogBuildFileTest.java @@ -1,98 +1,98 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -package org.apache.tools.ant.types; - -import org.apache.tools.ant.BuildFileRule; -import org.junit.Rule; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -/** - * testcases for org.apache.tools.ant.types.XMLCatalog - * - * @see org.apache.tools.ant.types.XMLCatalogTest - * - */ -public class XMLCatalogBuildFileTest { - - @Rule - public BuildFileRule buildRule = new BuildFileRule(); - - - - // - // Ensure that an external entity resolves as expected with NO - // XMLCatalog involvement: - // - // Transform an XML file that refers to the entity into a text - // file, stuff result into property: val1 - // - @Test - public void testEntityNoCatalog() { - buildRule.configureProject("src/etc/testcases/types/xmlcatalog.xml"); - buildRule.executeTarget("testentitynocatalog"); - assertEquals("A stitch in time saves nine", buildRule.getProject().getProperty("val1")); - } - - // - // Ensure that an external entity resolves as expected Using an - // XMLCatalog: - // - // Transform an XML file that refers to the entity into a text - // file, entity is listed in the XMLCatalog pointing to a - // different file. Stuff result into property: val2 - // - @Test - public void testEntityWithCatalog() { - buildRule.configureProject("src/etc/testcases/types/xmlcatalog.xml"); - buildRule.executeTarget("testentitywithcatalog"); - assertEquals("No news is good news", buildRule.getProject().getProperty("val2")); - } - - // - // Ensure that an external entity resolves as expected with NO - // XMLCatalog involvement: - // - // Transform an XML file that contains a reference to a _second_ XML file - // via the document() function. The _second_ XML file refers to an entity. - // Stuff result into the property: val3 - // - @Test - public void testDocumentNoCatalog() { - buildRule.configureProject("src/etc/testcases/types/xmlcatalog.xml"); - buildRule.executeTarget("testdocumentnocatalog"); - assertEquals("A stitch in time saves nine", buildRule.getProject().getProperty("val3")); - } - - // - // Ensure that an external entity resolves as expected Using an - // XMLCatalog: - // - // Transform an XML file that contains a reference to a _second_ XML file - // via the document() function. The _second_ XML file refers to an entity. - // The entity is listed in the XMLCatalog pointing to a different file. - // Stuff result into the property: val4 - @Test - public void testDocumentWithCatalog() { - buildRule.configureProject("src/etc/testcases/types/xmlcatalog.xml"); - buildRule.executeTarget("testdocumentwithcatalog"); - assertEquals("No news is good news", buildRule.getProject().getProperty("val4")); - } -} +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.tools.ant.types; + +import org.apache.tools.ant.BuildFileRule; +import org.junit.Rule; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +/** + * testcases for org.apache.tools.ant.types.XMLCatalog + * + * @see org.apache.tools.ant.types.XMLCatalogTest + * + */ +public class XMLCatalogBuildFileTest { + + @Rule + public BuildFileRule buildRule = new BuildFileRule(); + + + + // + // Ensure that an external entity resolves as expected with NO + // XMLCatalog involvement: + // + // Transform an XML file that refers to the entity into a text + // file, stuff result into property: val1 + // + @Test + public void testEntityNoCatalog() { + buildRule.configureProject("src/etc/testcases/types/xmlcatalog.xml"); + buildRule.executeTarget("testentitynocatalog"); + assertEquals("A stitch in time saves nine", buildRule.getProject().getProperty("val1")); + } + + // + // Ensure that an external entity resolves as expected Using an + // XMLCatalog: + // + // Transform an XML file that refers to the entity into a text + // file, entity is listed in the XMLCatalog pointing to a + // different file. Stuff result into property: val2 + // + @Test + public void testEntityWithCatalog() { + buildRule.configureProject("src/etc/testcases/types/xmlcatalog.xml"); + buildRule.executeTarget("testentitywithcatalog"); + assertEquals("No news is good news", buildRule.getProject().getProperty("val2")); + } + + // + // Ensure that an external entity resolves as expected with NO + // XMLCatalog involvement: + // + // Transform an XML file that contains a reference to a _second_ XML file + // via the document() function. The _second_ XML file refers to an entity. + // Stuff result into the property: val3 + // + @Test + public void testDocumentNoCatalog() { + buildRule.configureProject("src/etc/testcases/types/xmlcatalog.xml"); + buildRule.executeTarget("testdocumentnocatalog"); + assertEquals("A stitch in time saves nine", buildRule.getProject().getProperty("val3")); + } + + // + // Ensure that an external entity resolves as expected Using an + // XMLCatalog: + // + // Transform an XML file that contains a reference to a _second_ XML file + // via the document() function. The _second_ XML file refers to an entity. + // The entity is listed in the XMLCatalog pointing to a different file. + // Stuff result into the property: val4 + @Test + public void testDocumentWithCatalog() { + buildRule.configureProject("src/etc/testcases/types/xmlcatalog.xml"); + buildRule.executeTarget("testdocumentwithcatalog"); + assertEquals("No news is good news", buildRule.getProject().getProperty("val4")); + } +} http://git-wip-us.apache.org/repos/asf/ant/blob/1ae68097/src/tests/junit/org/apache/tools/ant/types/XMLCatalogTest.java ---------------------------------------------------------------------- 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 c1adc30..ca06ec3 100644 --- a/src/tests/junit/org/apache/tools/ant/types/XMLCatalogTest.java +++ b/src/tests/junit/org/apache/tools/ant/types/XMLCatalogTest.java @@ -1,392 +1,392 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -package org.apache.tools.ant.types; - -import java.io.File; -import java.io.IOException; -import java.net.MalformedURLException; -import java.net.URL; - -import javax.xml.transform.Source; -import javax.xml.transform.TransformerException; -import javax.xml.transform.sax.SAXSource; - -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.Test; -import org.xml.sax.InputSource; -import org.xml.sax.SAXException; - -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; - -/** - * JUnit testcases for org.apache.tools.ant.types.XMLCatalog - * - */ -public class XMLCatalogTest { - - private Project project; - private XMLCatalog catalog; - - private XMLCatalog newCatalog() { - XMLCatalog cat = new XMLCatalog(); - cat.setProject(project); - return cat; - } - - private static String toURLString(File file) throws MalformedURLException { - return JAXPUtils.getSystemId(file); - } - - @Before - public void setUp() { - project = new Project(); - project.setBasedir(System.getProperty("root")); - - // This causes XMLCatalog to print out detailed logging - // messages for debugging - // - // DefaultLogger logger = new DefaultLogger(); - // logger.setMessageOutputLevel(Project.MSG_DEBUG); - // logger.setOutputPrintStream(System.out); - // logger.setErrorPrintStream(System.err); - // project.addBuildListener(logger); - - catalog = newCatalog(); - } - - @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()); - } - - 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()); - } - } - - private static String fileURLPartWithoutLeadingSlashes(SAXSource result) - throws MalformedURLException { - // - // These shenanigans are necessary b/c Norm Walsh's resolver - // has a different idea of how file URLs are created on windoze - // ie file://c:/foo instead of file:///c:/foo - // - String resultStr = - new URL(result.getInputSource().getSystemId()).getFile(); - // on Sun's Java6 this returns an unexpected number of four - // leading slashes, at least on Linux - strip all of them - while (resultStr.startsWith("/")) { - resultStr = resultStr.substring(1); - } - return resultStr; - } - - @Test - public void testNonExistentEntry() throws IOException, SAXException, TransformerException { - - ResourceLocation dtd = new ResourceLocation(); - dtd.setPublicId("PUBLIC ID ONE"); - dtd.setLocation("i/dont/exist.dtd"); - - InputSource isResult = catalog.resolveEntity("PUBLIC ID ONE", - "i/dont/exist.dtd"); - assertNull("Nonexistent Catalog entry should not be returned", isResult); - - Source result = catalog.resolve("i/dont/exist.dtd", null); - 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 " - + expected + " but was " + resultStr, - expected.endsWith(resultStr)); - } - - @Test - public void testEmptyElementIfIsReference() { - ResourceLocation dtd = new ResourceLocation(); - dtd.setPublicId("PUBLIC ID ONE"); - dtd.setLocation("i/dont/exist.dtd"); - catalog.addDTD(dtd); - project.addReference("catalog", catalog); - - 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 testCircularReferenceCheck() throws IOException, SAXException { - - // catalog <--> catalog - project.addReference("catalog", catalog); - catalog.setRefid(new Reference(project, "catalog")); - - try { - 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()); - } - - // 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()); - } - } - // inspired by Bugzilla Report 23913 - // a problem used to happen under Windows when the location of the DTD was given as an absolute path - // possibly with a mixture of file separators - @Test - public void testAbsolutePath() throws IOException, SAXException { - 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"; - 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()); - - - } - - @Test - public void testSimpleEntry() throws IOException, SAXException { - - ResourceLocation dtd = new ResourceLocation(); - dtd.setPublicId("-//stevo//DTD doc 1.0//EN"); - 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()); - - } - - @Test - public void testEntryReference() throws IOException, SAXException, TransformerException { - - String publicId = "-//stevo//DTD doc 1.0//EN"; - String sysid = "src/etc/testcases/taskdefs/optional/xml/doc.dtd"; - - // catalog2 --> catalog1 --> 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"; - - ResourceLocation entity = new ResourceLocation(); - 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); - - catalog1.setRefid(new Reference(project, "catalog")); - catalog2.setRefid(new Reference(project, "catalog1")); - - InputSource isResult = catalog2.resolveEntity(publicId, - "nap:chemical+brothers"); - - assertNotNull(isResult); - assertEquals(toURLString(dtdFile), - isResult.getSystemId()); - - - Source result = catalog.resolve(uri, null); - assertNotNull(result); - assertEquals(toURLString(xmlFile), - result.getSystemId()); - } - - @Test - public void testNestedCatalog() throws IOException, SAXException, TransformerException { - - String publicId = "-//stevo//DTD doc 1.0//EN"; - String dtdLoc = "src/etc/testcases/taskdefs/optional/xml/doc.dtd"; - - ResourceLocation dtd = new ResourceLocation(); - 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"; - - ResourceLocation entity = new ResourceLocation(); - 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"); - assertNotNull(isResult); - assertEquals(toURLString(dtdFile), - isResult.getSystemId()); - - Source result = catalog.resolve(uri, null); - assertNotNull(result); - assertEquals(toURLString(xmlFile), - result.getSystemId()); - - } - - @Test - public void testResolverBase() throws MalformedURLException, TransformerException { - - String uri = "http://foo.com/bar/blah.xml"; - String uriLoc = "etc/testcases/taskdefs/optional/xml/about.xml"; - String base = toURLString(project.getBaseDir()) + "/src/"; - - ResourceLocation entity = new ResourceLocation(); - entity.setPublicId(uri); - entity.setLocation(uriLoc); - catalog.addEntity(entity); - File xmlFile = project.resolveFile("src/" + uriLoc); - - Source result = catalog.resolve(uri, base); - assertNotNull(result); - assertEquals(toURLString(xmlFile), - result.getSystemId()); - - } - - @Test - public void testClasspath() throws IOException, TransformerException, SAXException { - - - String publicId = "-//stevo//DTD doc 1.0//EN"; - String dtdLoc = "testcases/taskdefs/optional/xml/doc.dtd"; - String path1 = project.getBaseDir().toString() + "/src/etc"; - - ResourceLocation dtd = new ResourceLocation(); - 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"; - String path2 = project.getBaseDir().toString() + "/src"; - - ResourceLocation entity = new ResourceLocation(); - 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)); - catalog.setClasspath(aPath); - - InputSource isResult = catalog.resolveEntity(publicId, - "nap:chemical+brothers"); - assertNotNull(isResult); - String resultStr1 = new URL(isResult.getSystemId()).getFile(); - assertTrue(toURLString(dtdFile).endsWith(resultStr1)); - - Source result = catalog.resolve(uri, null); - assertNotNull(result); - String resultStr = new URL(result.getSystemId()).getFile(); - assertTrue(toURLString(xmlFile).endsWith(resultStr)); - - } -} +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.tools.ant.types; + +import java.io.File; +import java.io.IOException; +import java.net.MalformedURLException; +import java.net.URL; + +import javax.xml.transform.Source; +import javax.xml.transform.TransformerException; +import javax.xml.transform.sax.SAXSource; + +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.Test; +import org.xml.sax.InputSource; +import org.xml.sax.SAXException; + +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; + +/** + * JUnit testcases for org.apache.tools.ant.types.XMLCatalog + * + */ +public class XMLCatalogTest { + + private Project project; + private XMLCatalog catalog; + + private XMLCatalog newCatalog() { + XMLCatalog cat = new XMLCatalog(); + cat.setProject(project); + return cat; + } + + private static String toURLString(File file) throws MalformedURLException { + return JAXPUtils.getSystemId(file); + } + + @Before + public void setUp() { + project = new Project(); + project.setBasedir(System.getProperty("root")); + + // This causes XMLCatalog to print out detailed logging + // messages for debugging + // + // DefaultLogger logger = new DefaultLogger(); + // logger.setMessageOutputLevel(Project.MSG_DEBUG); + // logger.setOutputPrintStream(System.out); + // logger.setErrorPrintStream(System.err); + // project.addBuildListener(logger); + + catalog = newCatalog(); + } + + @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()); + } + + 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()); + } + } + + private static String fileURLPartWithoutLeadingSlashes(SAXSource result) + throws MalformedURLException { + // + // These shenanigans are necessary b/c Norm Walsh's resolver + // has a different idea of how file URLs are created on windoze + // ie file://c:/foo instead of file:///c:/foo + // + String resultStr = + new URL(result.getInputSource().getSystemId()).getFile(); + // on Sun's Java6 this returns an unexpected number of four + // leading slashes, at least on Linux - strip all of them + while (resultStr.startsWith("/")) { + resultStr = resultStr.substring(1); + } + return resultStr; + } + + @Test + public void testNonExistentEntry() throws IOException, SAXException, TransformerException { + + ResourceLocation dtd = new ResourceLocation(); + dtd.setPublicId("PUBLIC ID ONE"); + dtd.setLocation("i/dont/exist.dtd"); + + InputSource isResult = catalog.resolveEntity("PUBLIC ID ONE", + "i/dont/exist.dtd"); + assertNull("Nonexistent Catalog entry should not be returned", isResult); + + Source result = catalog.resolve("i/dont/exist.dtd", null); + 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 " + + expected + " but was " + resultStr, + expected.endsWith(resultStr)); + } + + @Test + public void testEmptyElementIfIsReference() { + ResourceLocation dtd = new ResourceLocation(); + dtd.setPublicId("PUBLIC ID ONE"); + dtd.setLocation("i/dont/exist.dtd"); + catalog.addDTD(dtd); + project.addReference("catalog", catalog); + + 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 testCircularReferenceCheck() throws IOException, SAXException { + + // catalog <--> catalog + project.addReference("catalog", catalog); + catalog.setRefid(new Reference(project, "catalog")); + + try { + 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()); + } + + // 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()); + } + } + // inspired by Bugzilla Report 23913 + // a problem used to happen under Windows when the location of the DTD was given as an absolute path + // possibly with a mixture of file separators + @Test + public void testAbsolutePath() throws IOException, SAXException { + 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"; + 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()); + + + } + + @Test + public void testSimpleEntry() throws IOException, SAXException { + + ResourceLocation dtd = new ResourceLocation(); + dtd.setPublicId("-//stevo//DTD doc 1.0//EN"); + 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()); + + } + + @Test + public void testEntryReference() throws IOException, SAXException, TransformerException { + + String publicId = "-//stevo//DTD doc 1.0//EN"; + String sysid = "src/etc/testcases/taskdefs/optional/xml/doc.dtd"; + + // catalog2 --> catalog1 --> 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"; + + ResourceLocation entity = new ResourceLocation(); + 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); + + catalog1.setRefid(new Reference(project, "catalog")); + catalog2.setRefid(new Reference(project, "catalog1")); + + InputSource isResult = catalog2.resolveEntity(publicId, + "nap:chemical+brothers"); + + assertNotNull(isResult); + assertEquals(toURLString(dtdFile), + isResult.getSystemId()); + + + Source result = catalog.resolve(uri, null); + assertNotNull(result); + assertEquals(toURLString(xmlFile), + result.getSystemId()); + } + + @Test + public void testNestedCatalog() throws IOException, SAXException, TransformerException { + + String publicId = "-//stevo//DTD doc 1.0//EN"; + String dtdLoc = "src/etc/testcases/taskdefs/optional/xml/doc.dtd"; + + ResourceLocation dtd = new ResourceLocation(); + 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"; + + ResourceLocation entity = new ResourceLocation(); + 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"); + assertNotNull(isResult); + assertEquals(toURLString(dtdFile), + isResult.getSystemId()); + + Source result = catalog.resolve(uri, null); + assertNotNull(result); + assertEquals(toURLString(xmlFile), + result.getSystemId()); + + } + + @Test + public void testResolverBase() throws MalformedURLException, TransformerException { + + String uri = "http://foo.com/bar/blah.xml"; + String uriLoc = "etc/testcases/taskdefs/optional/xml/about.xml"; + String base = toURLString(project.getBaseDir()) + "/src/"; + + ResourceLocation entity = new ResourceLocation(); + entity.setPublicId(uri); + entity.setLocation(uriLoc); + catalog.addEntity(entity); + File xmlFile = project.resolveFile("src/" + uriLoc); + + Source result = catalog.resolve(uri, base); + assertNotNull(result); + assertEquals(toURLString(xmlFile), + result.getSystemId()); + + } + + @Test + public void testClasspath() throws IOException, TransformerException, SAXException { + + + String publicId = "-//stevo//DTD doc 1.0//EN"; + String dtdLoc = "testcases/taskdefs/optional/xml/doc.dtd"; + String path1 = project.getBaseDir().toString() + "/src/etc"; + + ResourceLocation dtd = new ResourceLocation(); + 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"; + String path2 = project.getBaseDir().toString() + "/src"; + + ResourceLocation entity = new ResourceLocation(); + 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)); + catalog.setClasspath(aPath); + + InputSource isResult = catalog.resolveEntity(publicId, + "nap:chemical+brothers"); + assertNotNull(isResult); + String resultStr1 = new URL(isResult.getSystemId()).getFile(); + assertTrue(toURLString(dtdFile).endsWith(resultStr1)); + + Source result = catalog.resolve(uri, null); + assertNotNull(result); + String resultStr = new URL(result.getSystemId()).getFile(); + assertTrue(toURLString(xmlFile).endsWith(resultStr)); + + } +}
