Skip to content
Permalink
Browse files
use jetty bom and simplify code (#38)
Signed-off-by: Olivier Lamy <olamy@apache.org>
  • Loading branch information
olamy committed Apr 18, 2022
1 parent 04d4667 commit 08e42ad3e716616608dc8939aa6f0506994cdef0
Showing 7 changed files with 26 additions and 62 deletions.
11 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>
@@ -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>
@@ -30,41 +30,34 @@
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;
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 @@
* @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 @@ JacksonJaxbJsonProvider getJsonProvider() {
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 void startServer()
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 @@ protected FakeCreateAdminService getFakeCreateAdminService()
public void stopServer()
throws Exception
{
if ( getServer() != null )
{
log.info("Stopping server");
getServer().stop();
}
this.server.stop();
}

protected UserService getUserService()
@@ -30,9 +30,8 @@
/**
* @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
{
@@ -45,7 +45,7 @@
/**
* @author Olivier Lamy
*/
@RunWith( SpringJUnit4ClassRunner.class )
@RunWith(SpringJUnit4ClassRunner.class )
@ContextConfiguration(
locations = { "classpath:/spring-context.xml" } )
public class UserServiceTest
@@ -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>
@@ -147,44 +147,28 @@ public void testConnection()
{
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

0 comments on commit 08e42ad

Please sign in to comment.