Quellcode durchsuchen

Change to using @Rule ExpectedExpression instead of `try { ...; fail(...); } catch { assertThat(...); }`

Sam Jaffe vor 6 Jahren
Ursprung
Commit
28866e417c

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

@@ -48,4 +48,8 @@ public class ValidationException extends IllegalArgumentException {
 		return path.toString();
 	}
 	
+	@Override
+	public String toString() {
+		return getPath() + ": " + getLocalizedMessage();
+	}
 }

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

@@ -0,0 +1,34 @@
+package org.leumasjaffe.json.schema.matcher;
+
+import org.hamcrest.Description;
+import org.hamcrest.Matcher;
+import org.hamcrest.TypeSafeMatcher;
+import org.leumasjaffe.json.schema.ValidationException;
+
+public class JsonPath extends TypeSafeMatcher<ValidationException> {
+	public static Matcher<ValidationException> jsonPath(final String path) {
+		return new JsonPath(path);
+	}
+	
+	private final String path;
+	
+	private JsonPath(final String path) {
+		this.path = path;
+	}
+
+	@Override
+	public void describeTo(Description arg0) {
+		arg0.appendText("path is ").appendValue(path);
+	}
+	
+	@Override
+	public void describeMismatchSafely(ValidationException arg0, Description arg1) {
+		arg1.appendText("path was ").appendValue(arg0.getPath());
+	}
+
+	@Override
+	protected boolean matchesSafely(ValidationException arg0) {
+		return arg0.getPath().equals(path);
+	}
+
+}

+ 16 - 15
src/test/java/org/leumasjaffe/json/schema/tester/EqualsTesterTest.java

@@ -1,13 +1,15 @@
 package org.leumasjaffe.json.schema.tester;
 
-import static org.hamcrest.core.Is.*;
 import static org.junit.Assert.assertThat;
 import static org.junit.Assert.fail;
 import static org.leumasjaffe.json.schema.matcher.Accepts.accepts;
+import static org.leumasjaffe.json.schema.matcher.JsonPath.jsonPath;
 import static org.leumasjaffe.json.schema.matcher.Not.not;
 
 import org.junit.Before;
+import org.junit.Rule;
 import org.junit.Test;
+import org.junit.rules.ExpectedException;
 import org.leumasjaffe.json.schema.Tester;
 import org.leumasjaffe.json.schema.ValidationException;
 
