From a6cfff85738e339d35c8cf7e0dd4f36aa54cfdb5 Mon Sep 17 00:00:00 2001 From: Jan Bartel Date: Fri, 10 Jun 2016 11:00:50 +1000 Subject: [PATCH] Issue #632 fix jmx tests to use random jetty server port --- tests/test-jmx/jmx-webapp-it/pom.xml | 87 ++--------------- .../org/eclipse/jetty/test/jmx/JmxIT.java | 95 ++++++++++++++++--- .../org/eclipse/jetty/test/jmx/PingIT.java | 41 -------- 3 files changed, 88 insertions(+), 135 deletions(-) delete mode 100644 tests/test-jmx/jmx-webapp-it/src/test/java/org/eclipse/jetty/test/jmx/PingIT.java diff --git a/tests/test-jmx/jmx-webapp-it/pom.xml b/tests/test-jmx/jmx-webapp-it/pom.xml index 9d692117c7f..6664661e205 100644 --- a/tests/test-jmx/jmx-webapp-it/pom.xml +++ b/tests/test-jmx/jmx-webapp-it/pom.xml @@ -38,10 +38,13 @@ org.eclipse.jetty - jetty-distribution + jetty-annotations + ${project.version} + + + org.eclipse.jetty + jetty-jmx ${project.version} - zip - runtime org.eclipse.jetty.tests @@ -78,22 +81,6 @@ ${test-base-dir}/webapps - - unpack-jetty-distro - process-test-resources - - unpack-dependencies - - - jetty-distribution - runtime - zip - true - ${test-home-dir} - true - true - - @@ -109,68 +96,6 @@ - - org.apache.maven.plugins - maven-antrun-plugin - 1.7 - - - start-jetty - pre-integration-test - - run - - - - - - Integration Test : Setup Jetty - - - - - - - - - Integration Test : Starting Jetty ... - - - - - - - - - - - Integration Test : Jetty is now available - - - - - stop-jetty - post-integration-test - - run - - - - - - Integration Test : Stop Jetty - - - - - - - - - - - - diff --git a/tests/test-jmx/jmx-webapp-it/src/test/java/org/eclipse/jetty/test/jmx/JmxIT.java b/tests/test-jmx/jmx-webapp-it/src/test/java/org/eclipse/jetty/test/jmx/JmxIT.java index 56bec0f9124..00fa2c775d4 100644 --- a/tests/test-jmx/jmx-webapp-it/src/test/java/org/eclipse/jetty/test/jmx/JmxIT.java +++ b/tests/test-jmx/jmx-webapp-it/src/test/java/org/eclipse/jetty/test/jmx/JmxIT.java @@ -24,7 +24,9 @@ import static org.hamcrest.Matchers.notNullValue; import static org.hamcrest.Matchers.startsWith; import static org.junit.Assert.assertThat; -import java.io.IOException; +import java.io.File; +import java.lang.management.ManagementFactory; +import java.net.URI; import javax.management.MBeanServerConnection; import javax.management.ObjectName; @@ -32,6 +34,14 @@ import javax.management.remote.JMXConnector; import javax.management.remote.JMXConnectorFactory; import javax.management.remote.JMXServiceURL; +import org.eclipse.jetty.server.Server; +import org.eclipse.jetty.toolchain.test.MavenTestingUtils; +import org.eclipse.jetty.toolchain.test.SimpleRequest; +import org.eclipse.jetty.webapp.Configuration; +import org.eclipse.jetty.webapp.WebAppContext; +import org.eclipse.jetty.jmx.ConnectorServer; +import org.eclipse.jetty.jmx.MBeanContainer; +import org.eclipse.jetty.server.NetworkConnector; import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.Test; @@ -41,26 +51,78 @@ import org.junit.Test; */ public class JmxIT { - private static JMXConnector jmxc; - private static MBeanServerConnection mbsc; + private static JMXConnector __jmxc; + private static MBeanServerConnection __mbsc; + private static Server __server; + private static int __port; @BeforeClass - public static void connectToMBeanServer() throws IOException + public static void connectToMBeanServer() throws Exception { + startJetty(); JMXServiceURL url = new JMXServiceURL("service:jmx:rmi://localhost:1099/jndi/rmi://localhost:1099/jmxrmi"); - jmxc = JMXConnectorFactory.connect(url,null); - mbsc = jmxc.getMBeanServerConnection(); + __jmxc = JMXConnectorFactory.connect(url,null); + __mbsc = __jmxc.getMBeanServerConnection(); } @AfterClass - public static void disconnectFromMBeanServer() throws IOException + public static void disconnectFromMBeanServer() throws Exception { - jmxc.close(); + stopJetty(); + __jmxc.close(); + } + + public static void startJetty() throws Exception + { + File target = MavenTestingUtils.getTargetDir(); + File jettyBase = new File (target, "test-base"); + File webapps = new File (jettyBase, "webapps"); + File war = new File (webapps, "jmx-webapp.war"); + + //create server instance + __server = new Server(0); + + //set up the webapp + WebAppContext context = new WebAppContext(); + + context.setWar(war.getCanonicalPath()); + context.setContextPath("/jmx-webapp"); + + Configuration.ClassList classlist = Configuration.ClassList + .setServerDefault(__server); + classlist.addBefore("org.eclipse.jetty.webapp.JettyWebXmlConfiguration", + "org.eclipse.jetty.annotations.AnnotationConfiguration"); + + context.setAttribute( + "org.eclipse.jetty.server.webapp.ContainerIncludeJarPattern", + ".*/javax.servlet-[^/]*\\.jar$|.*/servlet-api-[^/]*\\.jar$"); + __server.setHandler(context); + + //set up jmx remote + MBeanContainer mbContainer = new MBeanContainer(ManagementFactory.getPlatformMBeanServer()); + __server.addBean(mbContainer); + + JMXServiceURL serviceUrl = new JMXServiceURL("rmi", "localhost", 1099, "/jndi/rmi://localhost:1099/jmxrmi"); + ConnectorServer jmxConnServer = new ConnectorServer(serviceUrl, "org.eclipse.jetty.jmx:name=rmiconnectorserver"); + __server.addBean(jmxConnServer); + + //start server + __server.start(); + + //remember chosen port + __port = ((NetworkConnector)__server.getConnectors()[0]).getLocalPort(); + } + + + public static void stopJetty () throws Exception + { + if (__server != null) + __server.stop(); } private String getStringAttribute(ObjectName objName, String attrName) throws Exception { - Object val = mbsc.getAttribute(objName,attrName); + Object val = __mbsc.getAttribute(objName,attrName); assertThat(attrName,val,notNullValue()); assertThat(attrName,val,instanceOf(String.class)); return (String)val; @@ -68,18 +130,25 @@ public class JmxIT private int getIntegerAttribute(ObjectName objName, String attrName) throws Exception { - Object val = mbsc.getAttribute(objName,attrName); + Object val = __mbsc.getAttribute(objName,attrName); assertThat(attrName,val,notNullValue()); assertThat(attrName,val,instanceOf(Integer.class)); return (Integer)val; } + @Test + public void testBasic() throws Exception + { + URI serverURI = new URI("http://localhost:"+String.valueOf(__port)+"/jmx-webapp/"); + SimpleRequest req = new SimpleRequest(serverURI); + assertThat(req.getString("ping"),startsWith("Servlet Pong at ")); + } + @Test public void testObtainRunningServerVersion() throws Exception { ObjectName serverName = new ObjectName("org.eclipse.jetty.server:type=server,id=0"); String version = getStringAttribute(serverName,"version"); - System.err.println("Running version: " + version); assertThat("Version",version,startsWith("9.3.")); } @@ -117,7 +186,7 @@ public class JmxIT // Get initial count int count = getIntegerAttribute(pingerName,"count"); // Operations - Object val = mbsc.invoke(pingerName,"ping",null,null); + Object val = __mbsc.invoke(pingerName,"ping",null,null); assertThat("ping() return",val.toString(),startsWith("Pong")); // Attributes assertThat("count",getIntegerAttribute(pingerName,"count"),is(count+1)); @@ -134,7 +203,7 @@ public class JmxIT // Get initial count int count = getIntegerAttribute(echoerName,"count"); // Operations - Object val = mbsc.invoke(echoerName,"echo",new Object[]{"Its Me"},new String[]{String.class.getName()}); + Object val = __mbsc.invoke(echoerName,"echo",new Object[]{"Its Me"},new String[]{String.class.getName()}); assertThat("echo() return",val.toString(),is("Its Me")); // Attributes assertThat("count",getIntegerAttribute(echoerName,"count"),is(count+1)); diff --git a/tests/test-jmx/jmx-webapp-it/src/test/java/org/eclipse/jetty/test/jmx/PingIT.java b/tests/test-jmx/jmx-webapp-it/src/test/java/org/eclipse/jetty/test/jmx/PingIT.java deleted file mode 100644 index fb104f967ae..00000000000 --- a/tests/test-jmx/jmx-webapp-it/src/test/java/org/eclipse/jetty/test/jmx/PingIT.java +++ /dev/null @@ -1,41 +0,0 @@ -// -// ======================================================================== -// Copyright (c) 1995-2016 Mort Bay Consulting Pty. Ltd. -// ------------------------------------------------------------------------ -// All rights reserved. This program and the accompanying materials -// are made available under the terms of the Eclipse Public License v1.0 -// and Apache License v2.0 which accompanies this distribution. -// -// The Eclipse Public License is available at -// http://www.eclipse.org/legal/epl-v10.html -// -// The Apache License v2.0 is available at -// http://www.opensource.org/licenses/apache2.0.php -// -// You may elect to redistribute this code under either of these licenses. -// ======================================================================== -// - -package org.eclipse.jetty.test.jmx; - -import static org.hamcrest.Matchers.startsWith; -import static org.junit.Assert.assertThat; - -import java.net.URI; - -import org.eclipse.jetty.toolchain.test.SimpleRequest; -import org.junit.Test; - -/** - * Basic tests for a simple Servlet with no JMX involved (yet) - */ -public class PingIT -{ - @Test - public void testBasic() throws Exception - { - URI serverURI = new URI("http://localhost:58080/jmx-webapp/"); - SimpleRequest req = new SimpleRequest(serverURI); - assertThat(req.getString("ping"),startsWith("Servlet Pong at ")); - } -}