19
19
import org .bson .BsonDocument ;
20
20
import org .bson .BsonString ;
21
21
import org .bson .BsonValue ;
22
- import org .bson .codecs .BsonDocumentCodec ;
23
- import org .bson .codecs .DecoderContext ;
24
- import org .bson .json .JsonReader ;
25
22
26
23
import java .io .BufferedReader ;
27
24
import java .io .IOException ;
28
25
import java .io .InputStream ;
29
26
import java .io .InputStreamReader ;
30
27
import java .net .URI ;
28
+ import java .net .URL ;
31
29
import java .nio .charset .StandardCharsets ;
32
30
import java .nio .file .FileSystem ;
33
31
import java .nio .file .FileSystems ;
42
40
import java .util .Collections ;
43
41
import java .util .List ;
44
42
45
- import static org .bson .assertions .Assertions .assertNotNull ;
46
43
import static org .junit .jupiter .api .Assertions .fail ;
47
44
48
45
public final class JsonPoweredTestHelper {
49
46
47
+ private static final String SPECIFICATIONS_PREFIX = "/specifications/source/" ;
48
+
50
49
public static BsonDocument getTestDocument (final String resourcePath ) {
51
- BsonDocument testDocument = getTestDocumentWithMetaData (resourcePath );
50
+ BsonDocument testDocument = getTestDocumentWithMetaData (SPECIFICATIONS_PREFIX + resourcePath );
52
51
testDocument .remove ("resourcePath" );
53
52
testDocument .remove ("fileName" );
54
53
return testDocument ;
55
54
}
56
55
57
- public static Collection <Object []> getTestData (final String resourcePath ) {
56
+ public static Collection <Object []> getLegacyTestData (final String resourcePath ) {
58
57
List <Object []> data = new ArrayList <>();
59
- for (BsonDocument document : getTestDocuments (resourcePath )) {
58
+ for (BsonDocument document : getSpecTestDocuments (resourcePath )) {
60
59
for (BsonValue test : document .getArray ("tests" )) {
61
60
BsonDocument testDocument = test .asDocument ();
62
61
data .add (new Object []{document .getString ("fileName" ).getValue (),
@@ -68,10 +67,19 @@ public static Collection<Object[]> getTestData(final String resourcePath) {
68
67
return data ;
69
68
}
70
69
70
+ public static List <BsonDocument > getSpecTestDocuments (final String resourcePath ) {
71
+ return getTestDocuments (SPECIFICATIONS_PREFIX + resourcePath );
72
+ }
73
+
71
74
public static List <BsonDocument > getTestDocuments (final String resourcePath ) {
72
75
List <BsonDocument > files = new ArrayList <>();
73
76
try {
74
- URI resource = assertNotNull (JsonPoweredTestHelper .class .getResource (resourcePath )).toURI ();
77
+ URL urlResource = JsonPoweredTestHelper .class .getResource (resourcePath );
78
+ if (urlResource == null ) {
79
+ fail ("No such resource: " + resourcePath );
80
+ }
81
+
82
+ URI resource = urlResource .toURI ();
75
83
try (FileSystem fileSystem = (resource .getScheme ().equals ("jar" ) ? FileSystems .newFileSystem (resource , Collections .emptyMap ()) : null )) {
76
84
Path myPath = Paths .get (resource );
77
85
Files .walkFileTree (myPath , new SimpleFileVisitor <Path >() {
@@ -89,14 +97,13 @@ public FileVisitResult visitFile(final Path filePath, final BasicFileAttributes
89
97
});
90
98
}
91
99
} catch (Exception e ) {
92
- fail ("Unable to load resource" , e );
100
+ fail ("Unable to load resource: " + resourcePath , e );
93
101
}
94
102
return files ;
95
103
}
96
104
97
105
private static BsonDocument getTestDocumentWithMetaData (final String resourcePath ) {
98
- JsonReader jsonReader = new JsonReader (resourcePathToString (resourcePath ));
99
- BsonDocument testDocument = new BsonDocumentCodec ().decode (jsonReader , DecoderContext .builder ().build ());
106
+ BsonDocument testDocument = BsonDocument .parse (resourcePathToString (resourcePath ));
100
107
testDocument .append ("resourcePath" , new BsonString (resourcePath ))
101
108
.append ("fileName" , new BsonString (resourcePath .substring (resourcePath .lastIndexOf ('/' ) + 1 )));
102
109
return testDocument ;
@@ -107,7 +114,9 @@ private static String resourcePathToString(final String resourcePath) {
107
114
String line ;
108
115
String ls = System .lineSeparator ();
109
116
try (InputStream inputStream = JsonPoweredTestHelper .class .getResourceAsStream (resourcePath )) {
110
- assertNotNull (inputStream );
117
+ if (inputStream == null ) {
118
+ fail ("Unable to load resource: " + resourcePath );
119
+ }
111
120
try (BufferedReader reader = new BufferedReader (new InputStreamReader (inputStream , StandardCharsets .UTF_8 ))) {
112
121
while ((line = reader .readLine ()) != null ) {
113
122
stringBuilder .append (line );
0 commit comments