Skip to content

Commit 8a7e642

Browse files
committed
added IssueTest and IssueServlet to serve as a simple way of reproducing bugs
1 parent c699587 commit 8a7e642

File tree

7 files changed

+233
-26
lines changed

7 files changed

+233
-26
lines changed
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
* Copyright (C) 2011 Everit Kft. (http://www.everit.org)
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.everit.json.schema;
17+
18+
import java.io.BufferedReader;
19+
import java.io.File;
20+
import java.io.FileInputStream;
21+
import java.io.IOException;
22+
import java.io.InputStreamReader;
23+
import java.util.Arrays;
24+
import java.util.Objects;
25+
26+
import javax.servlet.ServletException;
27+
import javax.servlet.http.HttpServlet;
28+
import javax.servlet.http.HttpServletRequest;
29+
import javax.servlet.http.HttpServletResponse;
30+
31+
public class IssueServlet extends HttpServlet {
32+
private static final long serialVersionUID = -951266179406031349L;
33+
34+
private final File documentRoot;
35+
36+
public IssueServlet(final File documentRoot) {
37+
this.documentRoot = Objects.requireNonNull(documentRoot, "documentRoot cannot be null");
38+
}
39+
40+
@Override
41+
protected void doGet(final HttpServletRequest req, final HttpServletResponse resp)
42+
throws ServletException, IOException {
43+
File content = fileByPath(req.getPathInfo());
44+
resp.setContentType("application/json");
45+
try (
46+
BufferedReader bis = new BufferedReader(
47+
new InputStreamReader(new FileInputStream(content)));) {
48+
String line;
49+
while ((line = bis.readLine()) != null) {
50+
resp.getWriter().write(line);
51+
}
52+
}
53+
}
54+
55+
private File fileByPath(final String pathInfo) {
56+
File rval = documentRoot;
57+
if (pathInfo != null && !pathInfo.equals("/") && !pathInfo.isEmpty()) {
58+
String[] segments = pathInfo.trim().split("/");
59+
for (String fileName : segments) {
60+
if (fileName.isEmpty()) {
61+
continue;
62+
}
63+
rval = Arrays.stream(rval.listFiles())
64+
.filter(file -> file.getName().equals(fileName))
65+
.findFirst()
66+
.orElseThrow(() -> new RuntimeException("file [" + pathInfo + "] not found"));
67+
}
68+
}
69+
return rval;
70+
}
71+
72+
}
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
/*
2+
* Copyright (C) 2011 Everit Kft. (http://www.everit.org)
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.everit.json.schema;
17+
18+
import java.io.File;
19+
import java.io.FileInputStream;
20+
import java.io.FileNotFoundException;
21+
import java.io.IOException;
22+
import java.io.UncheckedIOException;
23+
import java.net.URISyntaxException;
24+
import java.util.ArrayList;
25+
import java.util.Arrays;
26+
import java.util.List;
27+
import java.util.Objects;
28+
import java.util.Optional;
29+
30+
import org.eclipse.jetty.server.Server;
31+
import org.eclipse.jetty.servlet.ServletHandler;
32+
import org.eclipse.jetty.servlet.ServletHolder;
33+
import org.everit.json.schema.loader.SchemaLoader;
34+
import org.json.JSONException;
35+
import org.json.JSONObject;
36+
import org.json.JSONTokener;
37+
import org.junit.Assert;
38+
import org.junit.Test;
39+
import org.junit.runner.RunWith;
40+
import org.junit.runners.Parameterized;
41+
import org.junit.runners.Parameterized.Parameters;
42+
43+
@RunWith(Parameterized.class)
44+
public class IssueTest {
45+
46+
@Parameters
47+
public static List<Object[]> params() {
48+
List<Object[]> rval = new ArrayList<>();
49+
try {
50+
File issuesDir = new File(
51+
IssueTest.class.getResource("/org/everit/json/schema/issues").toURI());
52+
for (File issue : issuesDir.listFiles()) {
53+
rval.add(new Object[] { issue });
54+
}
55+
} catch (URISyntaxException e) {
56+
throw new RuntimeException(e);
57+
}
58+
return rval;
59+
}
60+
61+
private final File issueDir;
62+
63+
private Server server;
64+
65+
public IssueTest(final File issueDir) {
66+
this.issueDir = Objects.requireNonNull(issueDir, "issueDir cannot be null");
67+
}
68+
69+
private Optional<File> fileByName(final String fileName) {
70+
return Arrays.stream(issueDir.listFiles())
71+
.filter(file -> file.getName().equals(fileName))
72+
.findFirst();
73+
}
74+
75+
private void initJetty(final File documentRoot) {
76+
server = new Server(1234);
77+
ServletHandler handler = new ServletHandler();
78+
server.setHandler(handler);
79+
handler.addServletWithMapping(new ServletHolder(new IssueServlet(documentRoot)), "/*");
80+
try {
81+
server.start();
82+
} catch (Exception e) {
83+
throw new RuntimeException(e);
84+
}
85+
}
86+
87+
private Schema loadSchema() {
88+
Optional<File> schemaFile = fileByName("schema.json");
89+
try {
90+
if (schemaFile.isPresent()) {
91+
JSONObject schemaObj = new JSONObject(
92+
new JSONTokener(new FileInputStream(schemaFile.get())));
93+
return SchemaLoader.load(schemaObj);
94+
}
95+
throw new RuntimeException(issueDir.getCanonicalPath() + "/schema.json is not found");
96+
} catch (IOException e) {
97+
throw new UncheckedIOException(e);
98+
}
99+
}
100+
101+
private void stopJetty() {
102+
if (server != null) {
103+
try {
104+
server.stop();
105+
} catch (Exception e) {
106+
throw new RuntimeException(e);
107+
}
108+
}
109+
server = null;
110+
}
111+
112+
@Test
113+
public void test() {
114+
fileByName("remotes").ifPresent(this::initJetty);
115+
Schema schema = loadSchema();
116+
fileByName("subject-valid.json").ifPresent(file -> validate(file, schema, true));
117+
fileByName("subject-invalid.json").ifPresent(file -> validate(file, schema, false));
118+
stopJetty();
119+
}
120+
121+
private void validate(final File file, final Schema schema, final boolean shouldBeValid) {
122+
ValidationException thrown = null;
123+
try {
124+
JSONObject subject = new JSONObject(new JSONTokener(new FileInputStream(file)));
125+
try {
126+
schema.validate(subject);
127+
} catch (ValidationException e) {
128+
thrown = e;
129+
}
130+
} catch (JSONException e) {
131+
throw new RuntimeException("failed to parse subject json file", e);
132+
} catch (FileNotFoundException e) {
133+
throw new UncheckedIOException(e);
134+
}
135+
if (shouldBeValid && thrown != null) {
136+
Assert.fail("validation failed with: " + thrown);
137+
}
138+
if (!shouldBeValid && thrown == null) {
139+
Assert.fail("did not throw ValidationException for invalid subject");
140+
}
141+
}
142+
}

tests/src/test/resources/org/everit/json/schema/draft4/yaala-child.json

Lines changed: 0 additions & 26 deletions
This file was deleted.
File renamed without changes.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"id":"http://localhost:1234/yaala/_schemas/child#",
3+
"allOf":[
4+
{ "$ref":"parent" },
5+
{
6+
"required":[ "s" ],
7+
"type":"object",
8+
"properties":{ "s":{ "type":"string" } }
9+
}
10+
]
11+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"n":"1",
3+
"s": "test"
4+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"n":1,
3+
"s": "test"
4+
}

0 commit comments

Comments
 (0)