1 17 18 package org.apache.avalon.repository.main ; 19 20 import java.io.File ; 21 import java.net.URL ; 22 import java.util.Map ; 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.RepositoryException; 29 import org.apache.avalon.repository.provider.Factory; 30 import org.apache.avalon.repository.provider.InitialContext; 31 import org.apache.avalon.repository.provider.InitialContextFactory; 32 import org.apache.avalon.repository.main.DefaultInitialContextFactory; 33 34 import org.apache.avalon.util.env.Env; 35 import org.apache.avalon.util.exception.ExceptionHelper; 36 37 42 public class DefaultInitialContextTest extends TestCase 43 { 44 private static final String KEY = "test"; 45 46 private static final File BASEDIR = 47 new File ( System.getProperty( "basedir" ) ); 48 49 53 public DefaultInitialContextTest( String name ) 54 { 55 super( name ); 56 } 57 58 public void testRepositoryBootstrap() throws Exception 59 { 60 DefaultInitialContextFactory factory = 61 new DefaultInitialContextFactory( KEY, BASEDIR ); 62 factory.setCacheDirectory( getMavenRepositoryDirectory() ); 63 factory.setHosts( getDefaultHosts() ); 64 65 InitialContext context = factory.createInitialContext(); 66 67 assertEquals( 68 "cache", 69 context.getInitialCacheDirectory(), 70 getMavenRepositoryDirectory() ); 71 72 String [] defaults = getDefaultHosts(); 73 String [] hosts = context.getInitialHosts(); 74 assertNotNull( "hosts", hosts ); 75 assertEquals( "hosts count", defaults.length, hosts.length ); 76 77 for( int i=0; i<defaults.length; i++ ) 78 { 79 assertEquals( 80 "host", defaults[i], hosts[i] ); 81 } 82 83 Factory initialFactory = context.getInitialFactory(); 84 assertNotNull( initialFactory ); 85 86 Repository repository = (Repository) context.getRepository() ; 87 assertNotNull( repository ) ; 88 89 Artifact artifact = Artifact.createArtifact( 90 "avalon-framework", "avalon-framework-api", "4.1.5" ); 91 URL url = repository.getResource( artifact ); 92 assertNotNull( "url", url ); 93 } 94 95 private static File getMavenRepositoryDirectory() 96 { 97 return new File ( getMavenHomeDirectory(), "repository" ); 98 } 99 100 private static File getMavenHomeDirectory() 101 { 102 return new File ( getMavenHome() ); 103 } 104 105 private static String getMavenHome() 106 { 107 try 108 { 109 String local = 110 System.getProperty( 111 "maven.home.local", 112 Env.getEnvVariable( "MAVEN_HOME_LOCAL" ) ); 113 if( null != local ) return local; 114 115 return System.getProperty( "user.home" ) + File.separator + ".maven"; 116 117 } 118 catch( Throwable e ) 119 { 120 final String error = 121 "Internal error while attempting to access environment."; 122 final String message = 123 ExceptionHelper.packException( error, e, true ); 124 throw new RuntimeException ( message ); 125 } 126 } 127 128 private static String [] getDefaultHosts() 129 { 130 return new String []{ 131 "http://www.dpml.net/", 132 "http://www.ibiblio.org/maven/" 133 }; 134 } 135 } 136 | Popular Tags |