http://git-wip-us.apache.org/repos/asf/ant/blob/1ae68097/src/tests/junit/org/apache/tools/ant/util/JavaEnvUtilsTest.java ---------------------------------------------------------------------- diff --git a/src/tests/junit/org/apache/tools/ant/util/JavaEnvUtilsTest.java b/src/tests/junit/org/apache/tools/ant/util/JavaEnvUtilsTest.java index 8bcb1f2..14387e7 100644 --- a/src/tests/junit/org/apache/tools/ant/util/JavaEnvUtilsTest.java +++ b/src/tests/junit/org/apache/tools/ant/util/JavaEnvUtilsTest.java @@ -1,144 +1,144 @@ -/* - * 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.util; - -import java.io.File; - -import junit.framework.AssertionFailedError; - -import org.apache.tools.ant.taskdefs.condition.Os; -import org.junit.Assume; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; - -/** - * TestCase for JavaEnvUtils. - * - */ -public class JavaEnvUtilsTest { - - private static final FileUtils FILE_UTILS = FileUtils.getFileUtils(); - - - @Test - public void testGetExecutableNetware() { - Assume.assumeTrue("Test only runs on netware", Os.isName("netware")); - assertEquals("java", JavaEnvUtils.getJreExecutable("java")); - assertEquals("javac", JavaEnvUtils.getJdkExecutable("javac")); - assertEquals("foo", JavaEnvUtils.getJreExecutable("foo")); - assertEquals("foo", JavaEnvUtils.getJdkExecutable("foo")); - } - - @Test - public void testGetExecutableWindows() { - Assume.assumeTrue("Test only runs on windows", Os.isFamily("windows")); - String javaHome = - FILE_UTILS.normalize(System.getProperty("java.home")) - .getAbsolutePath(); - - String j = JavaEnvUtils.getJreExecutable("java"); - assertTrue(j.endsWith(".exe")); - assertTrue(j+" is absolute", (new File(j)).isAbsolute()); - try { - assertTrue(j+" is normalized and in the JRE dir", - j.startsWith(javaHome)); - } catch (AssertionFailedError e) { - // java.home is bogus - assertEquals("java.exe", j); - } - - j = JavaEnvUtils.getJdkExecutable("javac"); - assertTrue(j.endsWith(".exe")); - try { - assertTrue(j+" is absolute", (new File(j)).isAbsolute()); - String javaHomeParent = - FILE_UTILS.normalize(javaHome+"/..").getAbsolutePath(); - assertTrue(j+" is normalized and in the JDK dir", - j.startsWith(javaHomeParent)); - assertTrue(j+" is normalized and not in the JRE dir", - !j.startsWith(javaHome)); - - } catch (AssertionFailedError e) { - // java.home is bogus - assertEquals("javac.exe", j); - } - - assertEquals("foo.exe", JavaEnvUtils.getJreExecutable("foo")); - assertEquals("foo.exe", JavaEnvUtils.getJdkExecutable("foo")); - } - - @Test - public void testGetExecutableMostPlatforms() { - Assume.assumeTrue("Test only runs on non Netware and non Windows systems", - !Os.isName("netware") && !Os.isFamily("windows")); - String javaHome = - FILE_UTILS.normalize(System.getProperty("java.home")) - .getAbsolutePath(); - - // could still be OS/2 - String extension = Os.isFamily("dos") ? ".exe" : ""; - - String j = JavaEnvUtils.getJreExecutable("java"); - if (!extension.equals("")) { - assertTrue(j.endsWith(extension)); - } - assertTrue(j+" is absolute", (new File(j)).isAbsolute()); - assertTrue(j+" is normalized and in the JRE dir", - j.startsWith(javaHome)); - - j = JavaEnvUtils.getJdkExecutable("javac"); - if (!extension.equals("")) { - assertTrue(j.endsWith(extension)); - } - assertTrue(j+" is absolute", (new File(j)).isAbsolute()); - - String javaHomeParent = - FILE_UTILS.normalize(javaHome+"/..").getAbsolutePath(); - assertTrue(j+" is normalized and in the JDK dir", - j.startsWith(javaHomeParent)); - - if ((Os.isFamily("mac") && JavaEnvUtils.getJavaVersionNumber() <= JavaEnvUtils.VERSION_1_6) - || JavaEnvUtils.isAtLeastJavaVersion(JavaEnvUtils.JAVA_1_9)) { - assertTrue(j+" is normalized and in the JRE dir", - j.startsWith(javaHome)); - } else { - assertTrue(j+" is normalized and not in the JRE dir", - !j.startsWith(javaHome)); - } - - assertEquals("foo"+extension, - JavaEnvUtils.getJreExecutable("foo")); - assertEquals("foo"+extension, - JavaEnvUtils.getJdkExecutable("foo")); - } - - @Test - public void testIsAtLeastJavaVersion() - { - assertTrue( - "Current java version is not at least the current java version...", - JavaEnvUtils.isAtLeastJavaVersion(JavaEnvUtils.getJavaVersion())); - assertFalse( - "In case the current java version is higher than 9.0 definitely a new algorithem will be needed", - JavaEnvUtils.isAtLeastJavaVersion("9.0")); - } - -} +/* + * 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.util; + +import java.io.File; + +import junit.framework.AssertionFailedError; + +import org.apache.tools.ant.taskdefs.condition.Os; +import org.junit.Assume; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +/** + * TestCase for JavaEnvUtils. + * + */ +public class JavaEnvUtilsTest { + + private static final FileUtils FILE_UTILS = FileUtils.getFileUtils(); + + + @Test + public void testGetExecutableNetware() { + Assume.assumeTrue("Test only runs on netware", Os.isName("netware")); + assertEquals("java", JavaEnvUtils.getJreExecutable("java")); + assertEquals("javac", JavaEnvUtils.getJdkExecutable("javac")); + assertEquals("foo", JavaEnvUtils.getJreExecutable("foo")); + assertEquals("foo", JavaEnvUtils.getJdkExecutable("foo")); + } + + @Test + public void testGetExecutableWindows() { + Assume.assumeTrue("Test only runs on windows", Os.isFamily("windows")); + String javaHome = + FILE_UTILS.normalize(System.getProperty("java.home")) + .getAbsolutePath(); + + String j = JavaEnvUtils.getJreExecutable("java"); + assertTrue(j.endsWith(".exe")); + assertTrue(j+" is absolute", (new File(j)).isAbsolute()); + try { + assertTrue(j+" is normalized and in the JRE dir", + j.startsWith(javaHome)); + } catch (AssertionFailedError e) { + // java.home is bogus + assertEquals("java.exe", j); + } + + j = JavaEnvUtils.getJdkExecutable("javac"); + assertTrue(j.endsWith(".exe")); + try { + assertTrue(j+" is absolute", (new File(j)).isAbsolute()); + String javaHomeParent = + FILE_UTILS.normalize(javaHome+"/..").getAbsolutePath(); + assertTrue(j+" is normalized and in the JDK dir", + j.startsWith(javaHomeParent)); + assertTrue(j+" is normalized and not in the JRE dir", + !j.startsWith(javaHome)); + + } catch (AssertionFailedError e) { + // java.home is bogus + assertEquals("javac.exe", j); + } + + assertEquals("foo.exe", JavaEnvUtils.getJreExecutable("foo")); + assertEquals("foo.exe", JavaEnvUtils.getJdkExecutable("foo")); + } + + @Test + public void testGetExecutableMostPlatforms() { + Assume.assumeTrue("Test only runs on non Netware and non Windows systems", + !Os.isName("netware") && !Os.isFamily("windows")); + String javaHome = + FILE_UTILS.normalize(System.getProperty("java.home")) + .getAbsolutePath(); + + // could still be OS/2 + String extension = Os.isFamily("dos") ? ".exe" : ""; + + String j = JavaEnvUtils.getJreExecutable("java"); + if (!extension.equals("")) { + assertTrue(j.endsWith(extension)); + } + assertTrue(j+" is absolute", (new File(j)).isAbsolute()); + assertTrue(j+" is normalized and in the JRE dir", + j.startsWith(javaHome)); + + j = JavaEnvUtils.getJdkExecutable("javac"); + if (!extension.equals("")) { + assertTrue(j.endsWith(extension)); + } + assertTrue(j+" is absolute", (new File(j)).isAbsolute()); + + String javaHomeParent = + FILE_UTILS.normalize(javaHome+"/..").getAbsolutePath(); + assertTrue(j+" is normalized and in the JDK dir", + j.startsWith(javaHomeParent)); + + if ((Os.isFamily("mac") && JavaEnvUtils.getJavaVersionNumber() <= JavaEnvUtils.VERSION_1_6) + || JavaEnvUtils.isAtLeastJavaVersion(JavaEnvUtils.JAVA_1_9)) { + assertTrue(j+" is normalized and in the JRE dir", + j.startsWith(javaHome)); + } else { + assertTrue(j+" is normalized and not in the JRE dir", + !j.startsWith(javaHome)); + } + + assertEquals("foo"+extension, + JavaEnvUtils.getJreExecutable("foo")); + assertEquals("foo"+extension, + JavaEnvUtils.getJdkExecutable("foo")); + } + + @Test + public void testIsAtLeastJavaVersion() + { + assertTrue( + "Current java version is not at least the current java version...", + JavaEnvUtils.isAtLeastJavaVersion(JavaEnvUtils.getJavaVersion())); + assertFalse( + "In case the current java version is higher than 9.0 definitely a new algorithem will be needed", + JavaEnvUtils.isAtLeastJavaVersion("9.0")); + } + +}
http://git-wip-us.apache.org/repos/asf/ant/blob/1ae68097/src/tests/junit/org/apache/tools/ant/util/LayoutPreservingPropertiesTest.java ---------------------------------------------------------------------- diff --git a/src/tests/junit/org/apache/tools/ant/util/LayoutPreservingPropertiesTest.java b/src/tests/junit/org/apache/tools/ant/util/LayoutPreservingPropertiesTest.java index 89cd3da..b6c3f96 100644 --- a/src/tests/junit/org/apache/tools/ant/util/LayoutPreservingPropertiesTest.java +++ b/src/tests/junit/org/apache/tools/ant/util/LayoutPreservingPropertiesTest.java @@ -1,316 +1,316 @@ -/* - * 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.util; - -import java.io.File; -import java.io.FileInputStream; -import java.io.FileOutputStream; -import java.io.InputStreamReader; -import java.io.IOException; -import java.util.Properties; - -import org.junit.Test; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; - -public class LayoutPreservingPropertiesTest { - - /** - * Tests that a properties file read by the - * LayoutPreservingPropertiesFile and then saves the properties in - * it. - */ - @Test - public void testPreserve() throws Exception { - File simple = new File(System.getProperty("root"), - "src/etc/testcases/util/simple.properties"); - FileInputStream fis = new FileInputStream(simple); - LayoutPreservingProperties lpf = new LayoutPreservingProperties(); - lpf.load(fis); - - File tmp = File.createTempFile("tmp", "props"); - tmp.deleteOnExit(); - lpf.saveAs(tmp); - - // now compare original and tmp for property equivalence - Properties originalProps = new Properties(); - originalProps.load(new FileInputStream(simple)); - - Properties tmpProps = new Properties(); - tmpProps.load(new FileInputStream(tmp)); - - assertEquals("properties corrupted", originalProps, tmpProps); - - // and now make sure that the comments made it into the new file - String s = readFile(tmp); - assertTrue("missing comment", s.indexOf("# a comment") > -1); - assertTrue("missing comment", s.indexOf("! more comment") > -1); - } - - /** - * Tests that names and value are properly escaped when being - * written out. - */ - @Test - public void testEscaping() throws Exception { - LayoutPreservingProperties lpf = new LayoutPreservingProperties(); - - lpf.setProperty(" prop one ", " leading and trailing spaces "); - lpf.setProperty("prop\ttwo", "contains\ttab"); - lpf.setProperty("prop\nthree", "contains\nnewline"); - lpf.setProperty("prop\rfour", "contains\rcarraige return"); - lpf.setProperty("prop\ffive", "contains\fform feed"); - lpf.setProperty("prop\\six", "contains\\backslash"); - lpf.setProperty("prop:seven", "contains:colon"); - lpf.setProperty("prop=eight", "contains=equals"); - lpf.setProperty("prop#nine", "contains#hash"); - lpf.setProperty("prop!ten", "contains!exclamation"); - - File tmp = File.createTempFile("tmp", "props"); - tmp.deleteOnExit(); - lpf.saveAs(tmp); - - // and check that the resulting file looks okay - String s = readFile(tmp); - - assertTrue(s.indexOf("\\ prop\\ one\\ =\\ \\ leading and trailing" - + " spaces ") > -1); - assertTrue(s.indexOf("prop\\ttwo=contains\\ttab") > -1); - assertTrue(s.indexOf("prop\\nthree=contains\\nnewline") > -1); - assertTrue(s.indexOf("prop\\rfour=contains\\rcarraige return") > -1); - assertTrue(s.indexOf("prop\\\\six=contains\\\\backslash") > -1); - assertTrue(s.indexOf("prop\\:seven=contains\\:colon") > -1); - assertTrue(s.indexOf("prop\\=eight=contains\\=equals") > -1); - assertTrue(s.indexOf("prop\\#nine=contains\\#hash") > -1); - assertTrue(s.indexOf("prop\\!ten=contains\\!exclamation") > -1); - } - - /** - * Tests that properties are correctly indexed, so that when we set - * an existing property, it updates the logical line, and it doesn't - * append a new one. - */ - @Test - public void testOverwrite() throws Exception { - File unusual = new File(System.getProperty("root"), - "src/etc/testcases/util/unusual.properties"); - FileInputStream fis = new FileInputStream(unusual); - LayoutPreservingProperties lpf = new LayoutPreservingProperties(); - lpf.load(fis); - - lpf.setProperty(" prop one ", "new one"); - lpf.setProperty("prop\ttwo", "new two"); - lpf.setProperty("prop\nthree", "new three"); - - File tmp = File.createTempFile("tmp", "props"); - tmp.deleteOnExit(); - lpf.saveAs(tmp); - - // and check that the resulting file looks okay - String s = readFile(tmp); - - assertTrue(s.indexOf("\\ prop\\ one\\ =\\ \\ leading and" - + " trailing spaces ") == -1); - assertTrue(s.indexOf("\\ prop\\ one\\ =new one") > -1); - assertTrue(s.indexOf("prop\\ttwo=contains\\ttab") == -1); - assertTrue(s.indexOf("prop\\ttwo=new two") > -1); - assertTrue(s.indexOf("prop\\nthree=contains\\nnewline") == -1); - assertTrue(s.indexOf("prop\\nthree=new three") > -1); - } - - @Test - public void testStoreWithHeader() throws Exception { - File simple = new File(System.getProperty("root"), - "src/etc/testcases/util/simple.properties"); - FileInputStream fis = new FileInputStream(simple); - LayoutPreservingProperties lpf = new LayoutPreservingProperties(); - lpf.load(fis); - - File tmp = File.createTempFile("tmp", "props"); - tmp.deleteOnExit(); - FileOutputStream fos = new FileOutputStream(tmp); - lpf.store(fos, "file-header"); - fos.close(); - - // and check that the resulting file looks okay - String s = readFile(tmp); - - assertTrue("should have had header ", s.startsWith("#file-header")); - } - - @Test - public void testClear() throws Exception { - File simple = new File(System.getProperty("root"), - "src/etc/testcases/util/simple.properties"); - FileInputStream fis = new FileInputStream(simple); - LayoutPreservingProperties lpf = new LayoutPreservingProperties(); - lpf.load(fis); - - lpf.clear(); - - File tmp = File.createTempFile("tmp", "props"); - tmp.deleteOnExit(); - lpf.saveAs(tmp); - - // and check that the resulting file looks okay - String s = readFile(tmp); - - assertTrue("should have had no properties ", - s.indexOf("prop.alpha") == -1); - assertTrue("should have had no properties ", - s.indexOf("prop.beta") == -1); - assertTrue("should have had no properties ", - s.indexOf("prop.gamma") == -1); - - assertTrue("should have had no comments", - s.indexOf("# a comment") == -1); - assertTrue("should have had no comments", - s.indexOf("! more comment") == -1); - assertTrue("should have had no comments", - s.indexOf("# now a line wrapping one") == -1); - } - - @Test - public void testRemove() throws Exception { - File simple = new File(System.getProperty("root"), - "src/etc/testcases/util/simple.properties"); - FileInputStream fis = new FileInputStream(simple); - LayoutPreservingProperties lpf = new LayoutPreservingProperties(); - lpf.load(fis); - - lpf.remove("prop.beta"); - - File tmp = File.createTempFile("tmp", "props"); - tmp.deleteOnExit(); - lpf.saveAs(tmp); - - // and check that the resulting file looks okay - String s = readFile(tmp); - - assertTrue("should not have had prop.beta", - s.indexOf("prop.beta") == -1); - assertTrue("should have had prop.beta's comment", - s.indexOf("! more comment") > -1); - } - - @Test - public void testRemoveWithComment() throws Exception { - File simple = new File(System.getProperty("root"), - "src/etc/testcases/util/simple.properties"); - FileInputStream fis = new FileInputStream(simple); - LayoutPreservingProperties lpf = new LayoutPreservingProperties(); - lpf.load(fis); - - lpf.setRemoveComments(true); - - lpf.remove("prop.beta"); - - File tmp = File.createTempFile("tmp", "props"); - tmp.deleteOnExit(); - lpf.saveAs(tmp); - - // and check that the resulting file looks okay - String s = readFile(tmp); - - assertTrue("should not have had prop.beta", - s.indexOf("prop.beta") == -1); - assertTrue("should not have had prop.beta's comment", - s.indexOf("! more comment") == -1); - } - - @Test - public void testClone() throws Exception { - File simple = new File(System.getProperty("root"), - "src/etc/testcases/util/simple.properties"); - FileInputStream fis = new FileInputStream(simple); - LayoutPreservingProperties lpf1 = new LayoutPreservingProperties(); - lpf1.load(fis); - - LayoutPreservingProperties lpf2 = - (LayoutPreservingProperties) lpf1.clone(); - - lpf2.setProperty("prop.new", "a new property"); - lpf2.setProperty("prop.beta", "a new value for beta"); - - assertEquals("size of original is wrong", 3, lpf1.size()); - assertEquals("size of clone is wrong", 4, lpf2.size()); - - File tmp1 = File.createTempFile("tmp", "props"); - tmp1.deleteOnExit(); - lpf1.saveAs(tmp1); - String s1 = readFile(tmp1); - - File tmp2 = File.createTempFile("tmp", "props"); - tmp2.deleteOnExit(); - lpf2.saveAs(tmp2); - String s2 = readFile(tmp2); - - // check original is untouched - assertTrue("should have had 'simple'", s1.indexOf("simple") > -1); - assertTrue("should not have had prop.new", s1.indexOf("prop.new") == -1); - - // check clone has the changes - assertTrue("should have had 'a new value for beta'", - s2.indexOf("a new value for beta") > -1); - assertTrue("should have had prop.new", s2.indexOf("prop.new") > -1); - } - - @Test - public void testPreserveEscapeName() throws Exception { - LayoutPreservingProperties lpf = new LayoutPreservingProperties(); - File unusual = new File(System.getProperty("root"), - "src/etc/testcases/util/unusual.properties"); - FileInputStream fis = new FileInputStream(unusual); - lpf.load(fis); - - lpf.setProperty("prop:seven", "new value for seven"); - lpf.setProperty("prop=eight", "new value for eight"); - lpf.setProperty("prop eleven", "new value for eleven"); - - lpf.setProperty("alpha", "new value for alpha"); - lpf.setProperty("beta", "new value for beta"); - - File tmp = File.createTempFile("tmp", "props"); - tmp.deleteOnExit(); - lpf.saveAs(tmp); - - // and check that the resulting file looks okay - String s = readFile(tmp); - - assertTrue(s.indexOf("prop\\:seven=new value for seven") > -1); - assertTrue(s.indexOf("prop\\=eight=new value for eight") > -1); - assertTrue(s.indexOf("prop\\ eleven=new value for eleven") > -1); - assertTrue(s.indexOf("alpha=new value for alpha") > -1); - assertTrue(s.indexOf("beta=new value for beta") > -1); - - assertTrue(s.indexOf("prop\\:seven=contains\\:colon") == -1); - assertTrue(s.indexOf("prop\\=eight=contains\\=equals") == -1); - assertTrue(s.indexOf("alpha:set with a colon") == -1); - assertTrue(s.indexOf("beta set with a space") == -1); - } - - private static String readFile(File f) throws IOException { - FileInputStream fis = new FileInputStream(f); - InputStreamReader isr = new InputStreamReader(fis); - String s = FileUtils.readFully(isr); - isr.close(); - fis.close(); - return s; - } -} +/* + * 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.util; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.InputStreamReader; +import java.io.IOException; +import java.util.Properties; + +import org.junit.Test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +public class LayoutPreservingPropertiesTest { + + /** + * Tests that a properties file read by the + * LayoutPreservingPropertiesFile and then saves the properties in + * it. + */ + @Test + public void testPreserve() throws Exception { + File simple = new File(System.getProperty("root"), + "src/etc/testcases/util/simple.properties"); + FileInputStream fis = new FileInputStream(simple); + LayoutPreservingProperties lpf = new LayoutPreservingProperties(); + lpf.load(fis); + + File tmp = File.createTempFile("tmp", "props"); + tmp.deleteOnExit(); + lpf.saveAs(tmp); + + // now compare original and tmp for property equivalence + Properties originalProps = new Properties(); + originalProps.load(new FileInputStream(simple)); + + Properties tmpProps = new Properties(); + tmpProps.load(new FileInputStream(tmp)); + + assertEquals("properties corrupted", originalProps, tmpProps); + + // and now make sure that the comments made it into the new file + String s = readFile(tmp); + assertTrue("missing comment", s.indexOf("# a comment") > -1); + assertTrue("missing comment", s.indexOf("! more comment") > -1); + } + + /** + * Tests that names and value are properly escaped when being + * written out. + */ + @Test + public void testEscaping() throws Exception { + LayoutPreservingProperties lpf = new LayoutPreservingProperties(); + + lpf.setProperty(" prop one ", " leading and trailing spaces "); + lpf.setProperty("prop\ttwo", "contains\ttab"); + lpf.setProperty("prop\nthree", "contains\nnewline"); + lpf.setProperty("prop\rfour", "contains\rcarraige return"); + lpf.setProperty("prop\ffive", "contains\fform feed"); + lpf.setProperty("prop\\six", "contains\\backslash"); + lpf.setProperty("prop:seven", "contains:colon"); + lpf.setProperty("prop=eight", "contains=equals"); + lpf.setProperty("prop#nine", "contains#hash"); + lpf.setProperty("prop!ten", "contains!exclamation"); + + File tmp = File.createTempFile("tmp", "props"); + tmp.deleteOnExit(); + lpf.saveAs(tmp); + + // and check that the resulting file looks okay + String s = readFile(tmp); + + assertTrue(s.indexOf("\\ prop\\ one\\ =\\ \\ leading and trailing" + + " spaces ") > -1); + assertTrue(s.indexOf("prop\\ttwo=contains\\ttab") > -1); + assertTrue(s.indexOf("prop\\nthree=contains\\nnewline") > -1); + assertTrue(s.indexOf("prop\\rfour=contains\\rcarraige return") > -1); + assertTrue(s.indexOf("prop\\\\six=contains\\\\backslash") > -1); + assertTrue(s.indexOf("prop\\:seven=contains\\:colon") > -1); + assertTrue(s.indexOf("prop\\=eight=contains\\=equals") > -1); + assertTrue(s.indexOf("prop\\#nine=contains\\#hash") > -1); + assertTrue(s.indexOf("prop\\!ten=contains\\!exclamation") > -1); + } + + /** + * Tests that properties are correctly indexed, so that when we set + * an existing property, it updates the logical line, and it doesn't + * append a new one. + */ + @Test + public void testOverwrite() throws Exception { + File unusual = new File(System.getProperty("root"), + "src/etc/testcases/util/unusual.properties"); + FileInputStream fis = new FileInputStream(unusual); + LayoutPreservingProperties lpf = new LayoutPreservingProperties(); + lpf.load(fis); + + lpf.setProperty(" prop one ", "new one"); + lpf.setProperty("prop\ttwo", "new two"); + lpf.setProperty("prop\nthree", "new three"); + + File tmp = File.createTempFile("tmp", "props"); + tmp.deleteOnExit(); + lpf.saveAs(tmp); + + // and check that the resulting file looks okay + String s = readFile(tmp); + + assertTrue(s.indexOf("\\ prop\\ one\\ =\\ \\ leading and" + + " trailing spaces ") == -1); + assertTrue(s.indexOf("\\ prop\\ one\\ =new one") > -1); + assertTrue(s.indexOf("prop\\ttwo=contains\\ttab") == -1); + assertTrue(s.indexOf("prop\\ttwo=new two") > -1); + assertTrue(s.indexOf("prop\\nthree=contains\\nnewline") == -1); + assertTrue(s.indexOf("prop\\nthree=new three") > -1); + } + + @Test + public void testStoreWithHeader() throws Exception { + File simple = new File(System.getProperty("root"), + "src/etc/testcases/util/simple.properties"); + FileInputStream fis = new FileInputStream(simple); + LayoutPreservingProperties lpf = new LayoutPreservingProperties(); + lpf.load(fis); + + File tmp = File.createTempFile("tmp", "props"); + tmp.deleteOnExit(); + FileOutputStream fos = new FileOutputStream(tmp); + lpf.store(fos, "file-header"); + fos.close(); + + // and check that the resulting file looks okay + String s = readFile(tmp); + + assertTrue("should have had header ", s.startsWith("#file-header")); + } + + @Test + public void testClear() throws Exception { + File simple = new File(System.getProperty("root"), + "src/etc/testcases/util/simple.properties"); + FileInputStream fis = new FileInputStream(simple); + LayoutPreservingProperties lpf = new LayoutPreservingProperties(); + lpf.load(fis); + + lpf.clear(); + + File tmp = File.createTempFile("tmp", "props"); + tmp.deleteOnExit(); + lpf.saveAs(tmp); + + // and check that the resulting file looks okay + String s = readFile(tmp); + + assertTrue("should have had no properties ", + s.indexOf("prop.alpha") == -1); + assertTrue("should have had no properties ", + s.indexOf("prop.beta") == -1); + assertTrue("should have had no properties ", + s.indexOf("prop.gamma") == -1); + + assertTrue("should have had no comments", + s.indexOf("# a comment") == -1); + assertTrue("should have had no comments", + s.indexOf("! more comment") == -1); + assertTrue("should have had no comments", + s.indexOf("# now a line wrapping one") == -1); + } + + @Test + public void testRemove() throws Exception { + File simple = new File(System.getProperty("root"), + "src/etc/testcases/util/simple.properties"); + FileInputStream fis = new FileInputStream(simple); + LayoutPreservingProperties lpf = new LayoutPreservingProperties(); + lpf.load(fis); + + lpf.remove("prop.beta"); + + File tmp = File.createTempFile("tmp", "props"); + tmp.deleteOnExit(); + lpf.saveAs(tmp); + + // and check that the resulting file looks okay + String s = readFile(tmp); + + assertTrue("should not have had prop.beta", + s.indexOf("prop.beta") == -1); + assertTrue("should have had prop.beta's comment", + s.indexOf("! more comment") > -1); + } + + @Test + public void testRemoveWithComment() throws Exception { + File simple = new File(System.getProperty("root"), + "src/etc/testcases/util/simple.properties"); + FileInputStream fis = new FileInputStream(simple); + LayoutPreservingProperties lpf = new LayoutPreservingProperties(); + lpf.load(fis); + + lpf.setRemoveComments(true); + + lpf.remove("prop.beta"); + + File tmp = File.createTempFile("tmp", "props"); + tmp.deleteOnExit(); + lpf.saveAs(tmp); + + // and check that the resulting file looks okay + String s = readFile(tmp); + + assertTrue("should not have had prop.beta", + s.indexOf("prop.beta") == -1); + assertTrue("should not have had prop.beta's comment", + s.indexOf("! more comment") == -1); + } + + @Test + public void testClone() throws Exception { + File simple = new File(System.getProperty("root"), + "src/etc/testcases/util/simple.properties"); + FileInputStream fis = new FileInputStream(simple); + LayoutPreservingProperties lpf1 = new LayoutPreservingProperties(); + lpf1.load(fis); + + LayoutPreservingProperties lpf2 = + (LayoutPreservingProperties) lpf1.clone(); + + lpf2.setProperty("prop.new", "a new property"); + lpf2.setProperty("prop.beta", "a new value for beta"); + + assertEquals("size of original is wrong", 3, lpf1.size()); + assertEquals("size of clone is wrong", 4, lpf2.size()); + + File tmp1 = File.createTempFile("tmp", "props"); + tmp1.deleteOnExit(); + lpf1.saveAs(tmp1); + String s1 = readFile(tmp1); + + File tmp2 = File.createTempFile("tmp", "props"); + tmp2.deleteOnExit(); + lpf2.saveAs(tmp2); + String s2 = readFile(tmp2); + + // check original is untouched + assertTrue("should have had 'simple'", s1.indexOf("simple") > -1); + assertTrue("should not have had prop.new", s1.indexOf("prop.new") == -1); + + // check clone has the changes + assertTrue("should have had 'a new value for beta'", + s2.indexOf("a new value for beta") > -1); + assertTrue("should have had prop.new", s2.indexOf("prop.new") > -1); + } + + @Test + public void testPreserveEscapeName() throws Exception { + LayoutPreservingProperties lpf = new LayoutPreservingProperties(); + File unusual = new File(System.getProperty("root"), + "src/etc/testcases/util/unusual.properties"); + FileInputStream fis = new FileInputStream(unusual); + lpf.load(fis); + + lpf.setProperty("prop:seven", "new value for seven"); + lpf.setProperty("prop=eight", "new value for eight"); + lpf.setProperty("prop eleven", "new value for eleven"); + + lpf.setProperty("alpha", "new value for alpha"); + lpf.setProperty("beta", "new value for beta"); + + File tmp = File.createTempFile("tmp", "props"); + tmp.deleteOnExit(); + lpf.saveAs(tmp); + + // and check that the resulting file looks okay + String s = readFile(tmp); + + assertTrue(s.indexOf("prop\\:seven=new value for seven") > -1); + assertTrue(s.indexOf("prop\\=eight=new value for eight") > -1); + assertTrue(s.indexOf("prop\\ eleven=new value for eleven") > -1); + assertTrue(s.indexOf("alpha=new value for alpha") > -1); + assertTrue(s.indexOf("beta=new value for beta") > -1); + + assertTrue(s.indexOf("prop\\:seven=contains\\:colon") == -1); + assertTrue(s.indexOf("prop\\=eight=contains\\=equals") == -1); + assertTrue(s.indexOf("alpha:set with a colon") == -1); + assertTrue(s.indexOf("beta set with a space") == -1); + } + + private static String readFile(File f) throws IOException { + FileInputStream fis = new FileInputStream(f); + InputStreamReader isr = new InputStreamReader(fis); + String s = FileUtils.readFully(isr); + isr.close(); + fis.close(); + return s; + } +} http://git-wip-us.apache.org/repos/asf/ant/blob/1ae68097/src/tests/junit/org/apache/tools/ant/util/LazyFileOutputStreamTest.java ---------------------------------------------------------------------- diff --git a/src/tests/junit/org/apache/tools/ant/util/LazyFileOutputStreamTest.java b/src/tests/junit/org/apache/tools/ant/util/LazyFileOutputStreamTest.java index 82c3634..d7da277 100644 --- a/src/tests/junit/org/apache/tools/ant/util/LazyFileOutputStreamTest.java +++ b/src/tests/junit/org/apache/tools/ant/util/LazyFileOutputStreamTest.java @@ -1,75 +1,75 @@ -/* - * 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.util; - -import java.io.File; -import java.io.IOException; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; - -import static org.junit.Assert.assertTrue; - -/** - * @since Ant 1.6 - */ -public class LazyFileOutputStreamTest { - private LazyFileOutputStream los; - private final static File f = new File("test.txt"); - - @Before - public void setUp() { - los = new LazyFileOutputStream(f); - } - - @After - public void tearDown() throws IOException { - try { - los.close(); - } finally { - f.delete(); - } - } - - @Test - public void testNoFileWithoutWrite() throws IOException { - los.close(); - assertTrue(f + " has not been written.", !f.exists()); - } - - @Test - public void testOpen() throws IOException { - los.open(); - los.close(); - assertTrue(f + " has been written.", f.exists()); - } - - @Test - public void testSingleByte() throws IOException { - los.write(0); - los.close(); - assertTrue(f + " has been written.", f.exists()); - } - - @Test - public void testByteArray() throws IOException { - los.write(new byte[] {0}); - los.close(); - assertTrue(f + " has been written.", f.exists()); - } -} +/* + * 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.util; + +import java.io.File; +import java.io.IOException; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.assertTrue; + +/** + * @since Ant 1.6 + */ +public class LazyFileOutputStreamTest { + private LazyFileOutputStream los; + private final static File f = new File("test.txt"); + + @Before + public void setUp() { + los = new LazyFileOutputStream(f); + } + + @After + public void tearDown() throws IOException { + try { + los.close(); + } finally { + f.delete(); + } + } + + @Test + public void testNoFileWithoutWrite() throws IOException { + los.close(); + assertTrue(f + " has not been written.", !f.exists()); + } + + @Test + public void testOpen() throws IOException { + los.open(); + los.close(); + assertTrue(f + " has been written.", f.exists()); + } + + @Test + public void testSingleByte() throws IOException { + los.write(0); + los.close(); + assertTrue(f + " has been written.", f.exists()); + } + + @Test + public void testByteArray() throws IOException { + los.write(new byte[] {0}); + los.close(); + assertTrue(f + " has been written.", f.exists()); + } +} http://git-wip-us.apache.org/repos/asf/ant/blob/1ae68097/src/tests/junit/org/apache/tools/ant/util/LineOrientedOutputStreamTest.java ---------------------------------------------------------------------- diff --git a/src/tests/junit/org/apache/tools/ant/util/LineOrientedOutputStreamTest.java b/src/tests/junit/org/apache/tools/ant/util/LineOrientedOutputStreamTest.java index 1fa23e8..0ffd997 100644 --- a/src/tests/junit/org/apache/tools/ant/util/LineOrientedOutputStreamTest.java +++ b/src/tests/junit/org/apache/tools/ant/util/LineOrientedOutputStreamTest.java @@ -1,153 +1,153 @@ -/* - * 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.util; - -import java.io.IOException; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; - - -public class LineOrientedOutputStreamTest { - - private static String LINE = "This is a line"; - private DummyStream stream; - - - @Before - public void setUp() { - stream = new DummyStream(); - } - - @After - public void tearDown() throws IOException { - if (stream != null) { - stream.close(); - } - } - - @Test - public void testLineWithLinefeedArray() throws IOException { - writeByteArray(); - writeAsArray('\n'); - stream.assertInvoked(); - } - - @Test - public void testLineWithLinefeedSingleBytes() throws IOException { - writeSingleBytes(); - stream.write('\n'); - stream.assertInvoked(); - } - - @Test - public void testLineWithCariagereturnArray() throws IOException { - writeByteArray(); - writeAsArray('\r'); - stream.assertInvoked(); - } - - @Test - public void testLineWithCariagereturnSingleBytes() throws IOException { - writeSingleBytes(); - stream.write('\r'); - stream.assertInvoked(); - } - - @Test - public void testLineWithCariagereturnLinefeedArray() throws IOException { - writeByteArray(); - writeAsArray('\r'); - writeAsArray('\n'); - stream.assertInvoked(); - } - - @Test - public void testLineWithCariagereturnLinefeedSingleBytes() throws IOException { - writeSingleBytes(); - stream.write('\r'); - stream.write('\n'); - stream.assertInvoked(); - } - - @Test - public void testFlushArray() throws IOException { - writeByteArray(); - stream.flush(); - stream.assertNotInvoked(); - } - - @Test - public void testFlushSingleBytes() throws IOException { - writeSingleBytes(); - stream.flush(); - stream.assertNotInvoked(); - } - - @Test - public void testCloseArray() throws IOException { - writeByteArray(); - stream.close(); - stream.assertInvoked(); - stream = null; - } - - @Test - public void testCloseSingleBytes() throws IOException { - writeSingleBytes(); - stream.close(); - stream.assertInvoked(); - stream = null; - } - - private void writeByteArray() throws IOException { - stream.write(LINE.getBytes(), 0, LINE.length()); - } - - private void writeSingleBytes() throws IOException { - byte[] b = LINE.getBytes(); - for (int i = 0; i < b.length; i++) { - stream.write(b[i]); - } - } - - private void writeAsArray(char c) throws IOException { - stream.write(new byte[] {(byte) c}, 0, 1); - } - - private class DummyStream extends LineOrientedOutputStream { - private boolean invoked; - protected void processLine(String line) { - assertFalse("Only one line", invoked); - assertEquals(LINE, line); - invoked = true; - } - - private void assertInvoked() { - assertTrue("At least one line", invoked); - } - private void assertNotInvoked() { - assertTrue("No output", !invoked); - } - } -}// LineOrientedOutputStreamTest +/* + * 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.util; + +import java.io.IOException; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + + +public class LineOrientedOutputStreamTest { + + private static String LINE = "This is a line"; + private DummyStream stream; + + + @Before + public void setUp() { + stream = new DummyStream(); + } + + @After + public void tearDown() throws IOException { + if (stream != null) { + stream.close(); + } + } + + @Test + public void testLineWithLinefeedArray() throws IOException { + writeByteArray(); + writeAsArray('\n'); + stream.assertInvoked(); + } + + @Test + public void testLineWithLinefeedSingleBytes() throws IOException { + writeSingleBytes(); + stream.write('\n'); + stream.assertInvoked(); + } + + @Test + public void testLineWithCariagereturnArray() throws IOException { + writeByteArray(); + writeAsArray('\r'); + stream.assertInvoked(); + } + + @Test + public void testLineWithCariagereturnSingleBytes() throws IOException { + writeSingleBytes(); + stream.write('\r'); + stream.assertInvoked(); + } + + @Test + public void testLineWithCariagereturnLinefeedArray() throws IOException { + writeByteArray(); + writeAsArray('\r'); + writeAsArray('\n'); + stream.assertInvoked(); + } + + @Test + public void testLineWithCariagereturnLinefeedSingleBytes() throws IOException { + writeSingleBytes(); + stream.write('\r'); + stream.write('\n'); + stream.assertInvoked(); + } + + @Test + public void testFlushArray() throws IOException { + writeByteArray(); + stream.flush(); + stream.assertNotInvoked(); + } + + @Test + public void testFlushSingleBytes() throws IOException { + writeSingleBytes(); + stream.flush(); + stream.assertNotInvoked(); + } + + @Test + public void testCloseArray() throws IOException { + writeByteArray(); + stream.close(); + stream.assertInvoked(); + stream = null; + } + + @Test + public void testCloseSingleBytes() throws IOException { + writeSingleBytes(); + stream.close(); + stream.assertInvoked(); + stream = null; + } + + private void writeByteArray() throws IOException { + stream.write(LINE.getBytes(), 0, LINE.length()); + } + + private void writeSingleBytes() throws IOException { + byte[] b = LINE.getBytes(); + for (int i = 0; i < b.length; i++) { + stream.write(b[i]); + } + } + + private void writeAsArray(char c) throws IOException { + stream.write(new byte[] {(byte) c}, 0, 1); + } + + private class DummyStream extends LineOrientedOutputStream { + private boolean invoked; + protected void processLine(String line) { + assertFalse("Only one line", invoked); + assertEquals(LINE, line); + invoked = true; + } + + private void assertInvoked() { + assertTrue("At least one line", invoked); + } + private void assertNotInvoked() { + assertTrue("No output", !invoked); + } + } +}// LineOrientedOutputStreamTest http://git-wip-us.apache.org/repos/asf/ant/blob/1ae68097/src/tests/junit/org/apache/tools/ant/util/LinkedHashtableTest.java ---------------------------------------------------------------------- diff --git a/src/tests/junit/org/apache/tools/ant/util/LinkedHashtableTest.java b/src/tests/junit/org/apache/tools/ant/util/LinkedHashtableTest.java index bfd2cda..4b88792 100644 --- a/src/tests/junit/org/apache/tools/ant/util/LinkedHashtableTest.java +++ b/src/tests/junit/org/apache/tools/ant/util/LinkedHashtableTest.java @@ -1,165 +1,165 @@ -/* - * 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.util; - -import java.util.Hashtable; -import java.util.Iterator; -import java.util.Map; - -import org.junit.Test; - -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertSame; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertEquals; - -public class LinkedHashtableTest { - - private static final Object K1 = new Object(); - private static final Object K2 = new Object(); - private static final Object V1 = new Object(); - private static final Object V2 = new Object(); - private Hashtable h = new LinkedHashtable(); - - public void testClear() { - h.put(K1, V1); - h.clear(); - assertTrue(h.isEmpty()); - } - - public void testClone() { - h.put(K1, V1); - Hashtable h2 = (Hashtable) h.clone(); - assertTrue(h2 instanceof LinkedHashtable); - assertTrue(h2.containsKey(K1)); - } - - @Test - public void testContainsAndPut() { - h.put(K1, V1); - assertTrue(h.contains(K1)); - assertTrue(h.containsKey(K1)); - assertTrue(h.containsValue(V1)); - assertFalse(h.containsKey(K2)); - } - - @Test - public void testGet() { - assertNull(h.get(K1)); - h.put(K1, V1); - assertSame(V1, h.get(K1)); - } - - @Test - public void testIsEmpty() { - assertTrue(h.isEmpty()); - h.put(K1, V1); - assertFalse(h.isEmpty()); - } - - @Test - public void testPutReturnValue() { - assertNull(h.put(K1, V1)); - assertSame(V1, h.put(K1, V2)); - } - - @Test - public void testPutAll() { - LinkedHashtable h2 = new LinkedHashtable(); - h.put(K1, V1); - h2.putAll(h); - assertTrue(h2.containsKey(K1)); - } - - @Test - public void testRemove() { - h.put(K1, V1); - assertSame(V1, h.remove(K1)); - assertTrue(h.isEmpty()); - assertNull(h.remove(K1)); - } - - @Test - public void testSize() { - assertEquals(0, h.size()); - h.put(K1, V1); - assertEquals(1, h.size()); - } - - @Test - public void testKeys() { - multiSetup(); - assertKeys(CollectionUtils.asIterator(h.keys())); - } - - @Test - public void testKeySet() { - multiSetup(); - assertKeys(h.keySet().iterator()); - } - - @Test - public void testElements() { - multiSetup(); - assertValues(CollectionUtils.asIterator(h.elements())); - } - - @Test - public void testValues() { - multiSetup(); - assertValues(h.values().iterator()); - } - - @Test - public void testEntrySet() { - multiSetup(); - Iterator i = h.entrySet().iterator(); - assertTrue(i.hasNext()); - Map.Entry e = (Map.Entry) i.next(); - assertSame(K1, e.getKey()); - assertSame(V1, e.getValue()); - assertTrue(i.hasNext()); - e = (Map.Entry) i.next(); - assertSame(K2, e.getKey()); - assertSame(V2, e.getValue()); - assertFalse(i.hasNext()); - } - - private void multiSetup() { - h.put(K1, V1); - h.put(K2, V2); - } - - private static void assertKeys(Iterator i) { - assertTrue(i.hasNext()); - assertSame(K1, i.next()); - assertTrue(i.hasNext()); - assertSame(K2, i.next()); - assertFalse(i.hasNext()); - } - - private static void assertValues(Iterator i) { - assertTrue(i.hasNext()); - assertSame(V1, i.next()); - assertTrue(i.hasNext()); - assertSame(V2, i.next()); - assertFalse(i.hasNext()); - } -} +/* + * 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.util; + +import java.util.Hashtable; +import java.util.Iterator; +import java.util.Map; + +import org.junit.Test; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertSame; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertEquals; + +public class LinkedHashtableTest { + + private static final Object K1 = new Object(); + private static final Object K2 = new Object(); + private static final Object V1 = new Object(); + private static final Object V2 = new Object(); + private Hashtable h = new LinkedHashtable(); + + public void testClear() { + h.put(K1, V1); + h.clear(); + assertTrue(h.isEmpty()); + } + + public void testClone() { + h.put(K1, V1); + Hashtable h2 = (Hashtable) h.clone(); + assertTrue(h2 instanceof LinkedHashtable); + assertTrue(h2.containsKey(K1)); + } + + @Test + public void testContainsAndPut() { + h.put(K1, V1); + assertTrue(h.contains(K1)); + assertTrue(h.containsKey(K1)); + assertTrue(h.containsValue(V1)); + assertFalse(h.containsKey(K2)); + } + + @Test + public void testGet() { + assertNull(h.get(K1)); + h.put(K1, V1); + assertSame(V1, h.get(K1)); + } + + @Test + public void testIsEmpty() { + assertTrue(h.isEmpty()); + h.put(K1, V1); + assertFalse(h.isEmpty()); + } + + @Test + public void testPutReturnValue() { + assertNull(h.put(K1, V1)); + assertSame(V1, h.put(K1, V2)); + } + + @Test + public void testPutAll() { + LinkedHashtable h2 = new LinkedHashtable(); + h.put(K1, V1); + h2.putAll(h); + assertTrue(h2.containsKey(K1)); + } + + @Test + public void testRemove() { + h.put(K1, V1); + assertSame(V1, h.remove(K1)); + assertTrue(h.isEmpty()); + assertNull(h.remove(K1)); + } + + @Test + public void testSize() { + assertEquals(0, h.size()); + h.put(K1, V1); + assertEquals(1, h.size()); + } + + @Test + public void testKeys() { + multiSetup(); + assertKeys(CollectionUtils.asIterator(h.keys())); + } + + @Test + public void testKeySet() { + multiSetup(); + assertKeys(h.keySet().iterator()); + } + + @Test + public void testElements() { + multiSetup(); + assertValues(CollectionUtils.asIterator(h.elements())); + } + + @Test + public void testValues() { + multiSetup(); + assertValues(h.values().iterator()); + } + + @Test + public void testEntrySet() { + multiSetup(); + Iterator i = h.entrySet().iterator(); + assertTrue(i.hasNext()); + Map.Entry e = (Map.Entry) i.next(); + assertSame(K1, e.getKey()); + assertSame(V1, e.getValue()); + assertTrue(i.hasNext()); + e = (Map.Entry) i.next(); + assertSame(K2, e.getKey()); + assertSame(V2, e.getValue()); + assertFalse(i.hasNext()); + } + + private void multiSetup() { + h.put(K1, V1); + h.put(K2, V2); + } + + private static void assertKeys(Iterator i) { + assertTrue(i.hasNext()); + assertSame(K1, i.next()); + assertTrue(i.hasNext()); + assertSame(K2, i.next()); + assertFalse(i.hasNext()); + } + + private static void assertValues(Iterator i) { + assertTrue(i.hasNext()); + assertSame(V1, i.next()); + assertTrue(i.hasNext()); + assertSame(V2, i.next()); + assertFalse(i.hasNext()); + } +} http://git-wip-us.apache.org/repos/asf/ant/blob/1ae68097/src/tests/junit/org/apache/tools/ant/util/LoaderUtilsTest.java ---------------------------------------------------------------------- diff --git a/src/tests/junit/org/apache/tools/ant/util/LoaderUtilsTest.java b/src/tests/junit/org/apache/tools/ant/util/LoaderUtilsTest.java index 9935edd..a4e4735 100644 --- a/src/tests/junit/org/apache/tools/ant/util/LoaderUtilsTest.java +++ b/src/tests/junit/org/apache/tools/ant/util/LoaderUtilsTest.java @@ -1,43 +1,43 @@ -/* - * 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.util; - -import java.io.File; -import org.junit.Test; - -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertEquals; - -/** - * @since Ant 1.6 - */ -public class LoaderUtilsTest { - - @Test - public void testGetXyzSource() { - File f1 = LoaderUtils.getClassSource(LoaderUtils.class); - assertNotNull(f1); - - File f2 = LoaderUtils.getResourceSource(null, - "org/apache/tools/ant/taskdefs/defaults.properties"); - assertNotNull(f2); - - assertEquals(f1.getAbsolutePath(), f2.getAbsolutePath()); - } - -} +/* + * 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.util; + +import java.io.File; +import org.junit.Test; + +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertEquals; + +/** + * @since Ant 1.6 + */ +public class LoaderUtilsTest { + + @Test + public void testGetXyzSource() { + File f1 = LoaderUtils.getClassSource(LoaderUtils.class); + assertNotNull(f1); + + File f2 = LoaderUtils.getResourceSource(null, + "org/apache/tools/ant/taskdefs/defaults.properties"); + assertNotNull(f2); + + assertEquals(f1.getAbsolutePath(), f2.getAbsolutePath()); + } + +} http://git-wip-us.apache.org/repos/asf/ant/blob/1ae68097/src/tests/junit/org/apache/tools/ant/util/PackageNameMapperTest.java ---------------------------------------------------------------------- diff --git a/src/tests/junit/org/apache/tools/ant/util/PackageNameMapperTest.java b/src/tests/junit/org/apache/tools/ant/util/PackageNameMapperTest.java index fdf7393..d02d11e 100644 --- a/src/tests/junit/org/apache/tools/ant/util/PackageNameMapperTest.java +++ b/src/tests/junit/org/apache/tools/ant/util/PackageNameMapperTest.java @@ -1,43 +1,43 @@ -/* - * 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.util; - -import org.junit.Test; - -import java.io.File; - -import static org.junit.Assert.assertEquals; - -public class PackageNameMapperTest { - - @Test - public void testMapping() { - PackageNameMapper mapper = new PackageNameMapper(); - mapper.setFrom("*.java"); - mapper.setTo("TEST-*.xml"); - String file = fixupPath("org/apache/tools/ant/util/PackageNameMapperTest.java"); - String result = mapper.mapFileName(file)[0]; - - assertEquals("TEST-org.apache.tools.ant.util.PackageNameMapperTest.xml", - result); - } - - private String fixupPath(String file) { - return file.replace('/', File.separatorChar); - } -} +/* + * 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.util; + +import org.junit.Test; + +import java.io.File; + +import static org.junit.Assert.assertEquals; + +public class PackageNameMapperTest { + + @Test + public void testMapping() { + PackageNameMapper mapper = new PackageNameMapper(); + mapper.setFrom("*.java"); + mapper.setTo("TEST-*.xml"); + String file = fixupPath("org/apache/tools/ant/util/PackageNameMapperTest.java"); + String result = mapper.mapFileName(file)[0]; + + assertEquals("TEST-org.apache.tools.ant.util.PackageNameMapperTest.xml", + result); + } + + private String fixupPath(String file) { + return file.replace('/', File.separatorChar); + } +} http://git-wip-us.apache.org/repos/asf/ant/blob/1ae68097/src/tests/junit/org/apache/tools/ant/util/ReaderInputStreamTest.java ---------------------------------------------------------------------- diff --git a/src/tests/junit/org/apache/tools/ant/util/ReaderInputStreamTest.java b/src/tests/junit/org/apache/tools/ant/util/ReaderInputStreamTest.java index bd07251..3cfc93ffa 100644 --- a/src/tests/junit/org/apache/tools/ant/util/ReaderInputStreamTest.java +++ b/src/tests/junit/org/apache/tools/ant/util/ReaderInputStreamTest.java @@ -1,142 +1,142 @@ -/* - * 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.util; - -import org.junit.Test; - -import java.io.ByteArrayOutputStream; -import java.io.File; -import java.io.FileInputStream; -import java.io.InputStreamReader; -import java.io.StringReader; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.fail; - -/** - * Test for ReaderInputStream - */ -public class ReaderInputStreamTest { - - @Test - public void testSimple() throws Exception { - compareBytes("abc", "utf-8"); - } - - @Test - public void testSimple16() throws Exception { - compareBytes("a", "utf-16"); - } - - @Test - public void testSimpleAbc16() throws Exception { - // THIS WILL FAIL. - //compareBytes("abc", "utf-16"); - byte[] bytes = new byte[40]; - int pos = 0; - ReaderInputStream r = new ReaderInputStream( - new StringReader("abc"), "utf-16"); - for (int i = 0; true; ++i) { - int res = r.read(); - if (res == -1) { - break; - } - bytes[pos++] = (byte) res; - } - bytes = "abc".getBytes("utf-16"); - // String n = new String(bytes, 0, pos, "utf-16"); - new String(bytes, 0, bytes.length, "utf-16"); - } - - @Test - public void testReadZero() throws Exception { - ReaderInputStream r = new ReaderInputStream( - new StringReader("abc")); - byte[] bytes = new byte[30]; - // First read in zero bytes - r.read(bytes, 0, 0); - // Now read in the string - int readin = r.read(bytes, 0, 10); - // Make sure that the counts are the same - assertEquals("abc".getBytes().length, readin); - } - - @Test - public void testPreample() throws Exception { - byte[] bytes = "".getBytes("utf-16"); - System.out.println("Preample len is " + bytes.length); - } - - @Test - public void testIso88591ToUtf8() throws Exception { - InputStreamReader fin = null; - ReaderInputStream r = null; - FileInputStream utf8 = null; - try { - fin = new InputStreamReader(new FileInputStream(new File(System.getProperty("root"), "src/tests/antunit/taskdefs/exec/input/iso8859-1")), - "ISO8859_1"); - r = new ReaderInputStream(fin, "UTF8"); - - ByteArrayOutputStream actualOS = new ByteArrayOutputStream(); - int b = r.read(); - while (b > -1) { - actualOS.write((byte) b); - b = r.read(); - } - - utf8 = new FileInputStream(new File(System.getProperty("root"), "src/tests/antunit/taskdefs/exec/expected/utf-8")); - ByteArrayOutputStream expectedOS = new ByteArrayOutputStream(); - b = utf8.read(); - while (b > -1) { - expectedOS.write((byte) b); - b = utf8.read(); - } - - byte[] expected = expectedOS.toByteArray(); - byte[] actual = actualOS.toByteArray(); - assertEquals("length", expected.length, actual.length); - for (int i = 0; i < actual.length; i++) { - assertEquals("byte " + i, expected[i], actual[i]); - } - } finally { - FileUtils.close(fin); - FileUtils.close(r); - FileUtils.close(utf8); - } - } - - private void compareBytes(String s, String encoding) throws Exception { - byte[] expected = s.getBytes(encoding); - - ReaderInputStream r = new ReaderInputStream( - new StringReader(s), encoding); - for (int i = 0; i < expected.length; ++i) { - int expect = expected[i] & 0xFF; - int read = r.read(); - if (expect != read) { - fail("Mismatch in ReaderInputStream at index " + i - + " expecting " + expect + " got " + read + " for string " - + s + " with encoding " + encoding); - } - } - if (r.read() != -1) { - fail("Mismatch in ReaderInputStream - EOF not seen for string " - + s + " with encoding " + encoding); - } - } -} +/* + * 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.util; + +import org.junit.Test; + +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.InputStreamReader; +import java.io.StringReader; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; + +/** + * Test for ReaderInputStream + */ +public class ReaderInputStreamTest { + + @Test + public void testSimple() throws Exception { + compareBytes("abc", "utf-8"); + } + + @Test + public void testSimple16() throws Exception { + compareBytes("a", "utf-16"); + } + + @Test + public void testSimpleAbc16() throws Exception { + // THIS WILL FAIL. + //compareBytes("abc", "utf-16"); + byte[] bytes = new byte[40]; + int pos = 0; + ReaderInputStream r = new ReaderInputStream( + new StringReader("abc"), "utf-16"); + for (int i = 0; true; ++i) { + int res = r.read(); + if (res == -1) { + break; + } + bytes[pos++] = (byte) res; + } + bytes = "abc".getBytes("utf-16"); + // String n = new String(bytes, 0, pos, "utf-16"); + new String(bytes, 0, bytes.length, "utf-16"); + } + + @Test + public void testReadZero() throws Exception { + ReaderInputStream r = new ReaderInputStream( + new StringReader("abc")); + byte[] bytes = new byte[30]; + // First read in zero bytes + r.read(bytes, 0, 0); + // Now read in the string + int readin = r.read(bytes, 0, 10); + // Make sure that the counts are the same + assertEquals("abc".getBytes().length, readin); + } + + @Test + public void testPreample() throws Exception { + byte[] bytes = "".getBytes("utf-16"); + System.out.println("Preample len is " + bytes.length); + } + + @Test + public void testIso88591ToUtf8() throws Exception { + InputStreamReader fin = null; + ReaderInputStream r = null; + FileInputStream utf8 = null; + try { + fin = new InputStreamReader(new FileInputStream(new File(System.getProperty("root"), "src/tests/antunit/taskdefs/exec/input/iso8859-1")), + "ISO8859_1"); + r = new ReaderInputStream(fin, "UTF8"); + + ByteArrayOutputStream actualOS = new ByteArrayOutputStream(); + int b = r.read(); + while (b > -1) { + actualOS.write((byte) b); + b = r.read(); + } + + utf8 = new FileInputStream(new File(System.getProperty("root"), "src/tests/antunit/taskdefs/exec/expected/utf-8")); + ByteArrayOutputStream expectedOS = new ByteArrayOutputStream(); + b = utf8.read(); + while (b > -1) { + expectedOS.write((byte) b); + b = utf8.read(); + } + + byte[] expected = expectedOS.toByteArray(); + byte[] actual = actualOS.toByteArray(); + assertEquals("length", expected.length, actual.length); + for (int i = 0; i < actual.length; i++) { + assertEquals("byte " + i, expected[i], actual[i]); + } + } finally { + FileUtils.close(fin); + FileUtils.close(r); + FileUtils.close(utf8); + } + } + + private void compareBytes(String s, String encoding) throws Exception { + byte[] expected = s.getBytes(encoding); + + ReaderInputStream r = new ReaderInputStream( + new StringReader(s), encoding); + for (int i = 0; i < expected.length; ++i) { + int expect = expected[i] & 0xFF; + int read = r.read(); + if (expect != read) { + fail("Mismatch in ReaderInputStream at index " + i + + " expecting " + expect + " got " + read + " for string " + + s + " with encoding " + encoding); + } + } + if (r.read() != -1) { + fail("Mismatch in ReaderInputStream - EOF not seen for string " + + s + " with encoding " + encoding); + } + } +} http://git-wip-us.apache.org/repos/asf/ant/blob/1ae68097/src/tests/junit/org/apache/tools/ant/util/ResourceUtilsTest.java ---------------------------------------------------------------------- diff --git a/src/tests/junit/org/apache/tools/ant/util/ResourceUtilsTest.java b/src/tests/junit/org/apache/tools/ant/util/ResourceUtilsTest.java index 96cc9cb..b27ee34 100644 --- a/src/tests/junit/org/apache/tools/ant/util/ResourceUtilsTest.java +++ b/src/tests/junit/org/apache/tools/ant/util/ResourceUtilsTest.java @@ -1,63 +1,63 @@ -/* - * 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.util; - -import org.apache.tools.ant.Project; -import org.apache.tools.ant.taskdefs.Echo; -import org.apache.tools.ant.types.Resource; -import org.apache.tools.ant.types.ResourceFactory; -import org.junit.Before; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -/** - * Tests for org.apache.tools.ant.util.ResourceUtils. - */ -public class ResourceUtilsTest implements ResourceFactory, FileNameMapper { - - private Echo taskINeedForLogging = new Echo(); - - @Before - public void setUp() { - taskINeedForLogging.setProject(new Project()); - } - - @Test - public void testNoDuplicates() { - Resource r = new Resource("samual vimes", true, 1, false); - Resource[] toNew = - ResourceUtils.selectOutOfDateSources(taskINeedForLogging, - new Resource[] {r}, - this, this); - assertEquals(1, toNew.length); - } - - /* ============ ResourceFactory interface ====================== */ - public Resource getResource(String name) { - return new Resource(name); // implies lastModified == 0 - } - - /* ============ FileNameMapper interface ======================= */ - public void setFrom(String s) {} - public void setTo(String s) {} - public String[] mapFileName(String s) { - return new String[] {"fred colon", "carrot ironfoundersson"}; - } -} +/* + * 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.util; + +import org.apache.tools.ant.Project; +import org.apache.tools.ant.taskdefs.Echo; +import org.apache.tools.ant.types.Resource; +import org.apache.tools.ant.types.ResourceFactory; +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +/** + * Tests for org.apache.tools.ant.util.ResourceUtils. + */ +public class ResourceUtilsTest implements ResourceFactory, FileNameMapper { + + private Echo taskINeedForLogging = new Echo(); + + @Before + public void setUp() { + taskINeedForLogging.setProject(new Project()); + } + + @Test + public void testNoDuplicates() { + Resource r = new Resource("samual vimes", true, 1, false); + Resource[] toNew = + ResourceUtils.selectOutOfDateSources(taskINeedForLogging, + new Resource[] {r}, + this, this); + assertEquals(1, toNew.length); + } + + /* ============ ResourceFactory interface ====================== */ + public Resource getResource(String name) { + return new Resource(name); // implies lastModified == 0 + } + + /* ============ FileNameMapper interface ======================= */ + public void setFrom(String s) {} + public void setTo(String s) {} + public String[] mapFileName(String s) { + return new String[] {"fred colon", "carrot ironfoundersson"}; + } +}
