diff --git a/mongodb/util/src/main/java/example/springdata/mongodb/util/RequiresMongoDB.java b/mongodb/util/src/main/java/example/springdata/mongodb/util/RequiresMongoDB.java index 539ecd93..753c9525 100644 --- a/mongodb/util/src/main/java/example/springdata/mongodb/util/RequiresMongoDB.java +++ b/mongodb/util/src/main/java/example/springdata/mongodb/util/RequiresMongoDB.java @@ -1,5 +1,5 @@ /* - * Copyright 2014 the original author or authors. + * Copyright 2014-2015 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,12 +17,11 @@ package example.springdata.mongodb.util; import java.net.UnknownHostException; +import org.junit.AssumptionViolatedException; import org.junit.ClassRule; import org.junit.Rule; -import org.junit.internal.AssumptionViolatedException; +import org.junit.rules.ExternalResource; import org.junit.rules.TestRule; -import org.junit.runner.Description; -import org.junit.runners.model.Statement; import org.springframework.data.util.Version; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @@ -33,13 +32,14 @@ import com.mongodb.MongoClient; /** * {@link TestRule} verifying server tests are executed against match a given version. This one can be used as - * {@link ClassRule} eg. in context depending tests run with {@link SpringJUnit4ClassRunner} when the context would fail - * to start in case of invalid version, or as simple {@link Rule} on specific tests. + * {@link ClassRule} e.g. in context depending tests run with {@link SpringJUnit4ClassRunner} when the context would + * fail to start in case of invalid version, or as simple {@link Rule} on specific tests. * * @author Christoph Strobl + * @author Alexander Golonzovsky * @since 1.6 */ -public class RequiresMongoDB implements TestRule { +public class RequiresMongoDB extends ExternalResource { private String host = "localhost"; private int port = 27017; @@ -74,38 +74,26 @@ public class RequiresMongoDB implements TestRule { } @Override - public Statement apply(final Statement base, Description description) { + protected void before() throws Throwable { initCurrentVersion(); - return new Statement() { - @Override - public void evaluate() throws Throwable { - if (currentVersion != null) { - if (currentVersion.isLessThan(minVersion) || currentVersion.isGreaterThan(maxVersion)) { - throw new AssumptionViolatedException(String.format( - "Expected mongodb server to be in range %s to %s but found %s", minVersion, maxVersion, currentVersion)); - } - } - base.evaluate(); - } - }; + if (currentVersion.isLessThan(minVersion) || currentVersion.isGreaterThan(maxVersion)) { + throw new AssumptionViolatedException(String.format( + "Expected mongodb server to be in range %s to %s but found %s", minVersion, maxVersion, currentVersion)); + } } private void initCurrentVersion() { if (currentVersion == null) { try { - MongoClient client; - client = new MongoClient(host, port); - DB db = client.getDB("test"); + DB db = new MongoClient(host, port).getDB("test"); CommandResult result = db.command(new BasicDBObjectBuilder().add("buildInfo", 1).get()); this.currentVersion = Version.parse(result.get("version").toString()); } catch (com.mongodb.MongoTimeoutException | UnknownHostException e) { throw new AssumptionViolatedException("Seems as mongodb server is not running.", e); } } - } - }