Skip to content
Browse files

Many changes.

Various improvements learned from usage of lib.
  • Loading branch information...
1 parent 1543031 commit fe736ec4b810a20cd0cffa6545c8bda734b18578 @swilliams-pivotal swilliams-pivotal committed Sep 1, 2012
View
3 build.gradle
@@ -14,8 +14,9 @@
* limitations under the License.
*/
-apply from: 'gradle/setup.gradle'
apply plugin: 'java'
+apply from: 'gradle/setup.gradle'
+apply from: 'gradle/maven.gradle'
defaultTasks = ['assemble']
View
73 src/main/java/org/vertx/java/test/junit/Deployer.java
@@ -26,6 +26,7 @@
import java.util.Set;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicInteger;
import org.junit.runner.Description;
import org.vertx.java.core.json.JsonObject;
@@ -42,25 +43,25 @@
*/
public class Deployer {
+ private final AtomicInteger deploymentCounter = new AtomicInteger(0);
+
private final File modDir;
-
+
private final VerticleManager manager;
- Deployer(VerticleManager manager, File modDir) {
+ private long shutdownTimeout;
+
+ Deployer(VerticleManager manager, File modDir, long shutdownTimeout) {
this.manager = manager;
this.modDir = modDir;
+ this.shutdownTimeout = shutdownTimeout;
}
public void deploy(Description description) {
- final TestModules amodules = description.getAnnotation(TestModules.class);
- final TestModule amodule = description.getAnnotation(TestModule.class);
- final TestVerticles verticles = description.getAnnotation(TestVerticles.class);
- final TestVerticle verticle = description.getAnnotation(TestVerticle.class);
-
- deployModules(amodules);
- deployModule(amodule);
- deployVerticles(verticles);
- deployVerticle(verticle);
+ deployModules(description.getAnnotation(TestModules.class));
+ deployModule(description.getAnnotation(TestModule.class));
+ deployVerticles(description.getAnnotation(TestVerticles.class));
+ deployVerticle(description.getAnnotation(TestVerticle.class));
}
@@ -76,6 +77,7 @@ private void deployVerticles(TestVerticles verticles) {
URL[] urls = findVerticleURLs(v);
manager.deployVerticle(v.worker(), v.main(), config, urls, v.instances(), modDir, new CountDownLatchDoneHandler<String>(latch));
+ deploymentCounter.incrementAndGet();
}
await(latch);
@@ -89,7 +91,9 @@ private void deployVerticle(TestVerticle v) {
final CountDownLatch latch = new CountDownLatch(1);
JsonObject config = getJsonConfig(v.jsonConfig());
URL[] urls = findVerticleURLs(v);
+
manager.deployVerticle(v.worker(), v.main(), config, urls, v.instances(), modDir, new CountDownLatchDoneHandler<String>(latch));
+ deploymentCounter.incrementAndGet();
await(latch);
}
@@ -104,6 +108,7 @@ private void deployModules(TestModules amodules) {
for (TestModule m : amodules.value()) {
JsonObject config = getJsonConfig(m.jsonConfig());
manager.deployMod(m.name(), config, m.instances(), modDir, new CountDownLatchDoneHandler<String>(latch));
+ deploymentCounter.incrementAndGet();
}
await(latch);
@@ -118,6 +123,7 @@ private void deployModule(TestModule m) {
JsonObject config = getJsonConfig(m.jsonConfig());
manager.deployMod(m.name(), config, m.instances(), modDir, new CountDownLatchDoneHandler<String>(latch));
+ deploymentCounter.incrementAndGet();
await(latch);
}
@@ -127,24 +133,52 @@ private void deployModule(TestModule m) {
if (v.urls().length > 0) {
for (String path : v.urls()) {
+
try {
URL url = new File(path).toURI().toURL();
urlSet.add(url);
} catch (Exception e) {
+ // TODO log something here
e.printStackTrace();
}
}
}
try {
- // contortions to get parent
- URL url = getClass().getClassLoader().getResource(v.main());
- url = Paths.get(url.toURI()).getParent().toUri().toURL();
- urlSet.add(url);
+ String main = v.main();
+ if (main.indexOf(':') > -1) {
+ main = main.substring(main.indexOf(':') + 1);
+ }
+
+ // check for class, prep for locating root URL
+ int parts = 0;
+ if (!main.endsWith(".xml")) {
+ parts = main.split("\\.").length;
+ main = main.replaceAll("\\.", "/");
+ main = main + ".class";
+ }
+
+ // contortions to get parent, may not be entirely accurate...
+ // URL url = getClass().getClassLoader().getResource(main);
+ URL url = Thread.currentThread().getContextClassLoader().getResource(main);
+
+ if (url != null) {
+ Path path = Paths.get(url.toURI());
+
+ int i = parts;
+ while (i > 0) {
+ path = path.getParent();
+ i--;
+ }
+
+ url = path.toUri().toURL();
+ urlSet.add(url);
+ }
} catch (Exception e) {
+ // TODO log something here
e.printStackTrace();
}
@@ -177,11 +211,16 @@ private JsonObject getJsonConfig(String jsonConfig) {
private void await(final CountDownLatch latch) {
try {
- latch.await(30L, TimeUnit.SECONDS);
+ latch.await(shutdownTimeout, TimeUnit.SECONDS);
} catch (InterruptedException e) {
- // TODO Auto-generated catch block
+ // TODO log something here
e.printStackTrace();
}
}
+
+ public int getDeploymentCount() {
+ return deploymentCounter.get();
+ }
+
}
View
11 src/main/java/org/vertx/java/test/junit/UnexpectedTestRunnerException.java
@@ -0,0 +1,11 @@
+package org.vertx.java.test.junit;
+
+public class UnexpectedTestRunnerException extends RuntimeException {
+
+ private static final long serialVersionUID = 1L;
+
+ public UnexpectedTestRunnerException(Exception e) {
+ super(e);
+ }
+
+}
View
78 src/main/java/org/vertx/java/test/junit/VertxConfigurableJUnit4Runner.java
@@ -16,6 +16,7 @@
package org.vertx.java.test.junit;
import java.io.File;
+import java.lang.reflect.Method;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
@@ -26,8 +27,11 @@
import org.junit.runners.BlockJUnit4ClassRunner;
import org.junit.runners.model.InitializationError;
+import org.vertx.java.core.Vertx;
import org.vertx.java.core.impl.DefaultVertx;
import org.vertx.java.core.impl.VertxInternal;
+import org.vertx.java.deploy.Container;
+import org.vertx.java.deploy.Verticle;
import org.vertx.java.deploy.impl.VerticleManager;
import org.vertx.java.test.junit.annotations.VertxConfig;
import org.vertx.java.test.junit.support.VerticleManagerSupport;
@@ -48,18 +52,30 @@
private Deployer deployer;
+ private Verticle verticle;
+
+ /**
+ * @param klass
+ * @throws InitializationError
+ */
public VertxConfigurableJUnit4Runner(Class<?> klass) throws InitializationError {
super(klass);
}
+ /**
+ * {@inheritDoc}
+ */
@Override
public void run(RunNotifier notifier) {
+ long timeout;
VertxConfig vertxConfig = getDescription().getAnnotation(VertxConfig.class);
if (vertxConfig != null) {
+ timeout = vertxConfig.shutdownTimeoutSeconds();
this.vertx = new DefaultVertx(vertxConfig.port(), vertxConfig.hostname());
}
else {
+ timeout = 30L;
this.vertx = new DefaultVertx();
}
@@ -68,10 +84,10 @@ public void run(RunNotifier notifier) {
String vertxMods = System.getProperty("vertx.mods");
File currentModDir = new File(vertxMods);
if (!currentModDir.exists()) {
- throw new RuntimeException("vert.mods dir doesn't exist! " + vertxMods);
+ currentModDir.mkdirs();
}
- this.deployer = new Deployer(verticleManager, currentModDir);
+ this.deployer = new Deployer(verticleManager, currentModDir, timeout);
deployer.deploy(getDescription());
super.run(notifier);
@@ -80,11 +96,14 @@ public void run(RunNotifier notifier) {
}
+ /**
+ * {@inheritDoc}
+ */
@Override
protected List<TestRule> getTestRules(Object target) {
-
+
if (!classCache.contains(target.toString())) {
- configureTarget(target);
+ configureTargetTestClass(target);
classCache.add(target.toString());
}
@@ -93,29 +112,74 @@ public void run(RunNotifier notifier) {
return rules;
}
- private void configureTarget(Object target) {
+ /**
+ * @param target
+ */
+ private void configureTargetTestClass(Object target) {
+
+ if (target instanceof Verticle) {
+ // TODO do something clever here...
+ System.out.printf("Test class is a Verticle: %s %n", target.getClass().getName());
+ this.verticle = (Verticle) target;
+ verticle.setVertx(vertx);
+ verticle.setContainer(new Container(verticleManager));
+ try {
+ verticle.start();
+ } catch (Exception e) {
+ throw new UnexpectedTestRunnerException(e);
+ }
+ }
+ // discover VertxSupport
if (target instanceof VertxSupport) {
VertxSupport support = (VertxSupport) target;
support.setVertx(vertx);
}
+ // discover VerticleManager support
if (target instanceof VerticleManagerSupport) {
VerticleManagerSupport support = (VerticleManagerSupport) target;
support.setManager(verticleManager);
}
+ // discover setVertx method
+ try {
+ Method setVertxMethod = target.getClass().getMethod("setVertx", Vertx.class);
+ setVertxMethod.invoke(target, vertx);
+ } catch (Exception e) {
+ // we can ignore this if the method isn't present
+ }
+
+ // discover setVerticleManager method
+ try {
+ Method setVerticleManagerMethod = target.getClass().getMethod("setVerticleManager", VerticleManager.class);
+ setVerticleManagerMethod.invoke(target, verticleManager);
+ } catch (Exception e) {
+ // we can ignore this if the method isn't present
+ }
+
}
+ /**
+ *
+ */
private void undeployAll() {
+
CountDownLatch latch = new CountDownLatch(1);
verticleManager.undeployAll(new CountDownLatchDoneHandler<Void>(latch));
try {
latch.await();
} catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
+ //
+ }
+
+ if (verticle != null) {
+ try {
+ verticle.stop();
+ } catch (Exception e) {
+ throw new UnexpectedTestRunnerException(e);
+ }
}
}
View
1 src/main/java/org/vertx/java/test/junit/VertxRule.java
@@ -42,7 +42,6 @@ public void evaluate() throws Throwable {
deployer.deploy(description);
base.evaluate();
}};
-
}
}
View
4 src/main/java/org/vertx/java/test/junit/annotations/VertxConfig.java
@@ -35,6 +35,8 @@
String hostname() default "localhost";
- int port();
+ int port() default -1;
+
+ long shutdownTimeoutSeconds() default 30L;
}
View
18 ...rg/vertx/java/test/QueueReplyHandler.java → ...test/junit/support/QueueReplyHandler.java
@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-package org.vertx.java.test;
+package org.vertx.java.test.junit.support;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;
@@ -30,18 +30,32 @@
public class QueueReplyHandler<T> implements Handler<Message<T>> {
private final LinkedBlockingQueue<T> queue;
+
private final long timeout;
+ private final TimeUnit timeUnit;
+
+ public QueueReplyHandler(LinkedBlockingQueue<T> queue) {
+ this(queue, 5000L);
+ }
+
public QueueReplyHandler(LinkedBlockingQueue<T> queue, long timeout) {
+ this(queue, timeout, TimeUnit.MILLISECONDS);
+ }
+
+ public QueueReplyHandler(LinkedBlockingQueue<T> queue, long timeout, TimeUnit timeUnit) {
this.queue = queue;
this.timeout = timeout;
+ this.timeUnit = timeUnit;
}
@Override
public void handle(Message<T> event) {
try {
- queue.offer(event.body, timeout, TimeUnit.SECONDS);
+ if (event != null && event.body != null) {
+ queue.offer(event.body, timeout, timeUnit);
+ }
} catch (InterruptedException e) {
Assert.fail(e.getMessage());
View
28 src/main/java/org/vertx/java/test/junit/support/SimpleLatchAsyncResultHandler.java
@@ -0,0 +1,28 @@
+package org.vertx.java.test.junit.support;
+
+import java.util.concurrent.CountDownLatch;
+
+import org.vertx.java.core.AsyncResult;
+import org.vertx.java.core.AsyncResultHandler;
+
+public class SimpleLatchAsyncResultHandler implements AsyncResultHandler<Void> {
+
+ private final CountDownLatch latch;
+
+ private volatile boolean succeeded;
+
+ public SimpleLatchAsyncResultHandler(CountDownLatch latch) {
+ this.latch = latch;
+ }
+
+ @Override
+ public void handle(AsyncResult<Void> event) {
+ this.succeeded = event.succeeded();
+ latch.countDown();
+ }
+
+ public boolean isSucceeded() {
+ return succeeded;
+ }
+
+}
View
2 src/main/java/org/vertx/java/test/junit/support/VerticleManagerSupport.java
@@ -24,8 +24,6 @@
*/
public interface VerticleManagerSupport {
- VerticleManager getManager();
-
void setManager(VerticleManager manager);
}
View
2 src/main/java/org/vertx/java/test/junit/support/VertxSupport.java
@@ -26,6 +26,4 @@
void setVertx(Vertx vertx);
- Vertx getVertx();
-
}
View
103 src/main/java/org/vertx/java/test/junit/support/VertxTestBase.java
@@ -15,34 +15,127 @@
*/
package org.vertx.java.test.junit.support;
+import java.util.Arrays;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.LinkedBlockingQueue;
+import java.util.concurrent.TimeUnit;
+
+import org.junit.Assert;
+import org.vertx.java.core.Handler;
import org.vertx.java.core.Vertx;
+import org.vertx.java.core.eventbus.EventBus;
+import org.vertx.java.core.eventbus.Message;
import org.vertx.java.deploy.impl.VerticleManager;
/**
* @author swilliams
*
*/
-public class VertxTestBase implements VertxSupport, VerticleManagerSupport {
+public abstract class VertxTestBase implements VertxSupport, VerticleManagerSupport {
private Vertx vertx;
private VerticleManager manager;
+ private long awaitTimeout = 5000L;
+
+ protected VertxTestBase() {
+ super();
+ }
+
+ @Override
public void setVertx(Vertx vertx) {
this.vertx = vertx;
}
- public Vertx getVertx() {
+ @Override
+ public void setManager(VerticleManager manager) {
+ this.manager = manager;
+ }
+
+ protected Vertx getVertx() {
return vertx;
}
- public VerticleManager getManager() {
+ protected EventBus getEventBus() {
+ return vertx.eventBus();
+ }
+
+ protected VerticleManager getManager() {
return manager;
}
- public void setManager(VerticleManager manager) {
- this.manager = manager;
+ protected final void setAwaitTimeout(long awaitTimeout) {
+ this.awaitTimeout = awaitTimeout;
+ }
+
+ protected final void lightSleep(long timeout) {
+ try {
+ Thread.sleep(timeout);
+ } catch (InterruptedException e) {
+ //
+ }
+ }
+
+ protected final void testMessageEcho(String address, String message) throws Exception {
+
+ final long timeout = 2000L;
+ final TimeUnit timeUnit = TimeUnit.MILLISECONDS;
+ final LinkedBlockingQueue<String> queue = new LinkedBlockingQueue<>();
+
+ getVertx().eventBus().send(address, message, new QueueReplyHandler<String>(queue, timeout, timeUnit));
+
+ try {
+ String answer = queue.poll(timeout, timeUnit);
+ System.out.printf("For %s Q:%s A:%s %n", address, message, message.equals(answer));
+ Assert.assertTrue(message.equals(answer));
+
+ } catch (InterruptedException e) {
+ //
+ }
+ }
+
+ protected final <T, M extends Message<T>> String registerHandler(String address, Handler<M> handler) {
+ final CountDownLatch latch = new CountDownLatch(1);
+ String handlerId = vertx.eventBus().registerHandler(address, handler, new SimpleLatchAsyncResultHandler(latch));
+ await(latch);
+ return handlerId;
+ }
+
+ protected final <T, M extends Message<T>> String registerLocalHandler(String address, Handler<M> handler) {
+ final CountDownLatch latch = new CountDownLatch(1);
+ String handlerId = vertx.eventBus().registerLocalHandler(address, handler);
+ await(latch);
+ return handlerId;
+ }
+
+ protected final void unregisterHandlers(String... handlers) {
+ unregisterHandlers(Arrays.asList(handlers));
+ }
+
+ protected final void unregisterHandlers(Iterable<String> iterable) {
+ for (String handler : iterable) {
+ System.out.printf("unregisterHandler: %s %n", handler);
+ getVertx().eventBus().unregisterHandler(handler);
+ }
+ }
+
+ protected final void await(CountDownLatch latch) {
+ await(latch, this.awaitTimeout);
+ }
+
+ protected final void await(CountDownLatch latch, long timeout) {
+ await(latch, timeout, TimeUnit.MILLISECONDS);
+ }
+
+ protected final void await(CountDownLatch latch, long timeout, TimeUnit timeUnit) {
+ try {
+ latch.await(timeout, timeUnit);
+ }
+ catch (InterruptedException e) {
+ //
+ }
}
}
View
2 src/test/java/org/vertx/java/test/junit/ModuleAnnotationTest.java
@@ -23,10 +23,10 @@
import org.junit.Test;
import org.junit.runner.RunWith;
-import org.vertx.java.test.QueueReplyHandler;
import org.vertx.java.test.junit.VertxConfigurableJUnit4Runner;
import org.vertx.java.test.junit.annotations.TestModule;
import org.vertx.java.test.junit.annotations.TestModules;
+import org.vertx.java.test.junit.support.QueueReplyHandler;
import org.vertx.java.test.junit.support.VertxTestBase;
View
2 src/test/java/org/vertx/java/test/junit/SimpleEchoTest.java
@@ -24,8 +24,8 @@
import org.junit.runner.RunWith;
import org.vertx.java.core.Handler;
import org.vertx.java.core.eventbus.Message;
-import org.vertx.java.test.QueueReplyHandler;
import org.vertx.java.test.junit.VertxConfigurableJUnit4Runner;
+import org.vertx.java.test.junit.support.QueueReplyHandler;
import org.vertx.java.test.junit.support.VertxTestBase;
View
2 src/test/java/org/vertx/java/test/junit/VerticleAnnotationTest.java
@@ -23,10 +23,10 @@
import org.junit.Test;
import org.junit.runner.RunWith;
-import org.vertx.java.test.QueueReplyHandler;
import org.vertx.java.test.junit.VertxConfigurableJUnit4Runner;
import org.vertx.java.test.junit.annotations.TestVerticle;
import org.vertx.java.test.junit.annotations.TestVerticles;
+import org.vertx.java.test.junit.support.QueueReplyHandler;
import org.vertx.java.test.junit.support.VertxTestBase;
View
2 src/test/resources/test_client.js
@@ -33,7 +33,7 @@ function testSpring() {
tu.registerTests(this);
-var config = {springConfig: 'testConfig.xml'}
+var config = {springConfig: 'applicationConfig.xml'}
var modID = vertx.deployModule('spring-v1.0', config, 1, function() {
tu.appReady();
});

0 comments on commit fe736ec

Please sign in to comment.
Something went wrong with that request. Please try again.