1 package org.apache.maven.archiva.web.repository;
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.meterware.httpunit.WebConversation;
23 import com.meterware.httpunit.WebResponse;
25 import org.apache.commons.io.FileUtils;
26 import org.apache.maven.archiva.configuration.ProxyConnectorConfiguration;
27 import org.apache.maven.archiva.configuration.RemoteRepositoryConfiguration;
28 import org.apache.maven.archiva.policies.CachedFailuresPolicy;
29 import org.apache.maven.archiva.policies.ChecksumPolicy;
30 import org.apache.maven.archiva.policies.ReleasesPolicy;
31 import org.apache.maven.archiva.policies.SnapshotsPolicy;
32 import org.mortbay.jetty.Connector;
33 import org.mortbay.jetty.Server;
34 import org.mortbay.jetty.bio.SocketConnector;
35 import org.mortbay.jetty.handler.ContextHandler;
36 import org.mortbay.jetty.handler.ContextHandlerCollection;
37 import org.mortbay.jetty.servlet.DefaultServlet;
38 import org.mortbay.jetty.servlet.ServletHandler;
43 * AbstractRepositoryServletProxiedTestCase
45 * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
48 public abstract class AbstractRepositoryServletProxiedTestCase
49 extends AbstractRepositoryServletTestCase
57 public String context;
63 public RemoteRepositoryConfiguration config;
66 protected static final long ONE_SECOND = ( 1000 * 60 );
68 protected static final long ONE_MINUTE = ( ONE_SECOND * 60 );
70 protected static final long ONE_HOUR = ( ONE_MINUTE * 60 );
72 protected static final long ONE_DAY = ( ONE_HOUR * 24 );
74 protected static final long OVER_ONE_HOUR = ( ONE_HOUR + ONE_MINUTE );
76 protected static final long OVER_ONE_DAY = ( ONE_DAY + ONE_HOUR );
78 protected static final long OLDER = ( -1 );
80 protected static final long NEWER = 0;
82 protected static final int EXPECT_MANAGED_CONTENTS = 1;
84 protected static final int EXPECT_REMOTE_CONTENTS = 2;
86 protected static final int EXPECT_NOT_FOUND = 3;
88 protected static final boolean HAS_MANAGED_COPY = true;
90 protected static final boolean NO_MANAGED_COPY = false;
92 protected RemoteRepoInfo remoteCentral;
94 protected RemoteRepoInfo remoteSnapshots;
97 protected void setUp()
103 public RemoteRepoInfo createSnapshotsRepo()
106 RemoteRepoInfo snapshots = new RemoteRepoInfo();
107 snapshots.id = "snapshots";
108 snapshots.context = "/snapshots";
109 snapshots.root = getTestFile( "target/remote-repos/snapshots/" );
111 // Remove exising root contents.
112 if ( snapshots.root.exists() )
114 FileUtils.deleteDirectory( snapshots.root );
117 // Establish root directory.
118 if ( !snapshots.root.exists() )
120 snapshots.root.mkdirs();
123 snapshots.server = new Server();
124 ContextHandlerCollection contexts = new ContextHandlerCollection();
125 snapshots.server.setHandler( contexts );
127 SocketConnector connector = new SocketConnector();
128 connector.setPort( 0 ); // 0 means, choose and empty port. (we'll find out which, later)
130 snapshots.server.setConnectors( new Connector[] { connector } );
132 ContextHandler context = new ContextHandler();
133 context.setContextPath( snapshots.context );
134 context.setAttribute( "dirAllowed", true );
135 context.setAttribute( "maxCacheSize", 0 );
136 context.setResourceBase( snapshots.root.getAbsolutePath() );
137 ServletHandler servlet = new ServletHandler();
138 servlet.addServletWithMapping( DefaultServlet.class.getName(), "/" );
139 context.setHandler( servlet );
140 contexts.addHandler( context );
142 snapshots.server.start();
144 int port = connector.getLocalPort();
145 snapshots.url = "http://localhost:" + port + snapshots.context;
146 System.out.println( "Snapshot HTTP Server started on " + snapshots.url );
148 snapshots.config = createRemoteRepository( snapshots.id, "Testable [" + snapshots.id + "] Remote Repo",
154 private void assertServerSetupCorrectly( RemoteRepoInfo remoteRepo )
157 WebConversation wc = new WebConversation();
158 WebResponse response = wc.getResponse( remoteRepo.url );
159 assertResponseOK( response );
162 private RemoteRepoInfo createCentralRepo()
165 RemoteRepoInfo central = new RemoteRepoInfo();
166 central.id = "central";
167 central.context = "/central";
168 central.root = getTestFile( "target/remote-repos/central/" );
170 // Remove exising root contents.
171 if ( central.root.exists() )
173 FileUtils.deleteDirectory( central.root );
176 // Establish root directory.
177 if ( !central.root.exists() )
179 central.root.mkdirs();
182 central.server = new Server();
183 ContextHandlerCollection contexts = new ContextHandlerCollection();
184 central.server.setHandler( contexts );
186 SocketConnector connector = new SocketConnector();
187 connector.setPort( 0 ); // 0 means, choose and empty port. (we'll find out which, later)
189 central.server.setConnectors( new Connector[] { connector } );
191 ContextHandler context = new ContextHandler();
192 context.setContextPath( central.context );
193 context.setResourceBase( central.root.getAbsolutePath() );
194 context.setAttribute( "dirAllowed", true );
195 context.setAttribute( "maxCacheSize", 0 );
196 ServletHandler servlet = new ServletHandler();
197 servlet.addServletWithMapping( DefaultServlet.class.getName(), "/" );
198 context.setHandler( servlet );
199 contexts.addHandler( context );
201 central.server.start();
203 int port = connector.getLocalPort();
204 central.url = "http://localhost:" + port + central.context;
205 System.out.println( "Central HTTP Server started on " + central.url );
207 central.config = createRemoteRepository( central.id, "Testable [" + central.id + "] Remote Repo", central.url );
212 private void setupConnector( String repoId, RemoteRepoInfo remoteRepo, String releasesPolicy, String snapshotsPolicy )
214 ProxyConnectorConfiguration connector = new ProxyConnectorConfiguration();
215 connector.setSourceRepoId( repoId );
216 connector.setTargetRepoId( remoteRepo.id );
217 connector.addPolicy( ProxyConnectorConfiguration.POLICY_RELEASES, releasesPolicy );
218 connector.addPolicy( ProxyConnectorConfiguration.POLICY_SNAPSHOTS, snapshotsPolicy );
219 connector.addPolicy( ProxyConnectorConfiguration.POLICY_CHECKSUM, ChecksumPolicy.IGNORED );
220 connector.addPolicy( ProxyConnectorConfiguration.POLICY_CACHE_FAILURES, CachedFailuresPolicy.IGNORED );
222 archivaConfiguration.getConfiguration().addProxyConnector( connector );
225 private void shutdownServer( RemoteRepoInfo remoteRepo )
227 if ( remoteRepo != null )
229 if ( remoteRepo.server != null )
231 if ( remoteRepo.server.isRunning() )
235 remoteRepo.server.stop();
236 // int graceful = remoteRepo.server.getGracefulShutdown();
237 // System.out.println( "server set to graceful shutdown: " + graceful );
238 // remoteRepo = null;
240 catch ( Exception e )
242 e.printStackTrace( System.err );
249 protected File populateRepo( RemoteRepoInfo remoteRepo, String path, String contents )
252 File destFile = new File( remoteRepo.root, path );
253 destFile.getParentFile().mkdirs();
254 FileUtils.writeStringToFile( destFile, contents, null );
258 protected void setupCentralRemoteRepo()
261 remoteCentral = createCentralRepo();
263 assertServerSetupCorrectly( remoteCentral );
264 archivaConfiguration.getConfiguration().addRemoteRepository( remoteCentral.config );
265 setupCleanRepo( remoteCentral.root );
268 protected void setupConnector( String repoId, RemoteRepoInfo remoteRepo )
270 setupConnector( repoId, remoteRepo, ReleasesPolicy.IGNORED, SnapshotsPolicy.IGNORED );
273 protected void setupReleaseConnector( String managedRepoId, RemoteRepoInfo remoteRepo, String releasePolicy )
275 setupConnector( managedRepoId, remoteRepo, releasePolicy, SnapshotsPolicy.IGNORED );
278 protected void setupSnapshotConnector( String managedRepoId, RemoteRepoInfo remoteRepo, String snapshotsPolicy )
280 setupConnector( managedRepoId, remoteRepo, ReleasesPolicy.IGNORED, snapshotsPolicy );
283 protected void setupSnapshotsRemoteRepo()
286 remoteSnapshots = createSnapshotsRepo();
288 assertServerSetupCorrectly( remoteSnapshots );
289 archivaConfiguration.getConfiguration().addRemoteRepository( remoteSnapshots.config );
290 setupCleanRepo( remoteSnapshots.root );
294 protected void tearDown()
297 shutdownServer( remoteCentral );
298 shutdownServer( remoteSnapshots );