Skip to content

Commit 1ac33c6

Browse files
committed
testing cleanup
1 parent 4f3ba7a commit 1ac33c6

File tree

9 files changed

+159
-71
lines changed

9 files changed

+159
-71
lines changed

core/src/main/java/org/everit/json/schema/loader/SchemaLoader.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -539,14 +539,16 @@ private Schema.Builder<?> lookupReference(final String relPointerString, final J
539539
if (pointerSchemas.containsKey(absPointerString)) {
540540
return pointerSchemas.get(absPointerString);
541541
}
542-
JSONPointer pointer = absPointerString.startsWith("#")
543-
? JSONPointer.forDocument(rootSchemaJson, absPointerString)
544-
: JSONPointer.forURL(httpClient, absPointerString);
542+
boolean isExternal = !absPointerString.startsWith("#");
543+
JSONPointer pointer = isExternal
544+
? JSONPointer.forURL(httpClient, absPointerString)
545+
: JSONPointer.forDocument(rootSchemaJson, absPointerString);
545546
ReferenceSchema.Builder refBuilder = ReferenceSchema.builder();
546547
pointerSchemas.put(absPointerString, refBuilder);
547548
QueryResult result = pointer.query();
548549
JSONObject resultObject = extend(withoutRef(ctx), result.getQueryResult());
549-
SchemaLoader childLoader = selfBuilder().schemaJson(resultObject)
550+
SchemaLoader childLoader = selfBuilder().resolutionScope(isExternal ? null : id)
551+
.schemaJson(resultObject)
550552
.rootSchemaJson(result.getContainingDocument()).build();
551553
Schema referredSchema = childLoader.load().build();
552554
refBuilder.build().setReferredSchema(referredSchema);

tests/src/test/java/org/everit/json/schema/IssueServlet.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ public IssueServlet(final File documentRoot) {
4040
@Override
4141
protected void doGet(final HttpServletRequest req, final HttpServletResponse resp)
4242
throws ServletException, IOException {
43+
System.out.println("GET " + req.getPathInfo());
4344
File content = fileByPath(req.getPathInfo());
4445
resp.setContentType("application/json");
4546
try (

tests/src/test/java/org/everit/json/schema/IssueTest.java

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@
2828
import java.util.Optional;
2929

3030
import org.eclipse.jetty.server.Server;
31-
import org.eclipse.jetty.servlet.ServletHandler;
32-
import org.eclipse.jetty.servlet.ServletHolder;
3331
import org.everit.json.schema.loader.SchemaLoader;
3432
import org.json.JSONException;
3533
import org.json.JSONObject;
@@ -62,6 +60,8 @@ public static List<Object[]> params() {
6260

6361
private Server server;
6462

63+
private ServletSupport servletSupport;
64+
6565
public IssueTest(final File issueDir, final String ignored) {
6666
this.issueDir = Objects.requireNonNull(issueDir, "issueDir cannot be null");
6767
}
@@ -73,15 +73,8 @@ private Optional<File> fileByName(final String fileName) {
7373
}
7474

7575
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-
}
76+
servletSupport = new ServletSupport(documentRoot);
77+
servletSupport.initJetty();
8578
}
8679

8780
private Schema loadSchema() {
@@ -99,14 +92,9 @@ private Schema loadSchema() {
9992
}
10093

10194
private void stopJetty() {
102-
if (server != null) {
103-
try {
104-
server.stop();
105-
} catch (Exception e) {
106-
throw new RuntimeException(e);
107-
}
95+
if (servletSupport != null) {
96+
servletSupport.stopJetty();
10897
}
109-
server = null;
11098
}
11199

112100
@Test
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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.net.URISyntaxException;
19+
20+
import org.eclipse.jetty.server.Server;
21+
import org.everit.json.schema.loader.SchemaLoader;
22+
import org.json.JSONObject;
23+
import org.json.JSONTokener;
24+
import org.junit.Test;
25+
26+
public class RelativeURITest {
27+
28+
private Server server;
29+
30+
@Test
31+
public void test() throws URISyntaxException {
32+
ServletSupport.withDocumentRoot("/org/everit/json/schema/relative-uri/")
33+
.run(this::run);
34+
}
35+
36+
private void run() {
37+
SchemaLoader
38+
.builder()
39+
.resolutionScope("http://localhost:1234/schema/")
40+
.schemaJson(
41+
new JSONObject(new JSONTokener(getClass().getResourceAsStream(
42+
"/org/everit/json/schema/relative-uri/schema/main.json"))))
43+
.build().load().build();
44+
}
45+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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.net.URISyntaxException;
20+
import java.util.Objects;
21+
22+
import org.eclipse.jetty.server.Server;
23+
import org.eclipse.jetty.servlet.ServletHandler;
24+
import org.eclipse.jetty.servlet.ServletHolder;
25+
26+
public class ServletSupport {
27+
28+
public static ServletSupport withDocumentRoot(final String path) {
29+
try {
30+
return withDocumentRoot(new File(ServletSupport.class.getResource(path).toURI()));
31+
} catch (URISyntaxException e) {
32+
throw new RuntimeException(e);
33+
}
34+
}
35+
36+
public static ServletSupport withDocumentRoot(final File documentRoot) {
37+
return new ServletSupport(documentRoot);
38+
}
39+
40+
private final File documentRoot;
41+
42+
public ServletSupport(final File documentRoot) {
43+
this.documentRoot = Objects.requireNonNull(documentRoot, "documentRoot cannot be null");
44+
}
45+
46+
public void run(final Runnable runnable) {
47+
initJetty();
48+
runnable.run();
49+
stopJetty();
50+
}
51+
52+
private Server server;
53+
54+
public void initJetty() {
55+
server = new Server(1234);
56+
ServletHandler handler = new ServletHandler();
57+
server.setHandler(handler);
58+
handler.addServletWithMapping(new ServletHolder(new IssueServlet(documentRoot)), "/*");
59+
try {
60+
server.start();
61+
} catch (Exception e) {
62+
throw new RuntimeException(e);
63+
}
64+
}
65+
66+
public void stopJetty() {
67+
if (server != null) {
68+
try {
69+
server.stop();
70+
} catch (Exception e) {
71+
throw new RuntimeException(e);
72+
}
73+
}
74+
server = null;
75+
}
76+
77+
}

tests/src/test/java/org/everit/json/schema/TestSuiteTest.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package org.everit.json.schema;
1717

18+
import java.io.File;
1819
import java.io.InputStream;
1920
import java.util.ArrayList;
2021
import java.util.List;
@@ -23,6 +24,7 @@
2324

2425
import org.eclipse.jetty.server.Server;
2526
import org.eclipse.jetty.servlet.ServletHandler;
27+
import org.eclipse.jetty.servlet.ServletHolder;
2628
import org.everit.json.schema.loader.SchemaLoader;
2729
import org.json.JSONArray;
2830
import org.json.JSONException;
@@ -81,7 +83,8 @@ public static void startJetty() throws Exception {
8183
server = new Server(1234);
8284
ServletHandler handler = new ServletHandler();
8385
server.setHandler(handler);
84-
handler.addServletWithMapping(TestSuiteTestServlet.class, "/*");
86+
handler.addServletWithMapping(new ServletHolder(new IssueServlet(new File(ServletSupport.class
87+
.getResource("/org/everit/json/schema/draft4/remotes").toURI()))), "/*");
8588
server.start();
8689
}
8790

@@ -130,5 +133,4 @@ public void test() {
130133
throw new AssertionError("schema loading error for " + schemaDescription, e);
131134
}
132135
}
133-
134136
}

tests/src/test/java/org/everit/json/schema/TestSuiteTestServlet.java

Lines changed: 0 additions & 47 deletions
This file was deleted.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"type" : "object",
3+
"properties" : {
4+
"rect" : { "$ref" : "props.json#/definitions/Rectangle" }
5+
}
6+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"definitions" : {
3+
"Rectangle" : {
4+
"type" : "object",
5+
"properties" : {
6+
"a" : { "$ref" : "#/definitions/Size" },
7+
"b" : { "$ref" : "#/definitions/Size" }
8+
}
9+
},
10+
"Size" : {
11+
"type" : "number"
12+
}
13+
}
14+
}

0 commit comments

Comments
 (0)