5
5
import static java .util .stream .Collectors .joining ;
6
6
import static org .everit .json .schema .loader .OrgJsonUtil .toMap ;
7
7
8
- import java .io .File ;
9
- import java .io .FileInputStream ;
10
- import java .io .FileNotFoundException ;
11
8
import java .io .IOException ;
9
+ import java .io .InputStream ;
12
10
import java .io .UncheckedIOException ;
13
11
import java .lang .reflect .Constructor ;
14
- import java .net .URISyntaxException ;
15
12
import java .util .ArrayList ;
16
- import java .util .Arrays ;
17
13
import java .util .HashMap ;
18
14
import java .util .HashSet ;
19
15
import java .util .List ;
20
16
import java .util .Map ;
21
17
import java .util .Objects ;
22
18
import java .util .Optional ;
19
+ import java .util .Set ;
23
20
import java .util .function .Consumer ;
21
+ import java .util .regex .Pattern ;
22
+ import java .util .stream .Collectors ;
24
23
24
+ import org .apache .commons .io .IOUtils ;
25
25
import org .everit .json .schema .loader .SchemaLoader ;
26
26
import org .everit .json .schema .regexp .RE2JRegexpFactory ;
27
27
import org .json .JSONArray ;
34
34
import org .junit .runner .RunWith ;
35
35
import org .junit .runners .Parameterized ;
36
36
import org .junit .runners .Parameterized .Parameters ;
37
+ import org .reflections .Reflections ;
38
+ import org .reflections .scanners .ResourcesScanner ;
37
39
38
40
@ RunWith (Parameterized .class )
39
41
public class IssueTest {
40
42
41
43
@ Parameters (name = "{1}" )
42
44
public static List <Object []> params () {
43
45
List <Object []> rval = new ArrayList <>();
44
- try {
45
- File issuesDir = new File (
46
- IssueTest .class .getResource ("/org/everit/json/schema/issues" ).toURI ());
47
- for (File issue : issuesDir .listFiles ()) {
48
- rval .add (new Object [] { issue , issue .getName () });
49
- }
50
- } catch (URISyntaxException e ) {
51
- throw new RuntimeException (e );
46
+ Reflections refs = new Reflections ("org.everit.json.schema.issues" ,
47
+ new ResourcesScanner ());
48
+ Set <String > paths = refs .getResources (Pattern .compile ("schema.json" ))
49
+ .stream ().map (path -> path .substring (0 , path .lastIndexOf ('/' )))
50
+ .collect (Collectors .toSet ());
51
+ for (String path : paths ) {
52
+ rval .add (new Object [] { path , path .substring (path .lastIndexOf ('/' ) + 1 ) });
52
53
}
53
54
return rval ;
54
55
}
55
56
56
- private final File issueDir ;
57
+ private final String issueDir ;
58
+
59
+ private final String testCaseName ;
57
60
58
61
private JettyWrapper servletSupport ;
59
62
@@ -65,46 +68,44 @@ public static List<Object[]> params() {
65
68
66
69
private Validator .ValidatorBuilder validatorBuilder = Validator .builder ();
67
70
68
- public IssueTest (final File issueDir , final String ignored ) {
69
- this .issueDir = requireNonNull (issueDir , "issueDir cannot be null" );
71
+ public IssueTest (String issueDir , String testCaseName ) {
72
+ this .issueDir = "/" + requireNonNull (issueDir , "issueDir cannot be null" );
73
+ this .testCaseName = testCaseName ;
70
74
}
71
75
72
- private Optional <File > fileByName (final String fileName ) {
73
- return Arrays .stream (issueDir .listFiles ())
74
- .filter (file -> file .getName ().equals (fileName ))
75
- .findFirst ();
76
+ private Optional <InputStream > fileByName (final String fileName ) {
77
+ return Optional .ofNullable (getClass ().getResourceAsStream (issueDir + "/" + fileName ));
78
+ // return Arrays.stream(issueDir.listFiles())
79
+ // .filter(file -> file.getName().equals(fileName))
80
+ // .findFirst();
76
81
}
77
82
78
- private void initJetty (final File documentRoot ) {
83
+ private void initJetty () {
79
84
try {
80
- servletSupport = new JettyWrapper (documentRoot . getAbsolutePath () );
85
+ servletSupport = new JettyWrapper (issueDir + "/" + "remotes" );
81
86
servletSupport .start ();
82
87
} catch (Exception e ) {
83
88
throw new RuntimeException (e );
84
89
}
85
90
}
86
91
87
- private static JSONObject fileAsJson ( File file ) {
92
+ private static JSONObject streamAsJson ( InputStream file ) {
88
93
try {
89
- return new JSONObject (new JSONTokener (new FileInputStream (file )));
90
- } catch (FileNotFoundException e ) {
94
+ return new JSONObject (new JSONTokener (IOUtils . toString (file )));
95
+ } catch (java . io . IOException e ) {
91
96
throw new UncheckedIOException (e );
92
97
}
93
98
}
94
99
95
100
private Schema loadSchema () {
96
- Optional <File > schemaFile = fileByName ("schema.json" );
97
- try {
98
- if (schemaFile .isPresent ()) {
99
- JSONObject schemaObj = fileAsJson (schemaFile .get ());
100
- loaderBuilder = SchemaLoader .builder ().schemaJson (schemaObj );
101
- consumeValidatorConfig ();
102
- return loaderBuilder .build ().load ().build ();
103
- }
104
- throw new RuntimeException (issueDir .getCanonicalPath () + "/schema.json is not found" );
105
- } catch (IOException e ) {
106
- throw new UncheckedIOException (e );
101
+ Optional <InputStream > schemaFile = fileByName ("schema.json" );
102
+ if (schemaFile .isPresent ()) {
103
+ JSONObject schemaObj = streamAsJson (schemaFile .get ());
104
+ loaderBuilder = SchemaLoader .builder ().schemaJson (schemaObj );
105
+ consumeValidatorConfig ();
106
+ return loaderBuilder .build ().load ().build ();
107
107
}
108
+ throw new RuntimeException (issueDir + "/schema.json is not found" );
108
109
}
109
110
110
111
private void consumeValidatorConfig () {
@@ -138,7 +139,7 @@ private void consumeValidatorConfig() {
138
139
loaderBuilder .draftV7Support ();
139
140
}
140
141
});
141
- fileByName ("validator-config.json" ).map (file -> fileAsJson (file )).ifPresent (configJson -> {
142
+ fileByName ("validator-config.json" ).map (file -> streamAsJson (file )).ifPresent (configJson -> {
142
143
configKeyHandlers .entrySet ()
143
144
.stream ()
144
145
.filter (entry -> configJson .has (entry .getKey ()))
@@ -165,8 +166,8 @@ private void stopJetty() {
165
166
166
167
@ Test
167
168
public void test () {
168
- Assume .assumeFalse ("issue dir starts with 'x' - ignoring" , issueDir . getName () .startsWith ("x" ));
169
- fileByName ("remotes" ).ifPresent (this :: initJetty );
169
+ Assume .assumeFalse ("issue dir starts with 'x' - ignoring" , testCaseName .startsWith ("x" ));
170
+ fileByName ("remotes" ).ifPresent (unused -> initJetty () );
170
171
try {
171
172
Schema schema = loadSchema ();
172
173
fileByName ("subject-valid.json" ).ifPresent (file -> validate (file , schema , true ));
@@ -176,7 +177,7 @@ public void test() {
176
177
}
177
178
}
178
179
179
- private void validate (final File file , final Schema schema , final boolean shouldBeValid ) {
180
+ private void validate (InputStream file , Schema schema , boolean shouldBeValid ) {
180
181
ValidationException thrown = null ;
181
182
182
183
Object subject = loadJsonFile (file );
@@ -197,7 +198,7 @@ private void validate(final File file, final Schema schema, final boolean should
197
198
Assert .fail (failureBuilder .toString ());
198
199
}
199
200
if (!shouldBeValid && thrown != null ) {
200
- Optional <File > expectedFile = fileByName ("expectedException.json" );
201
+ Optional <InputStream > expectedFile = fileByName ("expectedException.json" );
201
202
if (expectedFile .isPresent ()) {
202
203
if (!checkExpectedValues (expectedFile .get (), thrown )) {
203
204
expectedFailureList .stream ()
@@ -220,12 +221,12 @@ private void validate(final File file, final Schema schema, final boolean should
220
221
221
222
// TODO - it would be nice to see this moved out of tests to the main
222
223
// source so that it can be used as a convenience method by users also...
223
- private Object loadJsonFile (final File file ) {
224
+ private Object loadJsonFile (InputStream file ) {
224
225
225
226
Object subject = null ;
226
227
227
228
try {
228
- JSONTokener jsonTok = new JSONTokener (new FileInputStream (file ));
229
+ JSONTokener jsonTok = new JSONTokener (IOUtils . toString (file ));
229
230
230
231
// Determine if we have a single JSON object or an array of them
231
232
Object jsonTest = jsonTok .nextValue ();
@@ -238,7 +239,7 @@ private Object loadJsonFile(final File file) {
238
239
}
239
240
} catch (JSONException e ) {
240
241
throw new RuntimeException ("failed to parse subject json file" , e );
241
- } catch (FileNotFoundException e ) {
242
+ } catch (IOException e ) {
242
243
throw new UncheckedIOException (e );
243
244
}
244
245
return subject ;
@@ -255,8 +256,8 @@ private Object loadJsonFile(final File file) {
255
256
* The expected contents are then compared against the actual validation failures reported in the
256
257
* ValidationException and nested causingExceptions.
257
258
*/
258
- private boolean checkExpectedValues (final File expectedExceptionsFile ,
259
- final ValidationException ve ) {
259
+ private boolean checkExpectedValues (InputStream expectedExceptionsFile ,
260
+ ValidationException ve ) {
260
261
// Read the expected values from user supplied file
261
262
Object expected = loadJsonFile (expectedExceptionsFile );
262
263
expectedFailureList = new ArrayList <String >();
@@ -297,8 +298,8 @@ private void readExpectedValues(final JSONObject expected) {
297
298
expectedFailureList .add ((String ) expected .get ("message" ));
298
299
if (expected .has ("causingExceptions" )) {
299
300
JSONArray causingEx = expected .getJSONArray ("causingExceptions" );
300
- for (Object subJson : causingEx ) {
301
- readExpectedValues (( JSONObject ) subJson );
301
+ for (int i = 0 ; i < causingEx . length (); ++ i ) {
302
+ readExpectedValues (causingEx . getJSONObject ( i ) );
302
303
}
303
304
}
304
305
}
0 commit comments