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