View Javadoc
1   package org.apache.archiva.webdav;
2   
3   /*
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
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
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
19   * under the License.
20   */
21  
22  import junit.framework.TestCase;
23  import org.apache.archiva.test.utils.ArchivaBlockJUnit4ClassRunner;
24  import org.junit.Before;
25  import org.junit.Test;
26  import org.junit.runner.RunWith;
27  
28  /**
29   */
30  @RunWith( ArchivaBlockJUnit4ClassRunner.class )
31  public class ArchivaDavResourceLocatorTest
32      extends TestCase
33  {
34      ArchivaDavLocatorFactory factory;
35  
36      @Override
37      @Before
38      public void setUp()
39          throws Exception
40      {
41          super.setUp();
42          factory = new ArchivaDavLocatorFactory();
43      }
44  
45      @Test
46      public void testAvoidDoubleSlashInHref()
47          throws Exception
48      {
49          String prefix = "http://myproxy/";
50          String href = "/repository/internal/";
51          ArchivaDavResourceLocator locator = getLocator( prefix, href );
52  
53          assertEquals( "internal", locator.getRepositoryId() );
54          assertEquals( "", locator.getWorkspaceName() );
55          assertEquals( "", locator.getWorkspacePath() );
56          assertEquals( "http://myproxy/", locator.getPrefix() );
57          assertEquals( "http://myproxy/repository/internal/", locator.getHref( false ) );
58          assertEquals( "http://myproxy/repository/internal/", locator.getHref( true ) );
59          assertEquals( "/repository/internal", locator.getResourcePath() );
60          assertEquals( "/repository/internal", locator.getRepositoryPath() );
61      }
62  
63      @Test
64      public void testLocatorWithPrefixHref()
65          throws Exception
66      {
67          String prefix = "http://myproxy/";
68          String href = "/repository/internal";
69          ArchivaDavResourceLocator locator = getLocator( prefix, href );
70  
71          assertEquals( "internal", locator.getRepositoryId() );
72          assertEquals( "", locator.getWorkspaceName() );
73          assertEquals( "", locator.getWorkspacePath() );
74          assertEquals( "http://myproxy/", locator.getPrefix() );
75          assertEquals( "http://myproxy/repository/internal", locator.getHref( false ) );
76          assertEquals( "http://myproxy/repository/internal/", locator.getHref( true ) );
77          assertEquals( "/repository/internal", locator.getResourcePath() );
78          assertEquals( "/repository/internal", locator.getRepositoryPath() );
79      }
80  
81      @Test
82      public void testLocatorWithHrefThatContainsPrefix()
83          throws Exception
84      {
85          String prefix = "http://myproxy/";
86          String href = "http://myproxy/repository/internal";
87          ArchivaDavResourceLocator locator = getLocator( prefix, href );
88  
89          assertEquals( "internal", locator.getRepositoryId() );
90          assertEquals( "", locator.getWorkspaceName() );
91          assertEquals( "", locator.getWorkspacePath() );
92          assertEquals( "http://myproxy/", locator.getPrefix() );
93          assertEquals( "http://myproxy/repository/internal", locator.getHref( false ) );
94          assertEquals( "http://myproxy/repository/internal/", locator.getHref( true ) );
95          assertEquals( "/repository/internal", locator.getResourcePath() );
96          assertEquals( "/repository/internal", locator.getRepositoryPath() );
97      }
98  
99      @Test
100     public void testLocatorWithRootHref()
101         throws Exception
102     {
103         String prefix = "http://myproxy/";
104         String href = "/";
105         ArchivaDavResourceLocator locator = getLocator( prefix, href );
106 
107         assertEquals( "", locator.getRepositoryId() );
108         assertEquals( "", locator.getWorkspaceName() );
109         assertEquals( "", locator.getWorkspacePath() );
110         assertEquals( "http://myproxy/", locator.getPrefix() );
111         assertEquals( "http://myproxy/", locator.getHref( false ) );
112         assertEquals( "http://myproxy/", locator.getHref( true ) );
113         assertEquals( "/", locator.getResourcePath() );
114         assertEquals( "/", locator.getRepositoryPath() );
115     }
116 
117     private ArchivaDavResourceLocator getLocator( String prefix, String href )
118     {
119         return (ArchivaDavResourceLocator) factory.createResourceLocator( prefix, href );
120     }
121 }