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
23 import com.gargoylesoftware.htmlunit.WebRequest;
24 import com.gargoylesoftware.htmlunit.WebResponse;
25 import org.fest.assertions.api.Assertions;
26 import org.jsoup.Jsoup;
27 import org.jsoup.nodes.Document;
28 import org.jsoup.nodes.Element;
29 import org.jsoup.select.Elements;
30 import org.junit.Before;
31 import org.junit.Test;
33 import javax.servlet.http.HttpServletResponse;
35 import java.util.Arrays;
36 import java.util.List;
39 * RepositoryServletBrowseTest
41 public class RepositoryServletBrowseTest
42 extends AbstractRepositoryServletTestCase
51 new File( repoRootInternal, "org/apache/archiva" ).mkdirs();
52 new File( repoRootInternal, "org/codehaus/mojo/" ).mkdirs();
53 new File( repoRootInternal, "net/sourceforge" ).mkdirs();
54 new File( repoRootInternal, "commons-lang" ).mkdirs();
60 public void testBrowse()
63 WebRequest request = new GetMethodWebRequest( "http://machine.com/repository/internal/" );
64 WebResponse response = getServletUnitClient().getResponse( request );
65 assertEquals( "Response", HttpServletResponse.SC_OK, response.getStatusCode() );
67 // dumpResponse( response );
69 List<String> expectedLinks = Arrays.asList( ".indexer/", "commons-lang/", "net/", "org/" );
71 Document document = Jsoup.parse( response.getContentAsString() );
72 Elements elements = document.getElementsByTag( "a" );
74 assertLinks( expectedLinks, elements );
78 public void testBrowseSubdirectory()
81 WebRequest request = new GetMethodWebRequest( "http://machine.com/repository/internal/org" );
82 WebResponse response = getServletUnitClient().getResponse( request );
83 assertEquals( "Response", HttpServletResponse.SC_OK, response.getStatusCode() );
85 List<String> expectedLinks = Arrays.asList( "../", "apache/", "codehaus/" );
87 Document document = Jsoup.parse( response.getContentAsString() );
88 Elements elements = document.getElementsByTag( "a" );
90 assertLinks( expectedLinks, elements );
94 public void testGetDirectoryWhichHasMatchingFile() //MRM-893
97 new File( repoRootInternal, "org/apache/archiva/artifactId/1.0" ).mkdirs();
98 new File( repoRootInternal, "org/apache/archiva/artifactId/1.0/artifactId-1.0.jar" ).createNewFile();
101 new GetMethodWebRequest( "http://machine.com/repository/internal/org/apache/archiva/artifactId" );
102 WebResponse response = getServletUnitClient().getResponse( request );
103 assertEquals( "1st Response", HttpServletResponse.SC_OK, response.getStatusCode() );
105 request = new GetMethodWebRequest( "http://machine.com/repository/internal/org/apache/archiva/artifactId/" );
106 response = getServletUnitClient().getResponse( request );
107 assertEquals( "2nd Response", HttpServletResponse.SC_OK, response.getStatusCode() );
109 request = new GetMethodWebRequest(
110 "http://machine.com/repository/internal/org/apache/archiva/artifactId/1.0/artifactId-1.0.jar" );
111 response = getServletUnitClient().getResponse( request );
112 assertEquals( "3rd Response", HttpServletResponse.SC_OK, response.getStatusCode() );
114 request = new GetMethodWebRequest(
115 "http://machine.com/repository/internal/org/apache/archiva/artifactId/1.0/artifactId-1.0.jar/" );
116 response = getServletUnitClient().getResponse( request );
117 assertEquals( "4th Response", HttpServletResponse.SC_NOT_FOUND, response.getStatusCode() );
120 private void assertLinks( List<String> expectedLinks, Elements actualLinks )
122 Assertions.assertThat( actualLinks ).hasSize( expectedLinks.size() );
124 for ( int i = 0; i < actualLinks.size(); i++ )
126 Element element = actualLinks.get( i );
127 assertEquals( "Link[" + i + "]", expectedLinks.get( i ), element.attr( "href" ) );