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