1 package org.apache.maven.archiva.web.repository;
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.GetMethodWebRequest;
23 import com.meterware.httpunit.PutMethodWebRequest;
24 import com.meterware.httpunit.WebLink;
25 import com.meterware.httpunit.WebRequest;
26 import com.meterware.httpunit.WebResponse;
27 import com.meterware.servletunit.ServletRunner;
28 import com.meterware.servletunit.ServletUnitClient;
30 import org.apache.commons.io.FileUtils;
31 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
32 import org.apache.maven.archiva.configuration.Configuration;
33 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
34 import org.apache.maven.archiva.configuration.RemoteRepositoryConfiguration;
35 import org.codehaus.plexus.PlexusConstants;
36 import org.codehaus.plexus.PlexusTestCase;
37 import org.codehaus.plexus.webdav.util.MimeTypes;
40 import java.io.IOException;
42 import javax.servlet.ServletContext;
43 import javax.servlet.http.HttpServletResponse;
44 import javax.servlet.http.HttpSession;
47 * RepositoryServletTest
49 * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
52 public class RepositoryServletTest
53 extends PlexusTestCase
55 private ServletUnitClient sc;
57 private static final String REQUEST_PATH = "http://localhost/repository/internal/path/to/artifact.jar";
59 private File repositoryLocation;
61 private ArchivaConfiguration configuration;
63 private static final String REPOSITORY_ID = "internal";
65 private static final String NEW_REPOSITORY_ID = "new-id";
67 private static final String NEW_REPOSITORY_NAME = "New Repository";
69 protected void setUp()
74 String appserverBase = getTestFile( "target/appserver-base" ).getAbsolutePath();
75 System.setProperty( "appserver.base", appserverBase );
77 File testConf = getTestFile( "src/test/resources/repository-archiva.xml" );
78 File testConfDest = new File( appserverBase, "conf/archiva.xml" );
79 FileUtils.copyFile( testConf, testConfDest );
81 configuration = (ArchivaConfiguration) lookup( ArchivaConfiguration.ROLE );
82 repositoryLocation = new File( appserverBase, "data/repositories/internal" );
83 Configuration config = configuration.getConfiguration();
85 config.addManagedRepository( createManagedRepository( "internal", "Internal Test Repo", repositoryLocation ) );
88 ServletRunner sr = new ServletRunner();
89 sr.registerServlet( "/repository/*", UnauthenticatedRepositoryServlet.class.getName() );
91 HttpSession session = sc.getSession( true );
92 ServletContext servletContext = session.getServletContext();
93 servletContext.setAttribute( PlexusConstants.PLEXUS_KEY, getContainer() );
96 public void testPutWithMissingParentCollection()
99 FileUtils.deleteDirectory( repositoryLocation );
101 WebRequest request = new PutMethodWebRequest( REQUEST_PATH, getClass().getResourceAsStream( "/artifact.jar" ),
102 "application/octet-stream" );
103 WebResponse response = sc.getResponse( request );
104 assertNotNull( "Should have received response", response );
105 assertEquals( "file contents", "artifact.jar\n", FileUtils
106 .readFileToString( new File( repositoryLocation, "path/to/artifact.jar" ), null ) );
109 public void testGetRepository()
112 RepositoryServlet servlet = (RepositoryServlet) sc.newInvocation( REQUEST_PATH ).getServlet();
113 assertNotNull( servlet );
115 assertRepositoryValid( servlet, REPOSITORY_ID );
118 public void testGetRepositoryAfterDelete()
121 RepositoryServlet servlet = (RepositoryServlet) sc.newInvocation( REQUEST_PATH ).getServlet();
122 assertNotNull( servlet );
124 Configuration c = configuration.getConfiguration();
125 c.removeManagedRepository( c.findManagedRepositoryById( REPOSITORY_ID ) );
128 ManagedRepositoryConfiguration repository = servlet.getRepository( REPOSITORY_ID );
129 assertNull( repository );
132 public void testGetRepositoryAfterAdd()
135 RepositoryServlet servlet = (RepositoryServlet) sc.newInvocation( REQUEST_PATH ).getServlet();
136 assertNotNull( servlet );
138 Configuration c = configuration.getConfiguration();
139 ManagedRepositoryConfiguration repo = new ManagedRepositoryConfiguration();
140 repo.setId( NEW_REPOSITORY_ID );
141 repo.setName( NEW_REPOSITORY_NAME );
142 File repoRoot = new File( getBasedir(), "target/test-repository-root" );
143 if ( !repoRoot.exists() )
147 repo.setLocation( repoRoot.getAbsolutePath() );
148 c.addManagedRepository( repo );
151 ManagedRepositoryConfiguration repository = servlet.getRepository( NEW_REPOSITORY_ID );
152 assertNotNull( repository );
153 assertEquals( NEW_REPOSITORY_NAME, repository.getName() );
155 // check other is still intact
156 assertRepositoryValid( servlet, REPOSITORY_ID );
159 public void testBrowse()
162 RepositoryServlet servlet = (RepositoryServlet) sc.newInvocation( REQUEST_PATH ).getServlet();
163 assertNotNull( servlet );
164 assertRepositoryValid( servlet, REPOSITORY_ID );
166 new File( repositoryLocation, "org/apache/archiva" ).mkdirs();
167 new File( repositoryLocation, "net/sourceforge" ).mkdirs();
168 new File( repositoryLocation, "commons-lang" ).mkdirs();
170 WebRequest request = new GetMethodWebRequest( "http://machine.com/repository/internal/" );
171 WebResponse response = sc.getResponse( request );
172 assertEquals( "Response", HttpServletResponse.SC_OK, response.getResponseCode() );
174 // dumpResponse( response );
176 WebLink links[] = response.getLinks();
177 String expectedLinks[] = new String[] { "./commons-lang/", "./net/", "./org/", "./path/" };
179 assertEquals( "Links.length", expectedLinks.length, links.length );
180 for ( int i = 0; i < links.length; i++ )
182 assertEquals( "Link[" + i + "]", expectedLinks[i], links[i].getURLString() );
186 public void testGetNoProxyChecksumDefaultLayout()
189 RepositoryServlet servlet = (RepositoryServlet) sc.newInvocation( REQUEST_PATH ).getServlet();
190 assertNotNull( servlet );
191 assertRepositoryValid( servlet, REPOSITORY_ID );
193 String commonsLangSha1 = "commons-lang/commons-lang/2.1/commons-lang-2.1.jar.sha1";
195 File checksumFile = new File( repositoryLocation, commonsLangSha1 );
196 checksumFile.getParentFile().mkdirs();
198 FileUtils.writeStringToFile( checksumFile, "dummy-checksum", null );
200 WebRequest request = new GetMethodWebRequest( "http://machine.com/repository/internal/" + commonsLangSha1 );
201 WebResponse response = sc.getResponse( request );
202 assertEquals( "Response OK", HttpServletResponse.SC_OK, response.getResponseCode() );
204 assertEquals( "Expected file contents", "dummy-checksum", response.getText() );
207 public void testGetNoProxyChecksumLegacyLayout()
210 RepositoryServlet servlet = (RepositoryServlet) sc.newInvocation( REQUEST_PATH ).getServlet();
211 assertNotNull( servlet );
212 assertRepositoryValid( servlet, REPOSITORY_ID );
214 String commonsLangSha1 = "commons-lang/commons-lang/2.1/commons-lang-2.1.jar.sha1";
216 File checksumFile = new File( repositoryLocation, commonsLangSha1 );
217 checksumFile.getParentFile().mkdirs();
219 FileUtils.writeStringToFile( checksumFile, "dummy-checksum", null );
221 WebRequest request = new GetMethodWebRequest( "http://machine.com/repository/internal/" +
222 "commons-lang/jars/commons-lang-2.1.jar.sha1" );
223 WebResponse response = sc.getResponse( request );
224 assertEquals( "Response OK", HttpServletResponse.SC_OK, response.getResponseCode() );
226 assertEquals( "Expected file contents", "dummy-checksum", response.getText() );
229 public void testGetNoProxyVersionedMetadataDefaultLayout()
232 RepositoryServlet servlet = (RepositoryServlet) sc.newInvocation( REQUEST_PATH ).getServlet();
233 assertNotNull( servlet );
234 assertRepositoryValid( servlet, REPOSITORY_ID );
236 String commonsLangMetadata = "commons-lang/commons-lang/2.1/maven-metadata.xml";
237 String expectedMetadataContents = "dummy-versioned-metadata";
239 File metadataFile = new File( repositoryLocation, commonsLangMetadata );
240 metadataFile.getParentFile().mkdirs();
242 FileUtils.writeStringToFile( metadataFile, expectedMetadataContents, null );
244 WebRequest request = new GetMethodWebRequest( "http://machine.com/repository/internal/" + commonsLangMetadata );
245 WebResponse response = sc.getResponse( request );
246 assertEquals( "Response OK", HttpServletResponse.SC_OK, response.getResponseCode() );
248 assertEquals( "Expected file contents", expectedMetadataContents, response.getText() );
251 public void testGetNoProxyProjectMetadataDefaultLayout()
254 RepositoryServlet servlet = (RepositoryServlet) sc.newInvocation( REQUEST_PATH ).getServlet();
255 assertNotNull( servlet );
256 assertRepositoryValid( servlet, REPOSITORY_ID );
258 String commonsLangMetadata = "commons-lang/commons-lang/maven-metadata.xml";
259 String expectedMetadataContents = "dummy-project-metadata";
261 File metadataFile = new File( repositoryLocation, commonsLangMetadata );
262 metadataFile.getParentFile().mkdirs();
264 FileUtils.writeStringToFile( metadataFile, expectedMetadataContents, null );
266 WebRequest request = new GetMethodWebRequest( "http://machine.com/repository/internal/" + commonsLangMetadata );
267 WebResponse response = sc.getResponse( request );
268 assertEquals( "Response OK", HttpServletResponse.SC_OK, response.getResponseCode() );
270 assertEquals( "Expected file contents", expectedMetadataContents, response.getText() );
273 public void testGetNoProxyArtifactDefaultLayout()
276 RepositoryServlet servlet = (RepositoryServlet) sc.newInvocation( REQUEST_PATH ).getServlet();
277 assertNotNull( servlet );
278 assertRepositoryValid( servlet, REPOSITORY_ID );
280 String commonsLangJar = "commons-lang/commons-lang/2.1/commons-lang-2.1.jar";
281 String expectedArtifactContents = "dummy-commons-lang-artifact";
283 File artifactFile = new File( repositoryLocation, commonsLangJar );
284 artifactFile.getParentFile().mkdirs();
286 FileUtils.writeStringToFile( artifactFile, expectedArtifactContents, null );
288 WebRequest request = new GetMethodWebRequest( "http://machine.com/repository/internal/" + commonsLangJar );
289 WebResponse response = sc.getResponse( request );
290 assertEquals( "Response OK", HttpServletResponse.SC_OK, response.getResponseCode() );
292 assertEquals( "Expected file contents", expectedArtifactContents, response.getText() );
295 public void testGetNoProxyArtifactLegacyLayout()
298 RepositoryServlet servlet = (RepositoryServlet) sc.newInvocation( REQUEST_PATH ).getServlet();
299 assertNotNull( servlet );
300 assertRepositoryValid( servlet, REPOSITORY_ID );
302 String commonsLangJar = "commons-lang/commons-lang/2.1/commons-lang-2.1.jar";
303 String expectedArtifactContents = "dummy-commons-lang-artifact";
305 File artifactFile = new File( repositoryLocation, commonsLangJar );
306 artifactFile.getParentFile().mkdirs();
308 FileUtils.writeStringToFile( artifactFile, expectedArtifactContents, null );
310 WebRequest request = new GetMethodWebRequest( "http://machine.com/repository/internal/" +
311 "commons-lang/jars/commons-lang-2.1.jar" );
312 WebResponse response = sc.getResponse( request );
313 assertEquals( "Response OK", HttpServletResponse.SC_OK, response.getResponseCode() );
315 assertEquals( "Expected file contents", expectedArtifactContents, response.getText() );
318 public void testMimeTypesAvailable()
321 MimeTypes mimeTypes = (MimeTypes) lookup( MimeTypes.class );
322 assertNotNull( mimeTypes );
324 // Test for some added types.
325 assertEquals( "sha1", "text/plain", mimeTypes.getMimeType( "foo.sha1" ) );
326 assertEquals( "md5", "text/plain", mimeTypes.getMimeType( "foo.md5" ) );
327 assertEquals( "pgp", "application/pgp-encrypted", mimeTypes.getMimeType( "foo.pgp" ) );
330 private void dumpResponse( WebResponse response )
332 System.out.println( "---(response)---" );
333 System.out.println( "" + response.getResponseCode() + " " + response.getResponseMessage() );
335 String headerNames[] = response.getHeaderFieldNames();
336 for ( String headerName : headerNames )
338 System.out.println( "[header] " + headerName + ": " + response.getHeaderField( headerName ) );
341 System.out.println( "---(text)---" );
344 System.out.println( response.getText() );
346 catch ( IOException e )
348 System.err.print( "[Exception] : " );
349 e.printStackTrace( System.err );
353 private void assertRepositoryValid( RepositoryServlet servlet, String repoId )
355 ManagedRepositoryConfiguration repository = servlet.getRepository( repoId );
356 assertNotNull( "Archiva Managed Repository id:<" + repoId + "> should exist.", repository );
357 File repoRoot = new File( repository.getLocation() );
358 assertTrue( "Archiva Managed Repository id:<" + repoId + "> should have a valid location on disk.", repoRoot
360 && repoRoot.isDirectory() );
363 private void saveConfiguration()
366 configuration.save( configuration.getConfiguration() );
367 // TODO it would be better to use a mock configuration and "save" to more accurately reflect the calls made
368 // RepositoryServlet servlet
369 // servlet.configurationEvent( new ConfigurationEvent( ConfigurationEvent.SAVED ) );
372 private ManagedRepositoryConfiguration createManagedRepository( String id, String name, File location )
374 ManagedRepositoryConfiguration repo = new ManagedRepositoryConfiguration();
376 repo.setName( name );
377 repo.setLocation( location.getAbsolutePath() );
381 private RemoteRepositoryConfiguration createRemoteRepository( String id, String name, String url )
383 RemoteRepositoryConfiguration repo = new RemoteRepositoryConfiguration();
385 repo.setName( name );