1 package org.apache.archiva.remotedownload;
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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
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;
47 * @author Olivier Lamy
49 public class RemoteRepositoryConnectivityCheckTest
50 extends AbstractDownloadTest
54 public static void setAppServerBase()
56 previousAppServerBase = System.getProperty( "appserver.base" );
57 System.setProperty( "appserver.base", "target/" + RemoteRepositoryConnectivityCheckTest.class.getName() );
62 public static void resetAppServerBase()
64 System.setProperty( "appserver.base", previousAppServerBase );
67 protected String getSpringConfigLocation()
69 return "classpath*:META-INF/spring-context.xml classpath*:spring-context-test-common.xml classpath*:spring-context-artifacts-download.xml";
73 public void checkRemoteConnectivity()
78 buildStaticServer( new File( System.getProperty( "basedir" ) + "/src/test/repositories/test-repo" ) );
81 RemoteRepositoriesService service = getRemoteRepositoriesService();
83 WebClient.client( service ).header( "Authorization", authorizationHeader );
88 int repoServerPort = repoServer.getConnectors()[0].getLocalPort();
90 RemoteRepository repo = getRemoteRepository();
92 repo.setUrl( "http://localhost:" + repoServerPort );
94 service.addRemoteRepository( repo );
96 Assertions.assertThat( service.checkRemoteConnectivity( repo.getId() ) ).isTrue();
100 service.deleteRemoteRepository( "id-new" );
106 public void checkRemoteConnectivityEmptyRemote()
110 File tmpDir = Files.createTempDir();
111 Server repoServer = buildStaticServer( tmpDir );
114 RemoteRepositoriesService service = getRemoteRepositoriesService();
116 WebClient.client( service ).header( "Authorization", authorizationHeader );
121 int repoServerPort = repoServer.getConnectors()[0].getLocalPort();
123 RemoteRepository repo = getRemoteRepository();
125 repo.setUrl( "http://localhost:" + repoServerPort );
127 service.addRemoteRepository( repo );
129 Assertions.assertThat( service.checkRemoteConnectivity( repo.getId() ) ).isTrue();
133 service.deleteRemoteRepository( "id-new" );
134 FileUtils.deleteQuietly( tmpDir );
140 public void checkRemoteConnectivityFail()
144 RemoteRepositoriesService service = getRemoteRepositoriesService();
146 WebClient.client( service ).header( "Authorization", authorizationHeader );
151 RemoteRepository repo = getRemoteRepository();
153 repo.setUrl( "http://localhost:8956" );
155 service.addRemoteRepository( repo );
157 Assertions.assertThat( service.checkRemoteConnectivity( repo.getId() ) ).isFalse();
161 service.deleteRemoteRepository( "id-new" );
166 protected Server buildStaticServer( File path )
168 Server repoServer = new Server( 0 );
170 ResourceHandler resourceHandler = new ResourceHandler();
171 resourceHandler.setDirectoriesListed( true );
172 resourceHandler.setWelcomeFiles( new String[]{ "index.html" } );
173 resourceHandler.setResourceBase( path.getAbsolutePath() );
175 HandlerList handlers = new HandlerList();
176 handlers.setHandlers( new Handler[]{ resourceHandler, new DefaultHandler() } );
177 repoServer.setHandler( handlers );
183 RemoteRepository getRemoteRepository()
185 return new RemoteRepository( "id-new", "new one", "http://foo.com", "default", "foo", "foopassword", 120,