Skip to content

Commit 2dcff16

Browse files
JooHyukKimcowtowncoder
authored andcommitted
Fix JavaDoc and test (#5408)
1 parent 3bbf6de commit 2dcff16

File tree

2 files changed

+58
-10
lines changed

2 files changed

+58
-10
lines changed

src/main/java/tools/jackson/databind/JsonNode.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1363,13 +1363,13 @@ public <T extends JsonNode> T requireNonNull() {
13631363
* JSON Object value) and has value for specified property with key {@code propertyName}
13641364
* (but note that value may be explicit JSON null value).
13651365
* If this node is Object Node and has value for specified property, matching value
1366-
* is returned; otherwise {@link IllegalArgumentException} is thrown.
1366+
* is returned; otherwise {@link JsonNodeException} is thrown.
13671367
*
13681368
* @param propertyName Name of property to access
13691369
*
13701370
* @return Value of the specified property of this Object node
13711371
*
1372-
* @throws IllegalArgumentException if this node is not an Object node or if it does not
1372+
* @throws JsonNodeException if this node is not an Object node or if it does not
13731373
* have value for specified property
13741374
*/
13751375
public abstract JsonNode required(String propertyName);
@@ -1383,13 +1383,13 @@ public <T extends JsonNode> T requireNonNull() {
13831383
* JSON Array value) and has value for specified {@code index}
13841384
* (but note that value may be explicit JSON null value).
13851385
* If this node is Array Node and has value for specified index, value at index
1386-
* is returned; otherwise {@link IllegalArgumentException} is thrown.
1386+
* is returned; otherwise {@link JsonNodeException} is thrown.
13871387
*
13881388
* @param index Index of the value of this Array node to access
13891389
*
13901390
* @return Value at specified index of this Array node
13911391
*
1392-
* @throws IllegalArgumentException if this node is not an Array node or if it does not
1392+
* @throws JsonNodeException if this node is not an Array node or if it does not
13931393
* have value for specified index
13941394
*/
13951395
public abstract JsonNode required(int index);
@@ -1403,13 +1403,13 @@ public <T extends JsonNode> T requireNonNull() {
14031403
* starting from {@code this} node
14041404
* (but note that value may be explicit JSON null value).
14051405
* If such value node exists it is returned;
1406-
* otherwise {@link IllegalArgumentException} is thrown.
1406+
* otherwise {@link JsonNodeException} is thrown.
14071407
*
14081408
* @param pathExpr {@link JsonPointer} expression (as String) to use for finding value node
14091409
*
14101410
* @return Matching value node for given expression
14111411
*
1412-
* @throws IllegalArgumentException if no value node exists at given {@code JSON Pointer} path
1412+
* @throws JsonNodeException if no value node exists at given {@code JSON Pointer} path
14131413
*/
14141414
public JsonNode requiredAt(String pathExpr) {
14151415
return requiredAt(JsonPointer.compile(pathExpr));
@@ -1424,13 +1424,13 @@ public JsonNode requiredAt(String pathExpr) {
14241424
* starting from {@code this} node
14251425
* (but note that value may be explicit JSON null value).
14261426
* If such value node exists it is returned;
1427-
* otherwise {@link IllegalArgumentException} is thrown.
1427+
* otherwise {@link JsonNodeException} is thrown.
14281428
*
14291429
* @param path {@link JsonPointer} expression to use for finding value node
14301430
*
14311431
* @return Matching value node for given expression
14321432
*
1433-
* @throws IllegalArgumentException if no value node exists at given {@code JSON Pointer} path
1433+
* @throws JsonNodeException if no value node exists at given {@code JSON Pointer} path
14341434
*/
14351435
public final JsonNode requiredAt(final JsonPointer path) {
14361436
JsonPointer currentExpr = path;

src/test/java/tools/jackson/databind/node/RequiredAccessorTest.java

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package tools.jackson.databind.node;
22

3+
import org.junit.jupiter.api.Assertions;
34
import org.junit.jupiter.api.Test;
45

56
import tools.jackson.core.JsonPointer;
67
import tools.jackson.databind.*;
8+
import tools.jackson.databind.exc.JsonNodeException;
79
import tools.jackson.databind.testutil.DatabindTestUtil;
810

911
import static org.junit.jupiter.api.Assertions.*;
@@ -97,21 +99,67 @@ public void testSimpleRequireOk() throws Exception {
9799
}
98100
}
99101

102+
@Test
103+
public void testSimpleRequireAtFailure() throws Exception {
104+
try {
105+
TEST_OBJECT.requiredAt("/some-random-path");
106+
fail("Should not pass");
107+
} catch (JsonNodeException e) {
108+
verifyException(e, "No node at");
109+
}
110+
try {
111+
TEST_ARRAY.requiredAt("/some-random-path");
112+
fail("Should not pass");
113+
} catch (JsonNodeException e) {
114+
verifyException(e, "No node at");
115+
}
116+
try {
117+
TEST_OBJECT.requiredAt(JsonPointer.compile("/some-random-path"));
118+
fail("Should not pass");
119+
} catch (JsonNodeException e) {
120+
verifyException(e, "No node at");
121+
}
122+
try {
123+
TEST_ARRAY.requiredAt(JsonPointer.compile("/some-random-path"));
124+
fail("Should not pass");
125+
} catch (JsonNodeException e) {
126+
verifyException(e, "No node at");
127+
}
128+
}
129+
100130
@Test
101131
public void testSimpleRequireFail() throws Exception {
132+
// required(String)
102133
try {
103134
TEST_OBJECT.required("bogus");
104135
fail("Should not pass");
105-
} catch (DatabindException e) {
136+
} catch (JsonNodeException e) {
106137
verifyException(e, "No value for property 'bogus'");
107138
}
108139

140+
// required(String)
109141
try {
110142
TEST_ARRAY.required("bogus");
111143
fail("Should not pass");
112-
} catch (DatabindException e) {
144+
} catch (JsonNodeException e) {
113145
verifyException(e, "Node of type `tools.jackson.databind.node.ArrayNode` has no fields");
114146
}
147+
148+
// required(int)
149+
try {
150+
TEST_OBJECT.required(-1);
151+
fail("Should not pass");
152+
} catch (JsonNodeException e) {
153+
verifyException(e, "has no indexed values");
154+
}
155+
156+
// required(int)
157+
try {
158+
TEST_ARRAY.required(-1);
159+
fail("Should not pass");
160+
} catch (JsonNodeException e) {
161+
verifyException(e, "No value at index #");
162+
}
115163
}
116164

117165
// [databind#3005]

0 commit comments

Comments
 (0)