KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > filters > ConcatFilterTest


1 /*
2  * Copyright 2003-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.filters;
19
20 import java.io.File JavaDoc;
21 import java.io.IOException JavaDoc;
22
23 import org.apache.tools.ant.BuildFileTest;
24 import org.apache.tools.ant.taskdefs.condition.Os;
25 import org.apache.tools.ant.util.FileUtils;
26
27 /**
28  * JUnit Testcases for ConcatReader
29  */

30 public class ConcatFilterTest extends BuildFileTest {
31
32     private static FileUtils fu = FileUtils.newFileUtils();
33     private static final String JavaDoc lSep =
34         Os.isFamily("mac") ? "\r" : System.getProperty("line.separator");
35
36     private static final String JavaDoc FILE_PREPEND_WITH =
37           "this-should-be-the-first-line" + lSep
38         + "Line 1" + lSep
39         + "Line 2" + lSep
40         + "Line 3" + lSep
41         + "Line 4" + lSep
42     ;
43
44     private static final String JavaDoc FILE_PREPEND =
45           "Line 1" + lSep
46         + "Line 2" + lSep
47         + "Line 3" + lSep
48         + "Line 4" + lSep
49         + "Line 5" + lSep
50     ;
51
52     private static final String JavaDoc FILE_APPEND_WITH =
53           "Line 57" + lSep
54         + "Line 58" + lSep
55         + "Line 59" + lSep
56         + "Line 60" + lSep
57         + "this-should-be-the-last-line" + lSep
58     ;
59
60     private static final String JavaDoc FILE_APPEND =
61           "Line 56" + lSep
62         + "Line 57" + lSep
63         + "Line 58" + lSep
64         + "Line 59" + lSep
65         + "Line 60" + lSep
66     ;
67
68
69     public ConcatFilterTest(String JavaDoc name) {
70         super(name);
71     }
72
73     public void setUp() {
74         configureProject("src/etc/testcases/filters/concat.xml");
75     }
76
77     public void tearDown() {
78         executeTarget("cleanup");
79     }
80
81     public void testFilterReaderNoArgs() throws IOException JavaDoc {
82         executeTarget("testFilterReaderNoArgs");
83         File JavaDoc expected = getProject().resolveFile("input/concatfilter.test");
84         File JavaDoc result = getProject().resolveFile("result/concat.FilterReaderNoArgs.test");
85         assertTrue("testFilterReaderNoArgs: Result not like expected", fu.contentEquals(expected, result));
86     }
87
88     public void testFilterReaderBefore() {
89         doTest("testFilterReaderPrepend", FILE_PREPEND_WITH, FILE_APPEND);
90     }
91
92     public void testFilterReaderAfter() {
93         doTest("testFilterReaderAppend", FILE_PREPEND, FILE_APPEND_WITH);
94     }
95
96     public void testFilterReaderBeforeAfter() {
97         doTest("testFilterReaderPrependAppend", FILE_PREPEND_WITH, FILE_APPEND_WITH);
98     }
99
100     public void testConcatFilter() {
101         doTest("testConcatFilter", FILE_PREPEND, FILE_APPEND);
102     }
103
104     public void testConcatFilterBefore() {
105         doTest("testConcatFilterPrepend", FILE_PREPEND_WITH, FILE_APPEND);
106     }
107
108     public void testConcatFilterAfter() {
109         doTest("testConcatFilterAppend", FILE_PREPEND, FILE_APPEND_WITH);
110     }
111
112     public void testConcatFilterBeforeAfter() {
113         doTest("testConcatFilterPrependAppend", FILE_PREPEND_WITH, FILE_APPEND_WITH);
114     }
115
116
117     /**
118      * Executes a target and checks the beginning and the ending of a file.
119      * The filename depends on the target name: target name <i>testHelloWorld</i>
120      * will search for a file <i>result/concat.HelloWorld.test</i>.
121      * @param target The target to invoke
122      * @param expectedStart The string which should be at the beginning of the file
123      * @param expectedEnd The string which should be at the end of the file
124      */

125     protected void doTest(String JavaDoc target, String JavaDoc expectedStart, String JavaDoc expectedEnd) {
126         executeTarget(target);
127         String JavaDoc resultContent = read("result/concat." + target.substring(4) + ".test");
128         assertTrue("First 5 lines differs.", resultContent.startsWith(expectedStart));
129         assertTrue("Last 5 lines differs.", resultContent.endsWith(expectedEnd));
130     }
131
132
133     /**
134      * Wrapper for FileUtils.readFully().
135      * Additionally it resolves the filename according the the projects basedir
136      * and closes the used reader.
137      * @param filename The name of the file to read
138      * @return the content of the file or <i>null</i> if something goes wrong
139      */

140     protected String JavaDoc read(String JavaDoc filename) {
141         String JavaDoc content = null;
142         try {
143             File JavaDoc file = getProject().resolveFile(filename);
144             java.io.FileReader JavaDoc rdr = new java.io.FileReader JavaDoc(file);
145             content = fu.readFully(rdr);
146             rdr.close();
147             rdr = null;
148         } catch (Exception JavaDoc e) {
149             e.printStackTrace();
150         }
151         return content;
152     }
153
154 }
155
Popular Tags