KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > taskdefs > optional > ReplaceRegExpTest


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */

17 package org.apache.tools.ant.taskdefs.optional;
18
19 import org.apache.tools.ant.BuildFileTest;
20 import org.apache.tools.ant.util.FileUtils;
21
22 import java.util.Properties JavaDoc;
23 import java.io.File JavaDoc;
24 import java.io.FileInputStream JavaDoc;
25 import java.io.IOException JavaDoc;
26
27 /**
28  * JUnit Testcase for the optional replaceregexp task.
29  *
30  */

31 public class ReplaceRegExpTest extends BuildFileTest {
32     private static final String JavaDoc PROJECT_PATH = "src/etc/testcases/taskdefs/optional";
33     public ReplaceRegExpTest(String JavaDoc name) {
34         super(name);
35     }
36
37     public void setUp() {
38         configureProject(PROJECT_PATH + "/replaceregexp.xml");
39     }
40
41     public void tearDown() {
42         executeTarget("cleanup");
43     }
44
45     public void testReplace() throws IOException JavaDoc {
46         Properties JavaDoc original = new Properties JavaDoc();
47         FileInputStream JavaDoc propsFile = null;
48         try {
49             propsFile = new FileInputStream JavaDoc(PROJECT_PATH + "/replaceregexp.properties");
50             original.load(propsFile);
51         } finally {
52             if (propsFile != null) {
53                 propsFile.close();
54                 propsFile = null;
55             }
56         }
57
58         assertEquals("Def", original.get("OldAbc"));
59
60         executeTarget("testReplace");
61
62         Properties JavaDoc after = new Properties JavaDoc();
63         try {
64             propsFile = new FileInputStream JavaDoc(PROJECT_PATH + "/test.properties");
65             after.load(propsFile);
66         } finally {
67             if (propsFile != null) {
68                 propsFile.close();
69                 propsFile = null;
70             }
71         }
72
73         assertNull(after.get("OldAbc"));
74         assertEquals("AbcDef", after.get("NewProp"));
75     }
76     // inspired by bug 22541
77
public void testDirectoryDateDoesNotChange() {
78         executeTarget("touchDirectory");
79         File JavaDoc myFile = new File JavaDoc(PROJECT_PATH + "/" + getProject().getProperty("tmpregexp"));
80         long timeStampBefore = myFile.lastModified();
81         executeTarget("testDirectoryDateDoesNotChange");
82         long timeStampAfter = myFile.lastModified();
83         assertEquals("directory date should not change",
84             timeStampBefore, timeStampAfter);
85     }
86     public void testDontAddNewline1() throws IOException JavaDoc {
87         executeTarget("testDontAddNewline1");
88         assertTrue("Files match",
89                    FileUtils.newFileUtils()
90                    .contentEquals(new File JavaDoc(PROJECT_PATH + "/test.properties"),
91                                   new File JavaDoc(PROJECT_PATH + "/replaceregexp2.result.properties")));
92     }
93
94     public void testDontAddNewline2() throws IOException JavaDoc {
95         executeTarget("testDontAddNewline2");
96         assertTrue("Files match",
97                    FileUtils.newFileUtils()
98                    .contentEquals(new File JavaDoc(PROJECT_PATH + "/test.properties"),
99                                   new File JavaDoc(PROJECT_PATH + "/replaceregexp2.result.properties")));
100     }
101
102 }// ReplaceRegExpTest
103
Popular Tags