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