KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > repository > main > DefaultInitialContextTest


1 /*
2  * Copyright 2004 Apache Software Foundation
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12  * implied.
13  *
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package org.apache.avalon.repository.main ;
19
20 import java.io.File JavaDoc;
21 import java.net.URL JavaDoc;
22 import java.util.Map JavaDoc;
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 /**
38  *
39  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
40  * @version $Revision: 1.9 $
41  */

42 public class DefaultInitialContextTest extends TestCase
43 {
44     private static final String JavaDoc KEY = "test";
45
46     private static final File JavaDoc BASEDIR =
47       new File JavaDoc( System.getProperty( "basedir" ) );
48
49     /**
50      * Constructor for DefaultInitialContextTest.
51      * @param name the test name
52      */

53     public DefaultInitialContextTest( String JavaDoc name )
54     {
55         super( name );
56     }
57
58     public void testRepositoryBootstrap() throws Exception JavaDoc
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 JavaDoc[] defaults = getDefaultHosts();
73         String JavaDoc[] 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 JavaDoc url = repository.getResource( artifact );
92         assertNotNull( "url", url );
93     }
94
95     private static File JavaDoc getMavenRepositoryDirectory()
96     {
97         return new File JavaDoc( getMavenHomeDirectory(), "repository" );
98     }
99
100     private static File JavaDoc getMavenHomeDirectory()
101     {
102         return new File JavaDoc( getMavenHome() );
103     }
104
105     private static String JavaDoc getMavenHome()
106     {
107         try
108         {
109             String JavaDoc 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 JavaDoc e )
119         {
120             final String JavaDoc error =
121               "Internal error while attempting to access environment.";
122             final String JavaDoc message =
123               ExceptionHelper.packException( error, e, true );
124             throw new RuntimeException JavaDoc( message );
125         }
126     }
127
128     private static String JavaDoc[] getDefaultHosts()
129     {
130         return new String JavaDoc[]{
131           "http://www.dpml.net/",
132           "http://www.ibiblio.org/maven/"
133         };
134     }
135 }
136
Popular Tags