KickJava   Java API By Example, From Geeks To Geeks.

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


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.Project;
26 import org.apache.tools.ant.BuildFileTest;
27 import org.apache.tools.ant.util.FileUtils;
28
29 /**
30  */

31 public class DynamicFilterTest extends BuildFileTest {
32
33     public DynamicFilterTest(String JavaDoc name) {
34         super(name);
35     }
36
37     public void setUp() {
38         configureProject("src/etc/testcases/filters/dynamicfilter.xml");
39         executeTarget("init");
40     }
41
42     public void tearDown() {
43         executeTarget("cleanup");
44     }
45     public void testCustomFilter() throws IOException JavaDoc {
46         expectFileContains("dynamicfilter", "result/dynamicfilter",
47                            "hellO wOrld");
48     }
49
50     // ------------------------------------------------------
51
// Helper methods
52
// -----------------------------------------------------
53

54
55     private void assertStringContains(String JavaDoc string, String JavaDoc contains) {
56         assertTrue("[" + string + "] does not contain [" + contains +"]",
57                    string.indexOf(contains) > -1);
58     }
59
60     private void assertStringNotContains(String JavaDoc string, String JavaDoc contains) {
61         assertTrue("[" + string + "] does contain [" + contains +"]",
62                    string.indexOf(contains) == -1);
63     }
64
65     private String JavaDoc getFileString(String JavaDoc filename)
66         throws IOException JavaDoc
67     {
68         Reader JavaDoc r = null;
69         try {
70             r = new FileReader JavaDoc(getProject().resolveFile(filename));
71             return FileUtils.newFileUtils().readFully(r);
72         }
73         finally {
74             try {r.close();} catch (Throwable JavaDoc ignore) {}
75         }
76
77     }
78
79     private String JavaDoc getFileString(String JavaDoc target, String JavaDoc filename)
80         throws IOException JavaDoc
81     {
82         executeTarget(target);
83         return getFileString(filename);
84     }
85
86     private void expectFileContains(String JavaDoc name, String JavaDoc contains)
87         throws IOException JavaDoc
88     {
89         String JavaDoc content = getFileString(name);
90         assertTrue(
91             "expecting file " + name + " to contain " + contains +
92             " but got " + content, content.indexOf(contains) > -1);
93     }
94
95     private void expectFileContains(
96         String JavaDoc target, String JavaDoc name, String JavaDoc contains)
97         throws IOException JavaDoc
98     {
99         executeTarget(target);
100         expectFileContains(name, contains);
101     }
102
103     public static class CustomFilter implements ChainableReader {
104         char replace = 'x';
105         char with = 'y';
106
107         public void setReplace(char replace) {
108             this.replace = replace;
109         }
110
111         public void setWith(char with) {
112             this.with = with;
113         }
114
115         public Reader JavaDoc chain(final Reader JavaDoc rdr) {
116             return new BaseFilterReader(rdr) {
117                 public int read()
118                     throws IOException JavaDoc
119                 {
120                     int c = in.read();
121                     if (c == replace)
122                         return with;
123                     else
124                         return c;
125                 }
126             };
127         }
128     }
129 }
130
Popular Tags