diff options
Diffstat (limited to 'archiva-modules/archiva-web/archiva-web-common/src/test')
-rw-r--r-- | archiva-modules/archiva-web/archiva-web-common/src/test/java/org/apache/archiva/web/rss/RssFeedServletTest.java | 455 |
1 files changed, 364 insertions, 91 deletions
diff --git a/archiva-modules/archiva-web/archiva-web-common/src/test/java/org/apache/archiva/web/rss/RssFeedServletTest.java b/archiva-modules/archiva-web/archiva-web-common/src/test/java/org/apache/archiva/web/rss/RssFeedServletTest.java index b0365abe9..ef42aad9f 100644 --- a/archiva-modules/archiva-web/archiva-web-common/src/test/java/org/apache/archiva/web/rss/RssFeedServletTest.java +++ b/archiva-modules/archiva-web/archiva-web-common/src/test/java/org/apache/archiva/web/rss/RssFeedServletTest.java @@ -19,13 +19,10 @@ package org.apache.archiva.web.rss; * under the License. */ -import com.meterware.httpunit.GetMethodWebRequest; -import com.meterware.httpunit.HttpException; -import com.meterware.httpunit.WebRequest; -import com.meterware.httpunit.WebResponse; -import com.meterware.servletunit.ServletRunner; -import com.meterware.servletunit.ServletUnitClient; + import junit.framework.TestCase; +import org.apache.archiva.test.utils.ArchivaBlockJUnit4ClassRunner; +import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner; import org.apache.commons.codec.Encoder; import org.apache.commons.codec.binary.Base64; import org.junit.After; @@ -35,21 +32,45 @@ import org.junit.BeforeClass; import org.junit.Ignore; import org.junit.Test; import org.junit.runner.RunWith; - +import org.springframework.beans.BeansException; +import org.springframework.beans.factory.BeanFactory; +import org.springframework.beans.factory.NoSuchBeanDefinitionException; +import org.springframework.beans.factory.config.AutowireCapableBeanFactory; +import org.springframework.context.ApplicationContext; +import org.springframework.context.ApplicationEvent; +import org.springframework.context.MessageSourceResolvable; +import org.springframework.context.NoSuchMessageException; +import org.springframework.core.env.Environment; +import org.springframework.core.io.Resource; +import org.springframework.mock.web.MockHttpServletRequest; +import org.springframework.mock.web.MockHttpServletResponse; +import org.springframework.mock.web.MockServletConfig; +import org.springframework.mock.web.MockServletContext; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.web.context.WebApplicationContext; + +import javax.inject.Inject; +import javax.servlet.ServletContext; import javax.servlet.http.HttpServletResponse; -import java.io.File; -import org.apache.archiva.test.utils.ArchivaBlockJUnit4ClassRunner; - -@RunWith( ArchivaBlockJUnit4ClassRunner.class ) +import java.io.IOException; +import java.lang.annotation.Annotation; +import java.util.Locale; +import java.util.Map; + +@RunWith( ArchivaSpringJUnit4ClassRunner.class ) +@ContextConfiguration( + locations = { "classpath*:/META-INF/spring-context.xml", "classpath*:/spring-context-test-common.xml", + "classpath*:/spring-context-rss-servlet.xml" } ) public class RssFeedServletTest extends TestCase { - private ServletRunner sr; - - private ServletUnitClient client; + private RssFeedServlet rssFeedServlet = new RssFeedServlet(); static String PREVIOUS_ARCHIVA_PATH; + @Inject + protected ApplicationContext applicationContext; + @BeforeClass public static void initConfigurationPath() throws Exception @@ -59,6 +80,7 @@ public class RssFeedServletTest System.getProperty( "test.resources.path/" ) + "empty-archiva.xml" ); } + @AfterClass public static void restoreConfigurationPath() throws Exception @@ -70,56 +92,307 @@ public class RssFeedServletTest public void setUp() throws Exception { - sr = new ServletRunner( new File( "src/test/webapp/WEB-INF/feedServletTest-web.xml" ) ); - client = sr.newClient(); + final MockServletContext mockServletContext = new MockServletContext(); + + WebApplicationContext webApplicationContext = + new TestWebapplicationContext( applicationContext, mockServletContext ); + + mockServletContext.setAttribute( WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, + webApplicationContext ); + + MockServletConfig mockServletConfig = new MockServletConfig() + { + @Override + public ServletContext getServletContext() + { + return mockServletContext; + } + }; + + rssFeedServlet.init( mockServletConfig ); } @After public void tearDown() throws Exception { - if ( client != null ) + super.tearDown(); + } + + public static class TestWebapplicationContext + implements WebApplicationContext + { + private ApplicationContext applicationContext; + + private ServletContext servletContext; + + TestWebapplicationContext( ApplicationContext applicationContext, ServletContext servletContext ) { - client.clearContents(); + this.applicationContext = applicationContext; } - if ( sr != null ) + @Override + public ServletContext getServletContext() { - sr.shutDown(); + return servletContext; } - super.tearDown(); - } + @Override + public String getId() + { + return applicationContext.getId(); + } - @Test - public void testRetrieveServlet() - throws Exception - { + @Override + public String getApplicationName() + { + return applicationContext.getApplicationName(); + } + + @Override + public String getDisplayName() + { + return applicationContext.getDisplayName(); + } + + @Override + public long getStartupDate() + { + return applicationContext.getStartupDate(); + } + + @Override + public ApplicationContext getParent() + { + return applicationContext.getParent(); + } + + @Override + public AutowireCapableBeanFactory getAutowireCapableBeanFactory() + throws IllegalStateException + { + return applicationContext.getAutowireCapableBeanFactory(); + } + + @Override + public void publishEvent( ApplicationEvent applicationEvent ) + { + applicationContext.publishEvent( applicationEvent ); + } + + @Override + public Environment getEnvironment() + { + return applicationContext.getEnvironment(); + } + + @Override + public BeanFactory getParentBeanFactory() + { + return applicationContext.getParentBeanFactory(); + } + + @Override + public boolean containsLocalBean( String s ) + { + return applicationContext.containsLocalBean( s ); + } + + @Override + public boolean containsBeanDefinition( String s ) + { + return applicationContext.containsBeanDefinition( s ); + } + + @Override + public int getBeanDefinitionCount() + { + return applicationContext.getBeanDefinitionCount(); + } + + @Override + public String[] getBeanDefinitionNames() + { + return applicationContext.getBeanDefinitionNames(); + } + + @Override + public String[] getBeanNamesForType( Class<?> aClass ) + { + return applicationContext.getBeanNamesForType( aClass ); + } + + @Override + public String[] getBeanNamesForType( Class<?> aClass, boolean b, boolean b2 ) + { + return applicationContext.getBeanNamesForType( aClass, b, b2 ); + } + + @Override + public <T> Map<String, T> getBeansOfType( Class<T> tClass ) + throws BeansException + { + return applicationContext.getBeansOfType( tClass ); + } + + @Override + public <T> Map<String, T> getBeansOfType( Class<T> tClass, boolean b, boolean b2 ) + throws BeansException + { + return applicationContext.getBeansOfType( tClass, b, b2 ); + } + + @Override + public String[] getBeanNamesForAnnotation( Class<? extends Annotation> aClass ) + { + return applicationContext.getBeanNamesForAnnotation( aClass ); + } + + @Override + public Map<String, Object> getBeansWithAnnotation( Class<? extends Annotation> aClass ) + throws BeansException + { + return applicationContext.getBeansWithAnnotation( aClass ); + } - RssFeedServlet servlet = - (RssFeedServlet) client.newInvocation( "http://localhost/feeds/test-repo" ).getServlet(); - assertNotNull( servlet ); + @Override + public <A extends Annotation> A findAnnotationOnBean( String s, Class<A> aClass ) + throws NoSuchBeanDefinitionException + { + return applicationContext.findAnnotationOnBean( s, aClass ); + } + + @Override + public Object getBean( String s ) + throws BeansException + { + return applicationContext.getBean( s ); + } + + @Override + public <T> T getBean( String s, Class<T> tClass ) + throws BeansException + { + return applicationContext.getBean( s, tClass ); + } + + @Override + public <T> T getBean( Class<T> tClass ) + throws BeansException + { + return applicationContext.getBean( tClass ); + } + + @Override + public Object getBean( String s, Object... objects ) + throws BeansException + { + return applicationContext.getBean( s, objects ); + } + + @Override + public boolean containsBean( String s ) + { + return applicationContext.containsBean( s ); + } + + @Override + public boolean isSingleton( String s ) + throws NoSuchBeanDefinitionException + { + return applicationContext.isSingleton( s ); + } + + @Override + public boolean isPrototype( String s ) + throws NoSuchBeanDefinitionException + { + return applicationContext.isPrototype( s ); + } + + @Override + public boolean isTypeMatch( String s, Class<?> aClass ) + throws NoSuchBeanDefinitionException + { + return applicationContext.isTypeMatch( s, aClass ); + } + + @Override + public Class<?> getType( String s ) + throws NoSuchBeanDefinitionException + { + return applicationContext.getType( s ); + } + + @Override + public String[] getAliases( String s ) + { + return applicationContext.getAliases( s ); + } + + @Override + public String getMessage( String s, Object[] objects, String s2, Locale locale ) + { + return applicationContext.getMessage( s, objects, s2, locale ); + } + + @Override + public String getMessage( String s, Object[] objects, Locale locale ) + throws NoSuchMessageException + { + return applicationContext.getMessage( s, objects, locale ); + } + + @Override + public String getMessage( MessageSourceResolvable messageSourceResolvable, Locale locale ) + throws NoSuchMessageException + { + return applicationContext.getMessage( messageSourceResolvable, locale ); + } + + @Override + public Resource[] getResources( String s ) + throws IOException + { + return applicationContext.getResources( s ); + } + + @Override + public Resource getResource( String s ) + { + return applicationContext.getResource( s ); + } + + @Override + public ClassLoader getClassLoader() + { + return applicationContext.getClassLoader(); + } } + @Test public void testRequestNewArtifactsInRepo() throws Exception { - RssFeedServlet servlet = - (RssFeedServlet) client.newInvocation( "http://localhost/feeds/test-repo" ).getServlet(); - assertNotNull( servlet ); - - WebRequest request = new GetMethodWebRequest( "http://localhost/feeds/test-repo" ); + MockHttpServletRequest request = new MockHttpServletRequest(); + request.setRequestURI( "/feeds/test-repo" ); + request.addHeader( "User-Agent", "Apache Archiva unit test" ); + request.setMethod( "GET" ); Base64 encoder = new Base64( 0, new byte[0] ); String userPass = "user1:password1"; String encodedUserPass = encoder.encodeToString( userPass.getBytes() ); - request.setHeaderField( "Authorization", "BASIC " + encodedUserPass ); + request.addHeader( "Authorization", "BASIC " + encodedUserPass ); + + MockHttpServletResponse mockHttpServletResponse = new MockHttpServletResponse(); + + rssFeedServlet.doGet( request, mockHttpServletResponse ); - WebResponse response = client.getResponse( request ); - assertEquals( RssFeedServlet.MIME_TYPE, response.getHeaderField( "CONTENT-TYPE" ) ); - assertNotNull( "Should have recieved a response", response ); - assertEquals( "Should have been an OK response code.", HttpServletResponse.SC_OK, response.getResponseCode() ); + assertEquals( RssFeedServlet.MIME_TYPE, mockHttpServletResponse.getHeader( "CONTENT-TYPE" ) ); + assertNotNull( "Should have recieved a response", mockHttpServletResponse.getContentAsString() ); + assertEquals( "Should have been an OK response code.", HttpServletResponse.SC_OK, + mockHttpServletResponse.getStatus() ); } @@ -127,95 +400,95 @@ public class RssFeedServletTest public void testRequestNewVersionsOfArtifact() throws Exception { - RssFeedServlet servlet = (RssFeedServlet) client.newInvocation( - "http://localhost/feeds/org/apache/archiva/artifact-two" ).getServlet(); - assertNotNull( servlet ); + MockHttpServletRequest request = new MockHttpServletRequest(); + request.setRequestURI( "/feeds/org/apache/archiva/artifact-two" ); + request.addHeader( "User-Agent", "Apache Archiva unit test" ); + request.setMethod( "GET" ); - WebRequest request = new GetMethodWebRequest( "http://localhost/feeds/org/apache/archiva/artifact-two" ); + //WebRequest request = new GetMethodWebRequest( "http://localhost/feeds/org/apache/archiva/artifact-two" ); Base64 encoder = new Base64( 0, new byte[0] ); String userPass = "user1:password1"; String encodedUserPass = encoder.encodeToString( userPass.getBytes() ); - request.setHeaderField( "Authorization", "BASIC " + encodedUserPass ); + request.addHeader( "Authorization", "BASIC " + encodedUserPass ); - WebResponse response = client.getResponse( request ); - assertEquals( RssFeedServlet.MIME_TYPE, response.getHeaderField( "CONTENT-TYPE" ) ); - assertNotNull( "Should have recieved a response", response ); - assertEquals( "Should have been an OK response code.", HttpServletResponse.SC_OK, response.getResponseCode() ); + MockHttpServletResponse mockHttpServletResponse = new MockHttpServletResponse(); + + rssFeedServlet.doGet( request, mockHttpServletResponse ); + + assertEquals( RssFeedServlet.MIME_TYPE, mockHttpServletResponse.getHeader( "CONTENT-TYPE" ) ); + assertNotNull( "Should have recieved a response", mockHttpServletResponse.getContentAsString() ); + assertEquals( "Should have been an OK response code.", HttpServletResponse.SC_OK, + mockHttpServletResponse.getStatus() ); } @Ignore public void XXX_testInvalidRequest() throws Exception { - RssFeedServlet servlet = - (RssFeedServlet) client.newInvocation( "http://localhost/feeds?invalid_param=xxx" ).getServlet(); - assertNotNull( servlet ); + //RssFeedServlet servlet = + // (RssFeedServlet) client.newInvocation( "http://localhost/feeds?invalid_param=xxx" ).getServlet(); + MockHttpServletRequest request = new MockHttpServletRequest(); + request.setRequestURI( "/feeds?invalid_param=xxx" ); + request.addHeader( "User-Agent", "Apache Archiva unit test" ); + request.setMethod( "GET" ); + + MockHttpServletResponse mockHttpServletResponse = new MockHttpServletResponse(); + + rssFeedServlet.doGet( request, mockHttpServletResponse ); + + assertEquals( HttpServletResponse.SC_BAD_REQUEST, mockHttpServletResponse.getStatus() ); - try - { - WebResponse resp = client.getResponse( "http://localhost/feeds?invalid_param=xxx" ); - assertEquals( HttpServletResponse.SC_BAD_REQUEST, resp.getResponseCode() ); - } - catch ( HttpException he ) - { - assertEquals( "Should have been a bad request response code.", HttpServletResponse.SC_BAD_REQUEST, - he.getResponseCode() ); - } } @Ignore public void XXX_testInvalidAuthenticationRequest() throws Exception { - RssFeedServlet servlet = - (RssFeedServlet) client.newInvocation( "http://localhost/feeds/unauthorized-repo" ).getServlet(); - assertNotNull( servlet ); + //RssFeedServlet servlet = + // (RssFeedServlet) client.newInvocation( "http://localhost/feeds/unauthorized-repo" ).getServlet(); - WebRequest request = new GetMethodWebRequest( "http://localhost/feeds/unauthorized-repo" ); + //WebRequest request = new GetMethodWebRequest( "http://localhost/feeds/unauthorized-repo" ); + + MockHttpServletRequest request = new MockHttpServletRequest(); + request.setRequestURI( "/feeds/unauthorized-repo" ); + request.addHeader( "User-Agent", "Apache Archiva unit test" ); + request.setMethod( "GET" ); Encoder encoder = new Base64(); String userPass = "unauthUser:unauthPass"; String encodedUserPass = new String( (byte[]) encoder.encode( userPass.getBytes() ) ); - request.setHeaderField( "Authorization", "BASIC " + encodedUserPass ); + request.addHeader( "Authorization", "BASIC " + encodedUserPass ); + + MockHttpServletResponse mockHttpServletResponse = new MockHttpServletResponse(); + rssFeedServlet.doGet( request, mockHttpServletResponse ); + + assertEquals( HttpServletResponse.SC_UNAUTHORIZED, mockHttpServletResponse.getStatus() ); - try - { - WebResponse resp = client.getResponse( request ); - assertEquals( HttpServletResponse.SC_UNAUTHORIZED, resp.getResponseCode() ); - } - catch ( HttpException he ) - { - assertEquals( "Should have been a unauthorized response.", HttpServletResponse.SC_UNAUTHORIZED, - he.getResponseCode() ); - } } @Ignore public void XXX_testUnauthorizedRequest() throws Exception { - RssFeedServlet servlet = - (RssFeedServlet) client.newInvocation( "http://localhost/feeds/unauthorized-repo" ).getServlet(); - assertNotNull( servlet ); - WebRequest request = new GetMethodWebRequest( "http://localhost/feeds/unauthorized-repo" ); + MockHttpServletRequest request = new MockHttpServletRequest(); + request.setRequestURI( "/feeds/unauthorized-repo" ); + request.addHeader( "User-Agent", "Apache Archiva unit test" ); + request.setMethod( "GET" ); + + //WebRequest request = new GetMethodWebRequest( "http://localhost/feeds/unauthorized-repo" ); Base64 encoder = new Base64( 0, new byte[0] ); String userPass = "user1:password1"; String encodedUserPass = encoder.encodeToString( userPass.getBytes() ); - request.setHeaderField( "Authorization", "BASIC " + encodedUserPass ); + request.addHeader( "Authorization", "BASIC " + encodedUserPass ); + + MockHttpServletResponse mockHttpServletResponse = new MockHttpServletResponse(); + rssFeedServlet.doGet( request, mockHttpServletResponse ); + + assertEquals( HttpServletResponse.SC_UNAUTHORIZED, mockHttpServletResponse.getStatus() ); - try - { - WebResponse resp = client.getResponse( request ); - assertEquals( HttpServletResponse.SC_UNAUTHORIZED, resp.getResponseCode() ); - } - catch ( HttpException he ) - { - assertEquals( "Should have been a unauthorized response.", HttpServletResponse.SC_UNAUTHORIZED, - he.getResponseCode() ); - } } |