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 java.io.File;
23  
24  import com.gargoylesoftware.htmlunit.WebRequest;
25  import com.gargoylesoftware.htmlunit.WebResponse;
26  import org.junit.After;
27  import org.junit.Before;
28  import org.junit.Test;
29  
30  /**
31   * RepositoryServlet Tests, Proxied, Get of resources that are not artifacts or metadata, with varying policy settings.
32   * 
33   */
34  public class RepositoryServletProxiedPassthroughTest
35      extends AbstractRepositoryServletProxiedTestCase
36  {
37      private static final String CONTENT_SHA1 = "2aab0a51c04c9023636852f3e63a68034ba10142";
38  
39      private static final String PATH_SHA1 = "org/apache/archiva/test/1.0/test-1.0.jar.sha1";
40  
41      private static final String CONTENT_ASC =
42          "-----BEGIN PGP SIGNATURE-----\n" + "Version: GnuPG v1.4.8 (Darwin)\n" + "\n"
43              + "iEYEABECAAYFAkiAIVgACgkQxbsDNW2stZZjyACeK3LW+ZDeawCyJj4XgvUaJkNh\n"
44              + "qIEAoIUiijY4Iw82RWOT75Rt3yZuY6ZI\n" + "=WLkm\n" + "-----END PGP SIGNATURE-----\n";
45  
46      private static final String PATH_ASC = "org/apache/archiva/test/1.0/test-1.0.jar.asc";
47  
48      @Before
49      public void setup()
50          throws Exception
51      {
52          super.setUp();
53      }
54  
55      @After
56      @Override
57      public void tearDown()
58          throws Exception
59      {
60          super.tearDown();
61      }
62  
63      @Test
64      public void testGetProxiedManagedNewerSha1()
65          throws Exception
66      {
67          assertGetProxiedResource( EXPECT_MANAGED_CONTENTS, HAS_MANAGED_COPY, ( NEWER * OVER_ONE_DAY ), PATH_SHA1,
68                                    CONTENT_SHA1 );
69      }
70  
71      @Test
72      public void testGetProxiedManagedOlderSha1()
73          throws Exception
74      {
75          assertGetProxiedResource( EXPECT_REMOTE_CONTENTS, HAS_MANAGED_COPY, ( OLDER * OVER_ONE_DAY ), PATH_SHA1,
76                                    CONTENT_SHA1 );
77      }
78  
79      @Test
80      public void testGetProxiedNoManagedContentSha1()
81          throws Exception
82      {
83          assertGetProxiedResource( EXPECT_REMOTE_CONTENTS, NO_MANAGED_COPY, PATH_SHA1, CONTENT_SHA1 );
84      }
85  
86      @Test
87      public void testGetProxiedEqualSha1()
88          throws Exception
89      {
90          assertGetProxiedResource( EXPECT_MANAGED_CONTENTS, HAS_MANAGED_COPY, PATH_SHA1, CONTENT_SHA1 );
91      }
92  
93      @Test
94      public void testGetProxiedManagedNewerAsc()
95          throws Exception
96      {
97          assertGetProxiedResource( EXPECT_MANAGED_CONTENTS, HAS_MANAGED_COPY, ( NEWER * OVER_ONE_DAY ), PATH_ASC,
98                                    CONTENT_ASC );
99      }
100 
101     @Test
102     public void testGetProxiedManagedOlderAsc()
103         throws Exception
104     {
105         assertGetProxiedResource( EXPECT_REMOTE_CONTENTS, HAS_MANAGED_COPY, ( OLDER * OVER_ONE_DAY ), PATH_ASC,
106                                   CONTENT_ASC );
107     }
108 
109     @Test
110     public void testGetProxiedNoManagedContentAsc()
111         throws Exception
112     {
113         assertGetProxiedResource( EXPECT_REMOTE_CONTENTS, NO_MANAGED_COPY, PATH_ASC, CONTENT_ASC );
114     }
115 
116     @Test
117     public void testGetProxiedEqualAsc()
118         throws Exception
119     {
120         assertGetProxiedResource( EXPECT_MANAGED_CONTENTS, HAS_MANAGED_COPY, PATH_ASC, CONTENT_ASC );
121     }
122 
123     private void assertGetProxiedResource( int expectation, boolean hasManagedCopy, String path, String content )
124         throws Exception
125     {
126         assertGetProxiedResource( expectation, hasManagedCopy, 0, path, content );
127     }
128 
129     private void assertGetProxiedResource( int expectation, boolean hasManagedCopy, long deltaManagedToRemoteTimestamp,
130                                            String path, String contents )
131         throws Exception
132     {
133         // --- Setup
134         setupCentralRemoteRepo();
135         setupCleanInternalRepo();
136 
137         String expectedRemoteContents = contents;
138         String expectedManagedContents = null;
139         File remoteFile = populateRepo( remoteCentral, path, expectedRemoteContents );
140 
141         if ( hasManagedCopy )
142         {
143             expectedManagedContents = contents;
144             File managedFile = populateRepo( repoRootInternal, path, expectedManagedContents );
145             managedFile.setLastModified( remoteFile.lastModified() + deltaManagedToRemoteTimestamp );
146         }
147 
148         setupConnector( REPOID_INTERNAL, remoteCentral );
149         saveConfiguration();
150 
151         // --- Execution
152         // process the response code later, not via an exception.
153         //HttpUnitOptions.setExceptionsThrownOnErrorStatus( false );
154 
155         WebRequest request = new GetMethodWebRequest( "http://machine.com/repository/internal/" + path );
156         WebResponse response = getServletUnitClient().getResponse( request );
157 
158         // --- Verification
159 
160         switch ( expectation )
161         {
162             case EXPECT_MANAGED_CONTENTS:
163                 assertResponseOK( response );
164                 assertTrue( "Invalid Test Case: Can't expect managed contents with "
165                     + "test that doesn't have a managed copy in the first place.", hasManagedCopy );
166                 assertEquals( "Expected managed file contents", expectedManagedContents, response.getContentAsString() );
167                 break;
168             case EXPECT_REMOTE_CONTENTS:
169                 assertResponseOK( response, path );
170                 assertEquals( "Expected remote file contents", expectedRemoteContents, response.getContentAsString() );
171                 break;
172             case EXPECT_NOT_FOUND:
173                 assertResponseNotFound( response );
174                 assertManagedFileNotExists( repoRootInternal, path );
175                 break;
176         }
177     }
178 }