1 16 17 package org.apache.avalon.repository.main; 18 19 import java.util.Map ; 20 import java.util.Properties ; 21 import java.io.File ; 22 import java.io.IOException ; 23 24 import junit.framework.TestCase ; 25 26 import org.apache.avalon.repository.Artifact; 27 import org.apache.avalon.repository.Repository; 28 import org.apache.avalon.repository.provider.Factory; 29 import org.apache.avalon.repository.provider.Builder; 30 import org.apache.avalon.repository.provider.InitialContext; 31 import org.apache.avalon.repository.provider.InitialContextFactory; 32 import org.apache.avalon.util.exception.ExceptionHelper; 33 import org.apache.avalon.util.env.Env; 34 35 import org.apache.avalon.framework.configuration.Configuration; 36 import org.apache.avalon.framework.configuration.DefaultConfigurationBuilder; 37 38 44 public class DefaultInitialContextFactoryTestCase extends TestCase 45 { 46 private static final String KEY = "merlin"; 47 48 private static final File BASEDIR = 49 new File ( System.getProperty( "basedir" ) ); 50 51 public void testInitialContextCreation() throws Exception 52 { 53 DefaultInitialContextFactory factory = 54 new DefaultInitialContextFactory( KEY, BASEDIR ); 55 56 65 System.out.println( "" ); 66 System.out.println( "InitialContextFactory" ); 67 System.out.println( "---------------------" ); 68 System.out.println( " key: " + factory.getApplicationKey() ); 69 System.out.println( " home: " + factory.getHomeDirectory() ); 70 System.out.println( " cache: " + factory.getCacheDirectory() ); 71 System.out.println( " work: " + factory.getWorkingDirectory() ); 72 System.out.println( " impl: " + factory.getImplementation() ); 73 String [] hosts = factory.getHosts(); 74 for( int i=0; i<hosts.length; i++ ) 75 { 76 System.out.println( 77 " host (" + (i+1) + "): " 78 + hosts[i] ); 79 } 80 System.out.println( "" ); 81 82 88 93 File repo = getMavenRepositoryDirectory(); 94 factory.setCacheDirectory( repo ); 95 96 101 Artifact[] artifacts = 102 getArtifactsToRegister( "src/test/conf/system.xml" ); 103 factory.setFactoryArtifacts( artifacts ); 104 105 112 InitialContext context = factory.createInitialContext(); 113 114 120 System.out.println( "InitialContext" ); 121 System.out.println( "--------------" ); 122 System.out.println( " work: " + context.getInitialWorkingDirectory() ); 123 System.out.println( " cache: " + context.getInitialCacheDirectory() ); 124 hosts = context.getInitialHosts(); 125 for( int i=0; i<hosts.length; i++ ) 126 { 127 System.out.println( 128 " host (" + (i+1) + "): " 129 + hosts[i] ); 130 } 131 System.out.println( "" ); 132 133 139 System.out.println( "Usage Example" ); 140 System.out.println( "-------------" ); 141 String key = Repository.class.getName(); 142 System.out.println( " key: " + key ); 143 Artifact[] candidates = 144 context.getRepository().getCandidates( Repository.class ); 145 for( int i=0; i<candidates.length; i++ ) 146 { 147 System.out.println( " artifact: " + candidates[i] ); 148 } 149 150 157 if( candidates.length > 0 ) 158 { 159 Builder builder = context.newBuilder( candidates[0] ); 160 Factory exampleFactory = builder.getFactory(); 161 Map criteria = exampleFactory.createDefaultCriteria(); 162 Repository exampleRepository = (Repository) exampleFactory.create( criteria ); 163 System.out.println( " instance: " + exampleRepository ); 164 System.out.println( "" ); 165 } 166 } 167 168 private Artifact[] getArtifactsToRegister( String path ) throws Exception 169 { 170 Configuration config = 171 getConfiguration( new File ( BASEDIR, path ) ); 172 Configuration[] children = 173 config.getChildren( "artifact" ); 174 Artifact[] artifacts = new Artifact[ children.length ]; 175 for( int i=0; i<children.length; i++ ) 176 { 177 Configuration child = children[i]; 178 String spec = child.getAttribute( "spec" ); 179 Artifact artifact = Artifact.createArtifact( "artifact:" + spec ); 180 artifacts[i] = artifact; 181 } 182 return artifacts; 183 } 184 185 private static File getMavenRepositoryDirectory() 186 { 187 return new File ( getMavenHomeDirectory(), "repository" ); 188 } 189 190 private static File getMavenHomeDirectory() 191 { 192 return new File ( getMavenHome() ); 193 } 194 195 private static String getMavenHome() 196 { 197 try 198 { 199 String local = 200 System.getProperty( 201 "maven.home.local", 202 Env.getEnvVariable( "MAVEN_HOME_LOCAL" ) ); 203 if( null != local ) return local; 204 205 return System.getProperty( "user.home" ) + File.separator + ".maven"; 206 207 } 208 catch( IOException e ) 209 { 210 final String error = 211 "Internal error while attempting to access environment."; 212 final String message = 213 ExceptionHelper.packException( error, e, true ); 214 throw new RuntimeException ( message ); 215 } 216 } 217 218 Configuration getConfiguration( File file ) throws Exception 219 { 220 DefaultConfigurationBuilder builder = 221 new DefaultConfigurationBuilder(); 222 return builder.buildFromFile( file ); 223 } 224 } 225 | Popular Tags |