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;
96 protected RemoteRepoInfo remotePrivateSnapshots;
99 protected void setUp()
105 protected RemoteRepoInfo createServer( String id )
108 RemoteRepoInfo repo = new RemoteRepoInfo();
110 repo.context = "/" + id;
111 repo.root = getTestFile( "target/remote-repos/" + id + "/" );
113 // Remove exising root contents.
114 if ( repo.root.exists() )
116 FileUtils.deleteDirectory( repo.root );
119 // Establish root directory.
120 if ( !repo.root.exists() )
125 repo.server = new Server();
126 ContextHandlerCollection contexts = new ContextHandlerCollection();
127 repo.server.setHandler( contexts );
129 SocketConnector connector = new SocketConnector();
130 connector.setPort( 0 ); // 0 means, choose and empty port. (we'll find out which, later)
132 repo.server.setConnectors( new Connector[] { connector } );
134 ContextHandler context = new ContextHandler();
135 context.setContextPath( repo.context );
136 context.setResourceBase( repo.root.getAbsolutePath() );
137 context.setAttribute( "dirAllowed", true );
138 context.setAttribute( "maxCacheSize", 0 );
139 ServletHandler servlet = new ServletHandler();
140 servlet.addServletWithMapping( DefaultServlet.class.getName(), "/" );
141 context.setHandler( servlet );
142 contexts.addHandler( context );
146 int port = connector.getLocalPort();
147 repo.url = "http://localhost:" + port + repo.context;
148 System.out.println( "Remote HTTP Server started on " + repo.url );
150 repo.config = createRemoteRepository( repo.id, "Testable [" + repo.id + "] Remote Repo", repo.url );
155 protected void assertServerSetupCorrectly( RemoteRepoInfo remoteRepo )
158 WebConversation wc = new WebConversation();
159 WebResponse response = wc.getResponse( remoteRepo.url );
160 assertResponseOK( response );
163 private void setupConnector( String repoId, RemoteRepoInfo remoteRepo, String releasesPolicy, String snapshotsPolicy )
165 ProxyConnectorConfiguration connector = new ProxyConnectorConfiguration();
166 connector.setSourceRepoId( repoId );
167 connector.setTargetRepoId( remoteRepo.id );
168 connector.addPolicy( ProxyConnectorConfiguration.POLICY_RELEASES, releasesPolicy );
169 connector.addPolicy( ProxyConnectorConfiguration.POLICY_SNAPSHOTS, snapshotsPolicy );
170 connector.addPolicy( ProxyConnectorConfiguration.POLICY_CHECKSUM, ChecksumPolicy.IGNORED );
171 connector.addPolicy( ProxyConnectorConfiguration.POLICY_CACHE_FAILURES, CachedFailuresPolicy.IGNORED );
173 archivaConfiguration.getConfiguration().addProxyConnector( connector );
176 protected void shutdownServer( RemoteRepoInfo remoteRepo )
178 if ( remoteRepo != null )
180 if ( remoteRepo.server != null )
182 if ( remoteRepo.server.isRunning() )
186 remoteRepo.server.stop();
187 // int graceful = remoteRepo.server.getGracefulShutdown();
188 // System.out.println( "server set to graceful shutdown: " + graceful );
189 // remoteRepo = null;
191 catch ( Exception e )
193 e.printStackTrace( System.err );
200 protected File populateRepo( RemoteRepoInfo remoteRepo, String path, String contents )
203 File destFile = new File( remoteRepo.root, path );
204 destFile.getParentFile().mkdirs();
205 FileUtils.writeStringToFile( destFile, contents, null );
209 protected void setupCentralRemoteRepo()
212 remoteCentral = createServer( "central" );
214 assertServerSetupCorrectly( remoteCentral );
215 archivaConfiguration.getConfiguration().addRemoteRepository( remoteCentral.config );
216 setupCleanRepo( remoteCentral.root );
219 protected void setupConnector( String repoId, RemoteRepoInfo remoteRepo )
221 setupConnector( repoId, remoteRepo, ReleasesPolicy.IGNORED, SnapshotsPolicy.IGNORED );
224 protected void setupReleaseConnector( String managedRepoId, RemoteRepoInfo remoteRepo, String releasePolicy )
226 setupConnector( managedRepoId, remoteRepo, releasePolicy, SnapshotsPolicy.IGNORED );
229 protected void setupSnapshotConnector( String managedRepoId, RemoteRepoInfo remoteRepo, String snapshotsPolicy )
231 setupConnector( managedRepoId, remoteRepo, ReleasesPolicy.IGNORED, snapshotsPolicy );
234 protected void setupSnapshotsRemoteRepo()
237 remoteSnapshots = createServer( "snapshots" );
239 assertServerSetupCorrectly( remoteSnapshots );
240 archivaConfiguration.getConfiguration().addRemoteRepository( remoteSnapshots.config );
241 setupCleanRepo( remoteSnapshots.root );
245 protected void tearDown()
248 shutdownServer( remoteCentral );
249 shutdownServer( remoteSnapshots );