1 package org.apache.maven.archiva.webdav;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.File;
23
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.mortbay.jetty.Server;
32 import org.mortbay.jetty.handler.ContextHandler;
33 import org.mortbay.jetty.handler.ContextHandlerCollection;
34 import org.mortbay.jetty.servlet.DefaultServlet;
35 import org.mortbay.jetty.servlet.ServletHandler;
36
37 import com.meterware.httpunit.WebConversation;
38 import com.meterware.httpunit.WebResponse;
39
40
41
42
43
44
45 public abstract class AbstractRepositoryServletProxiedTestCase
46 extends AbstractRepositoryServletTestCase
47 {
48 class RemoteRepoInfo
49 {
50 public String id;
51
52 public String url;
53
54 public String context;
55
56 public Server server;
57
58 public File root;
59
60 public RemoteRepositoryConfiguration config;
61 }
62
63 protected static final long ONE_SECOND = ( 1000
64
65 protected static final long ONE_MINUTE = ( ONE_SECOND * 60 );
66
67 protected static final long ONE_HOUR = ( ONE_MINUTE * 60 );
68
69 protected static final long ONE_DAY = ( ONE_HOUR * 24 );
70
71 protected static final long OVER_ONE_HOUR = ( ONE_HOUR + ONE_MINUTE );
72
73 protected static final long OVER_ONE_DAY = ( ONE_DAY + ONE_HOUR );
74
75 protected static final long OLDER = ( -1 );
76
77 protected static final long NEWER = 0;
78
79 protected static final int EXPECT_MANAGED_CONTENTS = 1;
80
81 protected static final int EXPECT_REMOTE_CONTENTS = 2;
82
83 protected static final int EXPECT_NOT_FOUND = 3;
84
85 protected static final boolean HAS_MANAGED_COPY = true;
86
87 protected static final boolean NO_MANAGED_COPY = false;
88
89 protected RemoteRepoInfo remoteCentral;
90
91 protected RemoteRepoInfo remoteSnapshots;
92
93 protected RemoteRepoInfo remotePrivateSnapshots;
94
95 @Override
96 protected void setUp()
97 throws Exception
98 {
99 super.setUp();
100 }
101
102 protected RemoteRepoInfo createServer( String id )
103 throws Exception
104 {
105 RemoteRepoInfo repo = new RemoteRepoInfo();
106 repo.id = id;
107 repo.context = "/" + id;
108 repo.root = getTestFile( "target/remote-repos/" + id + "/" );
109
110
111 if ( repo.root.exists() )
112 {
113 FileUtils.deleteDirectory( repo.root );
114 }
115
116
117 if ( !repo.root.exists() )
118 {
119 repo.root.mkdirs();
120 }
121
122 repo.server = new Server( 0 );
123 ContextHandlerCollection contexts = new ContextHandlerCollection();
124 repo.server.setHandler( contexts );
125
126 ContextHandler context = new ContextHandler();
127 context.setContextPath( repo.context );
128 context.setResourceBase( repo.root.getAbsolutePath() );
129 context.setAttribute( "dirAllowed", true );
130 context.setAttribute( "maxCacheSize", 0 );
131 ServletHandler servlet = new ServletHandler();
132 servlet.addServletWithMapping( DefaultServlet.class.getName(), "/" );
133 context.setHandler( servlet );
134 contexts.addHandler( context );
135
136 repo.server.start();
137
138 int port = repo.server.getConnectors()[0].getLocalPort();
139 repo.url = "http://localhost:" + port + repo.context;
140 System.out.println( "Remote HTTP Server started on " + repo.url );
141
142 repo.config = createRemoteRepository( repo.id, "Testable [" + repo.id + "] Remote Repo", repo.url );
143
144 return repo;
145 }
146
147 protected void assertServerSetupCorrectly( RemoteRepoInfo remoteRepo )
148 throws Exception
149 {
150 WebConversation wc = new WebConversation();
151 WebResponse response = wc.getResponse( remoteRepo.url );
152 assertResponseOK( response );
153 }
154
155 private void setupConnector( String repoId, RemoteRepoInfo remoteRepo, String releasesPolicy, String snapshotsPolicy )
156 {
157 ProxyConnectorConfiguration connector = new ProxyConnectorConfiguration();
158 connector.setSourceRepoId( repoId );
159 connector.setTargetRepoId( remoteRepo.id );
160 connector.addPolicy( ProxyConnectorConfiguration.POLICY_RELEASES, releasesPolicy );
161 connector.addPolicy( ProxyConnectorConfiguration.POLICY_SNAPSHOTS, snapshotsPolicy );
162 connector.addPolicy( ProxyConnectorConfiguration.POLICY_CHECKSUM, ChecksumPolicy.IGNORE );
163 connector.addPolicy( ProxyConnectorConfiguration.POLICY_CACHE_FAILURES, CachedFailuresPolicy.NO );
164
165 archivaConfiguration.getConfiguration().addProxyConnector( connector );
166 }
167
168 protected void shutdownServer( RemoteRepoInfo remoteRepo )
169 {
170 if ( remoteRepo != null )
171 {
172 if ( remoteRepo.server != null )
173 {
174 if ( remoteRepo.server.isRunning() )
175 {
176 try
177 {
178 remoteRepo.server.stop();
179
180
181
182 }
183 catch ( Exception e )
184 {
185 e.printStackTrace( System.err );
186 }
187 }
188 }
189 }
190 }
191
192 protected File populateRepo( RemoteRepoInfo remoteRepo, String path, String contents )
193 throws Exception
194 {
195 File destFile = new File( remoteRepo.root, path );
196 destFile.getParentFile().mkdirs();
197 FileUtils.writeStringToFile( destFile, contents, null );
198 return destFile;
199 }
200
201 protected void setupCentralRemoteRepo()
202 throws Exception
203 {
204 remoteCentral = createServer( "central" );
205
206 assertServerSetupCorrectly( remoteCentral );
207 archivaConfiguration.getConfiguration().addRemoteRepository( remoteCentral.config );
208 setupCleanRepo( remoteCentral.root );
209 }
210
211 protected void setupConnector( String repoId, RemoteRepoInfo remoteRepo )
212 {
213 setupConnector( repoId, remoteRepo, ReleasesPolicy.ALWAYS, SnapshotsPolicy.ALWAYS );
214 }
215
216 protected void setupReleaseConnector( String managedRepoId, RemoteRepoInfo remoteRepo, String releasePolicy )
217 {
218 setupConnector( managedRepoId, remoteRepo, releasePolicy, SnapshotsPolicy.ALWAYS );
219 }
220
221 protected void setupSnapshotConnector( String managedRepoId, RemoteRepoInfo remoteRepo, String snapshotsPolicy )
222 {
223 setupConnector( managedRepoId, remoteRepo, ReleasesPolicy.ALWAYS, snapshotsPolicy );
224 }
225
226 protected void setupSnapshotsRemoteRepo()
227 throws Exception
228 {
229 remoteSnapshots = createServer( "snapshots" );
230
231 assertServerSetupCorrectly( remoteSnapshots );
232 archivaConfiguration.getConfiguration().addRemoteRepository( remoteSnapshots.config );
233 setupCleanRepo( remoteSnapshots.root );
234 }
235
236 @Override
237 protected void tearDown()
238 throws Exception
239 {
240 shutdownServer( remoteCentral );
241 shutdownServer( remoteSnapshots );
242 super.tearDown();
243 }
244 }