View Javadoc

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