001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018: package org.apache.tools.ant.taskdefs.optional;
019:
020: import org.apache.tools.ant.BuildFileTest;
021: import org.apache.tools.ant.util.FileUtils;
022:
023: import java.util.Properties;
024: import java.io.File;
025: import java.io.FileInputStream;
026: import java.io.IOException;
027:
028: /**
029: * JUnit Testcase for the optional replaceregexp task.
030: *
031: */
032: public class ReplaceRegExpTest extends BuildFileTest {
033: private static final String PROJECT_PATH = "src/etc/testcases/taskdefs/optional";
034: private static final FileUtils FILE_UTILS = FileUtils
035: .getFileUtils();
036:
037: public ReplaceRegExpTest(String name) {
038: super (name);
039: }
040:
041: public void setUp() {
042: configureProject(PROJECT_PATH + "/replaceregexp.xml");
043: }
044:
045: public void tearDown() {
046: executeTarget("cleanup");
047: }
048:
049: public void testReplace() throws IOException {
050: Properties original = new Properties();
051: FileInputStream propsFile = null;
052: try {
053: propsFile = new FileInputStream(new File(System
054: .getProperty("root"), PROJECT_PATH
055: + "/replaceregexp.properties"));
056: original.load(propsFile);
057: } finally {
058: if (propsFile != null) {
059: propsFile.close();
060: propsFile = null;
061: }
062: }
063:
064: assertEquals("Def", original.get("OldAbc"));
065:
066: executeTarget("testReplace");
067:
068: Properties after = new Properties();
069: try {
070: propsFile = new FileInputStream(new File(System
071: .getProperty("root"), PROJECT_PATH
072: + "/test.properties"));
073: after.load(propsFile);
074: } finally {
075: if (propsFile != null) {
076: propsFile.close();
077: propsFile = null;
078: }
079: }
080:
081: assertNull(after.get("OldAbc"));
082: assertEquals("AbcDef", after.get("NewProp"));
083: }
084:
085: // inspired by bug 22541
086: public void testDirectoryDateDoesNotChange() {
087: executeTarget("touchDirectory");
088: File myFile = new File(System.getProperty("root"), PROJECT_PATH
089: + "/" + getProject().getProperty("tmpregexp"));
090: long timeStampBefore = myFile.lastModified();
091: executeTarget("testDirectoryDateDoesNotChange");
092: long timeStampAfter = myFile.lastModified();
093: assertEquals("directory date should not change",
094: timeStampBefore, timeStampAfter);
095: }
096:
097: public void testDontAddNewline1() throws IOException {
098: executeTarget("testDontAddNewline1");
099: assertTrue("Files match", FILE_UTILS.contentEquals(new File(
100: System.getProperty("root"), PROJECT_PATH
101: + "/test.properties"), new File(System
102: .getProperty("root"), PROJECT_PATH
103: + "/replaceregexp2.result.properties")));
104: }
105:
106: public void testDontAddNewline2() throws IOException {
107: executeTarget("testDontAddNewline2");
108: assertTrue("Files match", FILE_UTILS.contentEquals(new File(
109: System.getProperty("root"), PROJECT_PATH
110: + "/test.properties"), new File(System
111: .getProperty("root"), PROJECT_PATH
112: + "/replaceregexp2.result.properties")));
113: }
114:
115: }// ReplaceRegExpTest
|