Skip to content

Added mongodb/specifications as a git submodule #1670

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 20 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
6 changes: 6 additions & 0 deletions .evergreen/.evg.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ functions:
# Applies the subitted patch, if any
# Deprecated. Should be removed. But still needed for certain agents (ZAP)
- command: git.apply_patch
# Fetch the specifications submodule
- command: shell.exec
params:
working_dir: "src"
script: |
git submodule update --init
# Make an evergreen expansion file with dynamic values
- command: shell.exec
params:
Expand Down
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "specifications"]
path = driver-core/src/test/resources/specifications
url = https://github.com/mongodb/specifications
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ Example for Maven:
Java 17+ and git is required to build and compile the source. To build and test the driver:

```
$ git clone https://github.com/mongodb/mongo-java-driver.git
$ git clone --recurse-submodules https://github.com/mongodb/mongo-java-driver.git
$ cd mongo-java-driver
$ ./gradlew check
```
Expand Down
12 changes: 12 additions & 0 deletions bson/src/main/org/bson/assertions/Assertions.java
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,18 @@ public static AssertionError fail(final String msg) throws AssertionError {
throw new AssertionError(assertNotNull(msg));
}

/**
* @param msg The failure message.
* @param cause The underlying cause
* @return Never completes normally. The return type is {@link AssertionError} to allow writing
* {@code throw fail("failure message", throwable)}.
* This may be helpful in non-{@code void} methods.
* @throws AssertionError Always
*/
public static AssertionError fail(final String msg, final Throwable cause) throws AssertionError {
throw new AssertionError(assertNotNull(msg), assertNotNull(cause));
}

/**
* @param value A value to check.
* @param <T> The type of {@code value}.
Expand Down
40 changes: 26 additions & 14 deletions bson/src/test/unit/util/JsonPoweredTestHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,14 @@
import org.bson.BsonDocument;
import org.bson.BsonString;
import org.bson.BsonValue;
import org.bson.codecs.BsonDocumentCodec;
import org.bson.codecs.DecoderContext;
import org.bson.json.JsonReader;
import org.bson.assertions.Assertions;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
Expand All @@ -42,21 +41,20 @@
import java.util.Collections;
import java.util.List;

import static org.bson.assertions.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.fail;

public final class JsonPoweredTestHelper {

private static final String SPECIFICATIONS_PREFIX = "/specifications/source/";

public static BsonDocument getTestDocument(final String resourcePath) {
BsonDocument testDocument = getTestDocumentWithMetaData(resourcePath);
BsonDocument testDocument = getTestDocumentWithMetaData(SPECIFICATIONS_PREFIX + resourcePath);
testDocument.remove("resourcePath");
testDocument.remove("fileName");
return testDocument;
}

public static Collection<Object[]> getTestData(final String resourcePath) {
List<Object[]> data = new ArrayList<>();
for (BsonDocument document : getTestDocuments(resourcePath)) {
for (BsonDocument document : getSpecTestDocuments(resourcePath)) {
for (BsonValue test : document.getArray("tests")) {
BsonDocument testDocument = test.asDocument();
data.add(new Object[]{document.getString("fileName").getValue(),
Expand All @@ -68,10 +66,19 @@ public static Collection<Object[]> getTestData(final String resourcePath) {
return data;
}

public static List<BsonDocument> getSpecTestDocuments(final String resourcePath) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

New helper to auto include the specifications location prefix.

return getTestDocuments(SPECIFICATIONS_PREFIX + resourcePath);
}

public static List<BsonDocument> getTestDocuments(final String resourcePath) {
List<BsonDocument> files = new ArrayList<>();
try {
URI resource = assertNotNull(JsonPoweredTestHelper.class.getResource(resourcePath)).toURI();
URL urlResource = JsonPoweredTestHelper.class.getResource(resourcePath);
if (urlResource == null) {
Assertions.fail("No such resource: " + resourcePath);
}

URI resource = urlResource.toURI();
try (FileSystem fileSystem = (resource.getScheme().equals("jar") ? FileSystems.newFileSystem(resource, Collections.emptyMap()) : null)) {
Path myPath = Paths.get(resource);
Files.walkFileTree(myPath, new SimpleFileVisitor<Path>() {
Expand All @@ -89,14 +96,17 @@ public FileVisitResult visitFile(final Path filePath, final BasicFileAttributes
});
}
} catch (Exception e) {
fail("Unable to load resource", e);
Assertions.fail("Unable to load resource: " + resourcePath, e);
}

if (files.isEmpty()) {
Assertions.fail("No test documents found in: " + resourcePath);
}
return files;
}

private static BsonDocument getTestDocumentWithMetaData(final String resourcePath) {
JsonReader jsonReader = new JsonReader(resourcePathToString(resourcePath));
BsonDocument testDocument = new BsonDocumentCodec().decode(jsonReader, DecoderContext.builder().build());
BsonDocument testDocument = BsonDocument.parse(resourcePathToString(resourcePath));
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Simplification of the parsing code.

testDocument.append("resourcePath", new BsonString(resourcePath))
.append("fileName", new BsonString(resourcePath.substring(resourcePath.lastIndexOf('/') + 1)));
return testDocument;
Expand All @@ -107,15 +117,17 @@ private static String resourcePathToString(final String resourcePath) {
String line;
String ls = System.lineSeparator();
try (InputStream inputStream = JsonPoweredTestHelper.class.getResourceAsStream(resourcePath)) {
assertNotNull(inputStream);
if (inputStream == null) {
Assertions.fail("Unable to load resource: " + resourcePath);
}
try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8))) {
while ((line = reader.readLine()) != null) {
stringBuilder.append(line);
stringBuilder.append(ls);
}
}
} catch (Exception e) {
fail("Unable to load resource", e);
Assertions.fail("Unable to load resource", e);
}
return stringBuilder.toString();
}
Expand Down
Loading