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;
020:
021: import org.apache.tools.ant.BuildFileTest;
022: import org.apache.tools.ant.util.FileUtils;
023:
024: import java.io.File;
025: import java.io.FileReader;
026: import java.io.IOException;
027: import java.io.Reader;
028:
029: /**
030: * A test class for the 'concat' task, used to concatenate a series of
031: * files into a single stream.
032: *
033: */
034: public class ConcatTest extends BuildFileTest {
035:
036: /**
037: * The name of the temporary file.
038: */
039: private static final String tempFile = "concat.tmp";
040:
041: /**
042: * The name of the temporary file.
043: */
044: private static final String tempFile2 = "concat.tmp.2";
045:
046: /** Utilities used for file operations */
047: private static final FileUtils FILE_UTILS = FileUtils
048: .getFileUtils();
049:
050: /**
051: * Required constructor.
052: */
053: public ConcatTest(String name) {
054: super (name);
055: }
056:
057: /**
058: * Test set up, called by the unit test framework prior to each
059: * test.
060: */
061: public void setUp() {
062: configureProject("src/etc/testcases/taskdefs/concat.xml");
063: }
064:
065: /**
066: * Test tear down, called by the unit test framework prior to each
067: * test.
068: */
069: public void tearDown() {
070: executeTarget("cleanup");
071: }
072:
073: /**
074: * Expect an exception when insufficient information is provided.
075: */
076: public void test1() {
077: expectBuildException("test1", "Insufficient information.");
078: }
079:
080: /**
081: * Expect an exception when the destination file is invalid.
082: */
083: public void test2() {
084: expectBuildException("test2", "Invalid destination file.");
085: }
086:
087: /**
088: * Cats the string 'Hello, World!' to a temporary file.
089: */
090: public void test3() {
091:
092: File file = new File(getProjectDir(), tempFile);
093: if (file.exists()) {
094: file.delete();
095: }
096:
097: executeTarget("test3");
098:
099: assertTrue(file.exists());
100: }
101:
102: /**
103: * Cats the file created in test3 three times.
104: */
105: public void test4() {
106: test3();
107:
108: File file = new File(getProjectDir(), tempFile);
109: final long origSize = file.length();
110:
111: executeTarget("test4");
112:
113: File file2 = new File(getProjectDir(), tempFile2);
114: final long newSize = file2.length();
115:
116: assertEquals(origSize * 3, newSize);
117: }
118:
119: /**
120: * Cats the string 'Hello, World!' to the console.
121: */
122: public void test5() {
123: expectLog("test5", "Hello, World!");
124: }
125:
126: public void test6() {
127: String filename = "src/etc/testcases/taskdefs/thisfiledoesnotexist"
128: .replace('/', File.separatorChar);
129: expectLogContaining("test6", filename + " does not exist.");
130: }
131:
132: public void testConcatNoNewline() {
133: expectLog("testConcatNoNewline", "ab");
134: }
135:
136: public void testConcatNoNewlineEncoding() {
137: expectLog("testConcatNoNewlineEncoding", "ab");
138: }
139:
140: public void testPath() {
141: test3();
142:
143: File file = new File(getProjectDir(), tempFile);
144: final long origSize = file.length();
145:
146: executeTarget("testPath");
147:
148: File file2 = new File(getProjectDir(), tempFile2);
149: final long newSize = file2.length();
150:
151: assertEquals(origSize, newSize);
152:
153: }
154:
155: public void testAppend() {
156: test3();
157:
158: File file = new File(getProjectDir(), tempFile);
159: final long origSize = file.length();
160:
161: executeTarget("testAppend");
162:
163: File file2 = new File(getProjectDir(), tempFile2);
164: final long newSize = file2.length();
165:
166: assertEquals(origSize * 2, newSize);
167:
168: }
169:
170: public void testFilter() {
171: executeTarget("testfilter");
172: assertTrue(getLog().indexOf("REPLACED") > -1);
173: }
174:
175: public void testNoOverwrite() {
176: executeTarget("testnooverwrite");
177: File file2 = new File(getProjectDir(), tempFile2);
178: long size = file2.length();
179: assertEquals(size, 0);
180: }
181:
182: public void testheaderfooter() {
183: test3();
184: expectLog("testheaderfooter", "headerHello, World!footer");
185: }
186:
187: public void testfileheader() {
188: test3();
189: expectLog("testfileheader", "Hello, World!Hello, World!");
190: }
191:
192: /**
193: * Expect an exception when attempting to cat an file to itself
194: */
195: public void testsame() {
196: expectBuildException("samefile", "output file same as input");
197: }
198:
199: /**
200: * Check if filter inline works
201: */
202: public void testfilterinline() {
203: executeTarget("testfilterinline");
204: assertTrue(getLog().indexOf("REPLACED") > -1);
205: }
206:
207: /**
208: * Check if multireader works
209: */
210: public void testmultireader() {
211: executeTarget("testmultireader");
212: assertTrue(getLog().indexOf("Bye") > -1);
213: assertTrue(getLog().indexOf("Hello") == -1);
214: }
215:
216: /**
217: * Check if fixlastline works
218: */
219: public void testfixlastline() throws IOException {
220: expectFileContains("testfixlastline", "concat.line4",
221: "end of line" + System.getProperty("line.separator")
222: + "This has");
223: }
224:
225: /**
226: * Check if fixlastline works with eol
227: */
228: public void testfixlastlineeol() throws IOException {
229: expectFileContains("testfixlastlineeol", "concat.linecr",
230: "end of line\rThis has");
231: }
232:
233: // ------------------------------------------------------
234: // Helper methods - should be in BuildFileTest
235: // -----------------------------------------------------
236:
237: private String getFileString(String filename) throws IOException {
238: Reader r = null;
239: try {
240: r = new FileReader(getProject().resolveFile(filename));
241: return FileUtils.readFully(r);
242: } finally {
243: FileUtils.close(r);
244: }
245:
246: }
247:
248: private String getFileString(String target, String filename)
249: throws IOException {
250: executeTarget(target);
251: return getFileString(filename);
252: }
253:
254: private void expectFileContains(String target, String filename,
255: String contains) throws IOException {
256: String content = getFileString(target, filename);
257: assertTrue("expecting file " + filename + " to contain "
258: + contains + " but got " + content, content
259: .indexOf(contains) > -1);
260: }
261:
262: public void testTranscoding() throws IOException {
263: executeTarget("testTranscoding");
264: File f1 = getProject().resolveFile("copy/expected/utf-8");
265: File f2 = getProject().resolveFile("concat.utf8");
266: assertTrue(FILE_UTILS.contentEquals(f1, f2));
267: }
268:
269: public void testResources() {
270: executeTarget("testResources");
271: }
272:
273: }
|