1 package org.apache.maven.archiva.webdav;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import com.meterware.httpunit.WebResponse;
23 import com.meterware.httpunit.HttpUnitOptions;
24 import com.meterware.servletunit.ServletRunner;
25 import com.meterware.servletunit.ServletUnitClient;
26 import net.sf.ehcache.CacheManager;
27 import org.apache.commons.io.FileUtils;
28 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
29 import org.apache.maven.archiva.configuration.Configuration;
30 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
31 import org.apache.maven.archiva.configuration.RemoteRepositoryConfiguration;
32 import org.apache.maven.archiva.webdav.RepositoryServlet;
33 import org.codehaus.plexus.spring.PlexusInSpringTestCase;
34
35 import javax.servlet.http.HttpServletResponse;
36 import java.io.File;
37 import java.io.IOException;
38
39 import junit.framework.Assert;
40
41
42
43
44
45
46 public abstract class AbstractRepositoryServletTestCase
47 extends PlexusInSpringTestCase
48 {
49 protected static final String REPOID_INTERNAL = "internal";
50
51 protected ServletUnitClient sc;
52
53 protected File repoRootInternal;
54
55 private ServletRunner sr;
56
57 protected ArchivaConfiguration archivaConfiguration;
58
59 protected void saveConfiguration()
60 throws Exception
61 {
62 saveConfiguration( archivaConfiguration );
63 }
64
65 protected void assertFileContents( String expectedContents, File repoRoot, String path )
66 throws IOException
67 {
68 File actualFile = new File( repoRoot, path );
69 assertTrue( "File <" + actualFile.getAbsolutePath() + "> should exist.", actualFile.exists() );
70 assertTrue( "File <" + actualFile.getAbsolutePath() + "> should be a file (not a dir/link/device/etc).",
71 actualFile.isFile() );
72
73 String actualContents = FileUtils.readFileToString( actualFile, null );
74 assertEquals( "File Contents of <" + actualFile.getAbsolutePath() + ">", expectedContents, actualContents );
75 }
76
77 protected void assertRepositoryValid( RepositoryServlet servlet, String repoId )
78 {
79 ManagedRepositoryConfiguration repository = servlet.getRepository( repoId );
80 assertNotNull( "Archiva Managed Repository id:<" + repoId + "> should exist.", repository );
81 File repoRoot = new File( repository.getLocation() );
82 assertTrue( "Archiva Managed Repository id:<" + repoId + "> should have a valid location on disk.", repoRoot
83 .exists()
84 && repoRoot.isDirectory() );
85 }
86
87 protected void assertResponseOK( WebResponse response )
88 {
89 assertNotNull( "Should have recieved a response", response );
90 Assert.assertEquals( "Should have been an OK response code", HttpServletResponse.SC_OK,
91 response.getResponseCode() );
92 }
93
94 protected void assertResponseOK( WebResponse response, String path )
95 {
96 assertNotNull( "Should have recieved a response", response );
97 Assert.assertEquals( "Should have been an OK response code for path: " + path, HttpServletResponse.SC_OK,
98 response.getResponseCode() );
99 }
100
101 protected void assertResponseNotFound( WebResponse response )
102 {
103 assertNotNull( "Should have recieved a response", response );
104 Assert.assertEquals( "Should have been an 404/Not Found response code.", HttpServletResponse.SC_NOT_FOUND, response
105 .getResponseCode() );
106 }
107
108 protected void assertResponseInternalServerError( WebResponse response )
109 {
110 assertNotNull( "Should have recieved a response", response );
111 Assert.assertEquals( "Should have been an 500/Internal Server Error response code.", HttpServletResponse.SC_INTERNAL_SERVER_ERROR, response
112 .getResponseCode() );
113 }
114
115 protected void assertResponseConflictError( WebResponse response )
116 {
117 assertNotNull( "Should have received a response", response );
118 Assert.assertEquals( "Should have been a 409/Conflict response code.", HttpServletResponse.SC_CONFLICT,
119 response.getResponseCode() );
120 }
121
122 protected ManagedRepositoryConfiguration createManagedRepository( String id, String name, File location, boolean blockRedeployments )
123 {
124 ManagedRepositoryConfiguration repo = new ManagedRepositoryConfiguration();
125 repo.setId( id );
126 repo.setName( name );
127 repo.setLocation( location.getAbsolutePath() );
128 repo.setBlockRedeployments( blockRedeployments );
129
130 return repo;
131 }
132
133 protected RemoteRepositoryConfiguration createRemoteRepository( String id, String name, String url )
134 {
135 RemoteRepositoryConfiguration repo = new RemoteRepositoryConfiguration();
136 repo.setId( id );
137 repo.setName( name );
138 repo.setUrl( url );
139 return repo;
140 }
141
142 protected void dumpResponse( WebResponse response )
143 {
144 System.out.println( "---(response)---" );
145 System.out.println( "" + response.getResponseCode() + " " + response.getResponseMessage() );
146
147 String headerNames[] = response.getHeaderFieldNames();
148 for ( String headerName : headerNames )
149 {
150 System.out.println( "[header] " + headerName + ": " + response.getHeaderField( headerName ) );
151 }
152
153 System.out.println( "---(text)---" );
154 try
155 {
156 System.out.println( response.getText() );
157 }
158 catch ( IOException e )
159 {
160 System.err.print( "[Exception] : " );
161 e.printStackTrace( System.err );
162 }
163 }
164
165 protected void saveConfiguration( ArchivaConfiguration archivaConfiguration )
166 throws Exception
167 {
168 archivaConfiguration.save( archivaConfiguration.getConfiguration() );
169 }
170
171 protected void setUp()
172 throws Exception
173 {
174 super.setUp();
175
176 String appserverBase = getTestFile( "target/appserver-base" ).getAbsolutePath();
177 System.setProperty( "appserver.base", appserverBase );
178
179 File testConf = getTestFile( "src/test/resources/repository-archiva.xml" );
180 File testConfDest = new File( appserverBase, "conf/archiva.xml" );
181 FileUtils.copyFile( testConf, testConfDest );
182
183 archivaConfiguration = (ArchivaConfiguration) lookup( ArchivaConfiguration.class );
184 repoRootInternal = new File( appserverBase, "data/repositories/internal" );
185 Configuration config = archivaConfiguration.getConfiguration();
186
187 config.addManagedRepository( createManagedRepository( REPOID_INTERNAL, "Internal Test Repo", repoRootInternal, true ) );
188 saveConfiguration( archivaConfiguration );
189
190 CacheManager.getInstance().removeCache( "url-failures-cache" );
191
192 HttpUnitOptions.setExceptionsThrownOnErrorStatus( false );
193
194 sr = new ServletRunner( getTestFile( "src/test/resources/WEB-INF/web.xml" ) );
195 sr.registerServlet( "/repository/*", UnauthenticatedRepositoryServlet.class.getName() );
196 sc = sr.newClient();
197 }
198
199 @Override
200 protected String getPlexusConfigLocation()
201 {
202 return "org/apache/maven/archiva/webdav/RepositoryServletTest.xml";
203 }
204
205 @Override
206 protected void tearDown()
207 throws Exception
208 {
209 if ( sc != null )
210 {
211 sc.clearContents();
212 }
213
214 if ( sr != null )
215 {
216 sr.shutDown();
217 }
218
219 if (repoRootInternal.exists())
220 {
221 FileUtils.deleteDirectory(repoRootInternal);
222 }
223
224 release( archivaConfiguration );
225
226 super.tearDown();
227 }
228
229 protected void setupCleanRepo( File repoRootDir )
230 throws IOException
231 {
232 FileUtils.deleteDirectory( repoRootDir );
233 if ( !repoRootDir.exists() )
234 {
235 repoRootDir.mkdirs();
236 }
237 }
238
239 protected void assertManagedFileNotExists( File repoRootInternal, String resourcePath )
240 {
241 File repoFile = new File( repoRootInternal, resourcePath );
242 assertFalse( "Managed Repository File <" + repoFile.getAbsolutePath() + "> should not exist.", repoFile
243 .exists() );
244 }
245
246 protected void setupCleanInternalRepo()
247 throws Exception
248 {
249 setupCleanRepo( repoRootInternal );
250 }
251
252 protected File populateRepo( File repoRootManaged, String path, String contents )
253 throws Exception
254 {
255 File destFile = new File( repoRootManaged, path );
256 destFile.getParentFile().mkdirs();
257 FileUtils.writeStringToFile( destFile, contents, null );
258 return destFile;
259 }
260 }