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
22 import com.meterware.httpunit.WebConversation;
23 import com.meterware.httpunit.WebResponse;
24 import org.apache.archiva.configuration.ProxyConnectorConfiguration;
25 import org.apache.archiva.configuration.RemoteRepositoryConfiguration;
26 import org.apache.archiva.policies.CachedFailuresPolicy;
27 import org.apache.archiva.policies.ChecksumPolicy;
28 import org.apache.archiva.policies.ReleasesPolicy;
29 import org.apache.archiva.policies.SnapshotsPolicy;
30 import org.apache.commons.io.FileUtils;
31 import org.eclipse.jetty.server.Server;
32 import org.eclipse.jetty.server.handler.ContextHandler;
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.ServletHandler;
37 import org.eclipse.jetty.servlet.ServletHolder;
38 import org.junit.After;
39 import org.junit.Before;
42 import java.nio.charset.Charset;
45 * AbstractRepositoryServletProxiedTestCase
49 public abstract class AbstractRepositoryServletProxiedTestCase
50 extends AbstractRepositoryServletTestCase
58 public String context;
64 public RemoteRepositoryConfiguration config;
67 protected static final long ONE_SECOND = ( 1000 /* milliseconds */ );
69 protected static final long ONE_MINUTE = ( ONE_SECOND * 60 );
71 protected static final long ONE_HOUR = ( ONE_MINUTE * 60 );
73 protected static final long ONE_DAY = ( ONE_HOUR * 24 );
75 protected static final long OVER_ONE_HOUR = ( ONE_HOUR + ONE_MINUTE );
77 protected static final long OVER_ONE_DAY = ( ONE_DAY + ONE_HOUR );
79 protected static final long OLDER = ( -1 );
81 protected static final long NEWER = 0;
83 protected static final int EXPECT_MANAGED_CONTENTS = 1;
85 protected static final int EXPECT_REMOTE_CONTENTS = 2;
87 protected static final int EXPECT_NOT_FOUND = 3;
89 protected static final boolean HAS_MANAGED_COPY = true;
91 protected static final boolean NO_MANAGED_COPY = false;
93 protected RemoteRepoInfo remoteCentral;
95 protected RemoteRepoInfo remoteSnapshots;
106 public void tearDown()
109 shutdownServer( remoteCentral );
110 shutdownServer( remoteSnapshots );
114 protected RemoteRepoInfo createServer( String id )
117 RemoteRepoInfo repo = new RemoteRepoInfo();
119 repo.context = "/" + id;
120 repo.root = new File( "target/remote-repos/" + id + "/" );
122 // Remove exising root contents.
123 if ( repo.root.exists() )
125 FileUtils.deleteDirectory( repo.root );
128 // Establish root directory.
129 if ( !repo.root.exists() )
134 repo.server = new Server( 0 );
135 ContextHandlerCollection contexts = new ContextHandlerCollection();
136 repo.server.setHandler( contexts );
138 ServletContextHandler context = new ServletContextHandler();
139 context.setContextPath( repo.context );
140 context.setResourceBase( repo.root.getAbsolutePath() );
141 context.setAttribute( "dirAllowed", true );
142 context.setAttribute( "maxCacheSize", 0 );
144 ServletHolder sh = new ServletHolder( DefaultServlet.class );
145 context.addServlet( sh, "/" );
147 contexts.addHandler( context );
151 int port = repo.server.getConnectors()[0].getLocalPort();
152 repo.url = "http://localhost:" + port + repo.context;
153 log.info( "Remote HTTP Server started on {}", repo.url );
155 repo.config = createRemoteRepository( repo.id, "Testable [" + repo.id + "] Remote Repo", repo.url );
160 protected void assertServerSetupCorrectly( RemoteRepoInfo remoteRepo )
163 WebConversation wc = new WebConversation();
164 WebResponse response = wc.getResponse( remoteRepo.url );
165 assertResponseOK( response );
168 private void setupConnector( String repoId, RemoteRepoInfo remoteRepo, String releasesPolicy,
169 String snapshotsPolicy )
171 ProxyConnectorConfiguration connector = new ProxyConnectorConfiguration();
172 connector.setSourceRepoId( repoId );
173 connector.setTargetRepoId( remoteRepo.id );
174 connector.addPolicy( ProxyConnectorConfiguration.POLICY_RELEASES, releasesPolicy );
175 connector.addPolicy( ProxyConnectorConfiguration.POLICY_SNAPSHOTS, snapshotsPolicy );
176 connector.addPolicy( ProxyConnectorConfiguration.POLICY_CHECKSUM, ChecksumPolicy.IGNORE );
177 connector.addPolicy( ProxyConnectorConfiguration.POLICY_CACHE_FAILURES, CachedFailuresPolicy.NO );
179 archivaConfiguration.getConfiguration().addProxyConnector( connector );
182 protected void shutdownServer( RemoteRepoInfo remoteRepo )
184 if ( remoteRepo != null )
186 if ( remoteRepo.server != null )
188 if ( remoteRepo.server.isRunning() )
192 remoteRepo.server.stop();
193 // int graceful = remoteRepo.server.getGracefulShutdown();
194 // System.out.println( "server set to graceful shutdown: " + graceful );
195 // remoteRepo = null;
197 catch ( Exception e )
199 e.printStackTrace( System.err );
206 protected File populateRepo( RemoteRepoInfo remoteRepo, String path, String contents )
209 File destFile = new File( remoteRepo.root, path );
210 if (destFile.exists())
214 destFile.getParentFile().mkdirs();
215 FileUtils.writeStringToFile( destFile, contents, Charset.defaultCharset() );
219 protected void setupCentralRemoteRepo()
222 remoteCentral = createServer( "central" );
224 assertServerSetupCorrectly( remoteCentral );
226 RemoteRepositoryConfiguration remoteRepositoryConfiguration =
227 archivaConfiguration.getConfiguration().getRemoteRepositoriesAsMap().get( remoteCentral.id );
228 if ( remoteRepositoryConfiguration != null )
230 archivaConfiguration.getConfiguration().removeRemoteRepository( remoteRepositoryConfiguration );
233 archivaConfiguration.getConfiguration().addRemoteRepository( remoteCentral.config );
234 setupCleanRepo( remoteCentral.root );
237 protected void setupConnector( String repoId, RemoteRepoInfo remoteRepo )
239 setupConnector( repoId, remoteRepo, ReleasesPolicy.ALWAYS, SnapshotsPolicy.ALWAYS );
242 protected void setupReleaseConnector( String managedRepoId, RemoteRepoInfo remoteRepo, String releasePolicy )
244 setupConnector( managedRepoId, remoteRepo, releasePolicy, SnapshotsPolicy.ALWAYS );
247 protected void setupSnapshotConnector( String managedRepoId, RemoteRepoInfo remoteRepo, String snapshotsPolicy )
249 setupConnector( managedRepoId, remoteRepo, ReleasesPolicy.ALWAYS, snapshotsPolicy );
252 protected void setupSnapshotsRemoteRepo()
255 remoteSnapshots = createServer( "snapshots" );
257 assertServerSetupCorrectly( remoteSnapshots );
258 RemoteRepositoryConfiguration remoteRepositoryConfiguration =
259 archivaConfiguration.getConfiguration().getRemoteRepositoriesAsMap().get( remoteSnapshots.id );
260 if ( remoteRepositoryConfiguration != null )
262 archivaConfiguration.getConfiguration().removeRemoteRepository( remoteRepositoryConfiguration );
264 archivaConfiguration.getConfiguration().addRemoteRepository( remoteSnapshots.config );
265 setupCleanRepo( remoteSnapshots.root );