]> source.dussan.org Git - archiva.git/commitdiff
use mock servlet here too to have faster tests
authorOlivier Lamy <olamy@apache.org>
Mon, 6 Jan 2014 05:32:38 +0000 (05:32 +0000)
committerOlivier Lamy <olamy@apache.org>
Mon, 6 Jan 2014 05:32:38 +0000 (05:32 +0000)
git-svn-id: https://svn.apache.org/repos/asf/archiva/trunk@1555678 13f79535-47bb-0310-9956-ffa450edef68

archiva-modules/archiva-web/archiva-webdav/pom.xml
archiva-modules/archiva-web/archiva-webdav/src/test/java/org/apache/archiva/webdav/AbstractRepositoryServletTestCase.java
archiva-modules/archiva-web/archiva-webdav/src/test/java/org/apache/archiva/webdav/RepositoryServletSecurityTest.java
archiva-modules/archiva-web/archiva-webdav/src/test/java/org/apache/archiva/webdav/RepositoryServletTest.java

index 65227715f5466eefcdeda37173606d5e83bbcbff..c0d18a00baef4b7524be5deba1ced6621eb857ff 100644 (file)
       <scope>test</scope>
     </dependency>
 
-    <dependency>
-      <groupId>org.apache.tomcat.embed</groupId>
-      <artifactId>tomcat-embed-core</artifactId>
-      <scope>test</scope>
-    </dependency>
-    <dependency>
-      <groupId>org.apache.tomcat</groupId>
-      <artifactId>tomcat-juli</artifactId>
-      <scope>test</scope>
-    </dependency>
-    <dependency>
-      <groupId>org.apache.tomcat.embed</groupId>
-      <artifactId>tomcat-embed-logging-juli</artifactId>
-      <scope>test</scope>
-    </dependency>
-
-
     <dependency>
       <groupId>org.apache.maven.wagon</groupId>
       <artifactId>wagon-http</artifactId>
index c1dbd95b18c1472e32e56c62401c914beb1193e3..9fea3a9793f110e6d5e10132ad4e51047ed2f828 100644 (file)
@@ -36,8 +36,6 @@ import org.apache.archiva.configuration.RemoteRepositoryConfiguration;
 import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
 import org.apache.archiva.webdav.httpunit.MkColMethodWebRequest;
 import org.apache.archiva.webdav.util.MavenIndexerCleaner;
-import org.apache.catalina.core.StandardContext;
-import org.apache.catalina.startup.Tomcat;
 import org.apache.commons.io.FileUtils;
 import org.apache.commons.io.IOUtils;
 import org.junit.After;
@@ -112,13 +110,6 @@ public abstract class AbstractRepositoryServletTestCase
         saveConfiguration( archivaConfiguration );
     }
 
-    protected Tomcat tomcat;
-
-    protected static int port;
-
-
-    StandardContext context;
-
     @Before
     public void setUp()
         throws Exception
@@ -193,7 +184,7 @@ public abstract class AbstractRepositoryServletTestCase
     }
 
 
-    static class TestWebapplicationContext
+    public static class TestWebapplicationContext
         implements WebApplicationContext
     {
         private ApplicationContext applicationContext;
@@ -675,11 +666,6 @@ public abstract class AbstractRepositoryServletTestCase
             FileUtils.deleteDirectory( repoRootLegacy );
         }
 
-        if ( this.tomcat != null )
-        {
-            this.tomcat.stop();
-        }
-
     }
 
 
index 37f4b93bd80555dc84920498b24f213fadedcd3a..507d7bebdc6b4e100137252156bc961b74f49556 100644 (file)
@@ -40,10 +40,6 @@ import org.apache.archiva.security.ServletAuthenticator;
 import org.apache.archiva.security.common.ArchivaRoleConstants;
 import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
 import org.apache.archiva.webdav.util.MavenIndexerCleaner;
-import org.apache.catalina.Container;
-import org.apache.catalina.core.StandardContext;
-import org.apache.catalina.deploy.ApplicationParameter;
-import org.apache.catalina.startup.Tomcat;
 import org.apache.commons.io.FileUtils;
 import org.apache.commons.io.IOUtils;
 import org.apache.commons.lang.StringUtils;
@@ -58,11 +54,15 @@ import org.junit.runner.RunWith;
 import org.springframework.context.ApplicationContext;
 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.ContextLoaderListener;
+import org.springframework.web.context.WebApplicationContext;
 
 import javax.inject.Inject;
 import javax.servlet.Servlet;
+import javax.servlet.ServletContext;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 import javax.servlet.http.HttpSession;
@@ -79,7 +79,7 @@ import static org.easymock.EasyMock.eq;
  * perform redback security checking.
  */
 @RunWith( ArchivaSpringJUnit4ClassRunner.class )
