]> source.dussan.org Git - archiva.git/blob
0da0394affa8d87c851522976f6abb2f681179e9
[archiva.git] /
1 package org.apache.archiva.webdav;
2
3 /*
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
11  *
12  *  http://www.apache.org/licenses/LICENSE-2.0
13  *
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
19  * under the License.
20  */
21
22
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;
42
43 import javax.servlet.http.HttpServletResponse;
44 import java.io.File;
45 import java.nio.charset.Charset;
46
47 /**
48  * AbstractRepositoryServletProxiedTestCase
49  *
50  */
51 public abstract class AbstractRepositoryServletProxiedTestCase
52     extends AbstractRepositoryServletTestCase
53 {
54     class RemoteRepoInfo
55     {
56         public String id;
57
58         public String url;
59
60         public String context;
61
62         public Server server;
63
64         public File root;
65
66         public RemoteRepositoryConfiguration config;
67     }
68
69     protected static final long ONE_SECOND = ( 1000 /* milliseconds */ );
70
71     protected static final long ONE_MINUTE = ( ONE_SECOND * 60 );
72
73     protected static final long ONE_HOUR = ( ONE_MINUTE * 60 );
74
75     protected static final long ONE_DAY = ( ONE_HOUR * 24 );
76
77     protected static final long OVER_ONE_HOUR = ( ONE_HOUR + ONE_MINUTE );
78
79     protected static final long OVER_ONE_DAY = ( ONE_DAY + ONE_HOUR );
80
81     protected static final long OLDER = ( -1 );
82
83     protected static final long NEWER = 0;
84
85     protected static final int EXPECT_MANAGED_CONTENTS = 1;
86
87     protected static final int EXPECT_REMOTE_CONTENTS = 2;
88
89     protected static final int EXPECT_NOT_FOUND = 3;
90
91     protected static final boolean HAS_MANAGED_COPY = true;
92
93     protected static final boolean NO_MANAGED_COPY = false;
94
95     protected RemoteRepoInfo remoteCentral;
96
97     protected RemoteRepoInfo remoteSnapshots;
98
99     @Before
100     @Override
101     public void setUp()
102         throws Exception
103     {
104         super.setUp();
105         startRepository();
106     }
107
108     @Override
109     @After
110     public void tearDown()
111         throws Exception
112     {
113         shutdownServer( remoteCentral );
114         shutdownServer( remoteSnapshots );
115         super.tearDown();
116     }
117
118     protected RemoteRepoInfo createServer( String id )
119         throws Exception
120     {
121         RemoteRepoInfo repo = new RemoteRepoInfo();
122         repo.id = id;
123         repo.context = "/" + id;
124         repo.root = Files.createTempDir();// new File( System.getProperty( "basedir" ) + "target/remote-repos/" + id + "/" );
125
126         // Remove exising root contents.
127         if ( repo.root.exists() )
128         {
129             FileUtils.deleteDirectory( repo.root );
130         }
131
132         // Establish root directory.
133         if ( !repo.root.exists() )
134         {
135             repo.root.mkdirs();
136         }
137
138         repo.server = new Server( 0 );
139         ContextHandlerCollection contexts = new ContextHandlerCollection();
140         repo.server.setHandler( contexts );
141
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 );
147
148         ServletHolder sh = new ServletHolder( DefaultServlet.class );
149         context.addServlet( sh, "/" );
150
151         contexts.addHandler( context );
152
153         repo.server.start();
154
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 );
158
159         repo.config = createRemoteRepository( repo.id, "Testable [" + repo.id + "] Remote Repo", repo.url );
160
161         return repo;
162     }
163
164     protected void assertServerSetupCorrectly( RemoteRepoInfo remoteRepo )
165         throws Exception
166     {
167
168         WebClient client = newClient();
169         int status = client.getPage( remoteRepo.url ).getWebResponse().getStatusCode();
170         Assertions.assertThat( status ).isEqualTo( HttpServletResponse.SC_OK );
171
172     }
173
174     private void setupConnector( String repoId, RemoteRepoInfo remoteRepo, String releasesPolicy,
175                                  String snapshotsPolicy )
176     {
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 );
184
185         archivaConfiguration.getConfiguration().addProxyConnector( connector );
186     }
187
188     protected void shutdownServer( RemoteRepoInfo remoteRepo )
189     {
190         if ( remoteRepo != null )
191         {
192             if ( remoteRepo.server != null )
193             {
194                 if ( remoteRepo.server.isRunning() )
195                 {
196                     try
197                     {
198                         remoteRepo.server.stop();
199                         // int graceful = remoteRepo.server.getGracefulShutdown();
200                         // System.out.println( "server set to graceful shutdown: " + graceful );
201                         // remoteRepo = null;
202                     }
203                     catch ( Exception e )
204                     {
205                         e.printStackTrace( System.err );
206                     }
207                 }
208             }
209         }
210     }
211
212     protected File populateRepo( RemoteRepoInfo remoteRepo, String path, String contents )
213         throws Exception
214     {
215         File destFile = new File( remoteRepo.root, path );
216         if (destFile.exists())
217         {
218             destFile.delete();
219         }
220         destFile.getParentFile().mkdirs();
221         FileUtils.writeStringToFile( destFile, contents, Charset.defaultCharset()  );
222         return destFile;
223     }
224
225     protected void setupCentralRemoteRepo()
226         throws Exception
227     {
228         remoteCentral = createServer( "central" );
229
230         assertServerSetupCorrectly( remoteCentral );
231
232         RemoteRepositoryConfiguration remoteRepositoryConfiguration =
233             archivaConfiguration.getConfiguration().getRemoteRepositoriesAsMap().get( remoteCentral.id );
234         if ( remoteRepositoryConfiguration != null )
235         {
236             archivaConfiguration.getConfiguration().removeRemoteRepository( remoteRepositoryConfiguration );
237         }
238
239         archivaConfiguration.getConfiguration().addRemoteRepository( remoteCentral.config );
240         setupCleanRepo( remoteCentral.root );
241     }
242
243     protected void setupConnector( String repoId, RemoteRepoInfo remoteRepo )
244     {
245         setupConnector( repoId, remoteRepo, ReleasesPolicy.ALWAYS, SnapshotsPolicy.ALWAYS );
246     }
247
248     protected void setupReleaseConnector( String managedRepoId, RemoteRepoInfo remoteRepo, String releasePolicy )
249     {
250         setupConnector( managedRepoId, remoteRepo, releasePolicy, SnapshotsPolicy.ALWAYS );
251     }
252
253     protected void setupSnapshotConnector( String managedRepoId, RemoteRepoInfo remoteRepo, String snapshotsPolicy )
254     {
255         setupConnector( managedRepoId, remoteRepo, ReleasesPolicy.ALWAYS, snapshotsPolicy );
256     }
257
258     protected void setupSnapshotsRemoteRepo()
259         throws Exception
260     {
261         remoteSnapshots = createServer( "snapshots" );
262
263
264         assertServerSetupCorrectly( remoteSnapshots );
265         RemoteRepositoryConfiguration remoteRepositoryConfiguration =
266             archivaConfiguration.getConfiguration().getRemoteRepositoriesAsMap().get( remoteSnapshots.id );
267         if ( remoteRepositoryConfiguration != null )
268         {
269             archivaConfiguration.getConfiguration().removeRemoteRepository( remoteRepositoryConfiguration );
270         }
271         archivaConfiguration.getConfiguration().addRemoteRepository( remoteSnapshots.config );
272         setupCleanRepo( remoteSnapshots.root );
273     }
274
275
276 }