diff --git a/mongodb/example/src/test/java/example/springdata/mongodb/customer/CustomerRepositoryIntegrationTest.java b/mongodb/example/src/test/java/example/springdata/mongodb/customer/CustomerRepositoryIntegrationTest.java index c9d37924..5d13027f 100644 --- a/mongodb/example/src/test/java/example/springdata/mongodb/customer/CustomerRepositoryIntegrationTest.java +++ b/mongodb/example/src/test/java/example/springdata/mongodb/customer/CustomerRepositoryIntegrationTest.java @@ -15,12 +15,14 @@ */ package example.springdata.mongodb.customer; -import static org.hamcrest.Matchers.*; -import static org.junit.Assert.*; +import static org.hamcrest.Matchers.closeTo; +import static org.hamcrest.Matchers.hasSize; +import static org.hamcrest.Matchers.is; +import static org.hamcrest.Matchers.notNullValue; +import static org.junit.Assert.assertThat; import java.util.List; -import org.hamcrest.number.IsCloseTo; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -108,6 +110,6 @@ public class CustomerRepositoryIntegrationTest { Distance distanceToFirstStore = result.getContent().get(0).getDistance(); assertThat(distanceToFirstStore.getMetric(), is(Metrics.KILOMETERS)); - assertThat(distanceToFirstStore.getValue(), IsCloseTo.closeTo(0.862, 0.001)); + assertThat(distanceToFirstStore.getValue(), closeTo(0.862, 0.001)); } } diff --git a/mongodb/text-search/src/test/java/example/springdata/mongodb/textsearch/TextSearchRepositoryTests.java b/mongodb/text-search/src/test/java/example/springdata/mongodb/textsearch/TextSearchRepositoryTests.java index 6c52aaea..981f6e57 100644 --- a/mongodb/text-search/src/test/java/example/springdata/mongodb/textsearch/TextSearchRepositoryTests.java +++ b/mongodb/text-search/src/test/java/example/springdata/mongodb/textsearch/TextSearchRepositoryTests.java @@ -15,23 +15,28 @@ */ package example.springdata.mongodb.textsearch; -import static example.springdata.mongodb.util.ConsoleResultPrinter.*; +import static example.springdata.mongodb.util.ConsoleResultPrinter.printResult; import java.util.List; +import org.junit.ClassRule; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.mongodb.core.mapping.TextScore; import org.springframework.data.mongodb.core.query.TextCriteria; +import org.springframework.data.util.Version; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import example.springdata.mongodb.util.MongoVersionRule; + /** * Integration tests showing the text search functionality using repositories. * * @author Christoph Strobl * @author Oliver Gierke + * @author Thomas Darimont */ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = { MongoTestConfiguration.class }) @@ -39,6 +44,8 @@ public class TextSearchRepositoryTests { @Autowired BlogPostRepository repo; + @ClassRule public static MongoVersionRule versionRule = MongoVersionRule.atLeast(new Version(2, 6)); + /** * Show how to do simple matching.
* Note that text search is case insensitive and will also find entries like {@literal releases}. diff --git a/mongodb/text-search/src/test/java/example/springdata/mongodb/textsearch/TextSearchTemplateTests.java b/mongodb/text-search/src/test/java/example/springdata/mongodb/textsearch/TextSearchTemplateTests.java index 0eda7067..8a25993f 100644 --- a/mongodb/text-search/src/test/java/example/springdata/mongodb/textsearch/TextSearchTemplateTests.java +++ b/mongodb/text-search/src/test/java/example/springdata/mongodb/textsearch/TextSearchTemplateTests.java @@ -15,12 +15,13 @@ */ package example.springdata.mongodb.textsearch; -import static example.springdata.mongodb.util.ConsoleResultPrinter.*; -import static org.springframework.data.mongodb.core.query.Query.*; +import static example.springdata.mongodb.util.ConsoleResultPrinter.printResult; +import static org.springframework.data.mongodb.core.query.Query.query; import java.util.List; import org.junit.Before; +import org.junit.ClassRule; import org.junit.Test; import org.springframework.boot.autoconfigure.mongo.MongoProperties; import org.springframework.data.mongodb.core.MongoOperations; @@ -29,16 +30,21 @@ import org.springframework.data.mongodb.core.index.TextIndexDefinition; import org.springframework.data.mongodb.core.index.TextIndexDefinition.TextIndexDefinitionBuilder; import org.springframework.data.mongodb.core.query.TextCriteria; import org.springframework.data.mongodb.core.query.TextQuery; +import org.springframework.data.util.Version; import example.springdata.mongodb.util.BlogPostInitializer; +import example.springdata.mongodb.util.MongoVersionRule; /** * @author Christoph Strobl + * @author Thomas Darimont */ public class TextSearchTemplateTests { MongoOperations operations; + @ClassRule public static MongoVersionRule versionRule = MongoVersionRule.atLeast(new Version(2, 6)); + @Before public void setUp() throws Exception { diff --git a/mongodb/text-search/src/test/java/example/springdata/mongodb/util/MongoVersionRule.java b/mongodb/text-search/src/test/java/example/springdata/mongodb/util/MongoVersionRule.java new file mode 100644 index 00000000..10947cf4 --- /dev/null +++ b/mongodb/text-search/src/test/java/example/springdata/mongodb/util/MongoVersionRule.java @@ -0,0 +1,109 @@ +/* + * Copyright 2014 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package example.springdata.mongodb.util; + +import org.junit.ClassRule; +import org.junit.Rule; +import org.junit.internal.AssumptionViolatedException; +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; + +import com.mongodb.BasicDBObjectBuilder; +import com.mongodb.CommandResult; +import com.mongodb.DB; +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. + * + * @author Christoph Strobl + * @since 1.6 + */ +public class MongoVersionRule implements TestRule { + + private String host = "localhost"; + private int port = 27017; + + private final Version minVersion; + private final Version maxVersion; + + private Version currentVersion; + + public MongoVersionRule(Version min, Version max) { + this.minVersion = min; + this.maxVersion = max; + } + + public static MongoVersionRule any() { + return new MongoVersionRule(new Version(0, 0, 0), new Version(9999, 9999, 9999)); + } + + public static MongoVersionRule atLeast(Version minVersion) { + return new MongoVersionRule(minVersion, new Version(9999, 9999, 9999)); + } + + public static MongoVersionRule atMost(Version maxVersion) { + return new MongoVersionRule(new Version(0, 0, 0), maxVersion); + } + + public MongoVersionRule withServerRunningAt(String host, int port) { + this.host = host; + this.port = port; + + return this; + } + + @Override + public Statement apply(final Statement base, Description description) { + + 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(); + } + }; + } + + private void initCurrentVersion() { + + if (currentVersion == null) { + try { + MongoClient client; + client = new MongoClient(host, port); + DB db = client.getDB("test"); + CommandResult result = db.command(new BasicDBObjectBuilder().add("buildInfo", 1).get()); + this.currentVersion = Version.parse(result.get("version").toString()); + } catch (Exception e) { + e.printStackTrace(); + } + } + + } + +}