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.gargoylesoftware.htmlunit.WebRequest;
23 import com.gargoylesoftware.htmlunit.WebResponse;
24 import org.apache.archiva.admin.model.beans.ManagedRepository;
25 import org.apache.archiva.configuration.ArchivaConfiguration;
26 import org.apache.archiva.configuration.Configuration;
27 import org.apache.archiva.configuration.ManagedRepositoryConfiguration;
28 import org.apache.catalina.Container;
29 import org.apache.catalina.startup.Tomcat;
30 import org.apache.commons.lang.StringUtils;
31 import org.fest.assertions.api.Assertions;
32 import org.junit.Before;
33 import org.junit.Test;
35 import javax.servlet.Servlet;
37 import java.util.Enumeration;
40 * RepositoryServletTest
42 public class RepositoryServletTest
43 extends AbstractRepositoryServletTestCase
45 private static final String REQUEST_PATH = "http://machine.com/repository/internal/";
47 private static final String NEW_REPOSITORY_ID = "new-id";
49 private static final String NEW_REPOSITORY_NAME = "New Repository";
61 public void testGetRepository()
65 RepositoryServlet servlet = RepositoryServlet.class.cast( findServlet( "repository" ) );
66 assertNotNull( servlet );
68 assertRepositoryValid( servlet, REPOID_INTERNAL );
71 Servlet findServlet( String name )
74 Container[] childs = context.findChildren();
75 for ( Container container : childs )
77 if ( StringUtils.equals( container.getName(), name ) )
79 Tomcat.ExistingStandardWrapper esw = Tomcat.ExistingStandardWrapper.class.cast( container );
80 Servlet servlet = esw.loadServlet();
89 public void testGetRepositoryAfterDelete()
92 RepositoryServlet servlet = RepositoryServlet.class.cast( findServlet( "repository" ) );
94 assertNotNull( servlet );
96 ArchivaConfiguration archivaConfiguration = servlet.getConfiguration();
97 Configuration c = archivaConfiguration.getConfiguration();
98 c.removeManagedRepository( c.findManagedRepositoryById( REPOID_INTERNAL ) );
99 saveConfiguration( archivaConfiguration );
101 ManagedRepository repository = servlet.getRepository( REPOID_INTERNAL );
102 assertNull( repository );
106 public void testGetRepositoryAfterAdd()
109 RepositoryServlet servlet =RepositoryServlet.class.cast( findServlet( "repository" ) );
110 assertNotNull( servlet );
112 ArchivaConfiguration archivaConfiguration = servlet.getConfiguration();
113 Configuration c = archivaConfiguration.getConfiguration();
114 ManagedRepositoryConfiguration repo = new ManagedRepositoryConfiguration();
115 repo.setId( NEW_REPOSITORY_ID );
116 repo.setName( NEW_REPOSITORY_NAME );
117 File repoRoot = new File( "target/test-repository-root" );
118 if ( !repoRoot.exists() )
122 repo.setLocation( repoRoot.getAbsolutePath() );
123 c.addManagedRepository( repo );
124 saveConfiguration( archivaConfiguration );
126 ManagedRepository repository = servlet.getRepository( NEW_REPOSITORY_ID );
127 assertNotNull( repository );
128 assertEquals( NEW_REPOSITORY_NAME, repository.getName() );
130 // check other is still intact
131 assertRepositoryValid( servlet, REPOID_INTERNAL );
135 public void testGetRepositoryInvalidPathPassthroughPresent()
138 String path = REQUEST_PATH + ".index/filecontent/segments.gen";
140 populateRepo( repoRootInternal, ".index/filecontent/segments.gen", "index file" );
142 WebRequest request = new GetMethodWebRequest( path );
143 WebResponse response = getServletUnitClient().getResponse( request );
144 assertResponseOK( response );
145 assertEquals( "index file", response.getContentAsString() );
149 public void testGetRepositoryInvalidPathPassthroughMissing()
152 String path = REQUEST_PATH + ".index/filecontent/foo.bar";
154 WebRequest request = new GetMethodWebRequest( path );
155 WebResponse response = getServletUnitClient().getResponse( request );
156 assertResponseNotFound( response );
157 Assertions.assertThat( response.getContentAsString() ).contains( "Invalid path to Artifact: legacy paths should have an expected type ending in [s] in the second part of the path." );