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 java.io.File;
021: import java.io.FileInputStream;
022: import java.io.FileOutputStream;
023: import java.util.Properties;
024:
025: import org.apache.tools.ant.BuildFileTest;
026:
027: /**
028: * JUnit testcase that excercises the optional PropertyFile task in ant.
029: * (this is really more of a functional test so far.., but it's enough to let
030: * me start refactoring...)
031: *
032: *@created October 2, 2001
033: */
034:
035: public class PropertyFileTest extends BuildFileTest {
036:
037: public PropertyFileTest(String name) {
038: super (name);
039: }
040:
041: /**
042: * The JUnit setup method
043: */
044: public void setUp() throws Exception {
045: destroyTempFiles();
046: initTestPropFile();
047: initBuildPropFile();
048: configureProject(projectFilePath);
049: project.setProperty(valueDoesNotGetOverwrittenPropertyFileKey,
050: valueDoesNotGetOverwrittenPropertyFile);
051: }
052:
053: /**
054: * The JUnit tearDown method
055: */
056: public void tearDown() {
057: destroyTempFiles();
058: }
059:
060: public void testNonExistingFile() {
061: PropertyFile props = new PropertyFile();
062: props.setProject(getProject());
063: File file = new File("this-file-does-not-exist.properties");
064: props.setFile(file);
065: assertFalse("Properties file exists before test.", file
066: .exists());
067: props.execute();
068: assertTrue("Properties file does not exist after test.", file
069: .exists());
070: file.delete();
071: }
072:
073: /**
074: * A unit test for JUnit- Excercises the propertyfile tasks ability to
075: * update properties that are already defined-
076: */
077: public void testUpdatesExistingProperties() throws Exception {
078: Properties beforeUpdate = getTestProperties();
079: assertEquals(FNAME, beforeUpdate.getProperty(FNAME_KEY));
080: assertEquals(LNAME, beforeUpdate.getProperty(LNAME_KEY));
081: assertEquals(EMAIL, beforeUpdate.getProperty(EMAIL_KEY));
082: assertEquals(null, beforeUpdate.getProperty(PHONE_KEY));
083: assertEquals(null, beforeUpdate.getProperty(AGE_KEY));
084: assertEquals(null, beforeUpdate.getProperty(DATE_KEY));
085:
086: // ask ant to update the properties...
087: executeTarget("update-existing-properties");
088:
089: Properties afterUpdate = getTestProperties();
090: assertEquals(NEW_FNAME, afterUpdate.getProperty(FNAME_KEY));
091: assertEquals(NEW_LNAME, afterUpdate.getProperty(LNAME_KEY));
092: assertEquals(NEW_EMAIL, afterUpdate.getProperty(EMAIL_KEY));
093: assertEquals(NEW_PHONE, afterUpdate.getProperty(PHONE_KEY));
094: assertEquals(NEW_AGE, afterUpdate.getProperty(AGE_KEY));
095: assertEquals(NEW_DATE, afterUpdate.getProperty(DATE_KEY));
096: }
097:
098: public void testExerciseDefaultAndIncrement() throws Exception {
099: executeTarget("exercise");
100: assertEquals("3", project.getProperty("int.with.default"));
101: assertEquals("1", project.getProperty("int.without.default"));
102: assertEquals("-->", project.getProperty("string.with.default"));
103: assertEquals(".", project.getProperty("string.without.default"));
104: assertEquals("2002/01/21 12:18", project
105: .getProperty("ethans.birth"));
106: assertEquals("2003/01/21", project
107: .getProperty("first.birthday"));
108: assertEquals("0124", project.getProperty("olderThanAWeek"));
109: assertEquals("37", project.getProperty("existing.prop"));
110: assertEquals("6", project.getProperty("int.without.value"));
111: }
112:
113: public void testValueDoesNotGetOverwritten() {
114: // this test shows that the bug report 21505 is fixed
115: executeTarget("bugDemo1");
116: executeTarget("bugDemo2");
117: assertEquals("5", project.getProperty("foo"));
118: }
119:
120: /*
121: public void testDirect() throws Exception {
122: PropertyFile pf = new PropertyFile();
123: pf.setProject(project);
124: pf.setFile(new File(System.getProperty("root"), testPropsFilePath));
125: PropertyFile.Entry entry = pf.createEntry();
126:
127: entry.setKey("date");
128: entry.setValue("123");
129: PropertyFile.Entry.Type type = new PropertyFile.Entry.Type();
130: type.setValue("date");
131: entry.setType(type);
132:
133: entry.setPattern("yyyy/MM/dd");
134:
135: PropertyFile.Entry.Operation operation = new PropertyFile.Entry.Operation();
136: operation.setValue("+");
137: pf.execute();
138:
139: Properties props = getTestProperties();
140: assertEquals("yeehaw", props.getProperty("date"));
141: }
142: */
143:
144: private Properties getTestProperties() throws Exception {
145: Properties testProps = new Properties();
146: FileInputStream propsFile = new FileInputStream(new File(System
147: .getProperty("root"), testPropsFilePath));
148: testProps.load(propsFile);
149: propsFile.close();
150: return testProps;
151: }
152:
153: private void initTestPropFile() throws Exception {
154: Properties testProps = new Properties();
155: testProps.put(FNAME_KEY, FNAME);
156: testProps.put(LNAME_KEY, LNAME);
157: testProps.put(EMAIL_KEY, EMAIL);
158: testProps.put("existing.prop", "37");
159:
160: FileOutputStream fos = new FileOutputStream(new File(System
161: .getProperty("root"), testPropsFilePath));
162: testProps.store(fos, "defaults");
163: fos.close();
164: }
165:
166: private void initBuildPropFile() throws Exception {
167: Properties buildProps = new Properties();
168: buildProps.put(testPropertyFileKey, testPropertyFile);
169: buildProps.put(FNAME_KEY, NEW_FNAME);
170: buildProps.put(LNAME_KEY, NEW_LNAME);
171: buildProps.put(EMAIL_KEY, NEW_EMAIL);
172: buildProps.put(PHONE_KEY, NEW_PHONE);
173: buildProps.put(AGE_KEY, NEW_AGE);
174: buildProps.put(DATE_KEY, NEW_DATE);
175:
176: FileOutputStream fos = new FileOutputStream(new File(System
177: .getProperty("root"), buildPropsFilePath));
178: buildProps.store(fos, null);
179: fos.close();
180: }
181:
182: private void destroyTempFiles() {
183: File tempFile = new File(System.getProperty("root"),
184: testPropsFilePath);
185: tempFile.delete();
186: tempFile = null;
187:
188: tempFile = new File(System.getProperty("root"),
189: buildPropsFilePath);
190: tempFile.delete();
191: tempFile = null;
192:
193: tempFile = new File(System.getProperty("root"),
194: valueDoesNotGetOverwrittenPropsFilePath);
195: tempFile.delete();
196: tempFile = null;
197: }
198:
199: private static final String projectFilePath = "src/etc/testcases/taskdefs/optional/propertyfile.xml",
200:
201: testPropertyFile = "propertyfile.test.properties",
202: testPropertyFileKey = "test.propertyfile",
203: testPropsFilePath = "src/etc/testcases/taskdefs/optional/"
204: + testPropertyFile,
205:
206: valueDoesNotGetOverwrittenPropertyFile = "overwrite.test.properties",
207: valueDoesNotGetOverwrittenPropertyFileKey = "overwrite.test.propertyfile",
208: valueDoesNotGetOverwrittenPropsFilePath = "src/etc/testcases/taskdefs/optional/"
209: + valueDoesNotGetOverwrittenPropertyFile,
210:
211: buildPropsFilePath = "src/etc/testcases/taskdefs/optional/propertyfile.build.properties",
212:
213: FNAME = "Bruce",
214: NEW_FNAME = "Clark",
215: FNAME_KEY = "firstname",
216:
217: LNAME = "Banner",
218: NEW_LNAME = "Kent",
219: LNAME_KEY = "lastname",
220:
221: EMAIL = "incredible@hulk.com",
222: NEW_EMAIL = "kc@superman.com", EMAIL_KEY = "email",
223:
224: NEW_PHONE = "(520) 555-1212", PHONE_KEY = "phone",
225:
226: NEW_AGE = "30", AGE_KEY = "age",
227:
228: NEW_DATE = "2001/01/01 12:45", DATE_KEY = "date";
229: }
|