Skip to content

Commit 986223f

Browse files
committed
Add JsonLogicException tests
1 parent 7c2e7ac commit 986223f

File tree

1 file changed

+107
-0
lines changed

1 file changed

+107
-0
lines changed
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
package io.github.jamsesso.jsonlogic;
2+
3+
import org.junit.Test;
4+
import org.junit.Assert;
5+
6+
import static org.junit.Assert.*;
7+
8+
public class JsonLogicExceptionTests {
9+
10+
@Test
11+
public void testExceptionWithoutJsonPath() {
12+
JsonLogicException exception = new JsonLogicException("Test message");
13+
assertEquals("Test message", exception.getMessage());
14+
assertEquals("", exception.getJsonPath());
15+
}
16+
17+
@Test
18+
public void testExceptionWithJsonPath() {
19+
JsonLogicException exception = new JsonLogicException("Test message", "$.rules[0]");
20+
assertEquals("Test message", exception.getMessage());
21+
assertEquals("$.rules[0]", exception.getJsonPath());
22+
}
23+
24+
@Test
25+
public void testExceptionWithCauseAndJsonPath() {
26+
Throwable cause = new IllegalArgumentException("Cause message");
27+
JsonLogicException exception = new JsonLogicException(cause, "$.data.field");
28+
assertEquals("java.lang.IllegalArgumentException: Cause message", exception.getMessage());
29+
assertEquals("$.data.field", exception.getJsonPath());
30+
assertSame(cause, exception.getCause());
31+
}
32+
33+
@Test
34+
public void testExceptionWithMessageCauseAndJsonPath() {
35+
Throwable cause = new IllegalArgumentException("Cause message");
36+
JsonLogicException exception = new JsonLogicException("Custom message", cause, "$.config");
37+
assertEquals("Custom message", exception.getMessage());
38+
assertEquals("$.config", exception.getJsonPath());
39+
assertSame(cause, exception.getCause());
40+
}
41+
42+
@Test
43+
public void testPrependPartialJsonPath() {
44+
JsonLogicException exception = new JsonLogicException("Test message");
45+
exception.prependPartialJsonPath(".field1");
46+
assertEquals(".field1", exception.getJsonPath());
47+
}
48+
49+
@Test
50+
public void testMultiplePrependsJsonPath() {
51+
JsonLogicException exception = new JsonLogicException("Test message");
52+
exception.prependPartialJsonPath(".field2");
53+
exception.prependPartialJsonPath("[1]");
54+
exception.prependPartialJsonPath("$.array");
55+
assertEquals("$.array[1].field2", exception.getJsonPath());
56+
}
57+
58+
@Test
59+
public void testPrependPartialJsonPathWithNullValue() {
60+
JsonLogicException exception = new JsonLogicException("Test message", "$.original");
61+
exception.prependPartialJsonPath(null);
62+
// Path should remain unchanged when prepending null
63+
assertEquals("$.original", exception.getJsonPath());
64+
}
65+
66+
@Test
67+
public void testExceptionWithNullJsonPath() {
68+
JsonLogicException exception = new JsonLogicException("Test message", (String) null);
69+
assertEquals("Test message", exception.getMessage());
70+
assertEquals("", exception.getJsonPath());
71+
}
72+
73+
@Test
74+
public void testComplexJsonPathConstruction() {
75+
JsonLogicException exception = new JsonLogicException("Validation error");
76+
exception.prependPartialJsonPath(".condition");
77+
exception.prependPartialJsonPath("[2]");
78+
exception.prependPartialJsonPath(".rules");
79+
exception.prependPartialJsonPath("$");
80+
assertEquals("$.rules[2].condition", exception.getJsonPath());
81+
}
82+
83+
@Test
84+
public void testExceptionWithCauseOnly() {
85+
Throwable cause = new RuntimeException("Root cause");
86+
JsonLogicException exception = new JsonLogicException(cause);
87+
assertEquals("java.lang.RuntimeException: Root cause", exception.getMessage());
88+
assertEquals("", exception.getJsonPath());
89+
assertSame(cause, exception.getCause());
90+
}
91+
92+
@Test
93+
public void testJsonPathPreservesInsertionOrder() {
94+
JsonLogicException exception = new JsonLogicException("Test error", "end");
95+
exception.prependPartialJsonPath("middle");
96+
exception.prependPartialJsonPath("start");
97+
assertEquals("startmiddleend", exception.getJsonPath());
98+
}
99+
100+
@Test
101+
public void testEmptyJsonPathInitially() {
102+
JsonLogicException exception = new JsonLogicException("Test message");
103+
assertNotNull("JsonPath should not be null", exception.getJsonPath());
104+
assertEquals("JsonPath should be empty initially", "", exception.getJsonPath());
105+
}
106+
}
107+

0 commit comments

Comments
 (0)