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