1 package org.apache.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
23 import com.gargoylesoftware.htmlunit.WebRequest;
24 import com.gargoylesoftware.htmlunit.WebResponse;
25 import org.apache.archiva.configuration.ProxyConnectorConfiguration;
26 import org.apache.archiva.configuration.RemoteRepositoryConfiguration;
27 import org.apache.archiva.policies.CachedFailuresPolicy;
28 import org.apache.archiva.policies.ChecksumPolicy;
29 import org.apache.archiva.policies.ReleasesPolicy;
30 import org.apache.archiva.policies.SnapshotsPolicy;
31 import org.apache.commons.io.FileUtils;
32 import org.eclipse.jetty.server.Server;
33 import org.eclipse.jetty.server.handler.ContextHandlerCollection;
34 import org.eclipse.jetty.servlet.DefaultServlet;
35 import org.eclipse.jetty.servlet.ServletContextHandler;
36 import org.eclipse.jetty.servlet.ServletHolder;
37 import org.junit.After;
38 import org.junit.Before;
41 import java.nio.charset.Charset;
44 * AbstractRepositoryServletProxiedTestCase
47 public abstract class AbstractRepositoryServletProxiedTestCase
48 extends AbstractRepositoryServletTestCase
56 public String context;
62 public RemoteRepositoryConfiguration config;
65 protected static final long ONE_SECOND = ( 1000 /* milliseconds */ );
67 protected static final long ONE_MINUTE = ( ONE_SECOND * 60 );
69 protected static final long ONE_HOUR = ( ONE_MINUTE * 60 );
71 protected static final long ONE_DAY = ( ONE_HOUR * 24 );
73 protected static final long OVER_ONE_HOUR = ( ONE_HOUR + ONE_MINUTE );
75 protected static final long OVER_ONE_DAY = ( ONE_DAY + ONE_HOUR );
77 protected static final long OLDER = ( -1 );
79 protected static final long NEWER = 0;
81 protected static final int EXPECT_MANAGED_CONTENTS = 1;
83 protected static final int EXPECT_REMOTE_CONTENTS = 2;
85 protected static final int EXPECT_NOT_FOUND = 3;
87 protected static final boolean HAS_MANAGED_COPY = true;
89 protected static final boolean NO_MANAGED_COPY = false;
91 protected RemoteRepoInfo remoteCentral;
93 protected RemoteRepoInfo remoteSnapshots;
104 public void tearDown()
107 shutdownServer( remoteCentral );
108 shutdownServer( remoteSnapshots );
112 protected RemoteRepoInfo createServer( String id )
115 RemoteRepoInfo repo = new RemoteRepoInfo();
117 repo.context = "/" + id;
118 repo.root = new File( "target/remote-repos/" + id + "/" );
120 // Remove exising root contents.
121 if ( repo.root.exists() )
123 FileUtils.deleteDirectory( repo.root );
126 // Establish root directory.
127 if ( !repo.root.exists() )
132 repo.server = new Server( 0 );
133 ContextHandlerCollection contexts = new ContextHandlerCollection();
134 repo.server.setHandler( contexts );
136 ServletContextHandler context = new ServletContextHandler();
137 context.setContextPath( repo.context );
138 context.setResourceBase( repo.root.getAbsolutePath() );
139 context.setAttribute( "dirAllowed", true );
140 context.setAttribute( "maxCacheSize", 0 );
142 ServletHolder sh = new ServletHolder( DefaultServlet.class );
143 context.addServlet( sh, "/" );
145 contexts.addHandler( context );
149 int port = repo.server.getConnectors()[0].getLocalPort();
150 repo.url = "http://localhost:" + port + repo.context;
151 log.info( "Remote HTTP Server started on {}", repo.url );
153 repo.config = createRemoteRepository( repo.id, "Testable [" + repo.id + "] Remote Repo", repo.url );
158 protected void assertServerSetupCorrectly( RemoteRepoInfo remoteRepo )
161 //WebConversation wc = new WebConversation();
162 WebRequest request = new GetMethodWebRequest( remoteRepo.url );
163 WebResponse response = getServletUnitClient().getResponse( request );
164 assertResponseOK( response );
167 private void setupConnector( String repoId, RemoteRepoInfo remoteRepo, String releasesPolicy,
168 String snapshotsPolicy )
170 ProxyConnectorConfiguration connector = new ProxyConnectorConfiguration();
171 connector.setSourceRepoId( repoId );
172 connector.setTargetRepoId( remoteRepo.id );
173 connector.addPolicy( ProxyConnectorConfiguration.POLICY_RELEASES, releasesPolicy );
174 connector.addPolicy( ProxyConnectorConfiguration.POLICY_SNAPSHOTS, snapshotsPolicy );
175 connector.addPolicy( ProxyConnectorConfiguration.POLICY_CHECKSUM, ChecksumPolicy.IGNORE );
176 connector.addPolicy( ProxyConnectorConfiguration.POLICY_CACHE_FAILURES, CachedFailuresPolicy.NO );
178 archivaConfiguration.getConfiguration().addProxyConnector( connector );
181 protected void shutdownServer( RemoteRepoInfo remoteRepo )
183 if ( remoteRepo != null )
185 if ( remoteRepo.server != null )
187 if ( remoteRepo.server.isRunning() )
191 remoteRepo.server.stop();
192 // int graceful = remoteRepo.server.getGracefulShutdown();
193 // System.out.println( "server set to graceful shutdown: " + graceful );
194 // remoteRepo = null;
196 catch ( Exception e )
198 e.printStackTrace( System.err );
205 protected File populateRepo( RemoteRepoInfo remoteRepo, String path, String contents )
208 File destFile = new File( remoteRepo.root, path );
209 if (destFile.exists())
213 destFile.getParentFile().mkdirs();
214 FileUtils.writeStringToFile( destFile, contents, Charset.defaultCharset() );
218 protected void setupCentralRemoteRepo()
221 remoteCentral = createServer( "central" );
223 assertServerSetupCorrectly( remoteCentral );
225 RemoteRepositoryConfiguration remoteRepositoryConfiguration =
226 archivaConfiguration.getConfiguration().getRemoteRepositoriesAsMap().get( remoteCentral.id );
227 if ( remoteRepositoryConfiguration != null )
229 archivaConfiguration.getConfiguration().removeRemoteRepository( remoteRepositoryConfiguration );
232 archivaConfiguration.getConfiguration().addRemoteRepository( remoteCentral.config );
233 setupCleanRepo( remoteCentral.root );
236 protected void setupConnector( String repoId, RemoteRepoInfo remoteRepo )
238 setupConnector( repoId, remoteRepo, ReleasesPolicy.ALWAYS, SnapshotsPolicy.ALWAYS );
241 protected void setupReleaseConnector( String managedRepoId, RemoteRepoInfo remoteRepo, String releasePolicy )
243 setupConnector( managedRepoId, remoteRepo, releasePolicy, SnapshotsPolicy.ALWAYS );
246 protected void setupSnapshotConnector( String managedRepoId, RemoteRepoInfo remoteRepo, String snapshotsPolicy )
248 setupConnector( managedRepoId, remoteRepo, ReleasesPolicy.ALWAYS, snapshotsPolicy );
251 protected void setupSnapshotsRemoteRepo()
254 remoteSnapshots = createServer( "snapshots" );
256 assertServerSetupCorrectly( remoteSnapshots );
257 RemoteRepositoryConfiguration remoteRepositoryConfiguration =
258 archivaConfiguration.getConfiguration().getRemoteRepositoriesAsMap().get( remoteSnapshots.id );
259 if ( remoteRepositoryConfiguration != null )
261 archivaConfiguration.getConfiguration().removeRemoteRepository( remoteRepositoryConfiguration );
263 archivaConfiguration.getConfiguration().addRemoteRepository( remoteSnapshots.config );
264 setupCleanRepo( remoteSnapshots.root );