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:
019: package org.apache.tools.ant.taskdefs.optional;
020:
021: import org.apache.tools.ant.BuildFileTest;
022:
023: import java.io.IOException;
024: import java.io.File;
025: import java.io.InputStream;
026: import java.io.BufferedInputStream;
027: import java.io.FileInputStream;
028: import java.io.FileReader;
029: import java.io.BufferedReader;
030: import java.util.Properties;
031:
032: /**
033: * Tests the EchoProperties task.
034: *
035: * @created 17-Jan-2002
036: * @since Ant 1.5
037: */
038: public class EchoPropertiesTest extends BuildFileTest {
039:
040: private final static String TASKDEFS_DIR = "src/etc/testcases/taskdefs/optional/";
041: private static final String GOOD_OUTFILE = "test.properties";
042: private static final String GOOD_OUTFILE_XML = "test.xml";
043: private static final String PREFIX_OUTFILE = "test-prefix.properties";
044: private static final String TEST_VALUE = "isSet";
045: private static final String BAD_OUTFILE = ".";
046:
047: public EchoPropertiesTest(String name) {
048: super (name);
049: }
050:
051: public void setUp() {
052: configureProject(TASKDEFS_DIR + "echoproperties.xml");
053: project.setProperty("test.property", TEST_VALUE);
054: }
055:
056: public void tearDown() {
057: executeTarget("cleanup");
058: }
059:
060: public void testEchoToLog() {
061: expectLogContaining("testEchoToLog", "test.property="
062: + TEST_VALUE);
063: }
064:
065: public void testEchoWithEmptyPrefixToLog() {
066: expectLogContaining("testEchoWithEmptyPrefixToLog",
067: "test.property=" + TEST_VALUE);
068: }
069:
070: public void testReadBadFile() {
071: expectBuildExceptionContaining("testReadBadFile",
072: "srcfile is a directory", "srcfile is a directory!");
073: }
074:
075: public void testReadBadFileFail() {
076: expectBuildExceptionContaining("testReadBadFile",
077: "srcfile is a directory", "srcfile is a directory!");
078: }
079:
080: public void testReadBadFileNoFail() {
081: expectLog("testReadBadFileNoFail", "srcfile is a directory!");
082: }
083:
084: public void testEchoToBadFile() {
085: expectBuildExceptionContaining("testEchoToBadFile",
086: "destfile is a directory", "destfile is a directory!");
087: }
088:
089: public void testEchoToBadFileFail() {
090: expectBuildExceptionContaining("testEchoToBadFileFail",
091: "destfile is a directory", "destfile is a directory!");
092: }
093:
094: public void testEchoToBadFileNoFail() {
095: expectLog("testEchoToBadFileNoFail", "destfile is a directory!");
096: }
097:
098: public void testEchoToGoodFile() throws Exception {
099: executeTarget("testEchoToGoodFile");
100: assertGoodFile();
101: }
102:
103: public void testEchoToGoodFileXml() throws Exception {
104: executeTarget("testEchoToGoodFileXml");
105:
106: // read in the file
107: File f = createRelativeFile(GOOD_OUTFILE_XML);
108: FileReader fr = new FileReader(f);
109: try {
110: BufferedReader br = new BufferedReader(fr);
111: String read = null;
112: while ((read = br.readLine()) != null) {
113: if (read
114: .indexOf("<property name=\"test.property\" value=\""
115: + TEST_VALUE + "\" />") >= 0) {
116: // found the property we set - it's good.
117: return;
118: }
119: }
120: fail("did not encounter set property in generated file.");
121: } finally {
122: try {
123: fr.close();
124: } catch (IOException e) {
125: }
126: }
127: }
128:
129: public void testEchoToGoodFileFail() throws Exception {
130: executeTarget("testEchoToGoodFileFail");
131: assertGoodFile();
132: }
133:
134: public void testEchoToGoodFileNoFail() throws Exception {
135: executeTarget("testEchoToGoodFileNoFail");
136: assertGoodFile();
137: }
138:
139: public void testEchoPrefix() throws Exception {
140: testEchoPrefixVarious("testEchoPrefix");
141: }
142:
143: public void testEchoPrefixAsPropertyset() throws Exception {
144: testEchoPrefixVarious("testEchoPrefixAsPropertyset");
145: }
146:
147: public void testEchoPrefixAsNegatedPropertyset() throws Exception {
148: testEchoPrefixVarious("testEchoPrefixAsNegatedPropertyset");
149: }
150:
151: public void testEchoPrefixAsDoublyNegatedPropertyset()
152: throws Exception {
153: testEchoPrefixVarious("testEchoPrefixAsDoublyNegatedPropertyset");
154: }
155:
156: public void testWithPrefixAndRegex() throws Exception {
157: expectSpecificBuildException(
158: "testWithPrefixAndRegex",
159: "The target must fail with prefix and regex attributes set",
160: "Please specify either prefix or regex, but not both");
161: }
162:
163: public void testWithEmptyPrefixAndRegex() throws Exception {
164: expectLogContaining("testEchoWithEmptyPrefixToLog",
165: "test.property=" + TEST_VALUE);
166: }
167:
168: public void testWithRegex() throws Exception {
169: executeTarget("testWithRegex");
170: assertDebuglogContaining("ant.home=");
171: }
172:
173: private void testEchoPrefixVarious(String target) throws Exception {
174: executeTarget(target);
175: Properties props = loadPropFile(PREFIX_OUTFILE);
176: assertEquals("prefix didn't include 'a.set' property", "true",
177: props.getProperty("a.set"));
178: assertNull("prefix failed to filter out property 'b.set'",
179: props.getProperty("b.set"));
180: }
181:
182: protected Properties loadPropFile(String relativeFilename)
183: throws IOException {
184: File f = createRelativeFile(relativeFilename);
185: Properties props = new Properties();
186: InputStream in = null;
187: try {
188: in = new BufferedInputStream(new FileInputStream(f));
189: props.load(in);
190: } finally {
191: if (in != null) {
192: try {
193: in.close();
194: } catch (IOException e) {
195: }
196: }
197: }
198: return props;
199: }
200:
201: protected void assertGoodFile() throws Exception {
202: File f = createRelativeFile(GOOD_OUTFILE);
203: assertTrue("Did not create " + f.getAbsolutePath(), f.exists());
204: Properties props = loadPropFile(GOOD_OUTFILE);
205: props.list(System.out);
206: assertEquals("test property not found ", TEST_VALUE, props
207: .getProperty("test.property"));
208: /*
209: // read in the file
210: FileReader fr = new FileReader( f );
211: try {
212: BufferedReader br = new BufferedReader( fr );
213: String read = null;
214: while ( (read = br.readLine()) != null)
215: {
216: if (read.indexOf("test.property" + TEST_VALUE) >= 0)
217: {
218: // found the property we set - it's good.
219: return;
220: }
221: }
222: fail( "did not encounter set property in generated file." );
223: } finally {
224: try { fr.close(); } catch(IOException e) {}
225: }
226: */
227: }
228:
229: protected String toAbsolute(String filename) {
230: return createRelativeFile(filename).getAbsolutePath();
231: }
232:
233: protected File createRelativeFile(String filename) {
234: if (filename.equals(".")) {
235: return getProjectDir();
236: }
237: // else
238: return new File(getProjectDir(), filename);
239: }
240: }
|