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
55
public static Collection <Object []> getTestData (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,17 @@ 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 );
100
+ }
101
+
102
+ if (files .isEmpty ()) {
103
+ Assertions .fail ("No test documents found in: " + resourcePath );
93
104
}
94
105
return files ;
95
106
}
96
107
97
108
private static BsonDocument getTestDocumentWithMetaData (final String resourcePath ) {
98
- JsonReader jsonReader = new JsonReader (resourcePathToString (resourcePath ));
99
- BsonDocument testDocument = new BsonDocumentCodec ().decode (jsonReader , DecoderContext .builder ().build ());
109
+ BsonDocument testDocument = BsonDocument .parse (resourcePathToString (resourcePath ));
100
110
testDocument .append ("resourcePath" , new BsonString (resourcePath ))
101
111
.append ("fileName" , new BsonString (resourcePath .substring (resourcePath .lastIndexOf ('/' ) + 1 )));
102
112
return testDocument ;
@@ -107,15 +117,17 @@ private static String resourcePathToString(final String resourcePath) {
107
117
String line ;
108
118
String ls = System .lineSeparator ();
109
119
try (InputStream inputStream = JsonPoweredTestHelper .class .getResourceAsStream (resourcePath )) {
110
- assertNotNull (inputStream );
120
+ if (inputStream == null ) {
121
+ Assertions .fail ("Unable to load resource: " + resourcePath );
122
+ }
111
123
try (BufferedReader reader = new BufferedReader (new InputStreamReader (inputStream , StandardCharsets .UTF_8 ))) {
112
124
while ((line = reader .readLine ()) != null ) {
113
125
stringBuilder .append (line );
114
126
stringBuilder .append (ls );
115
127
}
116
128
}
117
129
} catch (Exception e ) {
118
- fail ("Unable to load resource" , e );
130
+ Assertions . fail ("Unable to load resource" , e );
119
131
}
120
132
return stringBuilder .toString ();
121
133
}
0 commit comments