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.test.utils.ArchivaSpringJUnit4ClassRunner;
25 import org.apache.commons.codec.Encoder;
26 import org.apache.commons.codec.binary.Base64;
27 import org.junit.After;
28 import org.junit.AfterClass;
29 import org.junit.Before;
30 import org.junit.BeforeClass;
31 import org.junit.Ignore;
32 import org.junit.Test;
33 import org.junit.runner.RunWith;
34 import org.springframework.beans.BeansException;
35 import org.springframework.beans.factory.BeanFactory;
36 import org.springframework.beans.factory.NoSuchBeanDefinitionException;
37 import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
38 import org.springframework.context.ApplicationContext;
39 import org.springframework.context.ApplicationEvent;
40 import org.springframework.context.MessageSourceResolvable;
41 import org.springframework.context.NoSuchMessageException;
42 import org.springframework.core.env.Environment;
43 import org.springframework.core.io.Resource;
44 import org.springframework.mock.web.MockHttpServletRequest;
45 import org.springframework.mock.web.MockHttpServletResponse;
46 import org.springframework.mock.web.MockServletConfig;
47 import org.springframework.mock.web.MockServletContext;
48 import org.springframework.test.context.ContextConfiguration;
49 import org.springframework.web.context.WebApplicationContext;
51 import javax.inject.Inject;
52 import javax.servlet.ServletContext;
53 import javax.servlet.http.HttpServletResponse;
54 import java.io.IOException;
55 import java.lang.annotation.Annotation;
56 import java.util.Locale;
59 @RunWith( ArchivaSpringJUnit4ClassRunner.class )
60 @ContextConfiguration(
61 locations = { "classpath*:/META-INF/spring-context.xml", "classpath*:/spring-context-test-common.xml",
62 "classpath*:/spring-context-rss-servlet.xml" } )
63 public class RssFeedServletTest
66 private RssFeedServlet rssFeedServlet = new RssFeedServlet();
68 static String PREVIOUS_ARCHIVA_PATH;
71 protected ApplicationContext applicationContext;
74 public static void initConfigurationPath()
77 PREVIOUS_ARCHIVA_PATH = System.getProperty( "archiva.user.configFileName" );
78 System.setProperty( "archiva.user.configFileName",
79 System.getProperty( "test.resources.path/" ) + "empty-archiva.xml" );
84 public static void restoreConfigurationPath()
87 System.setProperty( "archiva.user.configFileName", PREVIOUS_ARCHIVA_PATH );
95 final MockServletContext mockServletContext = new MockServletContext();
97 WebApplicationContext webApplicationContext =
98 new TestWebapplicationContext( applicationContext, mockServletContext );
100 mockServletContext.setAttribute( WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE,
101 webApplicationContext );
103 MockServletConfig mockServletConfig = new MockServletConfig()
106 public ServletContext getServletContext()
108 return mockServletContext;
112 rssFeedServlet.init( mockServletConfig );
117 public void tearDown()
123 public static class TestWebapplicationContext
124 implements WebApplicationContext
126 private ApplicationContext applicationContext;
128 private ServletContext servletContext;
130 TestWebapplicationContext( ApplicationContext applicationContext, ServletContext servletContext )
132 this.applicationContext = applicationContext;
136 public ServletContext getServletContext()
138 return servletContext;
142 public String getId()
144 return applicationContext.getId();
148 public String getApplicationName()
150 return applicationContext.getApplicationName();
154 public String getDisplayName()
156 return applicationContext.getDisplayName();
160 public long getStartupDate()
162 return applicationContext.getStartupDate();
166 public ApplicationContext getParent()
168 return applicationContext.getParent();
172 public AutowireCapableBeanFactory getAutowireCapableBeanFactory()
173 throws IllegalStateException
175 return applicationContext.getAutowireCapableBeanFactory();
179 public void publishEvent( ApplicationEvent applicationEvent )
181 applicationContext.publishEvent( applicationEvent );
185 public Environment getEnvironment()
187 return applicationContext.getEnvironment();
191 public BeanFactory getParentBeanFactory()
193 return applicationContext.getParentBeanFactory();
197 public boolean containsLocalBean( String s )
199 return applicationContext.containsLocalBean( s );
203 public boolean containsBeanDefinition( String s )
205 return applicationContext.containsBeanDefinition( s );
209 public int getBeanDefinitionCount()
211 return applicationContext.getBeanDefinitionCount();
215 public String[] getBeanDefinitionNames()
217 return applicationContext.getBeanDefinitionNames();
221 public String[] getBeanNamesForType( Class<?> aClass )
223 return applicationContext.getBeanNamesForType( aClass );
227 public String[] getBeanNamesForType( Class<?> aClass, boolean b, boolean b2 )
229 return applicationContext.getBeanNamesForType( aClass, b, b2 );
233 public <T> Map<String, T> getBeansOfType( Class<T> tClass )
234 throws BeansException
236 return applicationContext.getBeansOfType( tClass );
240 public <T> T getBean( Class<T> aClass, Object... objects )
241 throws BeansException
243 return applicationContext.getBean( aClass, objects );
247 public <T> Map<String, T> getBeansOfType( Class<T> tClass, boolean b, boolean b2 )
248 throws BeansException
250 return applicationContext.getBeansOfType( tClass, b, b2 );
254 public String[] getBeanNamesForAnnotation( Class<? extends Annotation> aClass )
256 return applicationContext.getBeanNamesForAnnotation( aClass );
260 public Map<String, Object> getBeansWithAnnotation( Class<? extends Annotation> aClass )
261 throws BeansException
263 return applicationContext.getBeansWithAnnotation( aClass );
267 public <A extends Annotation> A findAnnotationOnBean( String s, Class<A> aClass )
268 throws NoSuchBeanDefinitionException
270 return applicationContext.findAnnotationOnBean( s, aClass );
274 public Object getBean( String s )
275 throws BeansException
277 return applicationContext.getBean( s );
281 public <T> T getBean( String s, Class<T> tClass )
282 throws BeansException
284 return applicationContext.getBean( s, tClass );
288 public <T> T getBean( Class<T> tClass )
289 throws BeansException
291 return applicationContext.getBean( tClass );
295 public Object getBean( String s, Object... objects )
296 throws BeansException
298 return applicationContext.getBean( s, objects );
302 public boolean containsBean( String s )
304 return applicationContext.containsBean( s );
308 public boolean isSingleton( String s )
309 throws NoSuchBeanDefinitionException
311 return applicationContext.isSingleton( s );
315 public boolean isPrototype( String s )
316 throws NoSuchBeanDefinitionException
318 return applicationContext.isPrototype( s );
322 public boolean isTypeMatch( String s, Class<?> aClass )
323 throws NoSuchBeanDefinitionException
325 return applicationContext.isTypeMatch( s, aClass );
329 public Class<?> getType( String s )
330 throws NoSuchBeanDefinitionException
332 return applicationContext.getType( s );
336 public String[] getAliases( String s )
338 return applicationContext.getAliases( s );
342 public String getMessage( String s, Object[] objects, String s2, Locale locale )
344 return applicationContext.getMessage( s, objects, s2, locale );
348 public String getMessage( String s, Object[] objects, Locale locale )
349 throws NoSuchMessageException
351 return applicationContext.getMessage( s, objects, locale );
355 public String getMessage( MessageSourceResolvable messageSourceResolvable, Locale locale )
356 throws NoSuchMessageException
358 return applicationContext.getMessage( messageSourceResolvable, locale );
362 public Resource[] getResources( String s )
365 return applicationContext.getResources( s );
369 public Resource getResource( String s )
371 return applicationContext.getResource( s );
375 public ClassLoader getClassLoader()
377 return applicationContext.getClassLoader();
383 public void testRequestNewArtifactsInRepo()
386 MockHttpServletRequest request = new MockHttpServletRequest();
387 request.setRequestURI( "/feeds/test-repo" );
388 request.addHeader( "User-Agent", "Apache Archiva unit test" );
389 request.setMethod( "GET" );
391 Base64 encoder = new Base64( 0, new byte[0] );
392 String userPass = "user1:password1";
393 String encodedUserPass = encoder.encodeToString( userPass.getBytes() );
394 request.addHeader( "Authorization", "BASIC " + encodedUserPass );
396 MockHttpServletResponse mockHttpServletResponse = new MockHttpServletResponse();
398 rssFeedServlet.doGet( request, mockHttpServletResponse );
400 assertEquals( RssFeedServlet.MIME_TYPE, mockHttpServletResponse.getHeader( "CONTENT-TYPE" ) );
401 assertNotNull( "Should have recieved a response", mockHttpServletResponse.getContentAsString() );
402 assertEquals( "Should have been an OK response code.", HttpServletResponse.SC_OK,
403 mockHttpServletResponse.getStatus() );
408 public void testRequestNewVersionsOfArtifact()
411 MockHttpServletRequest request = new MockHttpServletRequest();
412 request.setRequestURI( "/feeds/org/apache/archiva/artifact-two" );
413 request.addHeader( "User-Agent", "Apache Archiva unit test" );
414 request.setMethod( "GET" );
416 //WebRequest request = new GetMethodWebRequest( "http://localhost/feeds/org/apache/archiva/artifact-two" );
418 Base64 encoder = new Base64( 0, new byte[0] );
419 String userPass = "user1:password1";
420 String encodedUserPass = encoder.encodeToString( userPass.getBytes() );
421 request.addHeader( "Authorization", "BASIC " + encodedUserPass );
423 MockHttpServletResponse mockHttpServletResponse = new MockHttpServletResponse();
425 rssFeedServlet.doGet( request, mockHttpServletResponse );
427 assertEquals( RssFeedServlet.MIME_TYPE, mockHttpServletResponse.getHeader( "CONTENT-TYPE" ) );
428 assertNotNull( "Should have recieved a response", mockHttpServletResponse.getContentAsString() );
429 assertEquals( "Should have been an OK response code.", HttpServletResponse.SC_OK,
430 mockHttpServletResponse.getStatus() );
434 public void testInvalidRequest()
437 MockHttpServletRequest request = new MockHttpServletRequest();
438 request.setRequestURI( "/feeds?invalid_param=xxx" );
439 request.addHeader( "User-Agent", "Apache Archiva unit test" );
440 request.setMethod( "GET" );
442 MockHttpServletResponse mockHttpServletResponse = new MockHttpServletResponse();
444 rssFeedServlet.doGet( request, mockHttpServletResponse );
446 assertEquals( HttpServletResponse.SC_BAD_REQUEST, mockHttpServletResponse.getStatus() );
451 public void testInvalidAuthenticationRequest()
455 MockHttpServletRequest request = new MockHttpServletRequest();
456 request.setRequestURI( "/feeds/unauthorized-repo" );
457 request.addHeader( "User-Agent", "Apache Archiva unit test" );
458 request.setMethod( "GET" );
460 Encoder encoder = new Base64();
461 String userPass = "unauthUser:unauthPass";
462 String encodedUserPass = new String( (byte[]) encoder.encode( userPass.getBytes() ) );
463 request.addHeader( "Authorization", "BASIC " + encodedUserPass );
465 MockHttpServletResponse mockHttpServletResponse = new MockHttpServletResponse();
466 rssFeedServlet.doGet( request, mockHttpServletResponse );
468 assertEquals( HttpServletResponse.SC_UNAUTHORIZED, mockHttpServletResponse.getStatus() );
473 public void testUnauthorizedRequest()
477 MockHttpServletRequest request = new MockHttpServletRequest();
478 request.setRequestURI( "/feeds/unauthorized-repo" );
479 request.addHeader( "User-Agent", "Apache Archiva unit test" );
480 request.setMethod( "GET" );
482 Base64 encoder = new Base64( 0, new byte[0] );
483 String userPass = "user1:password1";
484 String encodedUserPass = encoder.encodeToString( userPass.getBytes() );
485 request.addHeader( "Authorization", "BASIC " + encodedUserPass );
487 MockHttpServletResponse mockHttpServletResponse = new MockHttpServletResponse();
488 rssFeedServlet.doGet( request, mockHttpServletResponse );
490 assertEquals( HttpServletResponse.SC_UNAUTHORIZED, mockHttpServletResponse.getStatus() );