use jetty bom and simplify code (#38)

Signed-off-by: Olivier Lamy <olamy@apache.org>
This commit is contained in:
Olivier Lamy 2022-04-18 10:18:52 +10:00 committed by GitHub
parent 04d4667aef
commit 08e42ad3e7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 26 additions and 62 deletions

11
pom.xml
View File

@ -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>

View File

@ -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>

View File

@ -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()

View File

@ -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
{

View File

@ -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

View File

@ -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>

View File

@ -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