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