Skip to content

Commit d80ee10

Browse files
committed
Gradle: Migrated tests to work with conventions (3/3)
Added library conventions and language based configurations. - Refactored `JsonPoweredTestHelper` to handle either local and jar sources - Refactored test cases to use new `JsonPoweredTestHelper.getTestDocuments` method - Refactored test cases to normalize usage of loading test data - Moved scala integration tests into a standardized `integerationTest` directory JAVA-5758
1 parent 3f41b65 commit d80ee10

File tree

130 files changed

+229
-525
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

130 files changed

+229
-525
lines changed

bson-scala/src/test/scala/org/mongodb/scala/bson/BaseSpec.scala

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,7 @@
1515
*/
1616
package org.mongodb.scala.bson
1717

18-
import org.junit.runner.RunWith
1918
import org.scalatest.flatspec.AnyFlatSpec
2019
import org.scalatest.matchers.should.Matchers
21-
import org.scalatestplus.junit.JUnitRunner
2220

23-
@RunWith(classOf[JUnitRunner])
2421
abstract class BaseSpec extends AnyFlatSpec with Matchers {}

bson-scala/src/test/scala/org/mongodb/scala/bson/codecs/MacrosSpec.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -531,6 +531,7 @@ class MacrosSpec extends BaseSpec {
531531
}
532532

