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.fs.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.ObjectProvider;
42 import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
43 import org.springframework.context.ApplicationContext;
44 import org.springframework.context.ApplicationEvent;
45 import org.springframework.context.MessageSourceResolvable;
46 import org.springframework.context.NoSuchMessageException;
47 import org.springframework.core.ResolvableType;
48 import org.springframework.core.env.Environment;
49 import org.springframework.core.io.Resource;
50 import org.springframework.mock.web.MockHttpServletRequest;
51 import org.springframework.mock.web.MockHttpServletResponse;
52 import org.springframework.mock.web.MockServletConfig;
53 import org.springframework.mock.web.MockServletContext;
54 import org.springframework.test.context.ContextConfiguration;
55 import org.springframework.web.context.WebApplicationContext;
57 import javax.inject.Inject;
58 import javax.servlet.ServletContext;
59 import javax.servlet.http.HttpServletResponse;
60 import java.io.IOException;
61 import java.lang.annotation.Annotation;
62 import java.nio.file.Paths;
63 import java.util.Locale;
66 @RunWith( ArchivaSpringJUnit4ClassRunner.class )
67 @ContextConfiguration(
68 locations = { "classpath*:/META-INF/spring-context.xml", "classpath*:/spring-context-test-common.xml",
69 "classpath*:/spring-context-rss-servlet.xml" } )
70 public class RssFeedServletTest
73 private RssFeedServlet rssFeedServlet = new RssFeedServlet();
75 static String PREVIOUS_ARCHIVA_PATH;
78 protected ApplicationContext applicationContext;
81 protected RepositoryRegistry repositoryRegistry;
84 public static void initConfigurationPath()
87 PREVIOUS_ARCHIVA_PATH = System.getProperty(ArchivaConfiguration.USER_CONFIG_PROPERTY);
88 System.setProperty( ArchivaConfiguration.USER_CONFIG_PROPERTY,
89 System.getProperty( "test.resources.path" ) + "/empty-archiva.xml" );
94 public static void restoreConfigurationPath()
97 System.setProperty( ArchivaConfiguration.USER_CONFIG_PROPERTY, PREVIOUS_ARCHIVA_PATH );
105 final MockServletContext mockServletContext = new MockServletContext();
107 WebApplicationContext webApplicationContext =
108 new TestWebapplicationContext( applicationContext, mockServletContext );
110 mockServletContext.setAttribute( WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE,
111 webApplicationContext );
113 MockServletConfig mockServletConfig = new MockServletConfig()
116 public ServletContext getServletContext()
118 return mockServletContext;
122 repositoryRegistry.reload();
123 repositoryRegistry.putRepository( new BasicManagedRepository( "internal", "internal",
124 new FilesystemStorage( Paths.get( "target/appserver-base/repositories/internal" ), new DefaultFileLockManager( ) ) ) );
125 rssFeedServlet.init( mockServletConfig );
130 public void tearDown()
136 public static class TestWebapplicationContext
137 implements WebApplicationContext
139 private ApplicationContext applicationContext;
141 private ServletContext servletContext;
143 TestWebapplicationContext( ApplicationContext applicationContext, ServletContext servletContext )
145 this.applicationContext = applicationContext;
149 public ServletContext getServletContext()
151 return servletContext;
155 public String getId()
157 return applicationContext.getId();
161 public String getApplicationName()
163 return applicationContext.getApplicationName();
167 public String getDisplayName()
169 return applicationContext.getDisplayName();
173 public long getStartupDate()
175 return applicationContext.getStartupDate();
179 public ApplicationContext getParent()
181 return applicationContext.getParent();
185 public AutowireCapableBeanFactory getAutowireCapableBeanFactory()
186 throws IllegalStateException
188 return applicationContext.getAutowireCapableBeanFactory();
192 public void publishEvent( ApplicationEvent applicationEvent )
194 applicationContext.publishEvent( applicationEvent );
198 public Environment getEnvironment()
200 return applicationContext.getEnvironment();
204 public BeanFactory getParentBeanFactory()
206 return applicationContext.getParentBeanFactory();
210 public boolean containsLocalBean( String s )
212 return applicationContext.containsLocalBean( s );
216 public boolean containsBeanDefinition( String s )
218 return applicationContext.containsBeanDefinition( s );
222 public int getBeanDefinitionCount()
224 return applicationContext.getBeanDefinitionCount();
228 public String[] getBeanDefinitionNames()
230 return applicationContext.getBeanDefinitionNames();
234 public String[] getBeanNamesForType( Class<?> aClass )
236 return applicationContext.getBeanNamesForType( aClass );
240 public String[] getBeanNamesForType( Class<?> aClass, boolean b, boolean b2 )
242 return applicationContext.getBeanNamesForType( aClass, b, b2 );
246 public <T> Map<String, T> getBeansOfType( Class<T> tClass )
247 throws BeansException
249 return applicationContext.getBeansOfType( tClass );
253 public <T> T getBean( Class<T> aClass, Object... objects )
254 throws BeansException
256 return applicationContext.getBean( aClass, objects );
260 public <T> ObjectProvider<T> getBeanProvider( Class<T> aClass )
266 public <T> ObjectProvider<T> getBeanProvider( ResolvableType resolvableType )
272 public <T> Map<String, T> getBeansOfType( Class<T> tClass, boolean b, boolean b2 )
273 throws BeansException
275 return applicationContext.getBeansOfType( tClass, b, b2 );
279 public String[] getBeanNamesForAnnotation( Class<? extends Annotation> aClass )
281 return applicationContext.getBeanNamesForAnnotation( aClass );
285 public Map<String, Object> getBeansWithAnnotation( Class<? extends Annotation> aClass )
286 throws BeansException
288 return applicationContext.getBeansWithAnnotation( aClass );
292 public <A extends Annotation> A findAnnotationOnBean( String s, Class<A> aClass )
293 throws NoSuchBeanDefinitionException
295 return applicationContext.findAnnotationOnBean( s, aClass );
299 public Object getBean( String s )
300 throws BeansException
302 return applicationContext.getBean( s );
306 public <T> T getBean( String s, Class<T> tClass )
307 throws BeansException
309 return applicationContext.getBean( s, tClass );
313 public <T> T getBean( Class<T> tClass )
314 throws BeansException
316 return applicationContext.getBean( tClass );
320 public Object getBean( String s, Object... objects )
321 throws BeansException
323 return applicationContext.getBean( s, objects );
327 public boolean containsBean( String s )
329 return applicationContext.containsBean( s );
333 public boolean isSingleton( String s )
334 throws NoSuchBeanDefinitionException
336 return applicationContext.isSingleton( s );
340 public boolean isPrototype( String s )
341 throws NoSuchBeanDefinitionException
343 return applicationContext.isPrototype( s );
347 public boolean isTypeMatch( String s, Class<?> aClass )
348 throws NoSuchBeanDefinitionException
350 return applicationContext.isTypeMatch( s, aClass );
354 public Class<?> getType( String s )
355 throws NoSuchBeanDefinitionException
357 return applicationContext.getType( s );
361 public Class<?> getType( String s, boolean b ) throws NoSuchBeanDefinitionException
367 public String[] getAliases( String s )
369 return applicationContext.getAliases( s );
373 public String getMessage( String s, Object[] objects, String s2, Locale locale )
375 return applicationContext.getMessage( s, objects, s2, locale );
379 public String getMessage( String s, Object[] objects, Locale locale )
380 throws NoSuchMessageException
382 return applicationContext.getMessage( s, objects, locale );
386 public String getMessage( MessageSourceResolvable messageSourceResolvable, Locale locale )
387 throws NoSuchMessageException
389 return applicationContext.getMessage( messageSourceResolvable, locale );
393 public Resource[] getResources( String s )
396 return applicationContext.getResources( s );
400 public Resource getResource( String s )
402 return applicationContext.getResource( s );
406 public ClassLoader getClassLoader()
408 return applicationContext.getClassLoader();
412 public void publishEvent( Object o )
418 public String[] getBeanNamesForType( ResolvableType resolvableType )
420 return new String[0];
424 public String[] getBeanNamesForType( ResolvableType resolvableType, boolean b, boolean b1 )
426 return new String[0];
430 public boolean isTypeMatch( String s, ResolvableType resolvableType )
431 throws NoSuchBeanDefinitionException
439 public void testRequestNewArtifactsInRepo()
442 MockHttpServletRequest request = new MockHttpServletRequest();
443 request.setRequestURI( "/feeds/test-repo" );
444 request.addHeader( "User-Agent", "Apache Archiva unit test" );
445 request.setMethod( "GET" );
447 Base64 encoder = new Base64( 0, new byte[0] );
448 String userPass = "user1:password1";
449 String encodedUserPass = encoder.encodeToString( userPass.getBytes() );
450 request.addHeader( "Authorization", "BASIC " + encodedUserPass );
452 MockHttpServletResponse mockHttpServletResponse = new MockHttpServletResponse();
454 rssFeedServlet.doGet( request, mockHttpServletResponse );
456 assertEquals( RssFeedServlet.MIME_TYPE, mockHttpServletResponse.getHeader( "CONTENT-TYPE" ) );
457 assertNotNull( "Should have recieved a response", mockHttpServletResponse.getContentAsString() );
458 assertEquals( "Should have been an OK response code.", HttpServletResponse.SC_OK,
459 mockHttpServletResponse.getStatus() );
464 public void testRequestNewVersionsOfArtifact()
467 MockHttpServletRequest request = new MockHttpServletRequest();
468 request.setRequestURI( "/feeds/org/apache/archiva/artifact-two" );
469 request.addHeader( "User-Agent", "Apache Archiva unit test" );
470 request.setMethod( "GET" );
472 //WebRequest request = new GetMethodWebRequest( "http://localhost/feeds/org/apache/archiva/artifact-two" );
474 Base64 encoder = new Base64( 0, new byte[0] );
475 String userPass = "user1:password1";
476 String encodedUserPass = encoder.encodeToString( userPass.getBytes() );
477 request.addHeader( "Authorization", "BASIC " + encodedUserPass );
479 MockHttpServletResponse mockHttpServletResponse = new MockHttpServletResponse();
481 rssFeedServlet.doGet( request, mockHttpServletResponse );
483 assertEquals( RssFeedServlet.MIME_TYPE, mockHttpServletResponse.getHeader( "CONTENT-TYPE" ) );
484 assertNotNull( "Should have recieved a response", mockHttpServletResponse.getContentAsString() );
485 assertEquals( "Should have been an OK response code.", HttpServletResponse.SC_OK,
486 mockHttpServletResponse.getStatus() );
490 public void testInvalidRequest()
493 MockHttpServletRequest request = new MockHttpServletRequest();
494 request.setRequestURI( "/feeds?invalid_param=xxx" );
495 request.addHeader( "User-Agent", "Apache Archiva unit test" );
496 request.setMethod( "GET" );
498 MockHttpServletResponse mockHttpServletResponse = new MockHttpServletResponse();
500 rssFeedServlet.doGet( request, mockHttpServletResponse );
502 assertEquals( HttpServletResponse.SC_BAD_REQUEST, mockHttpServletResponse.getStatus() );
507 public void testInvalidAuthenticationRequest()
511 MockHttpServletRequest request = new MockHttpServletRequest();
512 request.setRequestURI( "/feeds/unauthorized-repo" );
513 request.addHeader( "User-Agent", "Apache Archiva unit test" );
514 request.setMethod( "GET" );
516 Encoder encoder = new Base64();
517 String userPass = "unauthUser:unauthPass";
518 String encodedUserPass = new String( (byte[]) encoder.encode( userPass.getBytes() ) );
519 request.addHeader( "Authorization", "BASIC " + encodedUserPass );
521 MockHttpServletResponse mockHttpServletResponse = new MockHttpServletResponse();
522 rssFeedServlet.doGet( request, mockHttpServletResponse );
524 assertEquals( HttpServletResponse.SC_UNAUTHORIZED, mockHttpServletResponse.getStatus() );
529 public void testUnauthorizedRequest()
533 MockHttpServletRequest request = new MockHttpServletRequest();
534 request.setRequestURI( "/feeds/unauthorized-repo" );
535 request.addHeader( "User-Agent", "Apache Archiva unit test" );
536 request.setMethod( "GET" );
538 Base64 encoder = new Base64( 0, new byte[0] );
539 String userPass = "user1:password1";
540 String encodedUserPass = encoder.encodeToString( userPass.getBytes() );
541 request.addHeader( "Authorization", "BASIC " + encodedUserPass );
543 MockHttpServletResponse mockHttpServletResponse = new MockHttpServletResponse();
544 rssFeedServlet.doGet( request, mockHttpServletResponse );
546 assertEquals( HttpServletResponse.SC_UNAUTHORIZED, mockHttpServletResponse.getStatus() );