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