KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > hivemind > test > ant > TestConstructRegistry


1 // Copyright 2004, 2005 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15 package hivemind.test.ant;
16
17 import hivemind.test.FrameworkTestCase;
18
19 import java.io.BufferedInputStream JavaDoc;
20 import java.io.BufferedReader JavaDoc;
21 import java.io.File JavaDoc;
22 import java.io.FileInputStream JavaDoc;
23 import java.io.InputStream JavaDoc;
24 import java.io.InputStreamReader JavaDoc;
25 import java.io.LineNumberReader JavaDoc;
26
27 import org.apache.hivemind.ant.ConstructRegistry;
28 import org.apache.tools.ant.BuildException;
29 import org.apache.tools.ant.Project;
30 import org.apache.tools.ant.Target;
31 import org.apache.tools.ant.types.Path;
32
33 /**
34  * Tests for the {@link org.apache.hivemind.ant.ConstructRegistry} Ant task.
35  * <p>
36  * An earlier version of this suite built using the real hivemodule.xml (in src/META-INF) but that
37  * caused issues because it was constantly changing, so we use a copy
38  * (src/test-data/TestConstructRegistry/master.xml).
39  * <p>
40  * These tests are *VERY* dependent on JDK version (really, on the version of the XML parser
41  * provided with the JDK). Therefore, we skip the tests for JDK 1.3.
42  *
43  * TODO: Fix the tests which are commented out
44  *
45  * @author Howard Lewis Ship
46  */

47 public class TestConstructRegistry extends FrameworkTestCase
48 {
49     private static final boolean JDK1_3 = System.getProperty("java.version").startsWith("1.3.");
50
51     protected Project _project = new Project();
52
53     protected ConstructRegistry create()
54     {
55         Target t = new Target();
56
57         ConstructRegistry result = new ConstructRegistry();
58         result.setProject(_project);
59         result.setOwningTarget(t);
60         result.setTaskName("constructRegistry");
61
62         return result;
63     }
64
65     public void testNoFile() throws Exception JavaDoc
66     {
67         ConstructRegistry cr = create();
68
69         try
70         {
71             cr.execute();
72             unreachable();
73         }
74         catch (BuildException ex)
75         {
76             assertExceptionSubstring(ex, "You must specify an output file");
77         }
78     }
79
80     public void testNoDescriptors() throws Exception JavaDoc
81     {
82         ConstructRegistry cr = create();
83
84         File JavaDoc f = File.createTempFile("testNoDescriptors-", ".xml");
85
86         cr.setOutput(f);
87
88         assertSame(f, cr.getOutput());
89
90         try
91         {
92             cr.execute();
93             unreachable();
94         }
95         catch (BuildException ex)
96         {
97             assertExceptionSubstring(ex, "You must specify a set of modules");
98         }
99
100         f.delete();
101     }
102
103     public void _testBasic() throws Exception JavaDoc
104     {
105         if (JDK1_3)
106             return;
107
108         ConstructRegistry cr = create();
109
110         Path p = cr.createModules();
111
112         p.createPath().setLocation(
113                 new File JavaDoc(getFrameworkPath("/test-data/TestConstructRegistry/master.xml")));
114         p.createPath().setLocation(
115                 new File JavaDoc(getFrameworkPath("/test-data/TestConstructRegistry/Symbols.xml")));
116
117         File JavaDoc output = File.createTempFile("testBasic-", ".xml");
118
119         // Delete the file, to force the task to re-create it.
120

121         output.delete();
122
123         output.deleteOnExit();
124
125         cr.setOutput(output);
126
127         cr.execute();
128
129         compare(
130                 output,
131                 getFrameworkPath("/test-data/TestConstructRegistry/testBasic.xml.master"));
132     }
133
134     public void _testLocalRefs() throws Exception JavaDoc
135     {
136         if (JDK1_3)
137             return;
138
139         ConstructRegistry cr = create();
140
141         Path p = cr.createModules();
142
143         p.createPath().setLocation(
144                 new File JavaDoc(getFrameworkPath("/test-data/TestConstructRegistry/LocalRefs.xml")));
145         
146         File JavaDoc output = File.createTempFile("testLocalRefs-", ".xml");
147
148         // Delete the file, to force the task to re-create it.
149

150         output.delete();
151
152         output.deleteOnExit();
153
154         cr.setOutput(output);
155
156         cr.execute();
157
158         compare(
159                 output,
160                 getFrameworkPath("/test-data/TestConstructRegistry/testLocalRefs.xml.master"));
161     }
162
163     public void _testUptoDate() throws Exception JavaDoc
164     {
165         if (JDK1_3)
166             return;
167
168         ConstructRegistry cr = create();
169
170         Path p = cr.createModules();
171
172         p.createPath().setLocation(
173                 new File JavaDoc(getFrameworkPath("/test-data/TestConstructRegistry/master.xml")));
174         p.createPath().setLocation(
175                 new File JavaDoc(getFrameworkPath("/test-data/TestConstructRegistry/Symbols.xml")));
176
177         File JavaDoc output = File.createTempFile("testUptoDate-", ".xml");
178
179         // Delete the file, to force the task to re-create it.
180

181         output.delete();
182
183         output.deleteOnExit();
184
185         cr.setOutput(output);
186
187         cr.execute();
188
189         compare(
190                 output,
191                 getFrameworkPath("/test-data/TestConstructRegistry/testUptoDate.xml.master"));
192
193         long stamp = output.lastModified();
194
195         cr.execute();
196
197         assertEquals(stamp, output.lastModified());
198     }
199
200     public void _testJars() throws Exception JavaDoc
201     {
202         if (JDK1_3)
203             return;
204         
205         ConstructRegistry cr = create();
206
207         Path p = cr.createModules();
208
209         p.createPath().setLocation(
210                 new File JavaDoc(getFrameworkPath("/test-data/TestConstructRegistry/master.xml")));
211         p.createPath().setLocation(
212                 new File JavaDoc(getFrameworkPath("/test-data/TestConstructRegistry/empty.jar")));
213         p.createPath().setLocation(
214                 new File JavaDoc(getFrameworkPath("/test-data/TestConstructRegistry/module.jar")));
215
216
217         File JavaDoc output = File.createTempFile("testJars-", ".xml");
218
219         output.delete();
220
221         output.deleteOnExit();
222
223         cr.setOutput(output);
224
225         cr.execute();
226
227         compare(output, getFrameworkPath("/test-data/TestConstructRegistry/testJars.xml.master"));
228     }
229
230     protected void compare(File JavaDoc actual, String JavaDoc expectedPath) throws Exception JavaDoc
231     {
232         String JavaDoc expectedContent = readFile(new File JavaDoc(expectedPath));
233         String JavaDoc actualContent = readFile(actual);
234
235         assertEquals(expectedContent, actualContent);
236     }
237
238     protected String JavaDoc readFile(File JavaDoc f) throws Exception JavaDoc
239     {
240         InputStream JavaDoc in = new FileInputStream JavaDoc(f);
241         BufferedInputStream JavaDoc bin = new BufferedInputStream JavaDoc(in);
242         InputStreamReader JavaDoc reader = new InputStreamReader JavaDoc(bin);
243         BufferedReader JavaDoc br = new BufferedReader JavaDoc(reader);
244         LineNumberReader JavaDoc r = new LineNumberReader JavaDoc(br);
245
246         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
247
248         while (true)
249         {
250             String JavaDoc line = r.readLine();
251
252             if (line == null)
253                 break;
254
255             buffer.append(line);
256             buffer.append("\n");
257         }
258
259         r.close();
260
261         return buffer.toString();
262     }
263
264 }
Popular Tags