KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > taskdefs > ConcatTest


1 /*
2  * Copyright 2002-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;
19
20 import org.apache.tools.ant.BuildFileTest;
21 import org.apache.tools.ant.util.FileUtils;
22
23 import java.io.File JavaDoc;
24 import java.io.FileReader JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.io.Reader JavaDoc;
27
28 /**
29  * A test class for the 'concat' task, used to concatenate a series of
30  * files into a single stream.
31  *
32  */

33 public class ConcatTest
34     extends BuildFileTest {
35
36     /**
37      * The name of the temporary file.
38      */

39     private static final String JavaDoc tempFile = "concat.tmp";
40
41     /**
42      * The name of the temporary file.
43      */

44     private static final String JavaDoc tempFile2 = "concat.tmp.2";
45
46     /**
47      * Required constructor.
48      */

49     public ConcatTest(String JavaDoc name) {
50         super(name);
51     }
52
53     /**
54      * Test set up, called by the unit test framework prior to each
55      * test.
56      */

57     public void setUp() {
58         configureProject("src/etc/testcases/taskdefs/concat.xml");
59     }
60
61     /**
62      * Test tear down, called by the unit test framework prior to each
63      * test.
64      */

65     public void tearDown() {
66         executeTarget("cleanup");
67     }
68
69     /**
70      * Expect an exception when insufficient information is provided.
71      */

72     public void test1() {
73         expectBuildException("test1", "Insufficient information.");
74     }
75
76     /**
77      * Expect an exception when the destination file is invalid.
78      */

79     public void test2() {
80         expectBuildException("test2", "Invalid destination file.");
81     }
82
83     /**
84      * Cats the string 'Hello, World!' to a temporary file.
85      */

86     public void test3() {
87
88         File JavaDoc file = new File JavaDoc(getProjectDir(), tempFile);
89         if (file.exists()) {
90             file.delete();
91         }
92
93         executeTarget("test3");
94
95         assertTrue(file.exists());
96     }
97
98     /**
99      * Cats the file created in test3 three times.
100      */

101     public void test4() {
102         test3();
103
104         File JavaDoc file = new File JavaDoc(getProjectDir(), tempFile);
105         final long origSize = file.length();
106
107         executeTarget("test4");
108
109         File JavaDoc file2 = new File JavaDoc(getProjectDir(), tempFile2);
110         final long newSize = file2.length();
111
112         assertEquals(origSize * 3, newSize);
113     }
114
115     /**
116      * Cats the string 'Hello, World!' to the console.
117      */

118     public void test5() {
119         expectLog("test5", "Hello, World!");
120     }
121
122     public void test6() {
123         String JavaDoc filename = "src/etc/testcases/taskdefs/thisfiledoesnotexist"
124             .replace('/', File.separatorChar);
125         expectLogContaining("test6", filename +" does not exist.");
126     }
127
128     public void testConcatNoNewline() {
129         expectLog("testConcatNoNewline", "ab");
130     }
131
132     public void testConcatNoNewlineEncoding() {
133         expectLog("testConcatNoNewlineEncoding", "ab");
134     }
135
136     public void testPath() {
137         test3();
138
139         File JavaDoc file = new File JavaDoc(getProjectDir(), tempFile);
140         final long origSize = file.length();
141
142         executeTarget("testPath");
143
144         File JavaDoc file2 = new File JavaDoc(getProjectDir(), tempFile2);
145         final long newSize = file2.length();
146
147         assertEquals(origSize, newSize);
148
149     }
150     public void testAppend() {
151         test3();
152
153         File JavaDoc file = new File JavaDoc(getProjectDir(), tempFile);
154         final long origSize = file.length();
155
156         executeTarget("testAppend");
157
158         File JavaDoc file2 = new File JavaDoc(getProjectDir(), tempFile2);
159         final long newSize = file2.length();
160
161         assertEquals(origSize*2, newSize);
162
163     }
164
165     public void testFilter() {
166         executeTarget("testfilter");
167         assertTrue(getLog().indexOf("REPLACED") > -1);
168     }
169
170     public void testNoOverwrite() {
171         executeTarget("testnooverwrite");
172         File JavaDoc file2 = new File JavaDoc(getProjectDir(), tempFile2);
173         long size = file2.length();
174         assertEquals(size, 0);
175     }
176
177     public void testheaderfooter() {
178         test3();
179         expectLog("testheaderfooter", "headerHello, World!footer");
180     }
181
182     public void testfileheader() {
183         test3();
184         expectLog("testfileheader", "Hello, World!Hello, World!");
185     }
186
187     /**
188      * Expect an exception when attempting to cat an file to itself
189      */

190     public void testsame() {
191         expectBuildException("samefile", "output file same as input");
192     }
193
194     /**
195      * Check if filter inline works
196      */

197     public void testfilterinline() {
198         executeTarget("testfilterinline");
199         assertTrue(getLog().indexOf("REPLACED") > -1);
200     }
201
202     /**
203      * Check if multireader works
204      */

205     public void testmultireader() {
206         executeTarget("testmultireader");
207         assertTrue(getLog().indexOf("Bye") > -1);
208         assertTrue(getLog().indexOf("Hello") == -1);
209     }
210     /**
211      * Check if fixlastline works
212      */

213     public void testfixlastline()
214         throws IOException JavaDoc
215     {
216         expectFileContains(
217             "testfixlastline", "concat.line4",
218             "end of line" + System.getProperty("line.separator")
219             + "This has");
220     }
221
222     /**
223      * Check if fixlastline works with eol
224      */

225     public void testfixlastlineeol()
226         throws IOException JavaDoc
227     {
228         expectFileContains(
229             "testfixlastlineeol", "concat.linecr",
230             "end of line\rThis has");
231     }
232
233     // ------------------------------------------------------
234
// Helper methods - should be in BuildFileTest
235
// -----------------------------------------------------
236

237     private String JavaDoc getFileString(String JavaDoc filename)
238         throws IOException JavaDoc
239     {
240         Reader JavaDoc r = null;
241         try {
242             r = new FileReader JavaDoc(getProject().resolveFile(filename));
243             return FileUtils.newFileUtils().readFully(r);
244         }
245         finally {
246             try {r.close();} catch (Throwable JavaDoc ignore) {}
247         }
248
249     }
250
251     private String JavaDoc getFileString(String JavaDoc target, String JavaDoc filename)
252         throws IOException JavaDoc
253     {
254         executeTarget(target);
255         return getFileString(filename);
256     }
257
258     private void expectFileContains(
259         String JavaDoc target, String JavaDoc filename, String JavaDoc contains)
260         throws IOException JavaDoc
261     {
262         String JavaDoc content = getFileString(target, filename);
263         assertTrue(
264             "expecting file " + filename + " to contain " +
265             contains +
266             " but got " + content, content.indexOf(contains) > -1);
267     }
268
269     public void testTranscoding() throws IOException JavaDoc {
270         executeTarget("testTranscoding");
271         FileUtils fileUtils = FileUtils.newFileUtils();
272         File JavaDoc f1 = getProject().resolveFile("copy/expected/utf-8");
273         File JavaDoc f2 = getProject().resolveFile("concat.utf8");
274         assertTrue(fileUtils.contentEquals(f1, f2));
275     }
276 }
277
Popular Tags