Skip to content

Instantly share code, notes, and snippets.

@diega
Created June 24, 2012 15:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save diega/2983648 to your computer and use it in GitHub Desktop.
Save diega/2983648 to your computer and use it in GitHub Desktop.
Vert.x Junit4 verticles test
package org.vertx.java.junit.support;
import org.junit.rules.TestRule;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;
import org.vertx.java.core.Handler;
import org.vertx.java.core.impl.DefaultVertx;
import org.vertx.java.core.impl.VertxInternal;
import org.vertx.java.deploy.Verticle;
import org.vertx.java.deploy.impl.VerticleManager;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class RunInVertx implements TestRule {
private VerticleManager manager;
public RunInVertx() {
VertxInternal vertx = new DefaultVertx();
manager = new VerticleManager(vertx);
}
@Override
public Statement apply(final Statement base, final Description description) {
return new Statement() {
@Override
public void evaluate() throws Throwable {
Verticles verticles = description.getAnnotation(Verticles.class);
if(verticles == null) {
base.evaluate();
} else {
List<String> startedApps = deployVerticles(Arrays.asList(verticles.values()));
base.evaluate();
undeployVerticles(startedApps);
}
}
};
}
private void undeployVerticles(List<String> startedApps) {
for (final String startedApp : startedApps) {
manager.undeploy(startedApp, new Handler<Void>() {
@Override
public void handle(Void event) {
System.out.println(startedApp + " stopped");
}
});
}
}
private List<String> deployVerticles(List<Class<? extends Verticle>> verticles) {
List<String> startedApps = new ArrayList<>(verticles.size());
for (final Class<? extends Verticle> verticle : verticles) {
try {
URL url = getCompilationFolder(verticle);
String deploymentId = manager.deploy(false, null, verticle.getName(), null, new URL[]{url}, 1, null, new Handler<Void>() {
@Override
public void handle(Void event) {
System.out.println(verticle.getName() + " started");
}
});
startedApps.add(deploymentId);
} catch (Exception e) {
undeployVerticles(startedApps);
throw new IllegalStateException("Unable to deploy " + verticle.getName() + " verticle. Undeployed " + Arrays.toString(startedApps.toArray()));
}
}
return startedApps;
}
private URL getCompilationFolder(Class<? extends Verticle> verticle) throws MalformedURLException {
String classDir = verticle.getName().replace('.', '/') + ".class";
URL url = getClass().getClassLoader().getResource(classDir);
String surl = url.toString();
String surlroot = surl.substring(0, surl.length() - classDir.length());
url = new URL(surlroot);
return url;
}
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
public @interface Verticles {
Class<? extends Verticle>[] values();
}
}
package org.vertx.java.junit.support;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.junit.Rule;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class SimpleVertexTest {
@Rule
public RunInVertx vertxRule = new RunInVertx();
@Test
@RunInVertx.Verticles(values = {SimpleHttpVerticle.class})
public void testRun() throws Exception {
HttpClient httpclient = new DefaultHttpClient();
try {
HttpGet httpget = new HttpGet("http://localhost:8181/");
ResponseHandler<String> responseHandler = new BasicResponseHandler();
String responseBody = httpclient.execute(httpget, responseHandler);
assertEquals("done", responseBody);
} finally {
httpclient.getConnectionManager().shutdown();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment