1 8 package org.apache.avalon.excalibur.extension.test; 9 10 import java.io.File ; 11 import java.io.IOException ; 12 import java.io.InputStream ; 13 import java.util.ArrayList ; 14 import java.util.jar.Manifest ; 15 import junit.framework.TestCase; 16 import org.apache.avalon.excalibur.extension.DefaultPackageRepository; 17 import org.apache.avalon.excalibur.extension.Extension; 18 import org.apache.avalon.excalibur.extension.OptionalPackage; 19 import org.apache.avalon.excalibur.extension.PackageManager; 20 import org.apache.avalon.excalibur.extension.PackageRepository; 21 22 28 public class PackageRepositoryTestCase 29 extends TestCase 30 { 31 private File m_baseDirectory; 32 private File m_pathElement1; 33 private File m_pathElement2; 34 private File [] m_path; 35 36 public PackageRepositoryTestCase( String name ) 37 throws IOException 38 { 39 super( name ); 40 41 m_baseDirectory = 42 (new File ( "../../src/scratchpad/org/apache/avalon/excalibur/extension/test/" )).getCanonicalFile(); 43 44 m_pathElement1 = new File ( m_baseDirectory, "path1" ); 45 m_pathElement2 = new File ( m_baseDirectory, "path2" ); 46 m_path = new File [] { m_pathElement1, m_pathElement2 }; 47 } 48 49 public void testGoodPath() 50 throws Exception 51 { 52 final DefaultPackageRepository repository = new DefaultPackageRepository( m_path ); 53 } 54 55 public void testBadPath() 56 throws Exception 57 { 58 try 59 { 60 final File pathElement3 = new File ( m_baseDirectory, "path3" ); 61 final File [] path = new File [] { m_pathElement1, m_pathElement2, pathElement3 }; 62 final DefaultPackageRepository repository = 63 new DefaultPackageRepository( path ); 64 } 65 catch( final IllegalArgumentException iae ) 66 { 67 return; 68 } 69 70 assertTrue( "Exceptected to fail with bad path element", false ); 71 } 72 73 public void testBasicScanDependencies() 74 throws Exception 75 { 76 final PackageRepository repository = newPackagerepository(); 77 doRepositoryTest( repository ); 78 } 79 80 public void testFSScanDependencies() 81 throws Exception 82 { 83 final PackageRepository repository = new DefaultPackageRepository( m_path ); 84 doRepositoryTest( repository ); 85 } 86 87 private void doRepositoryTest( final PackageRepository repository ) 88 throws Exception 89 { 90 final PackageManager manager = new PackageManager( repository ); 91 92 final Manifest manifest2 = getManifest( "manifest-2.mf" ); 93 final Extension extension1 = Extension.getRequired( manifest2 )[ 0 ]; 94 95 final ArrayList dependencies = new ArrayList (); 96 final ArrayList unsatisfied = new ArrayList (); 97 98 manager.scanDependencies( extension1, new Extension[ 0 ], dependencies, unsatisfied ); 99 System.out.println( "dependencies: " + dependencies ); 100 101 assertEquals( "dependencies Count", 2, dependencies.size() ); 102 assertEquals( "unsatisfied Count", 0, unsatisfied.size() ); 103 104 final int size = dependencies.size(); 105 for( int i = 0; i < size; i++ ) 106 { 107 final OptionalPackage optionalPackage = (OptionalPackage)dependencies.get( i ); 108 final Extension[] extensions = optionalPackage.getAvailableExtensions(); 109 for( int j = 0; j < extensions.length; j++ ) 110 { 111 final String name = extensions[ j ].getExtensionName(); 112 if( !name.equals( "excalibur.required1" ) && 113 !name.equals( "excalibur.required2" ) ) 114 { 115 assertTrue( "Unexpected extension: " + name, false ); 116 } 117 } 118 } 119 } 120 121 private Manifest getManifest( final String name ) 122 throws Exception 123 { 124 final InputStream inputStream = getClass().getResourceAsStream( name ); 125 return new Manifest ( inputStream ); 126 } 127 128 private PackageRepository newPackagerepository() 129 throws Exception 130 { 131 final TestPackageRepository repository = new TestPackageRepository(); 132 repository.addEntry( "manifest-1.mf" ); 133 repository.addEntry( "manifest-2.mf" ); 134 repository.addEntry( "manifest-3.mf" ); 135 repository.addEntry( "manifest-4.mf" ); 136 repository.addEntry( "manifest-5.mf" ); 137 repository.addEntry( "manifest-6.mf" ); 138 repository.addEntry( "manifest-7.mf" ); 139 repository.addEntry( "manifest-8.mf" ); 140 return repository; 141 } 142 } 143 144 class TestPackageRepository 145 extends DefaultPackageRepository 146 { 147 TestPackageRepository() 148 throws Exception 149 { 150 super( new File [ 0 ] ); 151 } 152 153 void addEntry( final String manifestLocation ) 154 throws Exception 155 { 156 final InputStream inputStream = getClass().getResourceAsStream( manifestLocation ); 157 final Manifest manifest = new Manifest ( inputStream ); 158 final File file = new File ( manifestLocation ); 159 final Extension[] available = Extension.getAvailable( manifest ); 160 final Extension[] required = Extension.getRequired( manifest ); 161 162 cacheOptionalPackage( new OptionalPackage( file, available, required ) ); 163 } 164 } 165 | Popular Tags |