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.PutMethodWebRequest;
23  import com.meterware.httpunit.WebRequest;
24  import com.meterware.httpunit.WebResponse;
25  
26  import java.io.File;
27  import java.io.InputStream;
28  
29  import javax.servlet.http.HttpServletResponse;
30  
31  import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
32  import org.apache.maven.archiva.webdav.httpunit.MkColMethodWebRequest;
33  
34  
35  /**
36   * Deploy / Put Test cases for RepositoryServlet.  
37   *
38   * @version $Id: RepositoryServletDeployTest.java 883052 2009-11-22 09:32:35Z oching $
39   */
40  public class RepositoryServletDeployTest
41      extends AbstractRepositoryServletTestCase
42  {
43      private static final String ARTIFACT_DEFAULT_LAYOUT = "/path/to/artifact/1.0.0/artifact-1.0.0.jar";
44  	
45      public void testPutWithMissingParentCollection()
46          throws Exception
47      {
48          setupCleanRepo( repoRootInternal );
49  
50          String putUrl = "http://machine.com/repository/internal" + ARTIFACT_DEFAULT_LAYOUT;
51          InputStream is = getClass().getResourceAsStream( "/artifact.jar" );
52          // verify that the file exists in resources-dir
53          assertNotNull( "artifact.jar inputstream", is );
54  
55          WebRequest request = new PutMethodWebRequest( putUrl, is, "application/octet-stream" );
56  
57          WebResponse response = sc.getResponse( request );
58          assertResponseCreated( response );
59          assertFileContents( "artifact.jar\n", repoRootInternal, ARTIFACT_DEFAULT_LAYOUT );
60      }    
61  
62      /**
63       * MRM-747
64       * test whether trying to overwrite existing relase-artifact is blocked by returning HTTP-code 409 
65       * 
66       * @throws Exception
67       */
68      public void testReleaseArtifactsRedeploymentValidPath()
69          throws Exception
70      {
71          setupCleanRepo( repoRootInternal );
72  
73          String putUrl = "http://machine.com/repository/internal" + ARTIFACT_DEFAULT_LAYOUT;
74          String metadataUrl = "http://machine.com/repository/internal/path/to/artifact/maven-metadata.xml";
75          String checksumUrl = "http://machine.com/repository/internal" + ARTIFACT_DEFAULT_LAYOUT + ".sha1";
76          
77          InputStream is = getClass().getResourceAsStream( "/artifact.jar" );
78          // verify that the file exists in resources-dir
79          assertNotNull( "artifact.jar inputstream", is );
80  
81          // send request #1 and verify it's successful
82          WebRequest request = new PutMethodWebRequest( putUrl, is, "application/octet-stream" );
83          WebResponse response = sc.getResponse( request );
84          assertResponseCreated( response );
85          
86          is = getClass().getResourceAsStream( "/artifact.jar.sha1" );
87          request = new PutMethodWebRequest( checksumUrl, is, "application/octet-stream" );
88          response = sc.getResponse( request );
89          assertResponseCreated( response );
90          
91          is = getClass().getResourceAsStream( "/maven-metadata.xml" );
92          request = new PutMethodWebRequest( metadataUrl, is, "application/octet-stream" );
93          response = sc.getResponse( request );
94          assertResponseCreated( response );
95          
96          // send request #2 and verify it's blocked
97          is = getClass().getResourceAsStream( "/artifact.jar" );
98          request = new PutMethodWebRequest( putUrl, is, "application/octet-stream" );
99          response = sc.getResponse( request );
100         assertResponseConflictError( response );        
101     }
102     
103     public void testReleaseArtifactsRedeploymentIsAllowed()
104         throws Exception
105     {
106         setupCleanRepo( repoRootInternal );
107         
108         ManagedRepositoryConfiguration managedRepo = archivaConfiguration.getConfiguration().findManagedRepositoryById( REPOID_INTERNAL );
109         managedRepo.setBlockRedeployments( false );
110         
111         saveConfiguration( archivaConfiguration );
112     
113         String putUrl = "http://machine.com/repository/internal" + ARTIFACT_DEFAULT_LAYOUT;
114         String metadataUrl = "http://machine.com/repository/internal/path/to/artifact/maven-metadata.xml";
115         String checksumUrl = "http://machine.com/repository/internal" + ARTIFACT_DEFAULT_LAYOUT + ".sha1";
116         
117         InputStream is = getClass().getResourceAsStream( "/artifact.jar" );
118         // verify that the file exists in resources-dir
119         assertNotNull( "artifact.jar inputstream", is );
120     
121         // send request #1 and verify it's successful
122         WebRequest request = new PutMethodWebRequest( putUrl, is, "application/octet-stream" );
123         WebResponse response = sc.getResponse( request );
124         assertResponseCreated( response );
125         
126         is = getClass().getResourceAsStream( "/artifact.jar.sha1" );
127         request = new PutMethodWebRequest( checksumUrl, is, "application/octet-stream" );
128         response = sc.getResponse( request );
129         assertResponseCreated( response );
130         
131         is = getClass().getResourceAsStream( "/maven-metadata.xml" );
132         request = new PutMethodWebRequest( metadataUrl, is, "application/octet-stream" );
133         response = sc.getResponse( request );
134         assertResponseCreated( response );
135         
136         // send request #2 and verify if it's still successful
137         is = getClass().getResourceAsStream( "/artifact.jar" );
138         request = new PutMethodWebRequest( putUrl, is, "application/octet-stream" );
139         response = sc.getResponse( request );
140         assertResponseNoContent( response );        
141     }
142     
143     public void testReleaseArtifactsRedeploymentInvalidPath()
144         throws Exception
145     {
146         setupCleanRepo( repoRootInternal );
147 
148         String putUrl = "http://machine.com/repository/internal/artifact.jar";
149         String metadataUrl = "http://machine.com/repository/internal/maven-metadata.xml";
150         String checksumUrl = "http://machine.com/repository/internal/artifact.jar.sha1";
151         
152         InputStream is = getClass().getResourceAsStream( "/artifact.jar" );
153         // verify that the file exists in resources-dir
154         assertNotNull( "artifact.jar inputstream", is );
155 
156         // send request #1 and verify it's successful
157         WebRequest request = new PutMethodWebRequest( putUrl, is, "application/octet-stream" );
158         WebResponse response = sc.getResponse( request );
159         assertResponseCreated( response );
160         
161         is = getClass().getResourceAsStream( "/artifact.jar.sha1" );
162         request = new PutMethodWebRequest( checksumUrl, is, "application/octet-stream" );
163         response = sc.getResponse( request );
164         assertResponseCreated( response );
165         
166         is = getClass().getResourceAsStream( "/maven-metadata.xml" );
167         request = new PutMethodWebRequest( metadataUrl, is, "application/octet-stream" );
168         response = sc.getResponse( request );
169         assertResponseCreated( response );
170         
171         // send request #2 and verify it's re-deployed
172         is = getClass().getResourceAsStream( "/artifact.jar" );
173         request = new PutMethodWebRequest( putUrl, is, "application/octet-stream" );
174         response = sc.getResponse( request );
175         assertResponseNoContent( response );
176     } 
177     
178     public void testReleaseArtifactsRedeploymentArtifactIsSnapshot()
179         throws Exception
180     {
181         setupCleanRepo( repoRootInternal );
182 
183         String putUrl = "http://machine.com/repository/internal/path/to/artifact/1.0-SNAPSHOT/artifact-1.0-SNAPSHOT.jar";
184         String metadataUrl = "http://machine.com/repository/internal/path/to/artifact/maven-metadata.xml";
185         String checksumUrl = "http://machine.com/repository/internal/path/to/artifact/1.0-SNAPSHOT/artifact-1.0-SNAPSHOT.jar.sha1";
186         
187         InputStream is = getClass().getResourceAsStream( "/artifact.jar" );
188         // verify that the file exists in resources-dir
189         assertNotNull( "artifact.jar inputstream", is );
190 
191         // send request #1 and verify it's successful
192         WebRequest request = new PutMethodWebRequest( putUrl, is, "application/octet-stream" );
193         WebResponse response = sc.getResponse( request );
194         assertResponseCreated( response );
195         
196         is = getClass().getResourceAsStream( "/artifact.jar.sha1" );
197         request = new PutMethodWebRequest( checksumUrl, is, "application/octet-stream" );
198         response = sc.getResponse( request );
199         assertResponseCreated( response );
200         
201         is = getClass().getResourceAsStream( "/maven-metadata.xml" );
202         request = new PutMethodWebRequest( metadataUrl, is, "application/octet-stream" );
203         response = sc.getResponse( request );
204         assertResponseCreated( response );
205         
206         // send request #2 and verify it's re-deployed
207         is = getClass().getResourceAsStream( "/artifact.jar" );
208         request = new PutMethodWebRequest( putUrl, is, "application/octet-stream" );
209         response = sc.getResponse( request );
210         assertResponseNoContent( response );
211     } 
212     
213     public void testMkColWithMissingParentCollectionFails()
214         throws Exception
215     {
216         setupCleanRepo( repoRootInternal );
217 
218         String putUrl = "http://machine.com/repository/internal/path/to/";
219 
220         WebRequest request = new MkColMethodWebRequest( putUrl );
221 
222         WebResponse response = sc.getResponse( request );
223         
224         assertEquals(HttpServletResponse.SC_CONFLICT, response.getResponseCode());
225         
226         File mkColLocalPath = new File(repoRootInternal, "path/to/");
227         assertFalse(mkColLocalPath.exists());
228     }
229     
230     protected void assertResponseNoContent( WebResponse response )
231     {
232         assertNotNull( "Should have recieved a response", response );
233         assertEquals( "Should have been a 204/NO CONTENT response code.", HttpServletResponse.SC_NO_CONTENT, response
234             .getResponseCode() );
235     }
236     
237     protected void assertResponseCreated( WebResponse response )
238     {
239         assertNotNull( "Should have recieved a response", response );
240         assertEquals( "Should have been a 201/CREATED response code.", HttpServletResponse.SC_CREATED, response
241             .getResponseCode() );
242     }
243 }