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