-@ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath*:/spring-context.xml" } )
+@ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath*:/spring-context-servlet-security-test.xml" } )
 public class RepositoryServletSecurityTest
     extends TestCase
 {
@@ -88,6 +88,7 @@ public class RepositoryServletSecurityTest
 
     protected File repoRootInternal;
 
+    @Inject
     protected ArchivaConfiguration archivaConfiguration;
 
     private DavSessionProvider davSessionProvider;
@@ -102,12 +103,6 @@ public class RepositoryServletSecurityTest
 
     private RepositoryServlet servlet;
 
-    protected Tomcat tomcat;
-
-    protected static int port;
-
-    StandardContext context;
-
     @Inject
     ApplicationContext applicationContext;
 
@@ -126,7 +121,6 @@ public class RepositoryServletSecurityTest
 
         repoRootInternal = new File( appserverBase, "data/repositories/internal" );
 
-        archivaConfiguration = applicationContext.getBean( ArchivaConfiguration.class );
         Configuration config = archivaConfiguration.getConfiguration();
 
         if ( !config.getManagedRepositoriesAsMap().containsKey( REPOID_INTERNAL ) )
@@ -138,27 +132,6 @@ public class RepositoryServletSecurityTest
 
         CacheManager.getInstance().clearAll();
 
-        tomcat = new Tomcat();
-        tomcat.setBaseDir( System.getProperty( "java.io.tmpdir" ) );
-        tomcat.setPort( 0 );
-
-        context = StandardContext.class.cast( tomcat.addContext( "", System.getProperty( "java.io.tmpdir" ) ) );
-
-        ApplicationParameter applicationParameter = new ApplicationParameter();
-        applicationParameter.setName( "contextConfigLocation" );
-        applicationParameter.setValue( getSpringConfigLocation() );
-        context.addApplicationParameter( applicationParameter );
-
-        context.addApplicationListener( ContextLoaderListener.class.getName() );
-
-        context.addApplicationListener( MavenIndexerCleaner.class.getName() );
-
-        Tomcat.addServlet( context, "repository", new UnauthenticatedRepositoryServlet() );
-        context.addServletMapping( "/repository/*", "repository" );
-
-        tomcat.start();
-
-        this.port = tomcat.getConnector().getLocalPort();
 
         servletAuthControl = EasyMock.createControl();
 
@@ -170,21 +143,26 @@ public class RepositoryServletSecurityTest
 
         davSessionProvider = new ArchivaDavSessionProvider( servletAuth, httpAuth );
 
-        // FIXME use mock to avoid starting Tomcat
-        //RepositoryServlet repositoryServlet = new RepositoryServlet();
-        //MockServletConfig mockServletConfig = new MockServletConfig();
+        final MockServletContext mockServletContext = new MockServletContext();
 
-        //MockServletContext mockServletContext = new MockServletContext(  );
-        //mockServletContext
+        WebApplicationContext webApplicationContext =
+            new AbstractRepositoryServletTestCase.TestWebapplicationContext( applicationContext, mockServletContext );
 
-        //repositoryServlet.init( mockServletConfig );
+        mockServletContext.setAttribute( WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE,
+                                         webApplicationContext );
 
-        servlet = RepositoryServlet.class.cast( findServlet( "repository" ) );
-    }
+        MockServletConfig mockServletConfig = new MockServletConfig()
+        {
+            @Override
+            public ServletContext getServletContext()
+            {
+                return mockServletContext;
+            }
+        };
 
-    protected String getSpringConfigLocation()
-    {
-        return "classpath*:/META-INF/spring-context.xml,classpath*:/spring-context-servlet-security-test.xml";
+        servlet = new RepositoryServlet();
+
+        servlet.init( mockServletConfig );
     }
 
     protected ManagedRepositoryConfiguration createManagedRepository( String id, String name, File location )
@@ -229,32 +207,12 @@ public class RepositoryServletSecurityTest
             FileUtils.deleteDirectory( repoRootInternal );
         }
 
-        servlet = null;
-
-        if ( this.tomcat != null )
-        {
-            this.tomcat.stop();
-        }
+        applicationContext.getBean( MavenIndexerCleaner.class ).cleanupIndex();
 
         super.tearDown();
     }
 
-    protected Servlet findServlet( String name )
-        throws Exception
-    {
-        Container[] childs = context.findChildren();
-        for ( Container container : childs )
-        {
-            if ( StringUtils.equals( container.getName(), name ) )
-            {
-                Tomcat.ExistingStandardWrapper esw = Tomcat.ExistingStandardWrapper.class.cast( container );
-                Servlet servlet = esw.loadServlet();
 
-                return servlet;
-            }
-        }
-        return null;
-    }
 
     // test deploy with invalid user, and guest has no write access to repo
     // 401 must be returned
index 3f444d34131803c447723fd4b312bdb0f8565971..691bfacf784c98d01596cf6439429124825f8df3 100644 (file)
@@ -25,16 +25,11 @@ import org.apache.archiva.admin.model.beans.ManagedRepository;
 import org.apache.archiva.configuration.ArchivaConfiguration;
 import org.apache.archiva.configuration.Configuration;
 import org.apache.archiva.configuration.ManagedRepositoryConfiguration;
-import org.apache.catalina.Container;
-import org.apache.catalina.startup.Tomcat;
-import org.apache.commons.lang.StringUtils;
 import org.fest.assertions.api.Assertions;
 import org.junit.Before;
 import org.junit.Test;
 
-import javax.servlet.Servlet;
 import java.io.File;
-import java.util.Enumeration;
 
 /**
  * RepositoryServletTest