# HG changeset patch # User Severin Gehwolf # Date 1373982138 -7200 # Node ID c792f093228c3f48a96f54f18a894efb955e4709 # Parent d04860739261c41fa513e3b2fc8eb67ae4d5cd78 Synchronize WebAppTest to wait for a started context. Reviewed-by: vanaltj Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2013-July/007493.html diff -r d04860739261 -r c792f093228c integration-tests/src/test/java/com/redhat/thermostat/itest/WebAppTest.java --- a/integration-tests/src/test/java/com/redhat/thermostat/itest/WebAppTest.java Tue Jul 16 14:56:10 2013 +0200 +++ b/integration-tests/src/test/java/com/redhat/thermostat/itest/WebAppTest.java Tue Jul 16 15:42:18 2013 +0200 @@ -53,8 +53,11 @@ import java.util.HashSet; import java.util.Properties; import java.util.UUID; +import java.util.concurrent.CountDownLatch; import org.eclipse.jetty.server.Server; +import org.eclipse.jetty.util.component.LifeCycle; +import org.eclipse.jetty.util.component.LifeCycle.Listener; import org.eclipse.jetty.webapp.WebAppContext; import org.junit.After; import org.junit.AfterClass; @@ -151,11 +154,14 @@ } private static void startServer(int port) throws Exception { + final CountDownLatch contextStartedLatch = new CountDownLatch(1); server = new Server(port); ApplicationInfo appInfo = new ApplicationInfo(); String version = appInfo.getMavenVersion(); String warfile = "target/libs/thermostat-web-war-" + version + ".war"; WebAppContext ctx = new WebAppContext(warfile, "/thermostat"); + WebAppContextListener listener = new WebAppContextListener(contextStartedLatch); + ctx.addLifeCycleListener(listener); /* The web archive has a jetty-web.xml config file which sets up the * JAAS config. If done in code, this would look like this: * @@ -172,6 +178,11 @@ */ server.setHandler(ctx); server.start(); + // wait for context to start + contextStartedLatch.await(); + if (listener.failed) { + throw new IllegalStateException(listener.cause); + } } private static void connectStorage(String username, String password) { @@ -611,4 +622,42 @@ } assertEquals("Hello World", str.toString()); } + + private static class WebAppContextListener implements Listener { + + private Throwable cause; + private boolean failed = false; + private final CountDownLatch contextStartedLatch; + private WebAppContextListener(CountDownLatch latch) { + this.contextStartedLatch = latch; + } + + @Override + public void lifeCycleStarting(LifeCycle event) { + // nothing + } + + @Override + public void lifeCycleStarted(LifeCycle event) { + contextStartedLatch.countDown(); + } + + @Override + public void lifeCycleFailure(LifeCycle event, Throwable cause) { + this.failed = true; + this.cause = cause; + contextStartedLatch.countDown(); + } + + @Override + public void lifeCycleStopping(LifeCycle event) { + // nothing + } + + @Override + public void lifeCycleStopped(LifeCycle event) { + // nothing + } + + } }