1 /*
2 * Copyright (C) The Spice Group. All rights reserved.
3 *
4 * This software is published under the terms of the Spice
5 * Software License version 1.1, a copy of which has been included
6 * with this distribution in the LICENSE.txt file.
7 */
8 package org.codehaus.spice.extension;
9
10 import java.io.ByteArrayInputStream;
11 import java.io.IOException;
12 import java.util.jar.Manifest;
13 import java.util.HashSet;
14 import java.util.Arrays;
15
16 import junit.framework.TestCase;
17
18 /***
19 * TestCases for Specification.
20 *
21 * @author <a href="mailto:peter@apache.org">Peter Donald</a>
22 * @version $Revision: 1.1 $ $Date: 2003/12/02 07:56:59 $
23 */
24 public class SpecificationTestCase
25 extends TestCase
26 {
27 public SpecificationTestCase( final String name )
28 {
29 super( name );
30 }
31
32 public void testSingleSpecification()
33 throws Exception
34 {
35 final String manifest =
36 "Manifest-Version: 1.0\n" +
37 "\n" +
38 "Name: org/realityforge/dve\n" +
39 "Specification-Title: org.realityforge.dve\n" +
40 "Specification-Version: 1.0.2\n" +
41 "Specification-Vendor: Peter Donald\n" +
42 "Implementation-Title: DVE vi OS3P\n" +
43 "Implementation-Vendor: Peter Donald\n" +
44 "Implementation-Version: 1.0.2Alpha\n";
45 final Specification[] specifications = getSpecifications( manifest );
46
47 assertEquals( "Count", 1, specifications.length );
48 assertEquals( "Name", "org.realityforge.dve",
49 specifications[ 0 ].getSpecificationTitle() );
50 assertEquals( "SpecVendor", "Peter Donald",
51 specifications[ 0 ].getSpecificationVendor() );
52 assertEquals( "SpecVersion", "1.0.2",
53 specifications[ 0 ].getSpecificationVersion().toString() );
54 assertEquals( "ImpVendor", "Peter Donald",
55 specifications[ 0 ].getImplementationVendor() );
56 assertEquals( "ImpTitle", "DVE vi OS3P",
57 specifications[ 0 ].getImplementationTitle() );
58 assertEquals( "ImpVersion", "1.0.2Alpha",
59 specifications[ 0 ].getImplementationVersion().toString() );
60 }
61
62 public void testSingleSpecificationWithSpacesAtEOL()
63 throws Exception
64 {
65 final String manifest =
66 "Manifest-Version: 1.0\n" +
67 "\n" +
68 "Name: org/realityforge/dve \n" +
69 "Specification-Title: org.realityforge.dve \n" +
70 "Specification-Version: 1.0.2 \n" +
71 "Specification-Vendor: Peter Donald \n" +
72 "Implementation-Title: DVE vi OS3P \n" +
73 "Implementation-Vendor: Peter Donald \n" +
74 "Implementation-Version: 1.0.2Alpha \n";
75 final Specification[] specifications = getSpecifications( manifest );
76
77 assertEquals( "Count", 1, specifications.length );
78 assertEquals( "Name", "org.realityforge.dve",
79 specifications[ 0 ].getSpecificationTitle() );
80 assertEquals( "SpecVendor", "Peter Donald",
81 specifications[ 0 ].getSpecificationVendor() );
82 assertEquals( "SpecVersion", "1.0.2",
83 specifications[ 0 ].getSpecificationVersion().toString() );
84 assertEquals( "ImpVendor", "Peter Donald",
85 specifications[ 0 ].getImplementationVendor() );
86 assertEquals( "ImpTitle", "DVE vi OS3P",
87 specifications[ 0 ].getImplementationTitle() );
88 assertEquals( "ImpVersion", "1.0.2Alpha",
89 specifications[ 0 ].getImplementationVersion().toString() );
90 }
91
92 public void testSingleSpecificationWithMissingSpecificationVersion()
93 throws Exception
94 {
95 final String manifestString =
96 "Manifest-Version: 1.0\n" +
97 "\n" +
98 "Name: org/realityforge/dve\n" +
99 "Specification-Title: org.realityforge.dve\n" +
100 "Specification-Vendor: Peter Donald\n" +
101 "Implementation-Title: DVE vi OS3P\n" +
102 "Implementation-Vendor: Peter Donald\n" +
103 "Implementation-Version: 1.0.2Alpha\n";
104 final Manifest manifest = getManifest( manifestString );
105 try
106 {
107 Specification.getSpecifications( manifest );
108 }
109 catch( final Throwable t )
110 {
111 return;
112 }
113 fail( "Missing SpecificationVersion parsed" );
114 }
115
116 public void testSingleSpecificationWithMissingSpecificationVendor()
117 throws Exception
118 {
119 final String manifestString =
120 "Manifest-Version: 1.0\n" +
121 "\n" +
122 "Name: org/realityforge/dve\n" +
123 "Specification-Title: org.realityforge.dve\n" +
124 "Specification-Version: 1.0.2\n" +
125 "Implementation-Title: DVE vi OS3P\n" +
126 "Implementation-Vendor: Peter Donald\n" +
127 "Implementation-Version: 1.0.2Alpha\n";
128 final Manifest manifest = getManifest( manifestString );
129 try
130 {
131 Specification.getSpecifications( manifest );
132 }
133 catch( final Throwable t )
134 {
135 return;
136 }
137 fail( "Missing SpecificationVendor parsed" );
138 }
139
140 public void testSingleSpecificationMissingImplementationTitle()
141 throws Exception
142 {
143 final String manifestString =
144 "Manifest-Version: 1.0\n" +
145 "\n" +
146 "Name: org/realityforge/dve\n" +
147 "Specification-Title: org.realityforge.dve\n" +
148 "Specification-Version: 1.0.2\n" +
149 "Specification-Vendor: Peter Donald\n" +
150 "Implementation-Vendor: Peter Donald\n" +
151 "Implementation-Version: 1.0.2Alpha\n";
152 final Manifest manifest = getManifest( manifestString );
153 try
154 {
155 Specification.getSpecifications( manifest );
156 }
157 catch( final Throwable t )
158 {
159 return;
160 }
161 fail( "Missing ImplementationTitle parsed" );
162 }
163
164 public void testSingleSpecificationMissingImplementationVendor()
165 throws Exception
166 {
167 final String manifestString =
168 "Manifest-Version: 1.0\n" +
169 "\n" +
170 "Name: org/realityforge/dve\n" +
171 "Specification-Title: org.realityforge.dve\n" +
172 "Specification-Version: 1.0.2\n" +
173 "Specification-Vendor: Peter Donald\n" +
174 "Implementation-Title: DVE vi OS3P\n" +
175 "Implementation-Version: 1.0.2Alpha\n";
176 final Manifest manifest = getManifest( manifestString );
177 try
178 {
179 Specification.getSpecifications( manifest );
180 }
181 catch( final Throwable t )
182 {
183 return;
184 }
185 fail( "Missing ImplementationVendor parsed" );
186 }
187
188 public void testSingleSpecificationMissingImplementationVersion()
189 throws Exception
190 {
191 final String manifestString =
192 "Manifest-Version: 1.0\n" +
193 "\n" +
194 "Name: org/realityforge/dve\n" +
195 "Specification-Title: org.realityforge.dve\n" +
196 "Specification-Version: 1.0.2\n" +
197 "Specification-Vendor: Peter Donald\n" +
198 "Implementation-Title: DVE vi OS3P\n" +
199 "Implementation-Vendor: Peter Donald\n";
200 final Manifest manifest = getManifest( manifestString );
201 try
202 {
203 Specification.getSpecifications( manifest );
204 }
205 catch( final Throwable t )
206 {
207 return;
208 }
209 fail( "Missing ImplementationVersion parsed" );
210 }
211
212 public void testSingleSpecificationWithMultipleSections()
213 throws Exception
214 {
215 final String manifestString =
216 "Manifest-Version: 1.0\n" +
217 "\n" +
218 "Name: org/realityforge/dve\n" +
219 "Specification-Title: org.realityforge.dve\n" +
220 "Specification-Version: 1.0.2\n" +
221 "Specification-Vendor: Peter Donald\n" +
222 "Implementation-Title: DVE vi OS3P\n" +
223 "Implementation-Vendor: Peter Donald\n" +
224 "Implementation-Version: 1.0.2Alpha\n" +
225 "\n" +
226 "Name: org/realityforge/dve/input\n" +
227 "Specification-Title: org.realityforge.dve\n" +
228 "Specification-Version: 1.0.2\n" +
229 "Specification-Vendor: Peter Donald\n" +
230 "Implementation-Title: DVE vi OS3P\n" +
231 "Implementation-Vendor: Peter Donald\n" +
232 "Implementation-Version: 1.0.2Alpha\n" +
233 "\n" +
234 "Name: org/realityforge/dve/sim\n" +
235 "Specification-Title: org.realityforge.dve\n" +
236 "Specification-Version: 1.0.2\n" +
237 "Specification-Vendor: Peter Donald\n" +
238 "Implementation-Title: DVE vi OS3P\n" +
239 "Implementation-Vendor: Peter Donald\n" +
240 "Implementation-Version: 1.0.2Alpha\n";
241 final Specification[] specifications = getSpecifications( manifestString );
242
243 assertEquals( "Count", 1, specifications.length );
244 final String[] sections = specifications[ 0 ].getSections();
245 assertEquals( "sections.length", 3, sections.length );
246 final HashSet set = new HashSet();
247 set.addAll( Arrays.asList( sections ) );
248 assertTrue( "sections.contains(org/realityforge/dve)", set.contains( "org/realityforge/dve" ) );
249 assertTrue( "sections.contains(org/realityforge/dve/input)", set.contains( "org/realityforge/dve/input" ) );
250 assertTrue( "sections.contains(org/realityforge/dve/sim)", set.contains( "org/realityforge/dve/sim" ) );
251 assertEquals( "Name", "org.realityforge.dve",
252 specifications[ 0 ].getSpecificationTitle() );
253 assertEquals( "SpecVendor", "Peter Donald",
254 specifications[ 0 ].getSpecificationVendor() );
255 assertEquals( "SpecVersion", "1.0.2",
256 specifications[ 0 ].getSpecificationVersion().toString() );
257 assertEquals( "ImpVendor", "Peter Donald",
258 specifications[ 0 ].getImplementationVendor() );
259 assertEquals( "ImpTitle", "DVE vi OS3P",
260 specifications[ 0 ].getImplementationTitle() );
261 assertEquals( "ImpVersion", "1.0.2Alpha",
262 specifications[ 0 ].getImplementationVersion().toString() );
263 }
264
265 public void testMultipleSpecificationWithMultipleSections()
266 throws Exception
267 {
268 final String manifestString =
269 "Manifest-Version: 1.0\n" +
270 "\n" +
271 "Name: org/realityforge/dve\n" +
272 "Specification-Title: org.realityforge.dve\n" +
273 "Specification-Version: 1.0.2\n" +
274 "Specification-Vendor: Peter Donald\n" +
275 "Implementation-Title: DVE vi OS3P\n" +
276 "Implementation-Vendor: Peter Donald\n" +
277 "Implementation-Version: 1.0.2Alpha\n" +
278 "\n" +
279 "Name: org/realityforge/dve/input\n" +
280 "Specification-Title: org.realityforge.dve\n" +
281 "Specification-Version: 1.0.2\n" +
282 "Specification-Vendor: Peter Donald\n" +
283 "Implementation-Title: DVE vi OS3P\n" +
284 "Implementation-Vendor: Peter Donald\n" +
285 "Implementation-Version: 1.0.2Alpha\n" +
286 "\n" +
287 "Name: org/realityforge/dve/sim\n" +
288 "Specification-Title: org.realityforge.dve\n" +
289 "Specification-Version: 1.0.2\n" +
290 "Specification-Vendor: Peter Donald\n" +
291 "Implementation-Title: DVE vi OS3P\n" +
292 "Implementation-Vendor: Peter Donald\n" +
293 "Implementation-Version: 1.0.2Alpha\n" +
294 "\n" +
295 "Name: com/biz/foo\n" +
296 "Specification-Title: com.biz.foo\n" +
297 "Specification-Version: 1.0.2\n" +
298 "Specification-Vendor: Peter Donald\n" +
299 "Implementation-Title: DVE vi OS3P\n" +
300 "Implementation-Vendor: Peter Donald\n" +
301 "Implementation-Version: 1.0.2Alpha\n";
302 final Specification[] specifications = getSpecifications( manifestString );
303
304 assertEquals( "Count", 2, specifications.length );
305 Specification dveSpecification;
306 Specification fooSpecification;
307 if( 3 == specifications[ 0 ].getSections().length )
308 {
309 dveSpecification = specifications[ 0 ];
310 fooSpecification = specifications[ 1 ];
311 }
312 else
313 {
314 dveSpecification = specifications[ 1 ];
315 fooSpecification = specifications[ 0 ];
316 }
317
318 final String[] sections = dveSpecification.getSections();
319 assertEquals( "sections.length", 3, sections.length );
320 assertEquals( "sections.length", 3, sections.length );
321 final HashSet set = new HashSet();
322 set.addAll( Arrays.asList( sections ) );
323 assertTrue( "sections.contains(org/realityforge/dve)", set.contains( "org/realityforge/dve" ) );
324 assertTrue( "sections.contains(org/realityforge/dve/input)", set.contains( "org/realityforge/dve/input" ) );
325 assertTrue( "sections.contains(org/realityforge/dve/sim)", set.contains( "org/realityforge/dve/sim" ) );
326 assertEquals( "Name", "org.realityforge.dve",
327 dveSpecification.getSpecificationTitle() );
328 assertEquals( "SpecVendor", "Peter Donald",
329 dveSpecification.getSpecificationVendor() );
330 assertEquals( "SpecVersion", "1.0.2",
331 dveSpecification.getSpecificationVersion().toString() );
332 assertEquals( "ImpVendor", "Peter Donald",
333 dveSpecification.getImplementationVendor() );
334 assertEquals( "ImpTitle", "DVE vi OS3P",
335 dveSpecification.getImplementationTitle() );
336 assertEquals( "ImpVersion", "1.0.2Alpha",
337 dveSpecification.getImplementationVersion().toString() );
338
339 assertEquals( "sections.length", 1, fooSpecification.getSections().length );
340 assertEquals( "sections[0]", "com/biz/foo", fooSpecification.getSections()[ 0 ] );
341 assertEquals( "Name", "com.biz.foo",
342 fooSpecification.getSpecificationTitle() );
343 assertEquals( "SpecVendor", "Peter Donald",
344 fooSpecification.getSpecificationVendor() );
345 assertEquals( "SpecVersion", "1.0.2",
346 fooSpecification.getSpecificationVersion().toString() );
347 assertEquals( "ImpVendor", "Peter Donald",
348 fooSpecification.getImplementationVendor() );
349 assertEquals( "ImpTitle", "DVE vi OS3P",
350 fooSpecification.getImplementationTitle() );
351 assertEquals( "ImpVersion", "1.0.2Alpha",
352 fooSpecification.getImplementationVersion().toString() );
353 }
354
355 public void testCompatible()
356 throws Exception
357 {
358 final String title = "org.realityforge.dve";
359 final String version = "1.0.2";
360 final String vendor = "Peter Donald";
361 final String implTitle = "DVE vi OS3P";
362 final String implVendor = "Peter Donald";
363 final String implVersion = "1.0.2Alpha";
364
365 final Specification req1 =
366 new Specification( title, version, vendor,
367 implTitle, implVersion, implVendor );
368 final Specification req2 =
369 new Specification( title, version, vendor,
370 null, null, null );
371 final Specification req3 =
372 new Specification( title, "1.0.1", vendor,
373 null, null, null );
374 final Specification req4 =
375 new Specification( title, version, null,
376 null, null, null );
377 final Specification req5 =
378 new Specification( "another title", version, vendor,
379 implTitle, implVersion, implVendor );
380
381 final Specification avail1 =
382 new Specification( title, version, vendor,
383 implTitle, implVersion, implVendor );
384 final Specification avail2 =
385 new Specification( title, version, vendor,
386 implTitle, "another version", implVendor );
387 final Specification avail3 =
388 new Specification( title, version, vendor,
389 implTitle, implVersion, "another vendor" );
390
391 assertTrue( "avail1.isCompatibleWith( req1 )", avail1.isCompatibleWith( req1 ) );
392 assertTrue( "avail1.isCompatibleWith( req2 )", avail1.isCompatibleWith( req2 ) );
393 assertTrue( "avail1.isCompatibleWith( req3 )", avail1.isCompatibleWith( req3 ) );
394 assertTrue( "avail1.isCompatibleWith( req4 )", avail1.isCompatibleWith( req4 ) );
395 assertTrue( "!avail1.isCompatibleWith( req5 )", !avail1.isCompatibleWith( req5 ) );
396
397 assertTrue( "!avail2.isCompatibleWith( req1 )", !avail2.isCompatibleWith( req1 ) );
398 assertTrue( "avail2.isCompatibleWith( req2 )", avail2.isCompatibleWith( req2 ) );
399 assertTrue( "avail2.isCompatibleWith( req3 )", avail2.isCompatibleWith( req3 ) );
400 assertTrue( "avail2.isCompatibleWith( req4 )", avail2.isCompatibleWith( req4 ) );
401 assertTrue( "!avail2.isCompatibleWith( req5 )", !avail2.isCompatibleWith( req5 ) );
402
403 assertTrue( "!avail3.isCompatibleWith( req1 )", !avail3.isCompatibleWith( req1 ) );
404 assertTrue( "avail3.isCompatibleWith( req2 )", avail3.isCompatibleWith( req2 ) );
405 assertTrue( "avail3.isCompatibleWith( req3 )", avail3.isCompatibleWith( req3 ) );
406 assertTrue( "avail3.isCompatibleWith( req4 )", avail3.isCompatibleWith( req4 ) );
407 assertTrue( "!avail3.isCompatibleWith( req5 )", !avail3.isCompatibleWith( req5 ) );
408 }
409
410 private Specification[] getSpecifications( final String input )
411 throws Exception
412 {
413 final Manifest manifest = getManifest( input );
414 return Specification.getSpecifications( manifest );
415 }
416
417 private Manifest getManifest( final String manifestString )
418 throws IOException
419 {
420 final ByteArrayInputStream stream =
421 new ByteArrayInputStream( manifestString.getBytes() );
422 return new Manifest( stream );
423 }
424 }
This page was automatically generated by Maven