You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@archiva.apache.org by ol...@apache.org on 2022/04/18 00:18:56 UTC

[archiva-redback-core] branch master updated: use jetty bom and simplify code (#38)

This is an automated email from the ASF dual-hosted git repository.

olamy pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/archiva-redback-core.git


The following commit(s) were added to refs/heads/master by this push:
     new 08e42ad3 use jetty bom and simplify code (#38)
08e42ad3 is described below

commit 08e42ad3e716616608dc8939aa6f0506994cdef0
Author: Olivier Lamy <ol...@apache.org>
AuthorDate: Mon Apr 18 10:18:52 2022 +1000

    use jetty bom and simplify code (#38)
    
    Signed-off-by: Olivier Lamy <ol...@apache.org>
---
 pom.xml                                            | 11 +++++-
 .../redback-rest/redback-rest-services/pom.xml     |  4 +-
 .../rest/services/AbstractRestServicesTest.java    | 44 +++++-----------------
 .../redback/rest/services/LoginServiceTest.java    |  5 +--
 .../redback/rest/services/UserServiceTest.java     |  2 +-
 .../src/test/resources/log4j2-test.xml             |  2 -
 .../redback/users/ldap/LdapUserManagerTest.java    | 20 +---------
 7 files changed, 26 insertions(+), 62 deletions(-)

diff --git a/pom.xml b/pom.xml
index 381146e8..38c85461 100644
--- a/pom.xml
+++ b/pom.xml
@@ -94,6 +94,7 @@
 
 
     <jjwt.version>0.11.2</jjwt.version>
+    <surefire.redirectTestOutputToFile>true</surefire.redirectTestOutputToFile>
 
   </properties>
 
@@ -428,6 +429,14 @@
         <scope>test</scope>
       </dependency>
 
+      <dependency>
+        <groupId>org.eclipse.jetty</groupId>
+        <artifactId>jetty-bom</artifactId>
+        <version>${jetty.version}</version>
+        <type>pom</type>
+        <scope>import</scope>
+      </dependency>
+
       <dependency>
         <groupId>org.mockito</groupId>
         <artifactId>mockito-core</artifactId>
@@ -966,7 +975,7 @@
         <configuration>
           <argLine>-Xmx256m -Xms256m</argLine>
           <runOrder>alphabetical</runOrder>
-          <redirectTestOutputToFile>true</redirectTestOutputToFile>
+          <redirectTestOutputToFile>${surefire.redirectTestOutputToFile}</redirectTestOutputToFile>
         </configuration>
       </plugin>
       <plugin>
diff --git a/redback-integrations/redback-rest/redback-rest-services/pom.xml b/redback-integrations/redback-rest/redback-rest-services/pom.xml
index 0bd1ace6..099fda7d 100644
--- a/redback-integrations/redback-rest/redback-rest-services/pom.xml
+++ b/redback-integrations/redback-rest/redback-rest-services/pom.xml
@@ -284,18 +284,16 @@
     <dependency>
       <groupId>org.eclipse.jetty</groupId>
       <artifactId>jetty-server</artifactId>
-      <version>${jetty.version}</version>
       <exclusions>
         <exclusion>
           <groupId>jakarta.servlet</groupId>
-      <artifactId>jakarta.servlet-api</artifactId>
+          <artifactId>jakarta.servlet-api</artifactId>
         </exclusion>
       </exclusions>
     </dependency>
     <dependency>
       <groupId>org.eclipse.jetty</groupId>
       <artifactId>jetty-servlet</artifactId>
-      <version>${jetty.version}</version>
     </dependency>
 
     <dependency>
diff --git a/redback-integrations/redback-rest/redback-rest-services/src/test/java/org/apache/archiva/redback/rest/services/AbstractRestServicesTest.java b/redback-integrations/redback-rest/redback-rest-services/src/test/java/org/apache/archiva/redback/rest/services/AbstractRestServicesTest.java
index 839f8e79..828052c7 100644
--- a/redback-integrations/redback-rest/redback-rest-services/src/test/java/org/apache/archiva/redback/rest/services/AbstractRestServicesTest.java
+++ b/redback-integrations/redback-rest/redback-rest-services/src/test/java/org/apache/archiva/redback/rest/services/AbstractRestServicesTest.java
@@ -30,13 +30,11 @@ import org.apache.archiva.redback.rest.api.services.LoginService;
 import org.apache.archiva.redback.rest.api.services.RoleManagementService;
 import org.apache.archiva.redback.rest.api.services.UserService;
 import org.apache.archiva.redback.rest.api.services.v2.AuthenticationService;
-import org.apache.commons.lang3.StringUtils;
 import org.apache.commons.lang3.SystemUtils;
 import org.apache.cxf.common.util.Base64Utility;
 import org.apache.cxf.jaxrs.client.JAXRSClientFactory;
 import org.apache.cxf.jaxrs.client.WebClient;
 import org.apache.cxf.transport.servlet.CXFServlet;
-import org.eclipse.jetty.server.HttpConnectionFactory;
 import org.eclipse.jetty.server.Server;
 import org.eclipse.jetty.server.ServerConnector;
 import org.eclipse.jetty.server.session.SessionHandler;
@@ -44,27 +42,22 @@ import org.eclipse.jetty.servlet.ServletContextHandler;
 import org.eclipse.jetty.servlet.ServletHolder;
 import org.junit.After;
 import org.junit.Before;
-import org.junit.runner.RunWith;
-import org.junit.runners.JUnit4;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.web.context.ContextLoaderListener;
 
 import javax.ws.rs.core.MediaType;
 import java.util.Collections;
-import java.util.concurrent.atomic.AtomicReference;
 
 /**
  * @author Olivier Lamy
  */
-@RunWith(JUnit4.class)
 public abstract class AbstractRestServicesTest
     extends TestCase
 {
     protected Logger log = LoggerFactory.getLogger( getClass() );
 
-    private static AtomicReference<Server> server = new AtomicReference<>();
-    private static AtomicReference<ServerConnector> serverConnector = new AtomicReference<>();
+    protected Server server;
 
     public String authorizationHeader = getAdminAuthzHeader();
 
@@ -73,16 +66,14 @@ public abstract class AbstractRestServicesTest
      * @return
      */
     public Server getServer() {
-        return this.server.get();
+        return server;
     }
 
     public int getServerPort() {
-        ServerConnector connector = serverConnector.get();
-        if (connector!=null) {
-            return connector.getLocalPort();
-        } else {
-            return 0;
+        if (this.server == null || !this.server.isRunning()) {
+            throw new IllegalStateException("Server has not been started");
         }
+        return ((ServerConnector) server.getConnectors()[0]).getLocalPort();
     }
 
     JacksonJaxbJsonProvider getJsonProvider() {
@@ -93,14 +84,6 @@ public abstract class AbstractRestServicesTest
         return provider;
     }
 
-    /**
-     * Returns true, if the server does exist and is running.
-     * @return true, if server does exist and is running.
-     */
-    public boolean isServerRunning() {
-        return this.server.get() != null && this.server.get().isRunning();
-    }
-
     /**
      * Returns the timeout in ms for rest requests. The timeout can be set by
      * the system property <code>rest.test.timeout</code>.
@@ -137,21 +120,18 @@ public abstract class AbstractRestServicesTest
         throws Exception
     {
         log.info("Starting server");
-        Server myServer = new Server();
-        this.server.set(myServer);
-        this.serverConnector.set(new ServerConnector( myServer, new HttpConnectionFactory()));
-        myServer.addConnector(serverConnector.get());
+        this.server = new Server(0);
 
         ServletHolder servletHolder = new ServletHolder( new CXFServlet() );
         ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
         context.setResourceBase( SystemUtils.JAVA_IO_TMPDIR );
-        context.setSessionHandler( new SessionHandler(  ) );
+        context.setSessionHandler( new SessionHandler() );
         context.addServlet( servletHolder, "/" + getRestServicesPath() + "/*" );
         context.setInitParameter( "contextConfigLocation", getSpringConfigLocation() );
         context.addEventListener(new ContextLoaderListener());
 
-        getServer().setHandler( context );
-        getServer().start();
+        this.server.setHandler( context );
+        this.server.start();
 
         if (log.isDebugEnabled())
         {
@@ -187,11 +167,7 @@ public abstract class AbstractRestServicesTest
     public void stopServer()
         throws Exception
     {
-        if ( getServer() != null )
-        {
-            log.info("Stopping server");
-            getServer().stop();
-        }
+        this.server.stop();
     }
 
     protected UserService getUserService()
diff --git a/redback-integrations/redback-rest/redback-rest-services/src/test/java/org/apache/archiva/redback/rest/services/LoginServiceTest.java b/redback-integrations/redback-rest/redback-rest-services/src/test/java/org/apache/archiva/redback/rest/services/LoginServiceTest.java
index e13ead5b..dc87584e 100644
--- a/redback-integrations/redback-rest/redback-rest-services/src/test/java/org/apache/archiva/redback/rest/services/LoginServiceTest.java
+++ b/redback-integrations/redback-rest/redback-rest-services/src/test/java/org/apache/archiva/redback/rest/services/LoginServiceTest.java
@@ -30,9 +30,8 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
 /**
  * @author Olivier Lamy
  */
-@RunWith( SpringJUnit4ClassRunner.class )
-@ContextConfiguration(
-        locations = { "classpath:/spring-context.xml" } )
+@RunWith(SpringJUnit4ClassRunner.class )
+@ContextConfiguration(locations = { "classpath:/spring-context.xml" } )
 public class LoginServiceTest
     extends AbstractRestServicesTest
 {
diff --git a/redback-integrations/redback-rest/redback-rest-services/src/test/java/org/apache/archiva/redback/rest/services/UserServiceTest.java b/redback-integrations/redback-rest/redback-rest-services/src/test/java/org/apache/archiva/redback/rest/services/UserServiceTest.java
index 2e262ea2..3d95a7e5 100644
--- a/redback-integrations/redback-rest/redback-rest-services/src/test/java/org/apache/archiva/redback/rest/services/UserServiceTest.java
+++ b/redback-integrations/redback-rest/redback-rest-services/src/test/java/org/apache/archiva/redback/rest/services/UserServiceTest.java
@@ -45,7 +45,7 @@ import java.util.List;
 /**
  * @author Olivier Lamy
  */
-@RunWith( SpringJUnit4ClassRunner.class )
+@RunWith(SpringJUnit4ClassRunner.class )
 @ContextConfiguration(
         locations = { "classpath:/spring-context.xml" } )
 public class UserServiceTest
diff --git a/redback-integrations/redback-rest/redback-rest-services/src/test/resources/log4j2-test.xml b/redback-integrations/redback-rest/redback-rest-services/src/test/resources/log4j2-test.xml
index e058bdb7..9e512c6c 100644
--- a/redback-integrations/redback-rest/redback-rest-services/src/test/resources/log4j2-test.xml
+++ b/redback-integrations/redback-rest/redback-rest-services/src/test/resources/log4j2-test.xml
@@ -32,8 +32,6 @@
     <logger name="org.apache.archiva.redback.components.cache" level="error"/>
     <logger name="org.apache.archiva.redback.rest.services.interceptors" level="debug"/>
     <logger name="org.apache.archiva.redback.rest.services" level="debug"/>
-    <logger name="org.apache.catalina" level="off" />
-    <logger name="JPOX" level="ERROR"/>
     <root level="info">
       <appender-ref ref="console"/>
     </root>
diff --git a/redback-users/redback-users-providers/redback-users-ldap/src/test/java/org/apache/archiva/redback/users/ldap/LdapUserManagerTest.java b/redback-users/redback-users-providers/redback-users-ldap/src/test/java/org/apache/archiva/redback/users/ldap/LdapUserManagerTest.java
index ca061189..25a62c1f 100644
--- a/redback-users/redback-users-providers/redback-users-ldap/src/test/java/org/apache/archiva/redback/users/ldap/LdapUserManagerTest.java
+++ b/redback-users/redback-users-providers/redback-users-ldap/src/test/java/org/apache/archiva/redback/users/ldap/LdapUserManagerTest.java
@@ -147,44 +147,28 @@ public class LdapUserManagerTest
     {
         assertNotNull( connectionFactory );
 
-        LdapConnection connection = null;
-
-        try
+        try (LdapConnection connection = connectionFactory.getConnection())
         {
-            connection = connectionFactory.getConnection();
-
             assertNotNull( connection );
 
             DirContext context = connection.getDirContext();
 
             assertNotNull( context );
         }
-        finally
-        {
-            connection.close();
-        }
     }
 
     @Test
     public void testDirectUsersExistence()
         throws Exception
     {
-        LdapConnection connection = null;
-
-        try
+        try (LdapConnection connection = connectionFactory.getConnection())
         {
-            connection = connectionFactory.getConnection();
 
             DirContext context = connection.getDirContext();
 
             assertExist( context, createDn( "jesse" ), "cn", "jesse" );
             assertExist( context, createDn( "joakim" ), "cn", "joakim" );
         }
-        finally
-        {
-            connection.close();
-        }
-
     }
 
     @Test