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