Explorar o código

Move testing of the content of a sub-matcher to a test case for the ValidationException

Sam Jaffe %!s(int64=6) %!d(string=hai) anos
pai
achega
1b3f0936a4

+ 4 - 4
src/main/lombok/org/leumasjaffe/json/schema/ValidationException.java

@@ -26,21 +26,21 @@ public class ValidationException extends IllegalArgumentException {
 	}
 
 	public ValidationException(String key, String message) {
-		this(Either.ofLeft(key), message, Collections.emptyList());
+		this(key, message, Collections.emptyList());
 	}
 	
 	public ValidationException(int index, String message) {
-		this(Either.ofRight(index), message, Collections.emptyList());
+		this(index, message, Collections.emptyList());
 	}
 
 	public ValidationException(String key, String message,
 			ValidationException... causes) {
-		this(Either.ofLeft(key), message, Arrays.asList(causes));
+		this(key, message, Arrays.asList(causes));
 	}
 
 	public ValidationException(int index, String message,
 			ValidationException... causes) {
-		this(Either.ofRight(index), message, Arrays.asList(causes));
+		this(index, message, Arrays.asList(causes));
 	}
 
 	public ValidationException(String key, String message,

+ 2 - 1
src/test/java/org/leumasjaffe/json/schema/JsonSchemaSuite.java

@@ -7,7 +7,8 @@ import org.junit.runners.Suite;
 @Suite.SuiteClasses({
 	JsonFactorySuite.class,
 	JsonTesterSuite.class,
-	SchemaTest.class
+	SchemaTest.class,
+	ValidationExceptionTest.class
 })
 public class JsonSchemaSuite {
 

+ 34 - 0
src/test/java/org/leumasjaffe/json/schema/ValidationExceptionTest.java

@@ -0,0 +1,34 @@
+package org.leumasjaffe.json.schema;
+
+import static org.hamcrest.collection.IsIterableContainingInOrder.contains;
+import static org.junit.Assert.assertThat;
+import static org.leumasjaffe.json.schema.matcher.CausingExceptions.causedBy;
+import static org.leumasjaffe.json.schema.matcher.JsonPath.jsonPath;
+
+import org.junit.Test;
+
+public class ValidationExceptionTest {
+
+	@Test
+	public void testPathComesFromKey() {
+		assertThat(new ValidationException("test", ""), jsonPath("#/test"));
+		assertThat(new ValidationException(5, ""), jsonPath("#/5"));
+	}
+	
+	@Test @SuppressWarnings("unchecked")
+	public void causedExceptionsPrependParentPathForKey() {
+		ValidationException ex = new ValidationException("test", "", 
+				new ValidationException(0, ""),
+				new ValidationException(1, ""));
+		assertThat(ex, causedBy(contains(jsonPath("#/test/0"), jsonPath("#/test/1"))));
+	}
+	
+	@Test @SuppressWarnings("unchecked")
+	public void causedExceptionsPrependParentPathForIndex() {
+		ValidationException ex = new ValidationException(0, "", 
+				new ValidationException("B", ""),
+				new ValidationException("A", ""));
+		assertThat(ex, causedBy(contains(jsonPath("#/0/B"), jsonPath("#/0/A"))));
+	}
+
+}

+ 1 - 5
src/test/java/org/leumasjaffe/json/schema/tester/PropertyNameTesterTest.java

@@ -1,9 +1,6 @@
 package org.leumasjaffe.json.schema.tester;
 
-import static org.hamcrest.Matchers.allOf;
-import static org.hamcrest.Matchers.startsWith;
 import static org.hamcrest.collection.IsIterableWithSize.iterableWithSize;
-import static org.hamcrest.core.Every.everyItem;
 import static org.junit.Assert.assertThat;
 import static org.leumasjaffe.json.schema.matcher.Accepts.accepts;
 import static org.leumasjaffe.json.schema.matcher.CausingExceptions.causedBy;
@@ -81,8 +78,7 @@ public class PropertyNameTesterTest {
 		node.set("AB", NullNode.getInstance());
 		node.set("BC", NullNode.getInstance());
 		thrown.expect(ValidationException.class);
-		thrown.expect(causedBy(allOf(iterableWithSize(2),
-				everyItem(jsonPath(startsWith("#/propertyNames/"))))));
+		thrown.expect(causedBy(iterableWithSize(2)));
 		singleChar.validate(node);
 	}