Bläddra i källkod

Add more error checking and schema remapping tests to SchemaFactoryTest.

Sam Jaffe 6 år sedan
förälder
incheckning
62c75ee362

+ 42 - 5
src/test/java/org/leumasjaffe/json/schema/factory/SchemaFactoryTest.java

@@ -16,6 +16,13 @@ public class SchemaFactoryTest {
 	SchemaFactory factory;
 	ObjectMapper mapper;
 	
+	private String getError(Throwable from) {
+		final StringWriter sw = new StringWriter();
+		final PrintWriter pw = new PrintWriter(sw);
+		from.printStackTrace(pw);
+		return sw.toString();
+	}
+	
 	@Before
 	public void setUp() {
 		factory = new SchemaFactory();
@@ -43,12 +50,42 @@ public class SchemaFactoryTest {
 	@Test
 	public void testCanRouteV6Schemas() {
 		try {
-			factory.create(readTree("{ \"$id\": \"unit-test-schema\", \"$schema\":\"http://json-schema.org/draft-06/schema#\" }"));
+			factory.create(readTree("{" +
+					"\"$id\": \"unit-test-schema\"," +
+					"\"$schema\":\"http://json-schema.org/draft-06/schema#\"" +
+				"}"));
+		} catch (Throwable t) {
+			fail(getError(t));
+		}
+	}
+	
+	@Test
+	public void testSchemaWithinSchemaDoesNotNeedRedeclare() {
+		try {
+			factory.create(readTree("{" +
+				"\"$id\": \"unit-test-schema\"," +
+				"\"$schema\":\"http://json-schema.org/draft-06/schema#\"," +
+				"\"not\": { \"type\": \"string\" }" +
+			"}"));
 		} catch (Throwable t) {
-			final StringWriter sw = new StringWriter();
-			final PrintWriter pw = new PrintWriter(sw);
-			t.printStackTrace(pw);
-			fail(sw.toString());
+			fail(getError(t));
 		}
 	}
+	
+	@Test
+	public void testSchemaWithinSchemaCanRedeclare() {
+		try {
+			factory.create(readTree("{" +
+				"\"$id\": \"unit-test-schema\"," +
+				"\"$schema\":\"http://json-schema.org/draft-06/schema#\"," +
+				"\"not\": {" +
+					"\"$schema\":\"http://json-schema.org/draft-06/schema#\"," +
+					"\"type\": \"string\"" +
+				"}" +
+			"}"));
+		} catch (Throwable t) {
+			fail(getError(t));
+		}
+	}
+	
 }