Browse Source

Make “root” a field, where necessary

master
Gintas Grigelionis 7 years ago
parent
commit
cc88acc4f2
3 changed files with 49 additions and 53 deletions
  1. +4
    -2
      src/tests/junit/org/apache/tools/ant/types/resources/FileResourceTest.java
  2. +20
    -16
      src/tests/junit/org/apache/tools/ant/util/FileUtilsTest.java
  3. +25
    -35
      src/tests/junit/org/apache/tools/ant/util/LayoutPreservingPropertiesTest.java

+ 4
- 2
src/tests/junit/org/apache/tools/ant/types/resources/FileResourceTest.java View File

@@ -18,6 +18,7 @@
package org.apache.tools.ant.types.resources; package org.apache.tools.ant.types.resources;


import java.io.File; import java.io.File;
import java.io.IOException;


import org.apache.tools.ant.Project; import org.apache.tools.ant.Project;
import org.junit.Before; import org.junit.Before;
@@ -34,8 +35,9 @@ public class FileResourceTest {
private File root; private File root;


@Before @Before
public void setUp() {
root = new File(System.getProperty("root"));
public void setUp() throws IOException {
root = (System.getProperty("root") == null) ? new File(".").getCanonicalFile()
: new File(System.getProperty("root"));
} }


@Test @Test


+ 20
- 16
src/tests/junit/org/apache/tools/ant/util/FileUtilsTest.java View File

@@ -50,6 +50,7 @@ public class FileUtilsTest {
public ExpectedException thrown = ExpectedException.none(); public ExpectedException thrown = ExpectedException.none();


private static final FileUtils FILE_UTILS = FileUtils.getFileUtils(); private static final FileUtils FILE_UTILS = FileUtils.getFileUtils();
private static final String projectRoot = System.getProperty("root");
private File removeThis; private File removeThis;
private String root; private String root;


@@ -232,14 +233,17 @@ public class FileUtilsTest {
} }


@Test @Test
public void testNormalizeDosOrNetwareFailures() {
public void testNormalizeSlashDosOrNetware() {
assumeTrue("Not DOS or Netware", Os.isFamily("dos") || Os.isFamily("netware")); assumeTrue("Not DOS or Netware", Os.isFamily("dos") || Os.isFamily("netware"));
thrown.expect(BuildException.class); thrown.expect(BuildException.class);
try {
FILE_UTILS.normalize("/").getPath();
} finally {
FILE_UTILS.normalize("\\").getPath();
}
FILE_UTILS.normalize("/").getPath();
}

@Test
public void testNormalizeBackSlashDosOrNetware() {
assumeTrue("Not DOS or Netware", Os.isFamily("dos") || Os.isFamily("netware"));
thrown.expect(BuildException.class);
FILE_UTILS.normalize("\\").getPath();
} }


@Test @Test
@@ -401,20 +405,20 @@ public class FileUtilsTest {
@Test @Test
public void testContentEquals() throws IOException { public void testContentEquals() throws IOException {
assertTrue("Non existing files", assertTrue("Non existing files",
FILE_UTILS.contentEquals(new File(System.getProperty("root"), "foo"),
new File(System.getProperty("root"), "bar")));
FILE_UTILS.contentEquals(new File(projectRoot, "foo"),
new File(projectRoot, "bar")));
assertFalse("One exists, the other one doesn\'t", assertFalse("One exists, the other one doesn\'t",
FILE_UTILS.contentEquals(new File(System.getProperty("root"), "foo"),
new File(System.getProperty("root"), "build.xml")));
FILE_UTILS.contentEquals(new File(projectRoot, "foo"),
new File(projectRoot, "build.xml")));
assertFalse("Don\'t compare directories", assertFalse("Don\'t compare directories",
FILE_UTILS.contentEquals(new File(System.getProperty("root"), "src"),
new File(System.getProperty("root"), "src")));
FILE_UTILS.contentEquals(new File(projectRoot, "src"),
new File(projectRoot, "src")));
assertTrue("File equals itself", assertTrue("File equals itself",
FILE_UTILS.contentEquals(new File(System.getProperty("root"), "build.xml"),
new File(System.getProperty("root"), "build.xml")));
FILE_UTILS.contentEquals(new File(projectRoot, "build.xml"),
new File(projectRoot, "build.xml")));
assertFalse("Files are different", assertFalse("Files are different",
FILE_UTILS.contentEquals(new File(System.getProperty("root"), "build.xml"),
new File(System.getProperty("root"), "docs.xml")));
FILE_UTILS.contentEquals(new File(projectRoot, "build.xml"),
new File(projectRoot, "docs.xml")));
} }


/** /**


+ 25
- 35
src/tests/junit/org/apache/tools/ant/util/LayoutPreservingPropertiesTest.java View File

@@ -24,6 +24,7 @@ import java.io.IOException;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.util.Properties; import java.util.Properties;


import org.junit.Before;
import org.junit.Test; import org.junit.Test;


import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.containsString;
@@ -34,6 +35,14 @@ import static org.junit.Assert.assertThat;


public class LayoutPreservingPropertiesTest { public class LayoutPreservingPropertiesTest {


private static final String root = System.getProperty("root");

private LayoutPreservingProperties lpf;

@Before
public void setUp() {
lpf = new LayoutPreservingProperties();
}
/** /**
* Tests that a properties file read by the * Tests that a properties file read by the
* LayoutPreservingPropertiesFile and then saves the properties in * LayoutPreservingPropertiesFile and then saves the properties in
@@ -41,10 +50,8 @@ public class LayoutPreservingPropertiesTest {
*/ */
@Test @Test
public void testPreserve() throws Exception { public void testPreserve() throws Exception {
File simple = new File(System.getProperty("root"),
"src/etc/testcases/util/simple.properties");
File simple = new File(root, "src/etc/testcases/util/simple.properties");
FileInputStream fis = new FileInputStream(simple); FileInputStream fis = new FileInputStream(simple);
LayoutPreservingProperties lpf = new LayoutPreservingProperties();
lpf.load(fis); lpf.load(fis);


File tmp = File.createTempFile("tmp", "props"); File tmp = File.createTempFile("tmp", "props");
@@ -72,8 +79,6 @@ public class LayoutPreservingPropertiesTest {
*/ */
@Test @Test
public void testEscaping() throws Exception { public void testEscaping() throws Exception {
LayoutPreservingProperties lpf = new LayoutPreservingProperties();

lpf.setProperty(" prop one ", " leading and trailing spaces "); lpf.setProperty(" prop one ", " leading and trailing spaces ");
lpf.setProperty("prop\ttwo", "contains\ttab"); lpf.setProperty("prop\ttwo", "contains\ttab");
lpf.setProperty("prop\nthree", "contains\nnewline"); lpf.setProperty("prop\nthree", "contains\nnewline");
@@ -110,10 +115,8 @@ public class LayoutPreservingPropertiesTest {
*/ */
@Test @Test
public void testOverwrite() throws Exception { public void testOverwrite() throws Exception {
File unusual = new File(System.getProperty("root"),
"src/etc/testcases/util/unusual.properties");
File unusual = new File(root, "src/etc/testcases/util/unusual.properties");
FileInputStream fis = new FileInputStream(unusual); FileInputStream fis = new FileInputStream(unusual);
LayoutPreservingProperties lpf = new LayoutPreservingProperties();
lpf.load(fis); lpf.load(fis);


lpf.setProperty(" prop one ", "new one"); lpf.setProperty(" prop one ", "new one");
@@ -137,10 +140,8 @@ public class LayoutPreservingPropertiesTest {


@Test @Test
public void testStoreWithHeader() throws Exception { public void testStoreWithHeader() throws Exception {
File simple = new File(System.getProperty("root"),
"src/etc/testcases/util/simple.properties");
File simple = new File(root, "src/etc/testcases/util/simple.properties");
FileInputStream fis = new FileInputStream(simple); FileInputStream fis = new FileInputStream(simple);
LayoutPreservingProperties lpf = new LayoutPreservingProperties();
lpf.load(fis); lpf.load(fis);


File tmp = File.createTempFile("tmp", "props"); File tmp = File.createTempFile("tmp", "props");
@@ -155,10 +156,8 @@ public class LayoutPreservingPropertiesTest {


@Test @Test
public void testClear() throws Exception { public void testClear() throws Exception {
File simple = new File(System.getProperty("root"),
"src/etc/testcases/util/simple.properties");
File simple = new File(root, "src/etc/testcases/util/simple.properties");
FileInputStream fis = new FileInputStream(simple); FileInputStream fis = new FileInputStream(simple);
LayoutPreservingProperties lpf = new LayoutPreservingProperties();
lpf.load(fis); lpf.load(fis);


lpf.clear(); lpf.clear();
@@ -181,10 +180,8 @@ public class LayoutPreservingPropertiesTest {


@Test @Test
public void testRemove() throws Exception { public void testRemove() throws Exception {
File simple = new File(System.getProperty("root"),
"src/etc/testcases/util/simple.properties");
File simple = new File(root, "src/etc/testcases/util/simple.properties");
FileInputStream fis = new FileInputStream(simple); FileInputStream fis = new FileInputStream(simple);
LayoutPreservingProperties lpf = new LayoutPreservingProperties();
lpf.load(fis); lpf.load(fis);


lpf.remove("prop.beta"); lpf.remove("prop.beta");
@@ -202,10 +199,8 @@ public class LayoutPreservingPropertiesTest {


@Test @Test
public void testRemoveWithComment() throws Exception { public void testRemoveWithComment() throws Exception {
File simple = new File(System.getProperty("root"),
"src/etc/testcases/util/simple.properties");
File simple = new File(root, "src/etc/testcases/util/simple.properties");
FileInputStream fis = new FileInputStream(simple); FileInputStream fis = new FileInputStream(simple);
LayoutPreservingProperties lpf = new LayoutPreservingProperties();
lpf.load(fis); lpf.load(fis);


lpf.setRemoveComments(true); lpf.setRemoveComments(true);
@@ -225,29 +220,26 @@ public class LayoutPreservingPropertiesTest {


@Test @Test
public void testClone() throws Exception { public void testClone() throws Exception {
File simple = new File(System.getProperty("root"),
"src/etc/testcases/util/simple.properties");
File simple = new File(root, "src/etc/testcases/util/simple.properties");
FileInputStream fis = new FileInputStream(simple); FileInputStream fis = new FileInputStream(simple);
LayoutPreservingProperties lpf1 = new LayoutPreservingProperties();
lpf1.load(fis);
lpf.load(fis);


LayoutPreservingProperties lpf2 =
(LayoutPreservingProperties) lpf1.clone();
LayoutPreservingProperties lpfClone = (LayoutPreservingProperties) lpf.clone();


lpf2.setProperty("prop.new", "a new property");
lpf2.setProperty("prop.beta", "a new value for beta");
lpfClone.setProperty("prop.new", "a new property");
lpfClone.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());
assertEquals("size of original is wrong", 3, lpf.size());
assertEquals("size of clone is wrong", 4, lpfClone.size());


File tmp1 = File.createTempFile("tmp", "props"); File tmp1 = File.createTempFile("tmp", "props");
tmp1.deleteOnExit(); tmp1.deleteOnExit();
lpf1.saveAs(tmp1);
lpf.saveAs(tmp1);
String s1 = readFile(tmp1); String s1 = readFile(tmp1);


File tmp2 = File.createTempFile("tmp", "props"); File tmp2 = File.createTempFile("tmp", "props");
tmp2.deleteOnExit(); tmp2.deleteOnExit();
lpf2.saveAs(tmp2);
lpfClone.saveAs(tmp2);
String s2 = readFile(tmp2); String s2 = readFile(tmp2);


// check original is untouched // check original is untouched
@@ -261,9 +253,7 @@ public class LayoutPreservingPropertiesTest {


@Test @Test
public void testPreserveEscapeName() throws Exception { public void testPreserveEscapeName() throws Exception {
LayoutPreservingProperties lpf = new LayoutPreservingProperties();
File unusual = new File(System.getProperty("root"),
"src/etc/testcases/util/unusual.properties");
File unusual = new File(root, "src/etc/testcases/util/unusual.properties");
FileInputStream fis = new FileInputStream(unusual); FileInputStream fis = new FileInputStream(unusual);
lpf.load(fis); lpf.load(fis);




Loading…
Cancel
Save