KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright 2000-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
18 package org.apache.tools.ant.taskdefs.optional;
19
20 import org.apache.tools.ant.BuildFileTest;
21
22 import java.io.IOException JavaDoc;
23 import java.io.File JavaDoc;
24 import java.io.InputStream JavaDoc;
25 import java.io.BufferedInputStream JavaDoc;
26 import java.io.FileInputStream JavaDoc;
27 import java.io.FileReader JavaDoc;
28 import java.io.BufferedReader JavaDoc;
29 import java.util.Properties JavaDoc;
30
31 /**
32  * Tests the EchoProperties task.
33  *
34  * @created 17-Jan-2002
35  * @since Ant 1.5
36  */

37 public class EchoPropertiesTest extends BuildFileTest {
38
39     private final static String JavaDoc TASKDEFS_DIR = "src/etc/testcases/taskdefs/optional/";
40     private static final String JavaDoc GOOD_OUTFILE = "test.properties";
41     private static final String JavaDoc GOOD_OUTFILE_XML = "test.xml";
42     private static final String JavaDoc PREFIX_OUTFILE = "test-prefix.properties";
43     private static final String JavaDoc TEST_VALUE = "isSet";
44     private static final String JavaDoc BAD_OUTFILE = ".";
45
46     public EchoPropertiesTest(String JavaDoc name) {
47         super(name);
48     }
49
50
51     public void setUp() {
52         configureProject(TASKDEFS_DIR + "echoproperties.xml");
53         project.setProperty( "test.property", TEST_VALUE );
54     }
55
56
57     public void tearDown() {
58         executeTarget("cleanup");
59     }
60
61
62     public void testEchoToLog() {
63         expectLogContaining("testEchoToLog", "test.property="+TEST_VALUE);
64     }
65
66
67     public void testReadBadFile() {
68         expectBuildExceptionContaining( "testReadBadFile",
69             "srcfile is a directory", "srcfile is a directory!" );
70     }
71
72
73     public void testReadBadFileFail() {
74         expectBuildExceptionContaining( "testReadBadFile",
75             "srcfile is a directory", "srcfile is a directory!" );
76     }
77
78
79     public void testReadBadFileNoFail() {
80         expectLog( "testReadBadFileNoFail", "srcfile is a directory!" );
81     }
82
83
84     public void testEchoToBadFile() {
85         expectBuildExceptionContaining( "testEchoToBadFile",
86             "destfile is a directory", "destfile is a directory!" );
87     }
88
89
90     public void testEchoToBadFileFail() {
91         expectBuildExceptionContaining( "testEchoToBadFileFail",
92             "destfile is a directory", "destfile is a directory!" );
93     }
94
95
96     public void testEchoToBadFileNoFail() {
97         expectLog( "testEchoToBadFileNoFail", "destfile is a directory!");
98     }
99
100
101     public void testEchoToGoodFile() throws Exception JavaDoc {
102         executeTarget( "testEchoToGoodFile" );
103         assertGoodFile();
104     }
105
106
107     public void testEchoToGoodFileXml() throws Exception JavaDoc {
108         executeTarget( "testEchoToGoodFileXml" );
109
110         // read in the file
111
File JavaDoc f = createRelativeFile( GOOD_OUTFILE_XML );
112         FileReader JavaDoc fr = new FileReader JavaDoc( f );
113         try {
114             BufferedReader JavaDoc br = new BufferedReader JavaDoc( fr );
115             String JavaDoc read = null;
116             while ( (read = br.readLine()) != null) {
117                 if (read.indexOf("<property name=\"test.property\" value=\""+TEST_VALUE+"\"></property>") >= 0) {
118                     // found the property we set - it's good.
119
return;
120                 }
121             }
122             fail( "did not encounter set property in generated file." );
123         } finally {
124             try {
125                 fr.close();
126             } catch(IOException JavaDoc e) {}
127         }
128     }
129
130
131     public void testEchoToGoodFileFail() throws Exception JavaDoc {
132         executeTarget( "testEchoToGoodFileFail" );
133         assertGoodFile();
134     }
135
136
137     public void testEchoToGoodFileNoFail() throws Exception JavaDoc {
138         executeTarget( "testEchoToGoodFileNoFail" );
139         assertGoodFile();
140     }
141
142
143     public void testEchoPrefix() throws Exception JavaDoc {
144         testEchoPrefixVarious("testEchoPrefix");
145     }
146
147     public void testEchoPrefixAsPropertyset() throws Exception JavaDoc {
148         testEchoPrefixVarious("testEchoPrefixAsPropertyset");
149     }
150
151     public void testEchoPrefixAsNegatedPropertyset() throws Exception JavaDoc {
152         testEchoPrefixVarious("testEchoPrefixAsNegatedPropertyset");
153     }
154
155     public void testEchoPrefixAsDoublyNegatedPropertyset() throws Exception JavaDoc {
156         testEchoPrefixVarious("testEchoPrefixAsDoublyNegatedPropertyset");
157     }
158
159     private void testEchoPrefixVarious(String JavaDoc target) throws Exception JavaDoc {
160         executeTarget(target);
161         Properties JavaDoc props = loadPropFile(PREFIX_OUTFILE);
162         assertEquals("prefix didn't include 'a.set' property",
163             "true", props.getProperty("a.set"));
164         assertNull("prefix failed to filter out property 'b.set'",
165             props.getProperty("b.set"));
166     }
167
168     protected Properties JavaDoc loadPropFile(String JavaDoc relativeFilename)
169             throws IOException JavaDoc {
170         File JavaDoc f = createRelativeFile( relativeFilename );
171         Properties JavaDoc props=new Properties JavaDoc();
172         InputStream JavaDoc in=null;
173         try {
174             in=new BufferedInputStream JavaDoc(new FileInputStream JavaDoc(f));
175             props.load(in);
176         } finally {
177             if(in!=null) {
178                 try { in.close(); } catch(IOException JavaDoc e) {}
179             }
180         }
181         return props;
182     }
183
184     protected void assertGoodFile() throws Exception JavaDoc {
185         File JavaDoc f = createRelativeFile( GOOD_OUTFILE );
186         assertTrue(
187             "Did not create "+f.getAbsolutePath(),
188             f.exists() );
189         Properties JavaDoc props=loadPropFile(GOOD_OUTFILE);
190         props.list(System.out);
191         assertEquals("test property not found ",
192                      TEST_VALUE, props.getProperty("test.property"));
193 /*
194         // read in the file
195         FileReader fr = new FileReader( f );
196         try {
197             BufferedReader br = new BufferedReader( fr );
198             String read = null;
199             while ( (read = br.readLine()) != null)
200             {
201                 if (read.indexOf("test.property" + TEST_VALUE) >= 0)
202                 {
203                     // found the property we set - it's good.
204                     return;
205                 }
206             }
207             fail( "did not encounter set property in generated file." );
208         } finally {
209             try { fr.close(); } catch(IOException e) {}
210         }
211 */

212     }
213
214
215     protected String JavaDoc toAbsolute( String JavaDoc filename ) {
216         return createRelativeFile( filename ).getAbsolutePath();
217     }
218
219
220     protected File JavaDoc createRelativeFile( String JavaDoc filename ) {
221         if (filename.equals( "." )) {
222             return getProjectDir();
223         }
224         // else
225
return new File JavaDoc( getProjectDir(), filename );
226     }
227 }
228
229
Popular Tags