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