1 package org.apache.maven.archiva.webdav;
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;
24 import org.apache.commons.io.FileUtils;
25 import org.apache.maven.archiva.configuration.ProxyConnectorConfiguration;
26 import org.apache.maven.archiva.configuration.RemoteRepositoryConfiguration;
27 import org.apache.maven.archiva.policies.CachedFailuresPolicy;
28 import org.apache.maven.archiva.policies.ChecksumPolicy;
29 import org.apache.maven.archiva.policies.ReleasesPolicy;
30 import org.apache.maven.archiva.policies.SnapshotsPolicy;
31 import org.junit.After;
32 import org.junit.Before;
33 import org.mortbay.jetty.Server;
34 import org.mortbay.jetty.handler.ContextHandler;
35 import org.mortbay.jetty.handler.ContextHandlerCollection;
36 import org.mortbay.jetty.servlet.DefaultServlet;
37 import org.mortbay.jetty.servlet.ServletHandler;
42 * AbstractRepositoryServletProxiedTestCase
46 public abstract class AbstractRepositoryServletProxiedTestCase
47 extends AbstractRepositoryServletTestCase
55 public String context;
61 public RemoteRepositoryConfiguration config;
64 protected static final long ONE_SECOND = ( 1000 /* milliseconds */ );
66 protected static final long ONE_MINUTE = ( ONE_SECOND * 60 );
68 protected static final long ONE_HOUR = ( ONE_MINUTE * 60 );
70 protected static final long ONE_DAY = ( ONE_HOUR * 24 );
72 protected static final long OVER_ONE_HOUR = ( ONE_HOUR + ONE_MINUTE );
74 protected static final long OVER_ONE_DAY = ( ONE_DAY + ONE_HOUR );
76 protected static final long OLDER = ( -1 );
78 protected static final long NEWER = 0;
80 protected static final int EXPECT_MANAGED_CONTENTS = 1;
82 protected static final int EXPECT_REMOTE_CONTENTS = 2;
84 protected static final int EXPECT_NOT_FOUND = 3;
86 protected static final boolean HAS_MANAGED_COPY = true;
88 protected static final boolean NO_MANAGED_COPY = false;
90 protected RemoteRepoInfo remoteCentral;
92 protected RemoteRepoInfo remoteSnapshots;
94 protected RemoteRepoInfo remotePrivateSnapshots;
103 protected RemoteRepoInfo createServer( String id )
106 RemoteRepoInfo repo = new RemoteRepoInfo();
108 repo.context = "/" + id;
109 repo.root = new File( "target/remote-repos/" + id + "/" );
111 // Remove exising root contents.
112 if ( repo.root.exists() )
114 FileUtils.deleteDirectory( repo.root );
117 // Establish root directory.
118 if ( !repo.root.exists() )
123 repo.server = new Server( 0 );
124 ContextHandlerCollection contexts = new ContextHandlerCollection();
125 repo.server.setHandler( contexts );
127 ContextHandler context = new ContextHandler();
128 context.setContextPath( repo.context );
129 context.setResourceBase( repo.root.getAbsolutePath() );
130 context.setAttribute( "dirAllowed", true );
131 context.setAttribute( "maxCacheSize", 0 );
132 ServletHandler servlet = new ServletHandler();
133 servlet.addServletWithMapping( DefaultServlet.class.getName(), "/" );
134 context.setHandler( servlet );
135 contexts.addHandler( context );
139 int port = repo.server.getConnectors()[0].getLocalPort();
140 repo.url = "http://localhost:" + port + repo.context;
141 log.info( "Remote HTTP Server started on " + repo.url );
143 repo.config = createRemoteRepository( repo.id, "Testable [" + repo.id + "] Remote Repo", repo.url );
148 protected void assertServerSetupCorrectly( RemoteRepoInfo remoteRepo )
151 WebConversation wc = new WebConversation();
152 WebResponse response = wc.getResponse( remoteRepo.url );
153 assertResponseOK( response );
156 private void setupConnector( String repoId, RemoteRepoInfo remoteRepo, String releasesPolicy,
157 String snapshotsPolicy )
159 ProxyConnectorConfiguration connector = new ProxyConnectorConfiguration();
160 connector.setSourceRepoId( repoId );
161 connector.setTargetRepoId( remoteRepo.id );
162 connector.addPolicy( ProxyConnectorConfiguration.POLICY_RELEASES, releasesPolicy );
163 connector.addPolicy( ProxyConnectorConfiguration.POLICY_SNAPSHOTS, snapshotsPolicy );
164 connector.addPolicy( ProxyConnectorConfiguration.POLICY_CHECKSUM, ChecksumPolicy.IGNORE );
165 connector.addPolicy( ProxyConnectorConfiguration.POLICY_CACHE_FAILURES, CachedFailuresPolicy.NO );
167 archivaConfiguration.getConfiguration().addProxyConnector( connector );
170 protected void shutdownServer( RemoteRepoInfo remoteRepo )
172 if ( remoteRepo != null )
174 if ( remoteRepo.server != null )
176 if ( remoteRepo.server.isRunning() )
180 remoteRepo.server.stop();
181 // int graceful = remoteRepo.server.getGracefulShutdown();
182 // System.out.println( "server set to graceful shutdown: " + graceful );
183 // remoteRepo = null;
185 catch ( Exception e )
187 e.printStackTrace( System.err );
194 protected File populateRepo( RemoteRepoInfo remoteRepo, String path, String contents )
197 File destFile = new File( remoteRepo.root, path );
198 destFile.getParentFile().mkdirs();
199 FileUtils.writeStringToFile( destFile, contents, null );
203 protected void setupCentralRemoteRepo()
206 remoteCentral = createServer( "central" );
208 assertServerSetupCorrectly( remoteCentral );
210 RemoteRepositoryConfiguration remoteRepositoryConfiguration =
211 archivaConfiguration.getConfiguration().getRemoteRepositoriesAsMap().get( remoteCentral.id );
212 if ( remoteRepositoryConfiguration != null )
214 archivaConfiguration.getConfiguration().removeRemoteRepository( remoteRepositoryConfiguration );
217 archivaConfiguration.getConfiguration().addRemoteRepository( remoteCentral.config );
218 setupCleanRepo( remoteCentral.root );
221 protected void setupConnector( String repoId, RemoteRepoInfo remoteRepo )
223 setupConnector( repoId, remoteRepo, ReleasesPolicy.ALWAYS, SnapshotsPolicy.ALWAYS );
226 protected void setupReleaseConnector( String managedRepoId, RemoteRepoInfo remoteRepo, String releasePolicy )
228 setupConnector( managedRepoId, remoteRepo, releasePolicy, SnapshotsPolicy.ALWAYS );
231 protected void setupSnapshotConnector( String managedRepoId, RemoteRepoInfo remoteRepo, String snapshotsPolicy )
233 setupConnector( managedRepoId, remoteRepo, ReleasesPolicy.ALWAYS, snapshotsPolicy );
236 protected void setupSnapshotsRemoteRepo()
239 remoteSnapshots = createServer( "snapshots" );
241 assertServerSetupCorrectly( remoteSnapshots );
242 RemoteRepositoryConfiguration remoteRepositoryConfiguration =
243 archivaConfiguration.getConfiguration().getRemoteRepositoriesAsMap().get( remoteSnapshots.id );
244 if ( remoteRepositoryConfiguration != null )
246 archivaConfiguration.getConfiguration().removeRemoteRepository( remoteRepositoryConfiguration );
248 archivaConfiguration.getConfiguration().addRemoteRepository( remoteSnapshots.config );
249 setupCleanRepo( remoteSnapshots.root );
254 public void tearDown()
257 shutdownServer( remoteCentral );
258 shutdownServer( remoteSnapshots );