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