JsonTypeMismatchException.java

1
package com.github.dakusui.json;
2
3
import com.github.dakusui.json.JsonUtils.JsonTypes;
4
import com.google.gson.JsonElement;
5
6
import java.io.Serial;
7
import java.util.Arrays;
8
9
import static com.github.dakusui.json.JsonUtils.summarizeJsonElement;
10
11
/**
12
 * An exception thrown when a Json element is found on a certain path has an
13
 * invalid type.
14
 * 
15
 * @author hiroshi
16
 */
17
public class JsonTypeMismatchException extends JsonException {
18
	/**
19
	 * A serial version UID string.
20
	 */
21
	@Serial
22
	private static final long serialVersionUID = -4922304198740292631L;
23
24
	/**
25
	 * An array of strings that describe types or values that are allowed on the path
26
	 */
27
	private final JsonUtils.JsonTypes[] expectedTypes;
28
29
	/**
30
	 * A string that describes the reason why the element is considered invalid.
31
	 */
32
	private final String reason;
33
34
	/**
35
	 * Creates an object of this class.
36
	 * 
37
	 * @param elem A JSON element whose value is found invalid.
38
	 * @param expectedTypes Strings which describe expected values.
39
	 */
40
	JsonTypeMismatchException(JsonElement elem, JsonUtils.JsonTypes... expectedTypes) {
41
		this(elem, null, expectedTypes);
42
	}
43
44
	/**
45
	 * Creates an object of this class.
46
	 * 
47
	 * @param elem A JSON element whose value is found invalid.
48
	 * @param reason A string that describes the reason why <code>elem</code> was considered invalid.
49
	 */
50
	public JsonTypeMismatchException(JsonElement elem, String reason) {
51
		this(elem, reason, new JsonTypes[] {});
52
	}
53
54
	/**
55
	 * Creates an object of this class.
56
	 * 
57
	 * @param elem A JSON element whose value is found invalid.
58
	 * @param expectedTypes Strings which describe expected values.
59
	 * @param reason A string that describes the reason why <code>elem</code> was considered invalid.
60
	 */
61
	JsonTypeMismatchException(JsonElement elem, String reason, JsonUtils.JsonTypes... expectedTypes) {
62
		super(elem);
63
		this.expectedTypes = expectedTypes;
64
		this.reason = reason;
65
	}
66
67
	/*
68
	 * Formats an message.
69
	 */
70
	private static String formatMessage(String reason, JsonUtils.JsonTypes[] types, JsonElement actualJSON) {
71
		String ret = null;
72
		String r = "";
73 1 1. formatMessage : negated conditional → NO_COVERAGE
		if (reason != null) {
74
			r = String.format("(%s)", reason);
75
		}
76 2 1. formatMessage : negated conditional → NO_COVERAGE
2. formatMessage : negated conditional → NO_COVERAGE
		if (types == null || types.length == 0) {
77
			ret = String.format("%s is not allowed here %s.", summarizeJsonElement(actualJSON), r);
78
		} else {
79
			ret = String.format("%s is not allowed here %s. Acceptable type(s) are %s", summarizeJsonElement(actualJSON), r, Arrays.toString(types));
80
		}
81 1 1. formatMessage : replaced return value with "" for com/github/dakusui/json/JsonTypeMismatchException::formatMessage → NO_COVERAGE
		return ret;
82
	}
83
84
	/**
85
	 * Returns a formatted message.
86
	 */
87
	@Override
88
	public String getMessage() {
89 1 1. getMessage : replaced return value with "" for com/github/dakusui/json/JsonTypeMismatchException::getMessage → NO_COVERAGE
		return formatMessage(this.reason, this.expectedTypes, getProblemCausingNode());
90
	}
91
}

Mutations

73

1.1
Location : formatMessage
Killed by : none
negated conditional → NO_COVERAGE

76

1.1
Location : formatMessage
Killed by : none
negated conditional → NO_COVERAGE

2.2
Location : formatMessage
Killed by : none
negated conditional → NO_COVERAGE

81

1.1
Location : formatMessage
Killed by : none
replaced return value with "" for com/github/dakusui/json/JsonTypeMismatchException::formatMessage → NO_COVERAGE

89

1.1
Location : getMessage
Killed by : none
replaced return value with "" for com/github/dakusui/json/JsonTypeMismatchException::getMessage → NO_COVERAGE

Active mutators

Tests examined


Report generated by PIT 1.15.3