]> source.dussan.org Git - archiva.git/commitdiff
remove use of httpunit
authorOlivier Lamy <olamy@apache.org>
Wed, 8 Jan 2014 04:57:00 +0000 (04:57 +0000)
committerOlivier Lamy <olamy@apache.org>
Wed, 8 Jan 2014 04:57:00 +0000 (04:57 +0000)
git-svn-id: https://svn.apache.org/repos/asf/archiva/trunk@1556440 13f79535-47bb-0310-9956-ffa450edef68

archiva-modules/archiva-base/archiva-repository-admin/archiva-repository-admin-default/src/main/java/org/apache/archiva/admin/repository/runtime/DefaultRedbackRuntimeConfigurationAdmin.java
archiva-modules/archiva-web/archiva-web-common/pom.xml
archiva-modules/archiva-web/archiva-web-common/src/main/java/org/apache/archiva/web/rss/RssFeedServlet.java
archiva-modules/archiva-web/archiva-web-common/src/test/java/org/apache/archiva/web/rss/RssFeedServletTest.java
pom.xml

index 9e8331085eade4e6b0b06bfdaae38ee91eb13f3e..c4d179eeb07448c699b8e1caed117512f2bcb22c 100644 (file)
@@ -67,8 +67,9 @@ public class DefaultRedbackRuntimeConfigurationAdmin
     private Cache usersCache;
 
     @Inject
-    public DefaultRedbackRuntimeConfigurationAdmin( ArchivaConfiguration archivaConfiguration, @Named(
-        value = "userConfiguration#redback" ) UserConfiguration userConfiguration,
+    public DefaultRedbackRuntimeConfigurationAdmin( ArchivaConfiguration archivaConfiguration,//
+                                                    @Named(
+                                                        value = "userConfiguration#redback" ) UserConfiguration userConfiguration,
                                                     @Named( value = "cache#users" ) Cache usersCache )
     {
         this.archivaConfiguration = archivaConfiguration;
index 1e3667c56f7517ec3fae8d8b7d450d37cd6643a4..1898d9cc98d73efd8e315726e603dde50d9b1ef4 100644 (file)
       <groupId>org.springframework</groupId>
       <artifactId>spring-web</artifactId>
     </dependency>
-    <dependency>
-      <groupId>httpunit</groupId>
-      <artifactId>httpunit</artifactId>
-      <scope>test</scope>
-    </dependency>
     <dependency>
       <groupId>junit</groupId>
       <artifactId>junit</artifactId>
index d230c7ba7377a874e95c8d2c730b1d04affc9e20..e822c6b1df2513e4d8649b367f67483af60b7e2b 100644 (file)
@@ -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 );
index b0365abe9477b1b48db3d44a6795227f06fd0318..ef42aad9f9f1680b7693e5828626ad43db3d8cd5 100644 (file)
@@ -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() );
-        }
     }
 
 
diff --git a/pom.xml b/pom.xml
index 15f58fb8381cb0cb0d0f7fef6f0306199c8de40c..e21a96009d715eff8bedf54b12578565485ebc16 100644 (file)
--- a/pom.xml
+++ b/pom.xml
         <version>2.0M9</version>
         <scope>test</scope>
       </dependency>
-      <dependency>
-        <groupId>httpunit</groupId>
-        <artifactId>httpunit</artifactId>
-        <version>1.7</version>
-        <scope>test</scope>
-      </dependency>
       <dependency>
         <groupId>org.apache.tomcat.embed</groupId>
         <artifactId>tomcat-embed-core</artifactId>