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