KickJava   Java API By Example, From Geeks To Geeks.

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


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.Reader JavaDoc;
22 import java.io.FileReader JavaDoc;
23 import java.io.IOException JavaDoc;
24
25 import org.apache.tools.ant.BuildFileTest;
26 import org.apache.tools.ant.util.FileUtils;
27
28 /**
29  */

30 public class TokenFilterTest extends BuildFileTest {
31
32     public TokenFilterTest(String JavaDoc name) {
33         super(name);
34     }
35
36     public void setUp() {
37         configureProject("src/etc/testcases/filters/tokenfilter.xml");
38         executeTarget("init");
39     }
40
41     public void tearDown() {
42         executeTarget("cleanup");
43     }
44
45     /** make sure tokenfilter exists */
46     public void testTokenfilter() throws IOException JavaDoc {
47         executeTarget("tokenfilter");
48     }
49
50     public void testTrimignore() throws IOException JavaDoc {
51         expectLogContaining("trimignore", "Hello-World");
52     }
53
54     public void testStringTokenizer() throws IOException JavaDoc {
55         expectLogContaining(
56             "stringtokenizer", "#This#is#a#number#of#words#");
57     }
58
59     public void testUnixLineOutput() throws IOException JavaDoc {
60         expectFileContains(
61             "unixlineoutput", "result/unixlineoutput",
62             "\nThis\nis\na\nnumber\nof\nwords\n");
63     }
64
65     public void testDosLineOutput() throws IOException JavaDoc {
66         expectFileContains(
67             "doslineoutput", "result/doslineoutput",
68             "\r\nThis\r\nis\r\na\r\nnumber\r\nof\r\nwords\r\n");
69     }
70
71     public void testFileTokenizer() throws IOException JavaDoc {
72         String JavaDoc contents = getFileString(
73             "filetokenizer", "result/filetokenizer");
74         assertStringContains(contents, " of words");
75         assertStringNotContains(contents, " This is");
76     }
77
78     public void testReplaceString() throws IOException JavaDoc {
79         expectFileContains(
80             "replacestring", "result/replacestring",
81             "this is the moon");
82     }
83
84     public void testReplaceStrings() throws IOException JavaDoc {
85         expectLogContaining("replacestrings", "bar bar bar");
86     }
87
88     public void testContainsString() throws IOException JavaDoc {
89         String JavaDoc contents = getFileString(
90             "containsstring", "result/containsstring");
91         assertStringContains(contents, "this is a line contains foo");
92         assertStringNotContains(contents, "this line does not");
93     }
94
95     public void testReplaceRegex() throws IOException JavaDoc {
96         if (! hasRegex("testReplaceRegex"))
97             return;
98         String JavaDoc contents = getFileString(
99             "replaceregex", "result/replaceregex");
100         assertStringContains(contents, "world world world world");
101         assertStringContains(contents, "dog Cat dog");
102         assertStringContains(contents, "moon Sun Sun");
103         assertStringContains(contents, "found WhiteSpace");
104         assertStringContains(contents, "Found digits [1234]");
105         assertStringNotContains(contents, "This is a line with digits");
106     }
107
108     public void testFilterReplaceRegex() throws IOException JavaDoc {
109         if (! hasRegex("testFilterReplaceRegex"))
110             return;
111         String JavaDoc contents = getFileString(
112             "filterreplaceregex", "result/filterreplaceregex");
113         assertStringContains(contents, "world world world world");
114     }
115
116     public void testHandleDollerMatch() throws IOException JavaDoc {
117         if (! hasRegex("testFilterReplaceRegex"))
118             return;
119         executeTarget("dollermatch");
120     }
121
122     public void testTrimFile() throws IOException JavaDoc {
123         String JavaDoc contents = getFileString(
124             "trimfile", "result/trimfile");
125         assertTrue("no ws at start", contents.startsWith("This is th"));
126         assertTrue("no ws at end", contents.endsWith("second line."));
127         assertStringContains(contents, " This is the second");
128     }
129
130     public void testTrimFileByLine() throws IOException JavaDoc {
131         String JavaDoc contents = getFileString(
132             "trimfilebyline", "result/trimfilebyline");
133         assertFalse("no ws at start", contents.startsWith("This is th"));
134         assertFalse("no ws at end", contents.endsWith("second line."));
135         assertStringNotContains(contents, " This is the second");
136         assertStringContains(contents, "file.\nThis is the second");
137     }
138
139     public void testFilterReplaceString() throws IOException JavaDoc {
140         String JavaDoc contents = getFileString(
141             "filterreplacestring", "result/filterreplacestring");
142         assertStringContains(contents, "This is the moon");
143     }
144
145     public void testFilterReplaceStrings() throws IOException JavaDoc {
146         expectLogContaining("filterreplacestrings", "bar bar bar");
147     }
148
149     public void testContainsRegex() throws IOException JavaDoc {
150         if (! hasRegex("testContainsRegex"))
151             return;
152         String JavaDoc contents = getFileString(
153             "containsregex", "result/containsregex");
154         assertStringContains(contents, "hello world");
155         assertStringNotContains(contents, "this is the moon");
156         assertStringContains(contents, "World here");
157     }
158
159     public void testFilterContainsRegex() throws IOException JavaDoc {
160         if (! hasRegex("testFilterContainsRegex"))
161             return;
162         String JavaDoc contents = getFileString(
163             "filtercontainsregex", "result/filtercontainsregex");
164         assertStringContains(contents, "hello world");
165         assertStringNotContains(contents, "this is the moon");
166         assertStringContains(contents, "World here");
167     }
168
169     public void testContainsRegex2() throws IOException JavaDoc {
170         if (! hasRegex("testContainsRegex2"))
171             return;
172         String JavaDoc contents = getFileString(
173             "containsregex2", "result/containsregex2");
174         assertStringContains(contents, "void register_bits();");
175     }
176
177     public void testDeleteCharacters() throws IOException JavaDoc {
178         String JavaDoc contents = getFileString(
179             "deletecharacters", "result/deletechars");
180         assertStringNotContains(contents, "#");
181         assertStringNotContains(contents, "*");
182         assertStringContains(contents, "This is some ");
183     }
184
185     public void testScriptFilter() throws IOException JavaDoc {
186         if (! hasScript("testScriptFilter"))
187             return;
188
189         expectFileContains("scriptfilter", "result/scriptfilter",
190                            "HELLO WORLD");
191     }
192
193
194     public void testScriptFilter2() throws IOException JavaDoc {
195         if (! hasScript("testScriptFilter"))
196             return;
197
198         expectFileContains("scriptfilter2", "result/scriptfilter2",
199                            "HELLO MOON");
200     }
201
202     public void testCustomTokenFilter() throws IOException JavaDoc {
203         expectFileContains("customtokenfilter", "result/custom",
204                            "Hello World");
205     }
206
207     // ------------------------------------------------------
208
// Helper methods
209
// -----------------------------------------------------
210
private boolean hasScript(String JavaDoc test) {
211         try {
212             executeTarget("hasscript");
213         }
214         catch (Throwable JavaDoc ex) {
215             System.out.println(
216                 test + ": skipped - script not present ");
217             return false;
218         }
219         return true;
220     }
221
222     private boolean hasRegex(String JavaDoc test) {
223         try {
224             executeTarget("hasregex");
225             expectFileContains("result/replaceregexp", "bye world");
226         }
227         catch (Throwable JavaDoc ex) {
228             System.out.println(test + ": skipped - regex not present "
229                                + ex);
230             return false;
231         }
232         return true;
233     }
234
235     private void assertStringContains(String JavaDoc string, String JavaDoc contains) {
236         assertTrue("[" + string + "] does not contain [" + contains +"]",
237                    string.indexOf(contains) > -1);
238     }
239
240     private void assertStringNotContains(String JavaDoc string, String JavaDoc contains) {
241         assertTrue("[" + string + "] does contain [" + contains +"]",
242                    string.indexOf(contains) == -1);
243     }
244
245     private String JavaDoc getFileString(String JavaDoc filename)
246         throws IOException JavaDoc
247     {
248         Reader JavaDoc r = null;
249         try {
250             r = new FileReader JavaDoc(getProject().resolveFile(filename));
251             return FileUtils.newFileUtils().readFully(r);
252         }
253         finally {
254             try {r.close();} catch (Throwable JavaDoc ignore) {}
255         }
256
257     }
258
259     private String JavaDoc getFileString(String JavaDoc target, String JavaDoc filename)
260         throws IOException JavaDoc
261     {
262         executeTarget(target);
263         return getFileString(filename);
264     }
265
266     private void expectFileContains(String JavaDoc name, String JavaDoc contains)
267         throws IOException JavaDoc
268     {
269         String JavaDoc content = getFileString(name);
270         assertTrue(
271             "expecting file " + name + " to contain " + contains +
272             " but got " + content, content.indexOf(contains) > -1);
273     }
274
275     private void expectFileContains(
276         String JavaDoc target, String JavaDoc name, String JavaDoc contains)
277         throws IOException JavaDoc
278     {
279         executeTarget(target);
280         expectFileContains(name, contains);
281     }
282
283     public static class Capitalize
284         implements TokenFilter.Filter
285     {
286         public String JavaDoc filter(String JavaDoc token) {
287             if (token.length() == 0)
288                 return token;
289             return token.substring(0, 1).toUpperCase() +
290                 token.substring(1);
291         }
292     }
293
294 }
295
Popular Tags