533533
it should "support tagged types in case classes" in {
534+
assume(!scala.util.Properties.versionNumberString.startsWith("2.11"))
534535
val a = 1.asInstanceOf[Int with Tag]
535536
val b = "b".asInstanceOf[String with Tag]
536537
val c = Map("c" -> 0).asInstanceOf[Map[String with Tag, Int with Tag] with Tag]

bson/src/test/unit/org/bson/GenericBsonTest.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,9 @@
2525
import org.junit.jupiter.params.provider.MethodSource;
2626
import util.JsonPoweredTestHelper;
2727

28-
import java.io.File;
2928
import java.io.IOException;
3029
import java.io.StringReader;
3130
import java.io.StringWriter;
32-
import java.net.URISyntaxException;
3331
import java.nio.charset.StandardCharsets;
3432
import java.util.ArrayList;
3533
import java.util.Arrays;
@@ -283,10 +281,9 @@ private void throwIfValueIsStringContainingReplacementCharacter(final BsonDocume
283281
}
284282

285283

286-
private static Stream<Arguments> data() throws URISyntaxException, IOException {
284+
private static Stream<Arguments> data() {
287285
List<Arguments> data = new ArrayList<>();
288-
for (File file : JsonPoweredTestHelper.getTestFiles("/bson")) {
289-
BsonDocument testDocument = JsonPoweredTestHelper.getTestDocument(file);
286+
for (BsonDocument testDocument : JsonPoweredTestHelper.getTestDocuments("/bson")) {
290287
for (BsonValue curValue : testDocument.getArray("valid", new BsonArray())) {
291288
BsonDocument testCaseDocument = curValue.asDocument();
292289
data.add(Arguments.of(

bson/src/test/unit/org/bson/vector/BinaryVectorGenericBsonTest.java

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,20 @@
1616

1717
package org.bson.vector;
1818

19+
import org.bson.BinaryVector;
1920
import org.bson.BsonArray;
2021
import org.bson.BsonBinary;
2122
import org.bson.BsonDocument;
2223
import org.bson.BsonString;
2324
import org.bson.BsonValue;
2425
import org.bson.Float32BinaryVector;
2526
import org.bson.PackedBitBinaryVector;
26-
import org.bson.BinaryVector;
2727
import org.junit.jupiter.api.Assertions;
2828
import org.junit.jupiter.params.ParameterizedTest;
2929
import org.junit.jupiter.params.provider.Arguments;
3030
import org.junit.jupiter.params.provider.MethodSource;
3131
import util.JsonPoweredTestHelper;
3232

33-
import java.io.File;
34-
import java.io.IOException;
35-
import java.net.URISyntaxException;
3633
import java.util.ArrayList;
3734
import java.util.Arrays;
3835
import java.util.List;
@@ -73,7 +70,7 @@ class BinaryVectorGenericBsonTest {
7370

7471

7572
@ParameterizedTest(name = "{0}")
76-
@MethodSource("provideTestCases")
73+
@MethodSource("data")
7774
void shouldPassAllOutcomes(@SuppressWarnings("unused") final String description,
7875
final BsonDocument testDefinition, final BsonDocument testCase) {
7976
assumeFalse(TEST_NAMES_TO_IGNORE.contains(testCase.get("description").asString().getValue()));
@@ -254,10 +251,9 @@ private static float parseFloat(final BsonString bsonValue) {
254251
}
255252
}
256253

257-
private static Stream<Arguments> provideTestCases() throws URISyntaxException, IOException {
254+
private static Stream<Arguments> data() {
258255
List<Arguments> data = new ArrayList<>();
259-
for (File file : JsonPoweredTestHelper.getTestFiles("/bson-binary-vector")) {
260-
BsonDocument testDocument = JsonPoweredTestHelper.getTestDocument(file);
256+
for (BsonDocument testDocument : JsonPoweredTestHelper.getTestDocuments("/bson-binary-vector")) {
261257
for (BsonValue curValue : testDocument.getArray("tests", new BsonArray())) {
262258
BsonDocument testCaseDocument = curValue.asDocument();
263259
data.add(Arguments.of(createTestCaseDescription(testDocument, testCaseDocument), testDocument, testCaseDocument));

bson/src/test/unit/util/JsonPoweredTestHelper.java

Lines changed: 65 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -17,105 +17,107 @@
1717
package util;
1818

1919
import org.bson.BsonDocument;
20+
import org.bson.BsonString;
21+
import org.bson.BsonValue;
2022
import org.bson.codecs.BsonDocumentCodec;
2123
import org.bson.codecs.DecoderContext;
2224
import org.bson.json.JsonReader;
2325

2426
import java.io.BufferedReader;
25-
import java.io.File;
26-
import java.io.FileInputStream;
2727
import java.io.IOException;
28+
import java.io.InputStream;
2829
import java.io.InputStreamReader;
29-
import java.net.URISyntaxException;
30-
import java.net.URL;
30+
import java.net.URI;
3131
import java.nio.charset.StandardCharsets;
32+
import java.nio.file.FileSystem;
3233
import java.nio.file.FileSystems;
34+
import java.nio.file.FileVisitResult;
3335
import java.nio.file.Files;
3436
import java.nio.file.Path;
35-
import java.nio.file.PathMatcher;
3637
import java.nio.file.Paths;
38+
import java.nio.file.SimpleFileVisitor;
39+
import java.nio.file.attribute.BasicFileAttributes;
3740
import java.util.ArrayList;
41+
import java.util.Collection;
42+
import java.util.Collections;
3843
import java.util.List;
39-
import java.util.Map;
40-
import java.util.function.Function;
4144

42-
import static java.nio.file.Files.isDirectory;
43-
import static java.util.stream.Collectors.toMap;
45+
import static org.bson.assertions.Assertions.assertNotNull;
46+
import static org.junit.jupiter.api.Assertions.fail;
4447

4548
public final class JsonPoweredTestHelper {
4649

47-
public static BsonDocument getTestDocument(final File file) throws IOException {
48-
return new BsonDocumentCodec().decode(new JsonReader(getFileAsString(file)), DecoderContext.builder().build());
50+
public static BsonDocument getTestDocument(final String resourcePath) {
51+
BsonDocument testDocument = getTestDocumentWithMetaData(resourcePath);
52+
testDocument.remove("resourcePath");
53+
testDocument.remove("fileName");
54+
return testDocument;
4955
}
5056

51-
public static BsonDocument getTestDocument(final String resourcePath) throws IOException, URISyntaxException {
52-
return getTestDocument(new File(JsonPoweredTestHelper.class.getResource(resourcePath).toURI()));
53-
}
54-
55-
public static Path testDir(final String resourceName) {
56-
URL res = JsonPoweredTestHelper.class.getResource(resourceName);
57-
if (res == null) {
58-
throw new AssertionError("Did not find " + resourceName);
59-
}
60-
try {
61-
Path dir = Paths.get(res.toURI());
62-
if (!isDirectory(dir)) {
63-
throw new AssertionError(dir + " is not a directory");
57+
public static Collection<Object[]> getTestData(final String resourcePath) {
58+
List<Object[]> data = new ArrayList<>();
59+
for (BsonDocument document : getTestDocuments(resourcePath)) {
60+
for (BsonValue test : document.getArray("tests")) {
61+
BsonDocument testDocument = test.asDocument();
62+
data.add(new Object[]{document.getString("fileName").getValue(),
63+
testDocument.getString("description").getValue(),
64+
testDocument.getString("uri", new BsonString("")).getValue(),
65+
testDocument});
6466
}
65-
return dir;
66-
} catch (URISyntaxException e) {
67-
throw new RuntimeException(e);
6867
}
68+
return data;
6969
}
7070

71-
public static Map<Path, BsonDocument> testDocs(final Path dir) {
72-
PathMatcher jsonMatcher = FileSystems.getDefault().getPathMatcher("glob:**.json");
71+
public static List<BsonDocument> getTestDocuments(final String resourcePath) {
72+
List<BsonDocument> files = new ArrayList<>();
7373
try {
74-
return Files.list(dir)
75-
.filter(jsonMatcher::matches)
76-
.collect(toMap(Function.identity(), path -> {
77-
try {
78-
return getTestDocument(path.toFile());
79-
} catch (IOException e) {
80-
throw new RuntimeException(e);
74+
URI resource = assertNotNull(JsonPoweredTestHelper.class.getResource(resourcePath)).toURI();
75+
try (FileSystem fileSystem = (resource.getScheme().equals("jar") ? FileSystems.newFileSystem(resource, Collections.emptyMap()) : null)) {
76+
Path myPath = Paths.get(resource);
77+
Files.walkFileTree(myPath, new SimpleFileVisitor<Path>() {
78+
@Override
79+
public FileVisitResult visitFile(final Path filePath, final BasicFileAttributes attrs) throws IOException {
80+
if (filePath.toString().endsWith(".json")) {
81+
if (fileSystem == null) {
82+
files.add(getTestDocumentWithMetaData(filePath.toString().substring(filePath.toString().lastIndexOf(resourcePath))));
83+
} else {
84+
files.add(getTestDocumentWithMetaData(filePath.toString()));
85+
}
8186
}
82-
}));
83-
} catch (IOException e) {
84-
throw new RuntimeException(e);
87+
return super.visitFile(filePath, attrs);
88+
}
89+
});
90+
}
91+
} catch (Exception e) {
92+
fail("Unable to load resource", e);
8593
}
94+
return files;
8695
}
8796

88-
public static List<File> getTestFiles(final String resourcePath) throws URISyntaxException {
89-
List<File> files = new ArrayList<>();
90-
addFilesFromDirectory(new File(JsonPoweredTestHelper.class.getResource(resourcePath).toURI()), files);
91-
return files;
97+
private static BsonDocument getTestDocumentWithMetaData(final String resourcePath) {
98+
JsonReader jsonReader = new JsonReader(resourcePathToString(resourcePath));
99+
BsonDocument testDocument = new BsonDocumentCodec().decode(jsonReader, DecoderContext.builder().build());
100+
testDocument.append("resourcePath", new BsonString(resourcePath))
101+
.append("fileName", new BsonString(resourcePath.substring(resourcePath.lastIndexOf('/') + 1)));
102+
return testDocument;
92103
}
93104

94-
private static String getFileAsString(final File file) throws IOException {
105+
private static String resourcePathToString(final String resourcePath) {
95106
StringBuilder stringBuilder = new StringBuilder();
96107
String line;
97-
String ls = System.getProperty("line.separator");
98-
try (BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), StandardCharsets.UTF_8))) {
99-
while ((line = reader.readLine()) != null) {
100-
stringBuilder.append(line);
101-
stringBuilder.append(ls);
102-
}
103-
}
104-
return stringBuilder.toString();
105-
}
106-
107-
private static void addFilesFromDirectory(final File directory, final List<File> files) {
108-
String[] fileNames = directory.list();
109-
if (fileNames != null) {
110-
for (String fileName : fileNames) {
111-
File file = new File(directory, fileName);
112-
if (file.isDirectory()) {
113-
addFilesFromDirectory(file, files);
114-
} else if (file.getName().endsWith(".json")) {
115-
files.add(file);
108+
String ls = System.lineSeparator();
109+
try (InputStream inputStream = JsonPoweredTestHelper.class.getResourceAsStream(resourcePath)) {
110+
assertNotNull(inputStream);
111+
try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8))) {
112+
while ((line = reader.readLine()) != null) {
113+
stringBuilder.append(line);
114+
stringBuilder.append(ls);
116115
}
117116
}
117+
} catch (Exception e) {
118+
fail("Unable to load resource", e);
118119
}
120+
return stringBuilder.toString();
119121
}
120122

121123
private JsonPoweredTestHelper() {

driver-core/src/test/functional/com/mongodb/client/model/AggregatesFunctionalSpecification.groovy

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1336,7 +1336,8 @@ class AggregatesFunctionalSpecification extends OperationFunctionalSpecification
13361336
[1, 2, 3, 4, 1, 2, 3, 4, 4.1]
13371337
'num' | 'partitionId' | densify(field, partitionRangeWithStep(1), densifyOptions().partitionByFields([partitionByField])) |
13381338
[1, 2, 3, 4.1]
1339-
'date' | null | densify(field, rangeWithStep(Instant.EPOCH, Instant.ofEpochMilli(BigInteger.TWO.pow(32).longValueExact()) + 1,
1339+
'date' | null | densify(field, rangeWithStep(Instant.EPOCH, Instant.ofEpochMilli(BigInteger.TWO.pow(32).longValueExact())
1340+
.plusMillis(1),
13401341
// there is a server bug that prevents using `step` larger than 2^31 - 1 in versions before 6.1
13411342
BigInteger.TWO.pow(31).longValueExact() - 1, MongoTimeUnit.MILLISECOND)) |
13421343
[Instant.EPOCH,

driver-core/src/test/functional/com/mongodb/connection/netty/NettyStreamSpecification.groovy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import io.netty.channel.nio.NioEventLoopGroup
1313
import io.netty.channel.socket.nio.NioSocketChannel
1414
import spock.lang.IgnoreIf
1515
import spock.lang.Specification
16-
import util.spock.annotations.Slow
16+
import com.mongodb.spock.Slow
1717

1818
import java.util.concurrent.CountDownLatch
1919
import java.util.concurrent.TimeUnit

driver-core/src/test/functional/com/mongodb/internal/connection/AsyncSocketChannelStreamSpecification.groovy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import com.mongodb.connection.SslSettings
99
import com.mongodb.spi.dns.InetAddressResolver
1010
import spock.lang.IgnoreIf
1111
import spock.lang.Specification
12-
import util.spock.annotations.Slow
12+
import com.mongodb.spock.Slow
1313

1414
import java.util.concurrent.CountDownLatch
1515

driver-core/src/test/functional/com/mongodb/internal/connection/AsyncStreamTimeoutsSpecification.groovy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import com.mongodb.connection.ServerId
2626
import com.mongodb.connection.SocketSettings
2727
import com.mongodb.internal.connection.netty.NettyStreamFactory
2828
import spock.lang.IgnoreIf
29-
import util.spock.annotations.Slow
29+
import com.mongodb.spock.Slow
3030

3131
import java.util.concurrent.TimeUnit
3232

driver-core/src/test/functional/com/mongodb/internal/connection/StreamSocketAddressSpecification.groovy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import com.mongodb.spi.dns.InetAddressResolver
88
import spock.lang.Ignore
99
import spock.lang.IgnoreIf
1010
import spock.lang.Specification
11-
import util.spock.annotations.Slow
11+
import com.mongodb.spock.Slow
1212

1313
import javax.net.SocketFactory
1414
import java.util.concurrent.TimeUnit

driver-core/src/test/functional/com/mongodb/internal/operation/CommandOperationSpecification.groovy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import org.bson.BsonDocument
2323
import org.bson.BsonInt32
2424
import org.bson.BsonString
2525
import org.bson.codecs.BsonDocumentCodec
26-
import util.spock.annotations.Slow
26+
import com.mongodb.spock.Slow
2727

2828
class CommandOperationSpecification extends OperationFunctionalSpecification {
2929

driver-core/src/test/functional/com/mongodb/internal/operation/MixedBulkWriteOperationSpecification.groovy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ import org.bson.codecs.BsonDocumentCodec
4646
import org.bson.codecs.DocumentCodec
4747
import org.bson.types.ObjectId
4848
import spock.lang.IgnoreIf
49-
import util.spock.annotations.Slow
49+
import com.mongodb.spock.Slow
5050

5151
import static com.mongodb.ClusterFixture.configureFailPoint
5252
import static com.mongodb.ClusterFixture.disableFailPoint

driver-core/src/test/unit/com/mongodb/AuthConnectionStringTest.java

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,7 @@
2929
import org.junit.runners.Parameterized;
3030
import util.JsonPoweredTestHelper;
3131

32-
import java.io.File;
33-
import java.io.IOException;
34-
import java.net.URISyntaxException;
35-
import java.util.ArrayList;
3632
import java.util.Collection;
37-
import java.util.List;
3833

3934
import static com.mongodb.AuthenticationMechanism.MONGODB_OIDC;
4035
import static com.mongodb.MongoCredential.OIDC_CALLBACK_KEY;
@@ -61,16 +56,8 @@ public void shouldPassAllOutcomes() {
6156
}
6257

6358
@Parameterized.Parameters(name = "{1}")
64-
public static Collection<Object[]> data() throws URISyntaxException, IOException {
65-
List<Object[]> data = new ArrayList<>();
66-
for (File file : JsonPoweredTestHelper.getTestFiles("/auth/legacy")) {
67-
BsonDocument testDocument = JsonPoweredTestHelper.getTestDocument(file);
68-
for (BsonValue test : testDocument.getArray("tests")) {
69-
data.add(new Object[]{file.getName(), test.asDocument().getString("description").getValue(),
70-
test.asDocument().getString("uri").getValue(), test.asDocument()});
71-
}
72-
}
73-
return data;
59+
public static Collection<Object[]> data() {
60+
return JsonPoweredTestHelper.getTestData("/auth/legacy");
7461
}
7562

7663
private void testInvalidUris() {

driver-core/src/test/unit/com/mongodb/ConnectionStringTest.java

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,11 @@
1717
package com.mongodb;
1818

1919
import org.bson.BsonDocument;
20-
import org.bson.BsonValue;
2120
import org.junit.Test;
2221
import org.junit.runners.Parameterized;
2322
import util.JsonPoweredTestHelper;
2423

25-
import java.io.File;
26-
import java.io.IOException;
27-
import java.net.URISyntaxException;
28-
import java.util.ArrayList;
2924
import java.util.Collection;
30-
import java.util.List;
3125

3226
import static org.junit.Assume.assumeFalse;
3327

@@ -70,15 +64,7 @@ public void shouldPassAllOutcomes() {
7064

7165

7266
@Parameterized.Parameters(name = "{1}")
73-
public static Collection<Object[]> data() throws URISyntaxException, IOException {
74-
List<Object[]> data = new ArrayList<>();
75-
for (File file : JsonPoweredTestHelper.getTestFiles("/connection-string")) {
76-
BsonDocument testDocument = JsonPoweredTestHelper.getTestDocument(file);
77-
for (BsonValue test : testDocument.getArray("tests")) {
78-
data.add(new Object[]{file.getName(), test.asDocument().getString("description").getValue(),
79-
test.asDocument().getString("uri").getValue(), test.asDocument()});
80-
}
81-
}
82-
return data;
67+
public static Collection<Object[]> data() {
68+
return JsonPoweredTestHelper.getTestData("/connection-string");
8369
}
8470
}

0 commit comments

Comments
 (0)