Source code: hivemind/test/ant/TestConstructRegistry.java
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;
20 import java.io.BufferedReader;
21 import java.io.File;
22 import java.io.FileInputStream;
23 import java.io.InputStream;
24 import java.io.InputStreamReader;
25 import java.io.LineNumberReader;
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 * @author Howard Lewis Ship
44 */
45 public class TestConstructRegistry extends FrameworkTestCase
46 {
47 private static final boolean JDK1_3 = System.getProperty("java.version").startsWith("1.3.");
48
49 protected Project _project = new Project();
50
51 protected ConstructRegistry create()
52 {
53 Target t = new Target();
54
55 ConstructRegistry result = new ConstructRegistry();
56 result.setProject(_project);
57 result.setOwningTarget(t);
58 result.setTaskName("constructRegistry");
59
60 return result;
61 }
62
63 public void testNoFile() throws Exception
64 {
65 ConstructRegistry cr = create();
66
67 try
68 {
69 cr.execute();
70 unreachable();
71 }
72 catch (BuildException ex)
73 {
74 assertExceptionSubstring(ex, "You must specify an output file");
75 }
76 }
77
78 public void testNoDescriptors() throws Exception
79 {
80 ConstructRegistry cr = create();
81
82 File f = File.createTempFile("testNoDescriptors-", ".xml");
83
84 cr.setOutput(f);
85
86 assertSame(f, cr.getOutput());
87
88 try
89 {
90 cr.execute();
91 unreachable();
92 }
93 catch (BuildException ex)
94 {
95 assertExceptionSubstring(ex, "You must specify a set of module descriptors");
96 }
97
98 f.delete();
99 }
100
101 public void testBasic() throws Exception
102 {
103 if (JDK1_3)
104 return;
105
106 ConstructRegistry cr = create();
107
108 Path p = cr.createDescriptors();
109
110 p.createPath().setLocation(
111 new File(getFrameworkPath("src/test-data/TestConstructRegistry/master.xml")));
112 p.createPath().setLocation(
113 new File(getFrameworkPath("src/test-data/TestConstructRegistry/Symbols.xml")));
114
115 File output = File.createTempFile("testBasic-", ".xml");
116
117 // Delete the file, to force the task to re-create it.
118
119 output.delete();
120
121 output.deleteOnExit();
122
123 cr.setOutput(output);
124
125 cr.execute();
126
127 compare(
128 output,
129 getFrameworkPath("src/test-data/TestConstructRegistry/testBasic.xml.master"));
130 }
131
132 public void testLocalRefs() throws Exception
133 {
134 if (JDK1_3)
135 return;
136
137 ConstructRegistry cr = create();
138
139 Path p = cr.createDescriptors();
140
141 p.createPath().setLocation(
142 new File(getFrameworkPath("src/test-data/TestConstructRegistry/LocalRefs.xml")));
143
144 File output = File.createTempFile("testLocalRefs-", ".xml");
145
146 // Delete the file, to force the task to re-create it.
147
148 output.delete();
149
150 output.deleteOnExit();
151
152 cr.setOutput(output);
153
154 cr.execute();
155
156 compare(
157 output,
158 getFrameworkPath("src/test-data/TestConstructRegistry/testLocalRefs.xml.master"));
159 }
160
161 public void testUptoDate() throws Exception
162 {
163 if (JDK1_3)
164 return;
165
166 ConstructRegistry cr = create();
167
168 Path p = cr.createDescriptors();
169
170 p.createPath().setLocation(
171 new File(getFrameworkPath("src/test-data/TestConstructRegistry/master.xml")));
172 p.createPath().setLocation(
173 new File(getFrameworkPath("src/test-data/TestConstructRegistry/Symbols.xml")));
174
175 File output = File.createTempFile("testUptoDate-", ".xml");
176
177 // Delete the file, to force the task to re-create it.
178
179 output.delete();
180
181 output.deleteOnExit();
182
183 cr.setOutput(output);
184
185 cr.execute();
186
187 compare(
188 output,
189 getFrameworkPath("src/test-data/TestConstructRegistry/testUptoDate.xml.master"));
190
191 long stamp = output.lastModified();
192
193 cr.execute();
194
195 assertEquals(stamp, output.lastModified());
196 }
197
198 public void testJars() throws Exception
199 {
200 if (JDK1_3)
201 return;
202
203 ConstructRegistry cr = create();
204
205 Path p = cr.createDescriptors();
206
207 p.createPath().setLocation(
208 new File(getFrameworkPath("src/test-data/TestConstructRegistry/master.xml")));
209 p.createPath().setLocation(
210 new File(getFrameworkPath("src/test-data/TestConstructRegistry/empty.jar")));
211 p.createPath().setLocation(
212 new File(getFrameworkPath("src/test-data/TestConstructRegistry/module.jar")));
213
214 File output = File.createTempFile("testJars-", ".xml");
215
216 output.delete();
217
218 output.deleteOnExit();
219
220 cr.setOutput(output);
221
222 cr.execute();
223
224 compare(output, getFrameworkPath("src/test-data/TestConstructRegistry/testJars.xml.master"));
225 }
226
227 protected void compare(File actual, String expectedPath) throws Exception
228 {
229 String expectedContent = readFile(new File(expectedPath));
230 String actualContent = readFile(actual);
231
232 assertEquals(expectedContent, actualContent);
233 }
234
235 protected String readFile(File f) throws Exception
236 {
237 InputStream in = new FileInputStream(f);
238 BufferedInputStream bin = new BufferedInputStream(in);
239 InputStreamReader reader = new InputStreamReader(bin);
240 BufferedReader br = new BufferedReader(reader);
241 LineNumberReader r = new LineNumberReader(br);
242
243 StringBuffer buffer = new StringBuffer();
244
245 while (true)
246 {
247 String line = r.readLine();
248
249 if (line == null)
250 break;
251
252 buffer.append(line);
253 buffer.append("\n");
254 }
255
256 r.close();
257
258 return buffer.toString();
259 }
260
261 }