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.WebClient;
24 import com.gargoylesoftware.htmlunit.WebRequest;
25 import com.gargoylesoftware.htmlunit.WebResponse;
26 import com.google.common.io.Files;
27 import org.apache.archiva.configuration.ProxyConnectorConfiguration;
28 import org.apache.archiva.configuration.RemoteRepositoryConfiguration;
29 import org.apache.archiva.policies.CachedFailuresPolicy;
30 import org.apache.archiva.policies.ChecksumPolicy;
31 import org.apache.archiva.policies.ReleasesPolicy;
32 import org.apache.archiva.policies.SnapshotsPolicy;
33 import org.apache.commons.io.FileUtils;
34 import org.eclipse.jetty.server.Server;
35 import org.eclipse.jetty.server.handler.ContextHandlerCollection;
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.After;
41 import org.junit.Before;
43 import javax.servlet.http.HttpServletResponse;
45 import java.nio.charset.Charset;
48 * AbstractRepositoryServletProxiedTestCase
51 public abstract class AbstractRepositoryServletProxiedTestCase
52 extends AbstractRepositoryServletTestCase
60 public String context;
66 public RemoteRepositoryConfiguration config;
69 protected static final long ONE_SECOND = ( 1000 /* milliseconds */ );
71 protected static final long ONE_MINUTE = ( ONE_SECOND * 60 );
73 protected static final long ONE_HOUR = ( ONE_MINUTE * 60 );
75 protected static final long ONE_DAY = ( ONE_HOUR * 24 );
77 protected static final long OVER_ONE_HOUR = ( ONE_HOUR + ONE_MINUTE );
79 protected static final long OVER_ONE_DAY = ( ONE_DAY + ONE_HOUR );
81 protected static final long OLDER = ( -1 );
83 protected static final long NEWER = 0;
85 protected static final int EXPECT_MANAGED_CONTENTS = 1;
87 protected static final int EXPECT_REMOTE_CONTENTS = 2;
89 protected static final int EXPECT_NOT_FOUND = 3;
91 protected static final boolean HAS_MANAGED_COPY = true;
93 protected static final boolean NO_MANAGED_COPY = false;
95 protected RemoteRepoInfo remoteCentral;
97 protected RemoteRepoInfo remoteSnapshots;
110 public void tearDown()
113 shutdownServer( remoteCentral );
114 shutdownServer( remoteSnapshots );
118 protected RemoteRepoInfo createServer( String id )
121 RemoteRepoInfo repo = new RemoteRepoInfo();
123 repo.context = "/" + id;
124 repo.root = Files.createTempDir();// new File( System.getProperty( "basedir" ) + "target/remote-repos/" + id + "/" );
126 // Remove exising root contents.
127 if ( repo.root.exists() )
129 FileUtils.deleteDirectory( repo.root );
132 // Establish root directory.
133 if ( !repo.root.exists() )
138 repo.server = new Server( 0 );
139 ContextHandlerCollection contexts = new ContextHandlerCollection();
140 repo.server.setHandler( contexts );
142 ServletContextHandler context = new ServletContextHandler();
143 context.setContextPath( repo.context );
144 context.setResourceBase( repo.root.getAbsolutePath() );
145 context.setAttribute( "dirAllowed", true );
146 context.setAttribute( "maxCacheSize", 0 );
148 ServletHolder sh = new ServletHolder( DefaultServlet.class );
149 context.addServlet( sh, "/" );
151 contexts.addHandler( context );
155 int port = repo.server.getConnectors()[0].getLocalPort();
156 repo.url = "http://localhost:" + port + repo.context;
157 log.info( "Remote HTTP Server started on {}", repo.url );
159 repo.config = createRemoteRepository( repo.id, "Testable [" + repo.id + "] Remote Repo", repo.url );
164 protected void assertServerSetupCorrectly( RemoteRepoInfo remoteRepo )
168 WebClient client = newClient();
169 int status = client.getPage( remoteRepo.url ).getWebResponse().getStatusCode();
170 Assertions.assertThat( status ).isEqualTo( HttpServletResponse.SC_OK );
174 private void setupConnector( String repoId, RemoteRepoInfo remoteRepo, String releasesPolicy,
175 String snapshotsPolicy )
177 ProxyConnectorConfiguration connector = new ProxyConnectorConfiguration();
178 connector.setSourceRepoId( repoId );
179 connector.setTargetRepoId( remoteRepo.id );
180 connector.addPolicy( ProxyConnectorConfiguration.POLICY_RELEASES, releasesPolicy );
181 connector.addPolicy( ProxyConnectorConfiguration.POLICY_SNAPSHOTS, snapshotsPolicy );
182 connector.addPolicy( ProxyConnectorConfiguration.POLICY_CHECKSUM, ChecksumPolicy.IGNORE );
183 connector.addPolicy( ProxyConnectorConfiguration.POLICY_CACHE_FAILURES, CachedFailuresPolicy.NO );
185 archivaConfiguration.getConfiguration().addProxyConnector( connector );
188 protected void shutdownServer( RemoteRepoInfo remoteRepo )
190 if ( remoteRepo != null )
192 if ( remoteRepo.server != null )
194 if ( remoteRepo.server.isRunning() )
198 remoteRepo.server.stop();
199 // int graceful = remoteRepo.server.getGracefulShutdown();
200 // System.out.println( "server set to graceful shutdown: " + graceful );
201 // remoteRepo = null;
203 catch ( Exception e )
205 e.printStackTrace( System.err );
212 protected File populateRepo( RemoteRepoInfo remoteRepo, String path, String contents )
215 File destFile = new File( remoteRepo.root, path );
216 if (destFile.exists())
220 destFile.getParentFile().mkdirs();
221 FileUtils.writeStringToFile( destFile, contents, Charset.defaultCharset() );
225 protected void setupCentralRemoteRepo()
228 remoteCentral = createServer( "central" );
230 assertServerSetupCorrectly( remoteCentral );
232 RemoteRepositoryConfiguration remoteRepositoryConfiguration =
233 archivaConfiguration.getConfiguration().getRemoteRepositoriesAsMap().get( remoteCentral.id );
234 if ( remoteRepositoryConfiguration != null )
236 archivaConfiguration.getConfiguration().removeRemoteRepository( remoteRepositoryConfiguration );
239 archivaConfiguration.getConfiguration().addRemoteRepository( remoteCentral.config );
240 setupCleanRepo( remoteCentral.root );
243 protected void setupConnector( String repoId, RemoteRepoInfo remoteRepo )
245 setupConnector( repoId, remoteRepo, ReleasesPolicy.ALWAYS, SnapshotsPolicy.ALWAYS );
248 protected void setupReleaseConnector( String managedRepoId, RemoteRepoInfo remoteRepo, String releasePolicy )
250 setupConnector( managedRepoId, remoteRepo, releasePolicy, SnapshotsPolicy.ALWAYS );
253 protected void setupSnapshotConnector( String managedRepoId, RemoteRepoInfo remoteRepo, String snapshotsPolicy )
255 setupConnector( managedRepoId, remoteRepo, ReleasesPolicy.ALWAYS, snapshotsPolicy );
258 protected void setupSnapshotsRemoteRepo()
261 remoteSnapshots = createServer( "snapshots" );
264 assertServerSetupCorrectly( remoteSnapshots );
265 RemoteRepositoryConfiguration remoteRepositoryConfiguration =
266 archivaConfiguration.getConfiguration().getRemoteRepositoriesAsMap().get( remoteSnapshots.id );
267 if ( remoteRepositoryConfiguration != null )
269 archivaConfiguration.getConfiguration().removeRemoteRepository( remoteRepositoryConfiguration );
271 archivaConfiguration.getConfiguration().addRemoteRepository( remoteSnapshots.config );
272 setupCleanRepo( remoteSnapshots.root );