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.PutMethodWebRequest;
23 import com.meterware.httpunit.WebRequest;
24 import com.meterware.httpunit.WebResponse;
25 import com.meterware.servletunit.ServletRunner;
26 import com.meterware.servletunit.ServletUnitClient;
27 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
28 import org.apache.maven.archiva.configuration.Configuration;
29 import org.apache.maven.archiva.configuration.ConfigurationEvent;
30 import org.apache.maven.archiva.configuration.IndeterminateConfigurationException;
31 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
32 import org.codehaus.plexus.PlexusConstants;
33 import org.codehaus.plexus.PlexusTestCase;
34 import org.codehaus.plexus.registry.RegistryException;
35 import org.codehaus.plexus.util.FileUtils;
36 import org.xml.sax.SAXException;
38 import javax.servlet.ServletException;
40 import java.io.IOException;
42 public class RepositoryServletTest
43 extends PlexusTestCase
45 private ServletUnitClient sc;
47 private static final String REQUEST_PATH = "http://localhost/repository/internal/path/to/artifact.jar";
49 private File repositoryLocation;
51 private ArchivaConfiguration configuration;
53 private static final String REPOSITORY_ID = "internal";
55 private static final String NEW_REPOSITORY_ID = "new-id";
57 private static final String NEW_REPOSITORY_NAME = "New Repository";
59 protected void setUp()
64 // TODO: purely to quiet logging - shouldn't be needed
65 String appserverBase = getTestFile( "target/appserver-base" ).getAbsolutePath();
66 System.setProperty( "appserver.base", appserverBase );
68 configuration = (ArchivaConfiguration) lookup( ArchivaConfiguration.ROLE );
70 repositoryLocation = new File( appserverBase, "data/repositories/internal" );
72 ServletRunner sr = new ServletRunner();
73 sr.registerServlet( "/repository/*", UnauthenticatedRepositoryServlet.class.getName() );
75 sc.getSession( true ).getServletContext().setAttribute( PlexusConstants.PLEXUS_KEY, getContainer() );
78 public void testPutWithMissingParentCollection()
79 throws IOException, SAXException
81 FileUtils.deleteDirectory( repositoryLocation );
83 WebRequest request = new PutMethodWebRequest( REQUEST_PATH, getClass().getResourceAsStream( "/artifact.jar" ),
84 "application/octet-stream" );
85 WebResponse response = sc.getResponse( request );
86 assertNotNull( "No response received", response );
87 assertEquals( "file contents", "artifact.jar\n",
88 FileUtils.fileRead( new File( repositoryLocation, "path/to/artifact.jar" ) ) );
91 public void testGetRepository()
92 throws IOException, ServletException
94 RepositoryServlet servlet = (RepositoryServlet) sc.newInvocation( REQUEST_PATH ).getServlet();
95 assertNotNull( servlet );
97 ManagedRepositoryConfiguration repository = servlet.getRepository( REPOSITORY_ID );
98 assertNotNull( repository );
99 assertEquals( "Archiva Managed Internal Repository", repository.getName() );
102 public void testGetRepositoryAfterDelete()
103 throws IOException, ServletException, RegistryException, IndeterminateConfigurationException
105 RepositoryServlet servlet = (RepositoryServlet) sc.newInvocation( REQUEST_PATH ).getServlet();
106 assertNotNull( servlet );
108 Configuration c = configuration.getConfiguration();
109 c.removeManagedRepository( c.findManagedRepositoryById( REPOSITORY_ID ) );
110 // TODO it would be better to use a mock configuration and "save" to more accurately reflect the calls made
111 servlet.configurationEvent( new ConfigurationEvent( ConfigurationEvent.SAVED) );
113 ManagedRepositoryConfiguration repository = servlet.getRepository( REPOSITORY_ID );
114 assertNull( repository );
117 public void testGetRepositoryAfterAdd()
118 throws IOException, ServletException, RegistryException, IndeterminateConfigurationException
120 RepositoryServlet servlet = (RepositoryServlet) sc.newInvocation( REQUEST_PATH ).getServlet();
121 assertNotNull( servlet );
123 Configuration c = configuration.getConfiguration();
124 ManagedRepositoryConfiguration repo = new ManagedRepositoryConfiguration();
125 repo.setId( NEW_REPOSITORY_ID );
126 repo.setName( NEW_REPOSITORY_NAME );
127 File repoRoot = new File( getBasedir(), "target/test-repository-root" );
128 if ( !repoRoot.exists() )
132 repo.setLocation( repoRoot.getAbsolutePath() );
133 c.addManagedRepository( repo );
134 // TODO it would be better to use a mock configuration and "save" to more accurately reflect the calls made
135 servlet.configurationEvent( new ConfigurationEvent( ConfigurationEvent.SAVED) );
137 ManagedRepositoryConfiguration repository = servlet.getRepository( NEW_REPOSITORY_ID );
138 assertNotNull( repository );
139 assertEquals( NEW_REPOSITORY_NAME, repository.getName() );
141 // check other is still intact
142 repository = servlet.getRepository( REPOSITORY_ID );
143 assertNotNull( repository );
144 assertEquals( "Archiva Managed Internal Repository", repository.getName() );