|
1 | 1 | /*
|
2 |
| - * Copyright 2014 the original author or authors. |
| 2 | + * Copyright 2014-2015 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
17 | 17 |
|
18 | 18 | import java.net.UnknownHostException;
|
19 | 19 |
|
| 20 | +import org.junit.AssumptionViolatedException; |
20 | 21 | import org.junit.ClassRule;
|
21 | 22 | import org.junit.Rule;
|
22 |
| -import org.junit.internal.AssumptionViolatedException; |
| 23 | +import org.junit.rules.ExternalResource; |
23 | 24 | import org.junit.rules.TestRule;
|
24 |
| -import org.junit.runner.Description; |
25 |
| -import org.junit.runners.model.Statement; |
26 | 25 | import org.springframework.data.util.Version;
|
27 | 26 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
28 | 27 |
|
|
33 | 32 |
|
34 | 33 | /**
|
35 | 34 | * {@link TestRule} verifying server tests are executed against match a given version. This one can be used as
|
36 |
| - * {@link ClassRule} eg. in context depending tests run with {@link SpringJUnit4ClassRunner} when the context would fail |
37 |
| - * to start in case of invalid version, or as simple {@link Rule} on specific tests. |
| 35 | + * {@link ClassRule} e.g. in context depending tests run with {@link SpringJUnit4ClassRunner} when the context would |
| 36 | + * fail to start in case of invalid version, or as simple {@link Rule} on specific tests. |
38 | 37 | *
|
39 | 38 | * @author Christoph Strobl
|
| 39 | + * @author Alexander Golonzovsky |
40 | 40 | * @since 1.6
|
41 | 41 | */
|
42 |
| -public class RequiresMongoDB implements TestRule { |
| 42 | +public class RequiresMongoDB extends ExternalResource { |
43 | 43 |
|
44 | 44 | private String host = "localhost";
|
45 | 45 | private int port = 27017;
|
@@ -74,38 +74,26 @@ public RequiresMongoDB serverRunningAt(String host, int port) {
|
74 | 74 | }
|
75 | 75 |
|
76 | 76 | @Override
|
77 |
| - public Statement apply(final Statement base, Description description) { |
| 77 | + protected void before() throws Throwable { |
78 | 78 |
|
79 | 79 | initCurrentVersion();
|
80 |
| - return new Statement() { |
81 |
| - |
82 |
| - @Override |
83 |
| - public void evaluate() throws Throwable { |
84 |
| - if (currentVersion != null) { |
85 |
| - if (currentVersion.isLessThan(minVersion) || currentVersion.isGreaterThan(maxVersion)) { |
86 |
| - throw new AssumptionViolatedException(String.format( |
87 |
| - "Expected mongodb server to be in range %s to %s but found %s", minVersion, maxVersion, currentVersion)); |
88 |
| - } |
89 |
| - } |
90 |
| - base.evaluate(); |
91 |
| - } |
92 |
| - }; |
| 80 | + |
| 81 | + if (currentVersion.isLessThan(minVersion) || currentVersion.isGreaterThan(maxVersion)) { |
| 82 | + throw new AssumptionViolatedException(String.format( |
| 83 | + "Expected mongodb server to be in range %s to %s but found %s", minVersion, maxVersion, currentVersion)); |
| 84 | + } |
93 | 85 | }
|
94 | 86 |
|
95 | 87 | private void initCurrentVersion() {
|
96 | 88 |
|
97 | 89 | if (currentVersion == null) {
|
98 | 90 | try {
|
99 |
| - MongoClient client; |
100 |
| - client = new MongoClient(host, port); |
101 |
| - DB db = client.getDB("test"); |
| 91 | + DB db = new MongoClient(host, port).getDB("test"); |
102 | 92 | CommandResult result = db.command(new BasicDBObjectBuilder().add("buildInfo", 1).get());
|
103 | 93 | this.currentVersion = Version.parse(result.get("version").toString());
|
104 | 94 | } catch (com.mongodb.MongoTimeoutException | UnknownHostException e) {
|
105 | 95 | throw new AssumptionViolatedException("Seems as mongodb server is not running.", e);
|
106 | 96 | }
|
107 | 97 | }
|
108 |
| - |
109 | 98 | }
|
110 |
| - |
111 | 99 | }
|
0 commit comments