@@ -15,7 +17,8 @@ import com.fasterxml.jackson.databind.node.DoubleNode;
 import com.fasterxml.jackson.databind.node.TextNode;
 
 public class EqualsTesterTest {
-	Tester constTest, enumTest, mtEnumTest;
+    @Rule public ExpectedException thrown = ExpectedException.none();
+    Tester constTest, enumTest, mtEnumTest;
 	
 	@Before
 	public void setUp() {
@@ -64,19 +67,17 @@ public class EqualsTesterTest {
 	}
 	
 	@Test
-	public void testValidationPathIsConstOrEnum() {
-		try {
-			constTest.validate(new DoubleNode(1.0000000001));
-			fail("No exception was thrown...");
-		} catch (ValidationException ve) {
-			assertThat(ve.getPath(), is("#/const"));
-		}
-		try {
-			enumTest.validate(new DoubleNode(1.0000000001));
-			fail("No exception was thrown...");
-		} catch (ValidationException ve) {
-			assertThat(ve.getPath(), is("#/enum"));
-		}
+	public void testValidationPathIsConst() {
+		thrown.expect(ValidationException.class);
+		thrown.expect(jsonPath("#/const"));
+		constTest.validate(new DoubleNode(1.0000000001));
+	}
+	
+	@Test
+	public void testValidationPathIsEnum() {
+		thrown.expect(ValidationException.class);
+		thrown.expect(jsonPath("#/enum"));
+		enumTest.validate(new DoubleNode(1.0000000001));
 	}
 	
 }

+ 7 - 7
src/test/java/org/leumasjaffe/json/schema/tester/FormatTesterTest.java

@@ -1,13 +1,15 @@
 package org.leumasjaffe.json.schema.tester;
 
-import static org.hamcrest.core.Is.is;
 import static org.junit.Assert.assertThat;
 import static org.junit.Assert.fail;
 import static org.leumasjaffe.json.schema.matcher.Accepts.accepts;
+import static org.leumasjaffe.json.schema.matcher.JsonPath.jsonPath;
 import static org.leumasjaffe.json.schema.matcher.AcceptedTypes.acceptsTypes;
 import static org.leumasjaffe.json.schema.matcher.Not.not;
 
+import org.junit.Rule;
 import org.junit.Test;
+import org.junit.rules.ExpectedException;
 import org.leumasjaffe.json.schema.Tester;
 import org.leumasjaffe.json.schema.ValidationException;
 
@@ -16,6 +18,7 @@ import com.fasterxml.jackson.databind.node.NullNode;
 import com.fasterxml.jackson.databind.node.TextNode;
 
 public class FormatTesterTest {
+    @Rule public ExpectedException thrown = ExpectedException.none();
 
 	@Test(expected=IllegalArgumentException.class)
 	public void testThrowsForUnknownFormat() {
@@ -159,12 +162,9 @@ public class FormatTesterTest {
 	
 	@Test
 	public void testValidationPathIsNameOfMatcher() {
-		try {
-			FormatTester.forCode("ipv4").validate(new TextNode("::1"));
-			fail("No exception was thrown...");
-		} catch (ValidationException ve) {
-			assertThat(ve.getPath(), is("#/format"));
-		}
+		thrown.expect(ValidationException.class);
+		thrown.expect(jsonPath("#/format"));
+		FormatTester.forCode("ipv4").validate(new TextNode("::1"));
 	}
 	
 }

+ 19 - 31
src/test/java/org/leumasjaffe/json/schema/tester/NumberTesterTest.java

@@ -1,15 +1,17 @@
 package org.leumasjaffe.json.schema.tester;
 
-import static org.hamcrest.core.Is.is;
 import static org.junit.Assert.assertThat;
 import static org.junit.Assert.fail;
 import static org.leumasjaffe.json.schema.matcher.AcceptedTypes.acceptsTypes;
 import static org.leumasjaffe.json.schema.matcher.Accepts.accepts;
+import static org.leumasjaffe.json.schema.matcher.JsonPath.jsonPath;
 import static org.leumasjaffe.json.schema.matcher.Not.not;
 
 import java.util.function.DoublePredicate;
 
+import org.junit.Rule;
 import org.junit.Test;
+import org.junit.rules.ExpectedException;
 import org.leumasjaffe.json.schema.ValidationException;
 
 import com.fasterxml.jackson.databind.node.DoubleNode;
@@ -17,6 +19,7 @@ import com.fasterxml.jackson.databind.node.JsonNodeType;
 import com.fasterxml.jackson.databind.node.NullNode;
 
 public class NumberTesterTest {
+    @Rule public ExpectedException thrown = ExpectedException.none();
 	DoublePredicate nonZero = d -> d != 0;
 	
 	@Test
@@ -98,52 +101,37 @@ public class NumberTesterTest {
 	
 	@Test
 	public void testValidationPathIsMinimum() {
-		try {
-			NumberTester.minimum(1.0).validate(new DoubleNode(0.5));
-			fail("No exception was thrown...");
-		} catch (ValidationException ve) {
-			assertThat(ve.getPath(), is("#/minimum"));
-		}
+		thrown.expect(ValidationException.class);
+		thrown.expect(jsonPath("#/minimum"));
+		NumberTester.minimum(1.0).validate(new DoubleNode(0.5));
 	}
 	
 	@Test
 	public void testValidationPathIsExclusiveMinimum() {
-		try {
-			NumberTester.exclusiveMinimum(1.0).validate(new DoubleNode(0.5));
-			fail("No exception was thrown...");
-		} catch (ValidationException ve) {
-			assertThat(ve.getPath(), is("#/exclusiveMinimum"));
-		}
+		thrown.expect(ValidationException.class);
+		thrown.expect(jsonPath("#/exclusiveMinimum"));
+		NumberTester.exclusiveMinimum(1.0).validate(new DoubleNode(0.5));
 	}
 	
 	@Test
 	public void testValidationPathIsMaximum() {
-		try {
-			NumberTester.maximum(1.0).validate(new DoubleNode(1.5));
-			fail("No exception was thrown...");
-		} catch (ValidationException ve) {
-			assertThat(ve.getPath(), is("#/maximum"));
-		}
+		thrown.expect(ValidationException.class);
+		thrown.expect(jsonPath("#/maximum"));
+		NumberTester.maximum(1.0).validate(new DoubleNode(1.5));
 	}
 	
 	@Test
 	public void testValidationPathIsExclusiveMaximum() {
-		try {
-			NumberTester.exclusiveMaximum(1.0).validate(new DoubleNode(1.5));
-			fail("No exception was thrown...");
-		} catch (ValidationException ve) {
-			assertThat(ve.getPath(), is("#/exclusiveMaximum"));
-		}
+		thrown.expect(ValidationException.class);
+		thrown.expect(jsonPath("#/exclusiveMaximum"));
+		NumberTester.exclusiveMaximum(1.0).validate(new DoubleNode(1.5));
 	}
 	
 	@Test
 	public void testValidationPathIsMultipleOf() {
-		try {
-			NumberTester.multipleOf(1.0).validate(new DoubleNode(0.5));
-			fail("No exception was thrown...");
-		} catch (ValidationException ve) {
-			assertThat(ve.getPath(), is("#/multipleOf"));
-		}
+		thrown.expect(ValidationException.class);
+		thrown.expect(jsonPath("#/multipleOf"));
+		NumberTester.multipleOf(1.0).validate(new DoubleNode(0.5));
 	}
 	
 }

+ 7 - 7
src/test/java/org/leumasjaffe/json/schema/tester/TypeTesterTest.java

@@ -1,13 +1,15 @@
 package org.leumasjaffe.json.schema.tester;
 
-import static org.hamcrest.core.Is.is;
 import static org.junit.Assert.assertThat;
 import static org.junit.Assert.fail;
 import static org.leumasjaffe.json.schema.matcher.Accepts.accepts;
+import static org.leumasjaffe.json.schema.matcher.JsonPath.jsonPath;
 import static org.leumasjaffe.json.schema.matcher.AcceptedTypes.acceptsTypes;
 import static org.leumasjaffe.json.schema.matcher.Not.not;
 
+import org.junit.Rule;
 import org.junit.Test;
+import org.junit.rules.ExpectedException;
 import org.leumasjaffe.json.schema.Tester;
 import org.leumasjaffe.json.schema.ValidationException;
 import org.leumasjaffe.json.schema.tester.TypeTester;
@@ -23,6 +25,7 @@ import com.fasterxml.jackson.databind.node.ObjectNode;
 import com.fasterxml.jackson.databind.node.TextNode;
 
 public class TypeTesterTest {
+    @Rule public ExpectedException thrown = ExpectedException.none();
 	JsonNode jNull = NullNode.getInstance();
 	JsonNode bool = BooleanNode.TRUE;
 	JsonNode integral = new IntNode(5);
@@ -143,12 +146,9 @@ public class TypeTesterTest {
 	
 	@Test
 	public void testValidationPathIsType() {
-		try {
-			TypeTester.fromType("object").validate(new DoubleNode(0.5));
-			fail("No exception was thrown...");
-		} catch (ValidationException ve) {
-			assertThat(ve.getPath(), is("#/type"));
-		}
+		thrown.expect(ValidationException.class);
+		thrown.expect(jsonPath("#/type"));
+		TypeTester.fromType("object").validate(new DoubleNode(0.5));
 	}
 	
 }