]> source.dussan.org Git - archiva.git/blob
c882d8816824e5ef7f76b1edae0ecd037031b69d
[archiva.git] /
1 package org.apache.archiva;
2
3 import com.google.common.io.Files;
4 import org.apache.archiva.admin.model.beans.RemoteRepository;
5 import org.apache.archiva.remotedownload.AbstractDownloadTest;
6 import org.apache.archiva.rest.api.services.RemoteRepositoriesService;
7 import org.apache.catalina.startup.Tomcat;
8 import org.apache.commons.io.FileUtils;
9 import org.apache.cxf.jaxrs.client.WebClient;
10 import org.eclipse.jetty.server.Handler;
11 import org.eclipse.jetty.server.Server;
12 import org.eclipse.jetty.server.handler.ContextHandler;
13 import org.eclipse.jetty.server.handler.ContextHandlerCollection;
14 import org.eclipse.jetty.server.handler.DefaultHandler;
15 import org.eclipse.jetty.server.handler.HandlerList;
16 import org.eclipse.jetty.server.handler.ResourceHandler;
17 import org.eclipse.jetty.servlet.DefaultServlet;
18 import org.eclipse.jetty.servlet.ServletContextHandler;
19 import org.eclipse.jetty.servlet.ServletHolder;
20 import org.junit.AfterClass;
21 import org.junit.BeforeClass;
22 import org.junit.Test;
23
24 import java.io.File;
25
26 /**
27  * @author Olivier Lamy
28  */
29 public class RemoteRepositoryConnectivityCheckTest
30     extends AbstractDownloadTest
31 {
32
33     @BeforeClass
34     public static void setAppServerBase()
35     {
36         previousAppServerBase = System.getProperty( "appserver.base" );
37         System.setProperty( "appserver.base", "target/" + RemoteRepositoryConnectivityCheckTest.class.getName() );
38     }
39
40
41     @AfterClass
42     public static void resetAppServerBase()
43     {
44         System.setProperty( "appserver.base", previousAppServerBase );
45     }
46
47     protected String getSpringConfigLocation()
48     {
49         return "classpath*:META-INF/spring-context.xml classpath*:spring-context-test-common.xml classpath*:spring-context-artifacts-download.xml";
50     }
51
52     @Test
53     public void checkRemoteConnectivity()
54         throws Exception
55     {
56
57         Server repoServer =
58             buildStaticServer( new File( System.getProperty( "basedir" ) + "/src/test/repositories/test-repo" ) );
59         repoServer.start();
60
61         RemoteRepositoriesService service = getRemoteRepositoriesService();
62
63         WebClient.client( service ).header( "Authorization", authorizationHeader );
64
65         try
66         {
67
68             int repoServerPort = repoServer.getConnectors()[0].getLocalPort();
69
70             RemoteRepository repo = getRemoteRepository();
71
72             repo.setUrl( "http://localhost:" + repoServerPort );
73
74             service.addRemoteRepository( repo );
75
76             service.checkRemoteConnectivity( repo.getId() );
77         }
78         finally
79         {
80             service.deleteRemoteRepository( "id-new" );
81             repoServer.stop();
82         }
83     }
84
85     @Test
86     public void checkRemoteConnectivityEmptyRemote()
87         throws Exception
88     {
89
90         File tmpDir = Files.createTempDir();
91         Server repoServer = buildStaticServer( tmpDir );
92         repoServer.start();
93
94         RemoteRepositoriesService service = getRemoteRepositoriesService();
95
96         WebClient.client( service ).header( "Authorization", authorizationHeader );
97
98         try
99         {
100
101             int repoServerPort = repoServer.getConnectors()[0].getLocalPort();
102
103             RemoteRepository repo = getRemoteRepository();
104
105             repo.setUrl( "http://localhost:" + repoServerPort );
106
107             service.addRemoteRepository( repo );
108
109             service.checkRemoteConnectivity( repo.getId() );
110         }
111         finally
112         {
113             service.deleteRemoteRepository( "id-new" );
114             FileUtils.deleteQuietly( tmpDir );
115             repoServer.stop();
116         }
117     }
118
119     protected Server buildStaticServer( File path )
120     {
121         Server repoServer = new Server( 0 );
122
123         ResourceHandler resourceHandler = new ResourceHandler();
124         resourceHandler.setDirectoriesListed( true );
125         resourceHandler.setWelcomeFiles( new String[]{ "index.html" } );
126         resourceHandler.setResourceBase( path.getAbsolutePath() );
127
128         HandlerList handlers = new HandlerList();
129         handlers.setHandlers( new Handler[]{ resourceHandler, new DefaultHandler() } );
130         repoServer.setHandler( handlers );
131
132         return repoServer;
133     }
134
135
136     RemoteRepository getRemoteRepository()
137     {
138         return new RemoteRepository( "id-new", "new one", "http://foo.com", "default", "foo", "foopassword", 120,
139                                      "cool repo" );
140     }
141
142 }