KickJava   Java API By Example, From Geeks To Geeks.

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


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
21 import java.util.Properties JavaDoc;
22 import java.io.FileOutputStream JavaDoc;
23 import java.io.FileInputStream JavaDoc;
24 import java.io.InputStream JavaDoc;
25 import java.io.File JavaDoc;
26
27 /**
28  * JUnit testcase that excercises the optional PropertyFile task in ant.
29  * (this is really more of a functional test so far.., but it's enough to let
30  * me start refactoring...)
31  *
32  *@created October 2, 2001
33  */

34
35 public class PropertyFileTest extends BuildFileTest {
36
37     public PropertyFileTest(String JavaDoc name) {
38         super(name);
39     }
40
41
42     /**
43      * The JUnit setup method
44      */

45     public void setUp() throws Exception JavaDoc {
46         destroyTempFiles();
47         initTestPropFile();
48         initBuildPropFile();
49         configureProject(projectFilePath);
50         project.setProperty(valueDoesNotGetOverwrittenPropertyFileKey,valueDoesNotGetOverwrittenPropertyFile);
51     }
52
53
54     /**
55      * The JUnit tearDown method
56      */

57     public void tearDown() {
58         destroyTempFiles();
59     }
60
61     public void testNonExistingFile() {
62         PropertyFile props = new PropertyFile();
63         props.setProject( getProject() );
64         File JavaDoc file = new File JavaDoc("this-file-does-not-exist.properties");
65         props.setFile(file);
66         assertFalse("Properties file exists before test.", file.exists());
67         props.execute();
68         assertTrue("Properties file does not exist after test.", file.exists());
69         file.delete();
70     }
71
72     /**
73      * A unit test for JUnit- Excercises the propertyfile tasks ability to
74      * update properties that are already defined-
75      */

76     public void testUpdatesExistingProperties() throws Exception JavaDoc {
77         Properties JavaDoc beforeUpdate = getTestProperties();
78         assertEquals(FNAME, beforeUpdate.getProperty(FNAME_KEY));
79         assertEquals(LNAME, beforeUpdate.getProperty(LNAME_KEY));
80         assertEquals(EMAIL, beforeUpdate.getProperty(EMAIL_KEY));
81         assertEquals(null, beforeUpdate.getProperty(PHONE_KEY));
82         assertEquals(null, beforeUpdate.getProperty(AGE_KEY));
83         assertEquals(null, beforeUpdate.getProperty(DATE_KEY));
84
85         // ask ant to update the properties...
86
executeTarget("update-existing-properties");
87
88         Properties JavaDoc afterUpdate = getTestProperties();
89         assertEquals(NEW_FNAME, afterUpdate.getProperty(FNAME_KEY));
90         assertEquals(NEW_LNAME, afterUpdate.getProperty(LNAME_KEY));
91         assertEquals(NEW_EMAIL, afterUpdate.getProperty(EMAIL_KEY));
92         assertEquals(NEW_PHONE, afterUpdate.getProperty(PHONE_KEY));
93         assertEquals(NEW_AGE, afterUpdate.getProperty(AGE_KEY));
94         assertEquals(NEW_DATE, afterUpdate.getProperty(DATE_KEY));
95     }
96
97     public void testExerciseDefaultAndIncrement() throws Exception JavaDoc {
98         executeTarget("exercise");
99         assertEquals("3",project.getProperty("int.with.default"));
100         assertEquals("1",project.getProperty("int.without.default"));
101         assertEquals("-->",project.getProperty("string.with.default"));
102         assertEquals(".",project.getProperty("string.without.default"));
103         assertEquals("2002/01/21 12:18", project.getProperty("ethans.birth"));
104         assertEquals("2003/01/21", project.getProperty("first.birthday"));
105         assertEquals("0124", project.getProperty("olderThanAWeek"));
106         assertEquals("37", project.getProperty("existing.prop"));
107         assertEquals("6",project.getProperty("int.without.value"));
108     }
109
110     public void testValueDoesNotGetOverwritten() {
111         // this test shows that the bug report 21505 is fixed
112
executeTarget("bugDemo1");
113         executeTarget("bugDemo2");
114         assertEquals("5", project.getProperty("foo"));
115     }
116 /*
117     public void testDirect() throws Exception {
118         PropertyFile pf = new PropertyFile();
119         pf.setProject(project);
120         pf.setFile(new File(testPropsFilePath));
121         PropertyFile.Entry entry = pf.createEntry();
122
123         entry.setKey("date");
124         entry.setValue("123");
125         PropertyFile.Entry.Type type = new PropertyFile.Entry.Type();
126         type.setValue("date");
127         entry.setType(type);
128
129         entry.setPattern("yyyy/MM/dd");
130
131         PropertyFile.Entry.Operation operation = new PropertyFile.Entry.Operation();
132         operation.setValue("+");
133         pf.execute();
134
135         Properties props = getTestProperties();
136         assertEquals("yeehaw", props.getProperty("date"));
137     }
138 */

139
140     private Properties JavaDoc getTestProperties() throws Exception JavaDoc {
141         Properties JavaDoc testProps = new Properties JavaDoc();
142         FileInputStream JavaDoc propsFile = new FileInputStream JavaDoc(testPropsFilePath);
143         testProps.load(propsFile);
144         propsFile.close();
145         return testProps;
146     }
147
148
149     private void initTestPropFile() throws Exception JavaDoc {
150         Properties JavaDoc testProps = new Properties JavaDoc();
151         testProps.put(FNAME_KEY, FNAME);
152         testProps.put(LNAME_KEY, LNAME);
153         testProps.put(EMAIL_KEY, EMAIL);
154         testProps.put("existing.prop", "37");
155
156         FileOutputStream JavaDoc fos = new FileOutputStream JavaDoc(testPropsFilePath);
157         testProps.store(fos, "defaults");
158         fos.close();
159     }
160
161
162     private void initBuildPropFile() throws Exception JavaDoc {
163         Properties JavaDoc buildProps = new Properties JavaDoc();
164         buildProps.put(testPropertyFileKey, testPropertyFile);
165         buildProps.put(FNAME_KEY, NEW_FNAME);
166         buildProps.put(LNAME_KEY, NEW_LNAME);
167         buildProps.put(EMAIL_KEY, NEW_EMAIL);
168         buildProps.put(PHONE_KEY, NEW_PHONE);
169         buildProps.put(AGE_KEY, NEW_AGE);
170         buildProps.put(DATE_KEY, NEW_DATE);
171
172         FileOutputStream JavaDoc fos = new FileOutputStream JavaDoc(buildPropsFilePath);
173         buildProps.store(fos, null);
174         fos.close();
175     }
176
177
178     private void destroyTempFiles() {
179         File JavaDoc tempFile = new File JavaDoc(testPropsFilePath);
180         tempFile.delete();
181         tempFile = null;
182
183         tempFile = new File JavaDoc(buildPropsFilePath);
184         tempFile.delete();
185         tempFile = null;
186
187         tempFile = new File JavaDoc(valueDoesNotGetOverwrittenPropsFilePath);
188         tempFile.delete();
189         tempFile = null;
190     }
191
192
193
194     private static final String JavaDoc
195         projectFilePath = "src/etc/testcases/taskdefs/optional/propertyfile.xml",
196
197         testPropertyFile = "propertyfile.test.properties",
198         testPropertyFileKey = "test.propertyfile",
199         testPropsFilePath = "src/etc/testcases/taskdefs/optional/" + testPropertyFile,
200
201         valueDoesNotGetOverwrittenPropertyFile = "overwrite.test.properties",
202         valueDoesNotGetOverwrittenPropertyFileKey = "overwrite.test.propertyfile",
203         valueDoesNotGetOverwrittenPropsFilePath = "src/etc/testcases/taskdefs/optional/" + valueDoesNotGetOverwrittenPropertyFile,
204
205         buildPropsFilePath = "src/etc/testcases/taskdefs/optional/propertyfile.build.properties",
206
207         FNAME = "Bruce",
208         NEW_FNAME = "Clark",
209         FNAME_KEY = "firstname",
210
211         LNAME = "Banner",
212         NEW_LNAME = "Kent",
213         LNAME_KEY = "lastname",
214
215         EMAIL = "incredible@hulk.com",
216         NEW_EMAIL = "kc@superman.com",
217         EMAIL_KEY = "email",
218
219         NEW_PHONE = "(520) 555-1212",
220         PHONE_KEY = "phone",
221
222         NEW_AGE = "30",
223         AGE_KEY = "age",
224
225         NEW_DATE = "2001/01/01 12:45",
226         DATE_KEY = "date";
227 }
228
Popular Tags