diff options
author | Olivier Lamy <olamy@apache.org> | 2014-01-08 04:57:00 +0000 |
---|---|---|
committer | Olivier Lamy <olamy@apache.org> | 2014-01-08 04:57:00 +0000 |
commit | 2ac34a15b226be293adb5a98a5be8c35db7e2d46 (patch) | |
tree | d5bcc7a19d3e9fe4468c6ab380ba4d3de49daa40 /archiva-modules/archiva-web/archiva-web-common/src | |
parent | 9c775bf55afba5cb0ad66c5a4484e76e07b1b35f (diff) | |
download | archiva-2ac34a15b226be293adb5a98a5be8c35db7e2d46.tar.gz archiva-2ac34a15b226be293adb5a98a5be8c35db7e2d46.zip |
remove use of httpunit
git-svn-id: https://svn.apache.org/repos/asf/archiva/trunk@1556440 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'archiva-modules/archiva-web/archiva-web-common/src')
2 files changed, 366 insertions, 92 deletions
diff --git a/archiva-modules/archiva-web/archiva-web-common/src/main/java/org/apache/archiva/web/rss/RssFeedServlet.java b/archiva-modules/archiva-web/archiva-web-common/src/main/java/org/apache/archiva/web/rss/RssFeedServlet.java index d230c7ba7..e822c6b1d 100644 --- a/archiva-modules/archiva-web/archiva-web-common/src/main/java/org/apache/archiva/web/rss/RssFeedServlet.java +++ b/archiva-modules/archiva-web/archiva-web-common/src/main/java/org/apache/archiva/web/rss/RssFeedServlet.java @@ -50,6 +50,7 @@ import org.slf4j.LoggerFactory; import org.springframework.web.context.WebApplicationContext; import org.springframework.web.context.support.WebApplicationContextUtils; +import javax.servlet.ServletConfig; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; @@ -92,7 +93,7 @@ public class RssFeedServlet */ private RepositorySessionFactory repositorySessionFactory; - public void init( javax.servlet.ServletConfig servletConfig ) + public void init( ServletConfig servletConfig ) throws ServletException { super.init( servletConfig ); 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() ); - } } |