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
22 import com.meterware.httpunit.PutMethodWebRequest;
23 import com.meterware.httpunit.WebRequest;
24 import com.meterware.httpunit.WebResponse;
27 import java.io.InputStream;
29 import javax.servlet.http.HttpServletResponse;
31 import org.apache.archiva.configuration.ManagedRepositoryConfiguration;
32 import org.apache.archiva.webdav.httpunit.MkColMethodWebRequest;
33 import org.junit.Test;
37 * Deploy / Put Test cases for RepositoryServlet.
41 public class RepositoryServletDeployTest
42 extends AbstractRepositoryServletTestCase
44 private static final String ARTIFACT_DEFAULT_LAYOUT = "/path/to/artifact/1.0.0/artifact-1.0.0.jar";
47 public void testPutWithMissingParentCollection()
50 setupCleanRepo( repoRootInternal );
52 String putUrl = "http://machine.com/repository/internal" + ARTIFACT_DEFAULT_LAYOUT;
53 InputStream is = getClass().getResourceAsStream( "/artifact.jar" );
54 // verify that the file exists in resources-dir
55 assertNotNull( "artifact.jar inputstream", is );
57 WebRequest request = new PutMethodWebRequest( putUrl, is, "application/octet-stream" );
59 WebResponse response = getServletUnitClient().getResponse( request );
60 assertResponseCreated( response );
61 assertFileContents( "artifact.jar\n", repoRootInternal, ARTIFACT_DEFAULT_LAYOUT );
66 * test whether trying to overwrite existing relase-artifact is blocked by returning HTTP-code 409
71 public void testReleaseArtifactsRedeploymentValidPath()
74 setupCleanRepo( repoRootInternal );
76 String putUrl = "http://machine.com/repository/internal" + ARTIFACT_DEFAULT_LAYOUT;
77 String metadataUrl = "http://machine.com/repository/internal/path/to/artifact/maven-metadata.xml";
78 String checksumUrl = "http://machine.com/repository/internal" + ARTIFACT_DEFAULT_LAYOUT + ".sha1";
80 InputStream is = getClass().getResourceAsStream( "/artifact.jar" );
81 // verify that the file exists in resources-dir
82 assertNotNull( "artifact.jar inputstream", is );
84 // send request #1 and verify it's successful
85 WebRequest request = new PutMethodWebRequest( putUrl, is, "application/octet-stream" );
86 WebResponse response = getServletUnitClient().getResponse( request );
87 assertResponseCreated( response );
89 is = getClass().getResourceAsStream( "/artifact.jar.sha1" );
90 request = new PutMethodWebRequest( checksumUrl, is, "application/octet-stream" );
91 response = getServletUnitClient().getResponse( request );
92 assertResponseCreated( response );
94 is = getClass().getResourceAsStream( "/maven-metadata.xml" );
95 request = new PutMethodWebRequest( metadataUrl, is, "application/octet-stream" );
96 response = getServletUnitClient().getResponse( request );
97 assertResponseCreated( response );
99 // send request #2 and verify it's blocked
100 is = getClass().getResourceAsStream( "/artifact.jar" );
101 request = new PutMethodWebRequest( putUrl, is, "application/octet-stream" );
102 response = getServletUnitClient().getResponse( request );
103 assertResponseConflictError( response );
107 public void testReleaseArtifactsRedeploymentIsAllowed()
110 setupCleanRepo( repoRootInternal );
112 ManagedRepositoryConfiguration managedRepo = archivaConfiguration.getConfiguration().findManagedRepositoryById( REPOID_INTERNAL );
113 managedRepo.setBlockRedeployments( false );
115 saveConfiguration( archivaConfiguration );
117 String putUrl = "http://machine.com/repository/internal" + ARTIFACT_DEFAULT_LAYOUT;
118 String metadataUrl = "http://machine.com/repository/internal/path/to/artifact/maven-metadata.xml";
119 String checksumUrl = "http://machine.com/repository/internal" + ARTIFACT_DEFAULT_LAYOUT + ".sha1";
121 InputStream is = getClass().getResourceAsStream( "/artifact.jar" );
122 // verify that the file exists in resources-dir
123 assertNotNull( "artifact.jar inputstream", is );
125 // send request #1 and verify it's successful
126 WebRequest request = new PutMethodWebRequest( putUrl, is, "application/octet-stream" );
127 WebResponse response = getServletUnitClient().getResponse( request );
128 assertResponseCreated( response );
130 is = getClass().getResourceAsStream( "/artifact.jar.sha1" );
131 request = new PutMethodWebRequest( checksumUrl, is, "application/octet-stream" );
132 response = getServletUnitClient().getResponse( request );
133 assertResponseCreated( response );
135 is = getClass().getResourceAsStream( "/maven-metadata.xml" );
136 request = new PutMethodWebRequest( metadataUrl, is, "application/octet-stream" );
137 response = getServletUnitClient().getResponse( request );
138 assertResponseCreated( response );
140 // send request #2 and verify if it's still successful
141 is = getClass().getResourceAsStream( "/artifact.jar" );
142 request = new PutMethodWebRequest( putUrl, is, "application/octet-stream" );
143 response = getServletUnitClient().getResponse( request );
144 assertResponseNoContent( response );
148 public void testReleaseArtifactsRedeploymentInvalidPath()
151 setupCleanRepo( repoRootInternal );
153 String putUrl = "http://machine.com/repository/internal/artifact.jar";
154 String metadataUrl = "http://machine.com/repository/internal/maven-metadata.xml";
155 String checksumUrl = "http://machine.com/repository/internal/artifact.jar.sha1";
157 InputStream is = getClass().getResourceAsStream( "/artifact.jar" );
158 // verify that the file exists in resources-dir
159 assertNotNull( "artifact.jar inputstream", is );
161 // send request #1 and verify it's successful
162 WebRequest request = new PutMethodWebRequest( putUrl, is, "application/octet-stream" );
163 WebResponse response = getServletUnitClient().getResponse( request );
164 assertResponseCreated( response );
166 is = getClass().getResourceAsStream( "/artifact.jar.sha1" );
167 request = new PutMethodWebRequest( checksumUrl, is, "application/octet-stream" );
168 response = getServletUnitClient().getResponse( request );
169 assertResponseCreated( response );
171 is = getClass().getResourceAsStream( "/maven-metadata.xml" );
172 request = new PutMethodWebRequest( metadataUrl, is, "application/octet-stream" );
173 response = getServletUnitClient().getResponse( request );
174 assertResponseCreated( response );
176 // send request #2 and verify it's re-deployed
177 is = getClass().getResourceAsStream( "/artifact.jar" );
178 request = new PutMethodWebRequest( putUrl, is, "application/octet-stream" );
179 response = getServletUnitClient().getResponse( request );
180 assertResponseNoContent( response );
184 public void testReleaseArtifactsRedeploymentArtifactIsSnapshot()
187 setupCleanRepo( repoRootInternal );
189 String putUrl = "http://machine.com/repository/internal/path/to/artifact/1.0-SNAPSHOT/artifact-1.0-SNAPSHOT.jar";
190 String metadataUrl = "http://machine.com/repository/internal/path/to/artifact/maven-metadata.xml";
191 String checksumUrl = "http://machine.com/repository/internal/path/to/artifact/1.0-SNAPSHOT/artifact-1.0-SNAPSHOT.jar.sha1";
193 InputStream is = getClass().getResourceAsStream( "/artifact.jar" );
194 // verify that the file exists in resources-dir
195 assertNotNull( "artifact.jar inputstream", is );
197 // send request #1 and verify it's successful
198 WebRequest request = new PutMethodWebRequest( putUrl, is, "application/octet-stream" );
199 WebResponse response = getServletUnitClient().getResponse( request );
200 assertResponseCreated( response );
202 is = getClass().getResourceAsStream( "/artifact.jar.sha1" );
203 request = new PutMethodWebRequest( checksumUrl, is, "application/octet-stream" );
204 response = getServletUnitClient().getResponse( request );
205 assertResponseCreated( response );
207 is = getClass().getResourceAsStream( "/maven-metadata.xml" );
208 request = new PutMethodWebRequest( metadataUrl, is, "application/octet-stream" );
209 response = getServletUnitClient().getResponse( request );
210 assertResponseCreated( response );
212 // send request #2 and verify it's re-deployed
213 is = getClass().getResourceAsStream( "/artifact.jar" );
214 request = new PutMethodWebRequest( putUrl, is, "application/octet-stream" );
215 response = getServletUnitClient().getResponse( request );
216 assertResponseNoContent( response );
220 public void testMkColWithMissingParentCollectionFails()
223 setupCleanRepo( repoRootInternal );
225 String putUrl = "http://machine.com/repository/internal/path/to/";
227 WebRequest request = new MkColMethodWebRequest( putUrl );
229 WebResponse response = getServletUnitClient().getResponse( request );
231 assertEquals(HttpServletResponse.SC_CONFLICT, response.getResponseCode());
233 File mkColLocalPath = new File(repoRootInternal, "path/to/");
234 assertFalse(mkColLocalPath.exists());
237 protected void assertResponseNoContent( WebResponse response )
239 assertNotNull( "Should have recieved a response", response );
240 assertEquals( "Should have been a 204/NO CONTENT response code.", HttpServletResponse.SC_NO_CONTENT, response
241 .getResponseCode() );
244 protected void assertResponseCreated( WebResponse response )
246 assertNotNull( "Should have recieved a response", response );
247 assertEquals( "Should have been a 201/CREATED response code.", HttpServletResponse.SC_CREATED, response
248 .getResponseCode() );