KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > types > FilterSetTest


1 /*
2  * Copyright 2001-2002, 2004-2005 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.types;
19
20 import org.apache.tools.ant.BuildException;
21 import org.apache.tools.ant.Project;
22 import org.apache.tools.ant.BuildFileTest;
23
24 import junit.framework.TestCase;
25 import junit.framework.AssertionFailedError;
26
27 import java.io.*;
28 import java.util.Hashtable JavaDoc;
29
30 /**
31  * FilterSet testing
32  *
33  */

34 public class FilterSetTest extends BuildFileTest {
35
36     static private final int BUF_SIZE = 32768;
37
38     public FilterSetTest(String JavaDoc name) {
39         super(name);
40     }
41
42     public void setUp() {
43         configureProject("src/etc/testcases/types/filterset.xml");
44     }
45
46     public void tearDown() {
47         executeTarget("cleanup");
48     }
49
50     public void test1() {
51         executeTarget("test1");
52         assertTrue("Filterset 1 failed", compareFiles("src/etc/testcases/types/gold/filterset1.txt",
53                                                       "src/etc/testcases/types/dest1.txt"));
54     }
55
56     public void test2() {
57         executeTarget("test2");
58         assertTrue("Filterset 2 failed", compareFiles("src/etc/testcases/types/gold/filterset2.txt",
59                                                       "src/etc/testcases/types/dest2.txt"));
60     }
61
62     public void test3() {
63         executeTarget("test3");
64         assertTrue("Filterset 3 failed", compareFiles("src/etc/testcases/types/gold/filterset3.txt",
65                                                       "src/etc/testcases/types/dest3.txt"));
66     }
67
68     /**
69      * This will test the recursive FilterSet. Which means that if
70      * the filter value @test@ contains another filter value, it will
71      * actually resolve.
72      */

73     public void testRecursive() {
74         String JavaDoc result = "it works line";
75         String JavaDoc line="@test@ line";
76         FilterSet fs = new FilterSet();
77         fs.addFilter("test", "@test1@");
78         fs.addFilter("test1","@test2@");
79         fs.addFilter("test2", "it works");
80         fs.setBeginToken("@");
81         fs.setEndToken("@");
82         assertEquals(result, fs.replaceTokens(line));
83     }
84
85     /**
86      * Test to see what happens when the resolving occurs in an
87      * infinite loop.
88      */

89     public void testInfinite() {
90         String JavaDoc result = "@test@ line testvalue";
91         String JavaDoc line = "@test@ line @test3@";
92         FilterSet fs = new FilterSet();
93         fs.addFilter("test", "@test1@");
94         fs.addFilter("test1","@test2@");
95         fs.addFilter("test2", "@test@");
96         fs.addFilter("test3", "testvalue");
97         fs.setBeginToken("@");
98         fs.setEndToken("@");
99         assertEquals(result, fs.replaceTokens(line));
100     }
101
102     /**
103      * Test to see what happens when the resolving occurs in
104      * what would be an infinite loop, but with recursion disabled.
105      */

106     public void testRecursionDisabled() {
107         String JavaDoc result = "@test1@ line testvalue";
108         String JavaDoc line = "@test@ line @test2@";
109         FilterSet fs = new FilterSet();
110         fs.addFilter("test", "@test1@");
111         fs.addFilter("test1","@test@");
112         fs.addFilter("test2", "testvalue");
113         fs.setBeginToken("@");
114         fs.setEndToken("@");
115         fs.setRecurse(false);
116         assertEquals(result, fs.replaceTokens(line));
117     }
118
119     public void testNestedFilterSets() {
120         executeTarget("test-nested-filtersets");
121
122         FilterSet fs = (FilterSet) getProject().getReference("1");
123         Hashtable JavaDoc filters = fs.getFilterHash();
124         assertEquals(1, filters.size());
125         assertEquals("value1", filters.get("token1"));
126
127         fs = (FilterSet) getProject().getReference("2");
128         filters = fs.getFilterHash();
129         assertEquals(2, filters.size());
130         assertEquals("1111", filters.get("aaaa"));
131         assertEquals("2222", filters.get("bbbb"));
132
133         fs = (FilterSet) getProject().getReference("3");
134         filters = fs.getFilterHash();
135         assertEquals(1, filters.size());
136         assertEquals("value4", filters.get("token4"));
137
138         fs = (FilterSet) getProject().getReference("5");
139         filters = fs.getFilterHash();
140         assertEquals(1, filters.size());
141         assertEquals("value1", filters.get("token1"));
142     }
143
144     private boolean compareFiles(String JavaDoc name1, String JavaDoc name2) {
145         File file1 = new File(System.getProperty("root"), name1);
146         File file2 = new File(System.getProperty("root"), name2);
147
148         try {
149             if (!file1.exists() || !file2.exists()) {
150                 System.out.println("One or both files do not exist:" + name1 + ", " + name2);
151                 return false;
152             }
153
154             if (file1.length() != file2.length()) {
155                 System.out.println("File size mismatch:" + name1 + "(" + file1.length() + "), " +
156                                    name2 + "(" + file2.length() + ")");
157                 return false;
158             }
159
160             // byte - byte compare
161
byte[] buffer1 = new byte[BUF_SIZE];
162             byte[] buffer2 = new byte[BUF_SIZE];
163
164             FileInputStream fis1 = new FileInputStream(file1);
165             FileInputStream fis2 = new FileInputStream(file2);
166             int index = 0;
167             int read = 0;
168             while ((read = fis1.read(buffer1)) != -1) {
169                 fis2.read(buffer2);
170                 for (int i = 0; i < read; ++i, ++index) {
171                     if (buffer1[i] != buffer2[i]) {
172                         System.out.println("Bytes mismatch:" + name1 + ", " + name2 +
173                                            " at byte " + index);
174                         return false;
175                     }
176                 }
177             }
178             return true;
179         }
180         catch (IOException e) {
181             System.out.println("IOException comparing files: " + name1 + ", " + name2);
182             return false;
183         }
184     }
185 }
186
Popular Tags