1 package org.apache.archiva.web.rss;
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 junit.framework.TestCase;
24 import org.apache.archiva.common.filelock.DefaultFileLockManager;
25 import org.apache.archiva.configuration.ArchivaConfiguration;
26 import org.apache.archiva.repository.base.BasicManagedRepository;
27 import org.apache.archiva.repository.RepositoryRegistry;
28 import org.apache.archiva.repository.storage.FilesystemStorage;
29 import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
30 import org.apache.commons.codec.Encoder;
31 import org.apache.commons.codec.binary.Base64;
32 import org.junit.After;
33 import org.junit.AfterClass;
34 import org.junit.Before;
35 import org.junit.BeforeClass;
36 import org.junit.Test;
37 import org.junit.runner.RunWith;
38 import org.springframework.beans.BeansException;
39 import org.springframework.beans.factory.BeanFactory;
40 import org.springframework.beans.factory.NoSuchBeanDefinitionException;
41 import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
42 import org.springframework.context.ApplicationContext;
43 import org.springframework.context.ApplicationEvent;
44 import org.springframework.context.MessageSourceResolvable;
45 import org.springframework.context.NoSuchMessageException;
46 import org.springframework.core.ResolvableType;
47 import org.springframework.core.env.Environment;
48 import org.springframework.core.io.Resource;
49 import org.springframework.mock.web.MockHttpServletRequest;
50 import org.springframework.mock.web.MockHttpServletResponse;
51 import org.springframework.mock.web.MockServletConfig;
52 import org.springframework.mock.web.MockServletContext;
53 import org.springframework.test.context.ContextConfiguration;
54 import org.springframework.web.context.WebApplicationContext;
56 import javax.inject.Inject;
57 import javax.servlet.ServletContext;
58 import javax.servlet.http.HttpServletResponse;
59 import java.io.IOException;
60 import java.lang.annotation.Annotation;
61 import java.nio.file.Paths;
62 import java.util.Locale;
65 @RunWith( ArchivaSpringJUnit4ClassRunner.class )
66 @ContextConfiguration(
67 locations = { "classpath*:/META-INF/spring-context.xml", "classpath*:/spring-context-test-common.xml",
68 "classpath*:/spring-context-rss-servlet.xml" } )
69 public class RssFeedServletTest
72 private RssFeedServlet rssFeedServlet = new RssFeedServlet();
74 static String PREVIOUS_ARCHIVA_PATH;
77 protected ApplicationContext applicationContext;
80 protected RepositoryRegistry repositoryRegistry;
83 public static void initConfigurationPath()
86 PREVIOUS_ARCHIVA_PATH = System.getProperty(ArchivaConfiguration.USER_CONFIG_PROPERTY);
87 System.setProperty( ArchivaConfiguration.USER_CONFIG_PROPERTY,
88 System.getProperty( "test.resources.path" ) + "/empty-archiva.xml" );
93 public static void restoreConfigurationPath()
96 System.setProperty( ArchivaConfiguration.USER_CONFIG_PROPERTY, PREVIOUS_ARCHIVA_PATH );
104 final MockServletContext mockServletContext = new MockServletContext();
106 WebApplicationContext webApplicationContext =
107 new TestWebapplicationContext( applicationContext, mockServletContext );
109 mockServletContext.setAttribute( WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE,
110 webApplicationContext );
112 MockServletConfig mockServletConfig = new MockServletConfig()
115 public ServletContext getServletContext()
117 return mockServletContext;
121 repositoryRegistry.reload();
122 repositoryRegistry.putRepository( new BasicManagedRepository( "internal", "internal",
123 new FilesystemStorage( Paths.get( "target/appserver-base/repositories/internal" ), new DefaultFileLockManager( ) ) ) );
124 rssFeedServlet.init( mockServletConfig );
129 public void tearDown()
135 public static class TestWebapplicationContext
136 implements WebApplicationContext
138 private ApplicationContext applicationContext;
140 private ServletContext servletContext;
142 TestWebapplicationContext( ApplicationContext applicationContext, ServletContext servletContext )
144 this.applicationContext = applicationContext;
148 public ServletContext getServletContext()
150 return servletContext;
154 public String getId()
156 return applicationContext.getId();
160 public String getApplicationName()
162 return applicationContext.getApplicationName();
166 public String getDisplayName()
168 return applicationContext.getDisplayName();
172 public long getStartupDate()
174 return applicationContext.getStartupDate();
178 public ApplicationContext getParent()
180 return applicationContext.getParent();
184 public AutowireCapableBeanFactory getAutowireCapableBeanFactory()
185 throws IllegalStateException
187 return applicationContext.getAutowireCapableBeanFactory();
191 public void publishEvent( ApplicationEvent applicationEvent )
193 applicationContext.publishEvent( applicationEvent );
197 public Environment getEnvironment()
199 return applicationContext.getEnvironment();
203 public BeanFactory getParentBeanFactory()
205 return applicationContext.getParentBeanFactory();
209 public boolean containsLocalBean( String s )
211 return applicationContext.containsLocalBean( s );
215 public boolean containsBeanDefinition( String s )
217 return applicationContext.containsBeanDefinition( s );
221 public int getBeanDefinitionCount()
223 return applicationContext.getBeanDefinitionCount();
227 public String[] getBeanDefinitionNames()
229 return applicationContext.getBeanDefinitionNames();
233 public String[] getBeanNamesForType( Class<?> aClass )
235 return applicationContext.getBeanNamesForType( aClass );
239 public String[] getBeanNamesForType( Class<?> aClass, boolean b, boolean b2 )
241 return applicationContext.getBeanNamesForType( aClass, b, b2 );
245 public <T> Map<String, T> getBeansOfType( Class<T> tClass )
246 throws BeansException
248 return applicationContext.getBeansOfType( tClass );
252 public <T> T getBean( Class<T> aClass, Object... objects )
253 throws BeansException
255 return applicationContext.getBean( aClass, objects );
259 public <T> Map<String, T> getBeansOfType( Class<T> tClass, boolean b, boolean b2 )
260 throws BeansException
262 return applicationContext.getBeansOfType( tClass, b, b2 );
266 public String[] getBeanNamesForAnnotation( Class<? extends Annotation> aClass )
268 return applicationContext.getBeanNamesForAnnotation( aClass );
272 public Map<String, Object> getBeansWithAnnotation( Class<? extends Annotation> aClass )
273 throws BeansException
275 return applicationContext.getBeansWithAnnotation( aClass );
279 public <A extends Annotation> A findAnnotationOnBean( String s, Class<A> aClass )
280 throws NoSuchBeanDefinitionException
282 return applicationContext.findAnnotationOnBean( s, aClass );
286 public Object getBean( String s )
287 throws BeansException
289 return applicationContext.getBean( s );
293 public <T> T getBean( String s, Class<T> tClass )
294 throws BeansException
296 return applicationContext.getBean( s, tClass );
300 public <T> T getBean( Class<T> tClass )
301 throws BeansException
303 return applicationContext.getBean( tClass );
307 public Object getBean( String s, Object... objects )
308 throws BeansException
310 return applicationContext.getBean( s, objects );
314 public boolean containsBean( String s )
316 return applicationContext.containsBean( s );
320 public boolean isSingleton( String s )
321 throws NoSuchBeanDefinitionException
323 return applicationContext.isSingleton( s );
327 public boolean isPrototype( String s )
328 throws NoSuchBeanDefinitionException
330 return applicationContext.isPrototype( s );
334 public boolean isTypeMatch( String s, Class<?> aClass )
335 throws NoSuchBeanDefinitionException
337 return applicationContext.isTypeMatch( s, aClass );
341 public Class<?> getType( String s )
342 throws NoSuchBeanDefinitionException
344 return applicationContext.getType( s );
348 public String[] getAliases( String s )
350 return applicationContext.getAliases( s );
354 public String getMessage( String s, Object[] objects, String s2, Locale locale )
356 return applicationContext.getMessage( s, objects, s2, locale );
360 public String getMessage( String s, Object[] objects, Locale locale )
361 throws NoSuchMessageException
363 return applicationContext.getMessage( s, objects, locale );
367 public String getMessage( MessageSourceResolvable messageSourceResolvable, Locale locale )
368 throws NoSuchMessageException
370 return applicationContext.getMessage( messageSourceResolvable, locale );
374 public Resource[] getResources( String s )
377 return applicationContext.getResources( s );
381 public Resource getResource( String s )
383 return applicationContext.getResource( s );
387 public ClassLoader getClassLoader()
389 return applicationContext.getClassLoader();
393 public void publishEvent( Object o )
399 public String[] getBeanNamesForType( ResolvableType resolvableType )
401 return new String[0];
405 public boolean isTypeMatch( String s, ResolvableType resolvableType )
406 throws NoSuchBeanDefinitionException
414 public void testRequestNewArtifactsInRepo()
417 MockHttpServletRequest request = new MockHttpServletRequest();
418 request.setRequestURI( "/feeds/test-repo" );
419 request.addHeader( "User-Agent", "Apache Archiva unit test" );
420 request.setMethod( "GET" );
422 Base64 encoder = new Base64( 0, new byte[0] );
423 String userPass = "user1:password1";
424 String encodedUserPass = encoder.encodeToString( userPass.getBytes() );
425 request.addHeader( "Authorization", "BASIC " + encodedUserPass );
427 MockHttpServletResponse mockHttpServletResponse = new MockHttpServletResponse();
429 rssFeedServlet.doGet( request, mockHttpServletResponse );
431 assertEquals( RssFeedServlet.MIME_TYPE, mockHttpServletResponse.getHeader( "CONTENT-TYPE" ) );
432 assertNotNull( "Should have recieved a response", mockHttpServletResponse.getContentAsString() );
433 assertEquals( "Should have been an OK response code.", HttpServletResponse.SC_OK,
434 mockHttpServletResponse.getStatus() );
439 public void testRequestNewVersionsOfArtifact()
442 MockHttpServletRequest request = new MockHttpServletRequest();
443 request.setRequestURI( "/feeds/org/apache/archiva/artifact-two" );
444 request.addHeader( "User-Agent", "Apache Archiva unit test" );
445 request.setMethod( "GET" );
447 //WebRequest request = new GetMethodWebRequest( "http://localhost/feeds/org/apache/archiva/artifact-two" );
449 Base64 encoder = new Base64( 0, new byte[0] );
450 String userPass = "user1:password1";
451 String encodedUserPass = encoder.encodeToString( userPass.getBytes() );
452 request.addHeader( "Authorization", "BASIC " + encodedUserPass );
454 MockHttpServletResponse mockHttpServletResponse = new MockHttpServletResponse();
456 rssFeedServlet.doGet( request, mockHttpServletResponse );
458 assertEquals( RssFeedServlet.MIME_TYPE, mockHttpServletResponse.getHeader( "CONTENT-TYPE" ) );
459 assertNotNull( "Should have recieved a response", mockHttpServletResponse.getContentAsString() );
460 assertEquals( "Should have been an OK response code.", HttpServletResponse.SC_OK,
461 mockHttpServletResponse.getStatus() );
465 public void testInvalidRequest()
468 MockHttpServletRequest request = new MockHttpServletRequest();
469 request.setRequestURI( "/feeds?invalid_param=xxx" );
470 request.addHeader( "User-Agent", "Apache Archiva unit test" );
471 request.setMethod( "GET" );
473 MockHttpServletResponse mockHttpServletResponse = new MockHttpServletResponse();
475 rssFeedServlet.doGet( request, mockHttpServletResponse );
477 assertEquals( HttpServletResponse.SC_BAD_REQUEST, mockHttpServletResponse.getStatus() );
482 public void testInvalidAuthenticationRequest()
486 MockHttpServletRequest request = new MockHttpServletRequest();
487 request.setRequestURI( "/feeds/unauthorized-repo" );
488 request.addHeader( "User-Agent", "Apache Archiva unit test" );
489 request.setMethod( "GET" );
491 Encoder encoder = new Base64();
492 String userPass = "unauthUser:unauthPass";
493 String encodedUserPass = new String( (byte[]) encoder.encode( userPass.getBytes() ) );
494 request.addHeader( "Authorization", "BASIC " + encodedUserPass );
496 MockHttpServletResponse mockHttpServletResponse = new MockHttpServletResponse();
497 rssFeedServlet.doGet( request, mockHttpServletResponse );
499 assertEquals( HttpServletResponse.SC_UNAUTHORIZED, mockHttpServletResponse.getStatus() );
504 public void testUnauthorizedRequest()
508 MockHttpServletRequest request = new MockHttpServletRequest();
509 request.setRequestURI( "/feeds/unauthorized-repo" );
510 request.addHeader( "User-Agent", "Apache Archiva unit test" );
511 request.setMethod( "GET" );
513 Base64 encoder = new Base64( 0, new byte[0] );
514 String userPass = "user1:password1";
515 String encodedUserPass = encoder.encodeToString( userPass.getBytes() );
516 request.addHeader( "Authorization", "BASIC " + encodedUserPass );
518 MockHttpServletResponse mockHttpServletResponse = new MockHttpServletResponse();
519 rssFeedServlet.doGet( request, mockHttpServletResponse );
521 assertEquals( HttpServletResponse.SC_UNAUTHORIZED, mockHttpServletResponse.getStatus() );