]> source.dussan.org Git - archiva.git/blob
e53809279aac06d820563d82f5acb18359a4c316
[archiva.git] /
1 package org.apache.archiva.remotedownload;
2
3 /*
4  * Licensed to the Apache Software Foundation (ASF) under one
5  * or more contributor license agreements.  See the NOTICE file
6  * distributed with this work for additional information
7  * regarding copyright ownership.  The ASF licenses this file
8  * to you under the Apache License, Version 2.0 (the
9  * "License"); you may not use this file except in compliance
10  * with the License.  You may obtain a copy of the License at
11  *
12  *   http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing,
15  * software distributed under the License is distributed on an
16  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17  * KIND, either express or implied.  See the License for the
18  * specific language governing permissions and limitations
19  * under the License.
20  */
21
22 import com.google.common.io.Files;
23 import org.apache.archiva.admin.model.beans.RemoteRepository;
24 import org.apache.archiva.remotedownload.AbstractDownloadTest;
25 import org.apache.archiva.rest.api.services.RemoteRepositoriesService;
26 import org.apache.catalina.startup.Tomcat;
27 import org.apache.commons.io.FileUtils;
28 import org.apache.cxf.jaxrs.client.WebClient;
29 import org.eclipse.jetty.server.Handler;
30 import org.eclipse.jetty.server.Server;
31 import org.eclipse.jetty.server.handler.ContextHandler;
32 import org.eclipse.jetty.server.handler.ContextHandlerCollection;
33 import org.eclipse.jetty.server.handler.DefaultHandler;
34 import org.eclipse.jetty.server.handler.HandlerList;
35 import org.eclipse.jetty.server.handler.ResourceHandler;
36 import org.eclipse.jetty.servlet.DefaultServlet;
37 import org.eclipse.jetty.servlet.ServletContextHandler;
38 import org.eclipse.jetty.servlet.ServletHolder;
39 import org.fest.assertions.api.Assertions;
40 import org.junit.AfterClass;
41 import org.junit.BeforeClass;
42 import org.junit.Test;
43
44 import java.io.File;
45
46 /**
47  * @author Olivier Lamy
48  */
49 public class RemoteRepositoryConnectivityCheckTest
50     extends AbstractDownloadTest
51 {
52
53     @BeforeClass
54     public static void setAppServerBase()
55     {
56         previousAppServerBase = System.getProperty( "appserver.base" );
57         System.setProperty( "appserver.base", "target/" + RemoteRepositoryConnectivityCheckTest.class.getName() );
58     }
59
60
61     @AfterClass
62     public static void resetAppServerBase()
63     {
64         System.setProperty( "appserver.base", previousAppServerBase );
65     }
66
67     protected String getSpringConfigLocation()
68     {
69         return "classpath*:META-INF/spring-context.xml classpath*:spring-context-test-common.xml classpath*:spring-context-artifacts-download.xml";
70     }
71
72     @Test
73     public void checkRemoteConnectivity()
74         throws Exception
75     {
76
77         Server repoServer =
78             buildStaticServer( new File( System.getProperty( "basedir" ) + "/src/test/repositories/test-repo" ) );
79         repoServer.start();
80
81         RemoteRepositoriesService service = getRemoteRepositoriesService();
82
83         WebClient.client( service ).header( "Authorization", authorizationHeader );
84
85         try
86         {
87
88             int repoServerPort = repoServer.getConnectors()[0].getLocalPort();
89
90             RemoteRepository repo = getRemoteRepository();
91
92             repo.setUrl( "http://localhost:" + repoServerPort );
93
94             service.addRemoteRepository( repo );
95
96             Assertions.assertThat( service.checkRemoteConnectivity( repo.getId() ) ).isTrue();
97         }
98         finally
99         {
100             service.deleteRemoteRepository( "id-new" );
101             repoServer.stop();
102         }
103     }
104
105     @Test
106     public void checkRemoteConnectivityEmptyRemote()
107         throws Exception
108     {
109
110         File tmpDir = Files.createTempDir();
111         Server repoServer = buildStaticServer( tmpDir );
112         repoServer.start();
113
114         RemoteRepositoriesService service = getRemoteRepositoriesService();
115
116         WebClient.client( service ).header( "Authorization", authorizationHeader );
117
118         try
119         {
120
121             int repoServerPort = repoServer.getConnectors()[0].getLocalPort();
122
123             RemoteRepository repo = getRemoteRepository();
124
125             repo.setUrl( "http://localhost:" + repoServerPort );
126
127             service.addRemoteRepository( repo );
128
129             Assertions.assertThat( service.checkRemoteConnectivity( repo.getId() ) ).isTrue();
130         }
131         finally
132         {
133             service.deleteRemoteRepository( "id-new" );
134             FileUtils.deleteQuietly( tmpDir );
135             repoServer.stop();
136         }
137     }
138
139     @Test
140     public void checkRemoteConnectivityFail()
141         throws Exception
142     {
143
144         RemoteRepositoriesService service = getRemoteRepositoriesService();
145
146         WebClient.client( service ).header( "Authorization", authorizationHeader );
147
148         try
149         {
150
151             RemoteRepository repo = getRemoteRepository();
152
153             repo.setUrl( "http://localhost:8956" );
154
155             service.addRemoteRepository( repo );
156
157             Assertions.assertThat( service.checkRemoteConnectivity( repo.getId() ) ).isFalse();
158         }
159         finally
160         {
161             service.deleteRemoteRepository( "id-new" );
162
163         }
164     }
165
166     protected Server buildStaticServer( File path )
167     {
168         Server repoServer = new Server( 0 );
169
170         ResourceHandler resourceHandler = new ResourceHandler();
171         resourceHandler.setDirectoriesListed( true );
172         resourceHandler.setWelcomeFiles( new String[]{ "index.html" } );
173         resourceHandler.setResourceBase( path.getAbsolutePath() );
174
175         HandlerList handlers = new HandlerList();
176         handlers.setHandlers( new Handler[]{ resourceHandler, new DefaultHandler() } );
177         repoServer.setHandler( handlers );
178
179         return repoServer;
180     }
181
182
183     RemoteRepository getRemoteRepository()
184     {
185         return new RemoteRepository( "id-new", "new one", "http://foo.com", "default", "foo", "foopassword", 120,
186                                      "cool repo" );
187     }
188
189 }