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.managed.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 <T> ObjectProvider<T> getBeanProvider( Class<T> aClass, boolean b )
240 public <T> ObjectProvider<T> getBeanProvider( ResolvableType resolvableType, boolean b )
246 public String[] getBeanNamesForType( Class<?> aClass )
248 return applicationContext.getBeanNamesForType( aClass );
252 public String[] getBeanNamesForType( Class<?> aClass, boolean b, boolean b2 )
254 return applicationContext.getBeanNamesForType( aClass, b, b2 );
258 public <T> Map<String, T> getBeansOfType( Class<T> tClass )
259 throws BeansException
261 return applicationContext.getBeansOfType( tClass );
265 public <T> T getBean( Class<T> aClass, Object... objects )
266 throws BeansException
268 return applicationContext.getBean( aClass, objects );
272 public <T> ObjectProvider<T> getBeanProvider( Class<T> aClass )
278 public <T> ObjectProvider<T> getBeanProvider( ResolvableType resolvableType )
284 public <T> Map<String, T> getBeansOfType( Class<T> tClass, boolean b, boolean b2 )
285 throws BeansException
287 return applicationContext.getBeansOfType( tClass, b, b2 );
291 public String[] getBeanNamesForAnnotation( Class<? extends Annotation> aClass )
293 return applicationContext.getBeanNamesForAnnotation( aClass );
297 public Map<String, Object> getBeansWithAnnotation( Class<? extends Annotation> aClass )
298 throws BeansException
300 return applicationContext.getBeansWithAnnotation( aClass );
304 public <A extends Annotation> A findAnnotationOnBean( String s, Class<A> aClass )
305 throws NoSuchBeanDefinitionException
307 return applicationContext.findAnnotationOnBean( s, aClass );
311 public <A extends Annotation> A findAnnotationOnBean( String s, Class<A> aClass, boolean b ) throws NoSuchBeanDefinitionException
313 throw new UnsupportedOperationException( "Not implemented yet." );
317 public Object getBean( String s )
318 throws BeansException
320 return applicationContext.getBean( s );
324 public <T> T getBean( String s, Class<T> tClass )
325 throws BeansException
327 return applicationContext.getBean( s, tClass );
331 public <T> T getBean( Class<T> tClass )
332 throws BeansException
334 return applicationContext.getBean( tClass );
338 public Object getBean( String s, Object... objects )
339 throws BeansException
341 return applicationContext.getBean( s, objects );
345 public boolean containsBean( String s )
347 return applicationContext.containsBean( s );
351 public boolean isSingleton( String s )
352 throws NoSuchBeanDefinitionException
354 return applicationContext.isSingleton( s );
358 public boolean isPrototype( String s )
359 throws NoSuchBeanDefinitionException
361 return applicationContext.isPrototype( s );
365 public boolean isTypeMatch( String s, Class<?> aClass )
366 throws NoSuchBeanDefinitionException
368 return applicationContext.isTypeMatch( s, aClass );
372 public Class<?> getType( String s )
373 throws NoSuchBeanDefinitionException
375 return applicationContext.getType( s );
379 public Class<?> getType( String s, boolean b ) throws NoSuchBeanDefinitionException
385 public String[] getAliases( String s )
387 return applicationContext.getAliases( s );
391 public String getMessage( String s, Object[] objects, String s2, Locale locale )
393 return applicationContext.getMessage( s, objects, s2, locale );
397 public String getMessage( String s, Object[] objects, Locale locale )
398 throws NoSuchMessageException
400 return applicationContext.getMessage( s, objects, locale );
404 public String getMessage( MessageSourceResolvable messageSourceResolvable, Locale locale )
405 throws NoSuchMessageException
407 return applicationContext.getMessage( messageSourceResolvable, locale );
411 public Resource[] getResources( String s )
414 return applicationContext.getResources( s );
418 public Resource getResource( String s )
420 return applicationContext.getResource( s );
424 public ClassLoader getClassLoader()
426 return applicationContext.getClassLoader();
430 public void publishEvent( Object o )
436 public String[] getBeanNamesForType( ResolvableType resolvableType )
438 return new String[0];
442 public String[] getBeanNamesForType( ResolvableType resolvableType, boolean b, boolean b1 )
444 return new String[0];
448 public boolean isTypeMatch( String s, ResolvableType resolvableType )
449 throws NoSuchBeanDefinitionException
457 public void testRequestNewArtifactsInRepo()
460 MockHttpServletRequest request = new MockHttpServletRequest();
461 request.setRequestURI( "/feeds/test-repo" );
462 request.addHeader( "User-Agent", "Apache Archiva unit test" );
463 request.setMethod( "GET" );
465 Base64 encoder = new Base64( 0, new byte[0] );
466 String userPass = "user1:password1";
467 String encodedUserPass = encoder.encodeToString( userPass.getBytes() );
468 request.addHeader( "Authorization", "BASIC " + encodedUserPass );
470 MockHttpServletResponse mockHttpServletResponse = new MockHttpServletResponse();
472 rssFeedServlet.doGet( request, mockHttpServletResponse );
474 assertEquals( RssFeedServlet.MIME_TYPE, mockHttpServletResponse.getHeader( "CONTENT-TYPE" ) );
475 assertNotNull( "Should have recieved a response", mockHttpServletResponse.getContentAsString() );
476 assertEquals( "Should have been an OK response code.", HttpServletResponse.SC_OK,
477 mockHttpServletResponse.getStatus() );
482 public void testRequestNewVersionsOfArtifact()
485 MockHttpServletRequest request = new MockHttpServletRequest();
486 request.setRequestURI( "/feeds/org/apache/archiva/artifact-two" );
487 request.addHeader( "User-Agent", "Apache Archiva unit test" );
488 request.setMethod( "GET" );
490 //WebRequest request = new GetMethodWebRequest( "http://localhost/feeds/org/apache/archiva/artifact-two" );
492 Base64 encoder = new Base64( 0, new byte[0] );
493 String userPass = "user1:password1";
494 String encodedUserPass = encoder.encodeToString( userPass.getBytes() );
495 request.addHeader( "Authorization", "BASIC " + encodedUserPass );
497 MockHttpServletResponse mockHttpServletResponse = new MockHttpServletResponse();
499 rssFeedServlet.doGet( request, mockHttpServletResponse );
501 assertEquals( RssFeedServlet.MIME_TYPE, mockHttpServletResponse.getHeader( "CONTENT-TYPE" ) );
502 assertNotNull( "Should have recieved a response", mockHttpServletResponse.getContentAsString() );
503 assertEquals( "Should have been an OK response code.", HttpServletResponse.SC_OK,
504 mockHttpServletResponse.getStatus() );
508 public void testInvalidRequest()
511 MockHttpServletRequest request = new MockHttpServletRequest();
512 request.setRequestURI( "/feeds?invalid_param=xxx" );
513 request.addHeader( "User-Agent", "Apache Archiva unit test" );
514 request.setMethod( "GET" );
516 MockHttpServletResponse mockHttpServletResponse = new MockHttpServletResponse();
518 rssFeedServlet.doGet( request, mockHttpServletResponse );
520 assertEquals( HttpServletResponse.SC_BAD_REQUEST, mockHttpServletResponse.getStatus() );
525 public void testInvalidAuthenticationRequest()
529 MockHttpServletRequest request = new MockHttpServletRequest();
530 request.setRequestURI( "/feeds/unauthorized-repo" );
531 request.addHeader( "User-Agent", "Apache Archiva unit test" );
532 request.setMethod( "GET" );
534 Encoder encoder = new Base64();
535 String userPass = "unauthUser:unauthPass";
536 String encodedUserPass = new String( (byte[]) encoder.encode( userPass.getBytes() ) );
537 request.addHeader( "Authorization", "BASIC " + encodedUserPass );
539 MockHttpServletResponse mockHttpServletResponse = new MockHttpServletResponse();
540 rssFeedServlet.doGet( request, mockHttpServletResponse );
542 assertEquals( HttpServletResponse.SC_UNAUTHORIZED, mockHttpServletResponse.getStatus() );
547 public void testUnauthorizedRequest()
551 MockHttpServletRequest request = new MockHttpServletRequest();
552 request.setRequestURI( "/feeds/unauthorized-repo" );
553 request.addHeader( "User-Agent", "Apache Archiva unit test" );
554 request.setMethod( "GET" );
556 Base64 encoder = new Base64( 0, new byte[0] );
557 String userPass = "user1:password1";
558 String encodedUserPass = encoder.encodeToString( userPass.getBytes() );
559 request.addHeader( "Authorization", "BASIC " + encodedUserPass );
561 MockHttpServletResponse mockHttpServletResponse = new MockHttpServletResponse();
562 rssFeedServlet.doGet( request, mockHttpServletResponse );
564 assertEquals( HttpServletResponse.SC_UNAUTHORIZED, mockHttpServletResponse.getStatus() );