1 package org.apache.archiva.webdav;
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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
23 import java.io.InputStream;
25 import javax.servlet.http.HttpServletResponse;
27 import com.gargoylesoftware.htmlunit.WebRequest;
28 import com.gargoylesoftware.htmlunit.WebResponse;
29 import org.apache.archiva.configuration.ManagedRepositoryConfiguration;
30 import org.apache.archiva.webdav.httpunit.MkColMethodWebRequest;
31 import org.junit.Test;
35 * Deploy / Put Test cases for RepositoryServlet.
39 public class RepositoryServletDeployTest
40 extends AbstractRepositoryServletTestCase
42 private static final String ARTIFACT_DEFAULT_LAYOUT = "/path/to/artifact/1.0.0/artifact-1.0.0.jar";
45 public void testPutWithMissingParentCollection()
48 setupCleanRepo( repoRootInternal );
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 );
55 WebRequest request = new PutMethodWebRequest( putUrl, is, "application/octet-stream" );
57 WebResponse response = getServletUnitClient().getResponse( request );
58 assertResponseCreated( response );
59 assertFileContents( "artifact.jar\n", repoRootInternal, ARTIFACT_DEFAULT_LAYOUT );
64 * test whether trying to overwrite existing relase-artifact is blocked by returning HTTP-code 409
69 public void testReleaseArtifactsRedeploymentValidPath()
72 setupCleanRepo( repoRootInternal );
74 String putUrl = "http://machine.com/repository/internal" + ARTIFACT_DEFAULT_LAYOUT;
75 String metadataUrl = "http://machine.com/repository/internal/path/to/artifact/maven-metadata.xml";
76 String checksumUrl = "http://machine.com/repository/internal" + ARTIFACT_DEFAULT_LAYOUT + ".sha1";
78 InputStream is = getClass().getResourceAsStream( "/artifact.jar" );
79 // verify that the file exists in resources-dir
80 assertNotNull( "artifact.jar inputstream", is );
82 // send request #1 and verify it's successful
83 WebRequest request = new PutMethodWebRequest( putUrl, is, "application/octet-stream" );
84 WebResponse response = getServletUnitClient().getResponse( request );
85 assertResponseCreated( response );
87 is = getClass().getResourceAsStream( "/artifact.jar.sha1" );
88 request = new PutMethodWebRequest( checksumUrl, is, "application/octet-stream" );
89 response = getServletUnitClient().getResponse( request );
90 assertResponseCreated( response );
92 is = getClass().getResourceAsStream( "/maven-metadata.xml" );
93 request = new PutMethodWebRequest( metadataUrl, is, "application/octet-stream" );
94 response = getServletUnitClient().getResponse( request );
95 assertResponseCreated( response );
97 // send request #2 and verify it's blocked
98 is = getClass().getResourceAsStream( "/artifact.jar" );
99 request = new PutMethodWebRequest( putUrl, is, "application/octet-stream" );
100 response = getServletUnitClient().getResponse( request );
101 assertResponseConflictError( response );
105 public void testReleaseArtifactsRedeploymentIsAllowed()
108 setupCleanRepo( repoRootInternal );
110 ManagedRepositoryConfiguration managedRepo = archivaConfiguration.getConfiguration().findManagedRepositoryById( REPOID_INTERNAL );
111 managedRepo.setBlockRedeployments( false );
113 saveConfiguration( archivaConfiguration );
115 String putUrl = "http://machine.com/repository/internal" + ARTIFACT_DEFAULT_LAYOUT;
116 String metadataUrl = "http://machine.com/repository/internal/path/to/artifact/maven-metadata.xml";
117 String checksumUrl = "http://machine.com/repository/internal" + ARTIFACT_DEFAULT_LAYOUT + ".sha1";
119 InputStream is = getClass().getResourceAsStream( "/artifact.jar" );
120 // verify that the file exists in resources-dir
121 assertNotNull( "artifact.jar inputstream", is );
123 // send request #1 and verify it's successful
124 WebRequest request = new PutMethodWebRequest( putUrl, is, "application/octet-stream" );
125 WebResponse response = getServletUnitClient().getResponse( request );
126 assertResponseCreated( response );
128 is = getClass().getResourceAsStream( "/artifact.jar.sha1" );
129 request = new PutMethodWebRequest( checksumUrl, is, "application/octet-stream" );
130 response = getServletUnitClient().getResponse( request );
131 assertResponseCreated( response );
133 is = getClass().getResourceAsStream( "/maven-metadata.xml" );
134 request = new PutMethodWebRequest( metadataUrl, is, "application/octet-stream" );
135 response = getServletUnitClient().getResponse( request );
136 assertResponseCreated( response );
138 // send request #2 and verify if it's still successful
139 is = getClass().getResourceAsStream( "/artifact.jar" );
140 request = new PutMethodWebRequest( putUrl, is, "application/octet-stream" );
141 response = getServletUnitClient().getResponse( request );
142 assertResponseNoContent( response );
146 public void testReleaseArtifactsRedeploymentInvalidPath()
149 setupCleanRepo( repoRootInternal );
151 String putUrl = "http://machine.com/repository/internal/artifact.jar";
152 String metadataUrl = "http://machine.com/repository/internal/maven-metadata.xml";
153 String checksumUrl = "http://machine.com/repository/internal/artifact.jar.sha1";
155 InputStream is = getClass().getResourceAsStream( "/artifact.jar" );
156 // verify that the file exists in resources-dir
157 assertNotNull( "artifact.jar inputstream", is );
159 // send request #1 and verify it's successful
160 WebRequest request = new PutMethodWebRequest( putUrl, is, "application/octet-stream" );
161 WebResponse response = getServletUnitClient().getResponse( request );
162 assertResponseCreated( response );
164 is = getClass().getResourceAsStream( "/artifact.jar.sha1" );
165 request = new PutMethodWebRequest( checksumUrl, is, "application/octet-stream" );
166 response = getServletUnitClient().getResponse( request );
167 assertResponseCreated( response );
169 is = getClass().getResourceAsStream( "/maven-metadata.xml" );
170 request = new PutMethodWebRequest( metadataUrl, is, "application/octet-stream" );
171 response = getServletUnitClient().getResponse( request );
172 assertResponseCreated( response );
174 // send request #2 and verify it's re-deployed
175 is = getClass().getResourceAsStream( "/artifact.jar" );
176 request = new PutMethodWebRequest( putUrl, is, "application/octet-stream" );
177 response = getServletUnitClient().getResponse( request );
178 assertResponseNoContent( response );
182 public void testReleaseArtifactsRedeploymentArtifactIsSnapshot()
185 setupCleanRepo( repoRootInternal );
187 String putUrl = "http://machine.com/repository/internal/path/to/artifact/1.0-SNAPSHOT/artifact-1.0-SNAPSHOT.jar";
188 String metadataUrl = "http://machine.com/repository/internal/path/to/artifact/maven-metadata.xml";
189 String checksumUrl = "http://machine.com/repository/internal/path/to/artifact/1.0-SNAPSHOT/artifact-1.0-SNAPSHOT.jar.sha1";
191 InputStream is = getClass().getResourceAsStream( "/artifact.jar" );
192 // verify that the file exists in resources-dir
193 assertNotNull( "artifact.jar inputstream", is );
195 // send request #1 and verify it's successful
196 WebRequest request = new PutMethodWebRequest( putUrl, is, "application/octet-stream" );
197 WebResponse response = getServletUnitClient().getResponse( request );
198 assertResponseCreated( response );
200 is = getClass().getResourceAsStream( "/artifact.jar.sha1" );
201 request = new PutMethodWebRequest( checksumUrl, is, "application/octet-stream" );
202 response = getServletUnitClient().getResponse( request );
203 assertResponseCreated( response );
205 is = getClass().getResourceAsStream( "/maven-metadata.xml" );
206 request = new PutMethodWebRequest( metadataUrl, is, "application/octet-stream" );
207 response = getServletUnitClient().getResponse( request );
208 assertResponseCreated( response );
210 // send request #2 and verify it's re-deployed
211 is = getClass().getResourceAsStream( "/artifact.jar" );
212 request = new PutMethodWebRequest( putUrl, is, "application/octet-stream" );
213 response = getServletUnitClient().getResponse( request );
214 assertResponseNoContent( response );
218 public void testMkColWithMissingParentCollectionFails()
221 setupCleanRepo( repoRootInternal );
223 String putUrl = "http://machine.com/repository/internal/path/to/";
225 WebRequest request = new MkColMethodWebRequest( putUrl );
227 WebResponse response = getServletUnitClient().getResponse( request );
229 assertEquals(HttpServletResponse.SC_CONFLICT, response.getStatusCode());
231 File mkColLocalPath = new File(repoRootInternal, "path/to/");
232 assertFalse(mkColLocalPath.exists());
235 protected void assertResponseNoContent( WebResponse response )
237 assertNotNull( "Should have recieved a response", response );
238 assertEquals( "Should have been a 204/NO CONTENT response code.", HttpServletResponse.SC_NO_CONTENT, response
242 protected void assertResponseCreated( WebResponse response )
244 assertNotNull( "Should have recieved a response", response );
245 assertEquals( "Should have been a 201/CREATED response code.", HttpServletResponse.SC_CREATED, response