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 com.gargoylesoftware.htmlunit.WebRequest;
23  import com.gargoylesoftware.htmlunit.WebResponse;
24  import org.apache.archiva.configuration.ProxyConnectorConfiguration;
25  import org.apache.archiva.policies.SnapshotsPolicy;
26  import org.junit.After;
27  import org.junit.Before;
28  import org.junit.Test;
29  
30  import java.io.File;
31  import java.util.ArrayList;
32  
33  /**
34   * RepositoryServlet Tests, Proxied, Get of Timestamped Snapshot Artifacts, with varying policy settings.
35   *
36   *
37   */
38  public class RepositoryServletProxiedTimestampedSnapshotPolicyTest
39      extends AbstractRepositoryServletProxiedTestCase
40  {
41  
42      @Before
43      public void setup()
44          throws Exception
45      {
46          archivaConfiguration.getConfiguration().setProxyConnectors( new ArrayList<ProxyConnectorConfiguration>() );
47          super.setUp();
48      }
49  
50      @After
51      @Override
52      public void tearDown()
53          throws Exception
54      {
55          archivaConfiguration.getConfiguration().setProxyConnectors( new ArrayList<ProxyConnectorConfiguration>() );
56  
57          super.tearDown();
58      }
59  
60      @Test
61      public void testGetProxiedSnapshotsArtifactPolicyAlwaysManagedNewer()
62          throws Exception
63      {
64          assertGetProxiedSnapshotsArtifactWithPolicy( EXPECT_MANAGED_CONTENTS, SnapshotsPolicy.ALWAYS, HAS_MANAGED_COPY,
65                                                       ( NEWER * OVER_ONE_DAY ) );
66      }
67  
68      @Test
69      public void testGetProxiedSnapshotsArtifactPolicyAlwaysManagedOlder()
70          throws Exception
71      {
72          assertGetProxiedSnapshotsArtifactWithPolicy( EXPECT_REMOTE_CONTENTS, SnapshotsPolicy.ALWAYS, HAS_MANAGED_COPY,
73                                                       ( OLDER * OVER_ONE_DAY ) );
74      }
75  
76      @Test
77      public void testGetProxiedSnapshotsArtifactPolicyAlwaysNoManagedContent()
78          throws Exception
79      {
80          assertGetProxiedSnapshotsArtifactWithPolicy( EXPECT_REMOTE_CONTENTS, SnapshotsPolicy.ALWAYS, NO_MANAGED_COPY );
81      }
82  
83      @Test
84      public void testGetProxiedSnapshotsArtifactPolicyDailyFail()
85          throws Exception
86      {
87          assertGetProxiedSnapshotsArtifactWithPolicy( EXPECT_MANAGED_CONTENTS, SnapshotsPolicy.DAILY, HAS_MANAGED_COPY,
88                                                       ( NEWER * ONE_MINUTE ) );
89      }
90  
91      @Test
92      public void testGetProxiedSnapshotsArtifactPolicyDailyNoManagedContent()
93          throws Exception
94      {
95          assertGetProxiedSnapshotsArtifactWithPolicy( EXPECT_REMOTE_CONTENTS, SnapshotsPolicy.DAILY, NO_MANAGED_COPY );
96      }
97  
98      @Test
99      public void testGetProxiedSnapshotsArtifactPolicyDailyPass()
100         throws Exception
101     {
102         assertGetProxiedSnapshotsArtifactWithPolicy( EXPECT_REMOTE_CONTENTS, SnapshotsPolicy.DAILY, HAS_MANAGED_COPY,
103                                                      ( OLDER * OVER_ONE_DAY ) );
104     }
105 
106     @Test
107     public void testGetProxiedSnapshotsArtifactPolicyRejectFail()
108         throws Exception
109     {
110         assertGetProxiedSnapshotsArtifactWithPolicy( EXPECT_MANAGED_CONTENTS, SnapshotsPolicy.NEVER, HAS_MANAGED_COPY );
111     }
112 
113     @Test
114     public void testGetProxiedSnapshotsArtifactPolicyRejectNoManagedContentFail()
115         throws Exception
116     {
117         assertGetProxiedSnapshotsArtifactWithPolicy( EXPECT_NOT_FOUND, SnapshotsPolicy.NEVER, NO_MANAGED_COPY );
118     }
119 
120     @Test
121     public void testGetProxiedSnapshotsArtifactPolicyRejectPass()
122         throws Exception
123     {
124         assertGetProxiedSnapshotsArtifactWithPolicy( EXPECT_MANAGED_CONTENTS, SnapshotsPolicy.NEVER, HAS_MANAGED_COPY );
125     }
126 
127     @Test
128     public void testGetProxiedSnapshotsArtifactPolicyHourlyFail()
129         throws Exception
130     {
131         assertGetProxiedSnapshotsArtifactWithPolicy( EXPECT_MANAGED_CONTENTS, SnapshotsPolicy.HOURLY, HAS_MANAGED_COPY,
132                                                      ( NEWER * ONE_MINUTE ) );
133     }
134 
135     @Test
136     public void testGetProxiedSnapshotsArtifactPolicyHourlyNoManagedContent()
137         throws Exception
138     {
139         assertGetProxiedSnapshotsArtifactWithPolicy( EXPECT_REMOTE_CONTENTS, SnapshotsPolicy.HOURLY, NO_MANAGED_COPY );
140     }
141 
142     @Test
143     public void testGetProxiedSnapshotsArtifactPolicyHourlyPass()
144         throws Exception
145     {
146         assertGetProxiedSnapshotsArtifactWithPolicy( EXPECT_REMOTE_CONTENTS, SnapshotsPolicy.HOURLY, HAS_MANAGED_COPY,
147                                                      ( OLDER * OVER_ONE_HOUR ) );
148     }
149 
150     @Test
151     public void testGetProxiedSnapshotsArtifactPolicyOnceFail()
152         throws Exception
153     {
154         assertGetProxiedSnapshotsArtifactWithPolicy( EXPECT_MANAGED_CONTENTS, SnapshotsPolicy.ONCE, HAS_MANAGED_COPY );
155     }
156 
157     @Test
158     public void testGetProxiedSnapshotsArtifactPolicyOnceNoManagedContent()
159         throws Exception
160     {
161         assertGetProxiedSnapshotsArtifactWithPolicy( EXPECT_REMOTE_CONTENTS, SnapshotsPolicy.ONCE, NO_MANAGED_COPY );
162     }
163 
164     @Test
165     public void testGetProxiedSnapshotsArtifactPolicyOncePass()
166         throws Exception
167     {
168         assertGetProxiedSnapshotsArtifactWithPolicy( EXPECT_REMOTE_CONTENTS, SnapshotsPolicy.ONCE, NO_MANAGED_COPY );
169     }
170 
171     private void assertGetProxiedSnapshotsArtifactWithPolicy( int expectation, String snapshotsPolicy,
172                                                               boolean hasManagedCopy )
173         throws Exception
174     {
175         assertGetProxiedSnapshotsArtifactWithPolicy( expectation, snapshotsPolicy, hasManagedCopy, 0 );
176     }
177 
178     private void assertGetProxiedSnapshotsArtifactWithPolicy( int expectation, String snapshotsPolicy,
179                                                               boolean hasManagedCopy,
180                                                               long deltaManagedToRemoteTimestamp )
181         throws Exception
182     {
183         // --- Setup
184         setupSnapshotsRemoteRepo();
185         setupCleanInternalRepo();
186 
187         String resourcePath = "org/apache/archiva/test/3.0-SNAPSHOT/test-3.0-20070822.033400-42.jar";
188         String expectedRemoteContents = "archiva-test-3.0-20070822.033400-42|jar-remote-contents";
189         String expectedManagedContents = null;
190         File remoteFile = populateRepo( remoteSnapshots, resourcePath, expectedRemoteContents );
191 
192         if ( hasManagedCopy )
193         {
194             expectedManagedContents = "archiva-test-3.0-20070822.033400-42|jar-managed-contents";
195             File managedFile = populateRepo( repoRootInternal, resourcePath, expectedManagedContents );
196             managedFile.setLastModified( remoteFile.lastModified() + deltaManagedToRemoteTimestamp );
197         }
198 
199         setupSnapshotConnector( REPOID_INTERNAL, remoteSnapshots, snapshotsPolicy );
200         saveConfiguration();
201 
202         // --- Execution
203         // process the response code later, not via an exception.
204         //HttpUnitOptions.setExceptionsThrownOnErrorStatus( false );
205 
206         WebRequest request = new GetMethodWebRequest( "http://machine.com/repository/internal/" + resourcePath );
207         WebResponse response = getServletUnitClient().getResponse( request );
208 
209         // --- Verification
210 
211         switch ( expectation )
212         {
213             case EXPECT_MANAGED_CONTENTS:
214                 assertResponseOK( response );
215                 assertTrue( "Invalid Test Case: Can't expect managed contents with "
216                                 + "test that doesn't have a managed copy in the first place.", hasManagedCopy );
217                 assertEquals( "Expected managed file contents", expectedManagedContents, response.getContentAsString() );
218                 break;
219             case EXPECT_REMOTE_CONTENTS:
220                 assertResponseOK( response );
221                 assertEquals( "Expected remote file contents", expectedRemoteContents, response.getContentAsString() );
222                 break;
223             case EXPECT_NOT_FOUND:
224                 assertResponseNotFound( response );
225                 assertManagedFileNotExists( repoRootInternal, resourcePath );
226                 break;
227         }
228     }
229 }