Skip to content

Commit 3684fde

Browse files
committed
Improved test coverage in AbstractArray, did a few minor fixes to prevent rare but unexpected NPE's, deprecated the typed query helpers in AbstractArray as they have barely any use-case and unnecessarily bloat the code
1 parent 078a613 commit 3684fde

File tree

2 files changed

+246
-18
lines changed

2 files changed

+246
-18
lines changed

src/main/java/org/javawebstack/abstractdata/AbstractArray.java

Lines changed: 57 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -30,76 +30,109 @@ public AbstractObject object(boolean strict) throws AbstractCoercingException {
3030
return object;
3131
}
3232

33+
@Deprecated
3334
public AbstractObject object(String key) throws AbstractCoercingException {
34-
return query(key).object();
35+
AbstractElement e = query(key);
36+
return e != null ? e.object() : null;
3537
}
3638

39+
@Deprecated
3740
public AbstractArray array(String key) throws AbstractCoercingException {
38-
return query(key).array();
41+
AbstractElement e = query(key);
42+
return e != null ? e.array() : null;
3943
}
4044

45+
@Deprecated
4146
public AbstractPrimitive primitive(String key) throws AbstractCoercingException {
42-
return query(key).primitive();
47+
AbstractElement e = query(key);
48+
return e != null ? e.primitive() : null;
4349
}
4450

51+
@Deprecated
4552
public String string(String key) throws AbstractCoercingException {
46-
return query(key).string();
53+
AbstractElement e = query(key);
54+
return e != null ? e.string() : null;
4755
}
4856

57+
@Deprecated
4958
public Boolean bool(String key) throws AbstractCoercingException {
50-
return query(key).bool();
59+
AbstractElement e = query(key);
60+
return e != null ? e.bool() : null;
5161
}
5262

63+
@Deprecated
5364
public Number number(String key) throws AbstractCoercingException {
54-
return query(key).number();
65+
AbstractElement e = query(key);
66+
return e != null ? e.number() : null;
5567
}
5668

69+
@Deprecated
5770
public AbstractObject object(String key, AbstractObject orElse) throws AbstractCoercingException {
58-
return query(key, orElse).object();
71+
AbstractElement e = query(key, orElse);
72+
return e != null ? e.object() : null;
5973
}
6074

75+
@Deprecated
6176
public AbstractArray array(String key, AbstractArray orElse) throws AbstractCoercingException {
62-
return query(key, orElse).array();
77+
AbstractElement e = query(key, orElse);
78+
return e != null ? e.array() : null;
6379
}
6480

81+
@Deprecated
6582
public AbstractPrimitive primitive(String key, AbstractPrimitive orElse) throws AbstractCoercingException {
66-
return query(key, orElse).primitive();
83+
AbstractElement e = query(key, orElse);
84+
return e != null ? e.primitive() : null;
6785
}
6886

87+
@Deprecated
6988
public String string(String key, String orElse) throws AbstractCoercingException {
70-
return query(key, new AbstractPrimitive(orElse)).string();
89+
AbstractPrimitive orElsePrimitive = orElse != null ? new AbstractPrimitive(orElse) : null;
90+
AbstractElement e = primitive(key, orElsePrimitive);
91+
return e != null ? e.string() : null;
7192
}
7293

94+
@Deprecated
7395
public Boolean bool(String key, Boolean orElse) throws AbstractCoercingException {
74-
return query(key, new AbstractPrimitive(orElse)).bool();
96+
AbstractPrimitive orElsePrimitive = orElse != null ? new AbstractPrimitive(orElse) : null;
97+
AbstractElement e = primitive(key, orElsePrimitive);
98+
return e != null ? e.bool() : null;
7599
}
76100

101+
@Deprecated
77102
public Number number(String key, Number orElse) throws AbstractCoercingException {
78-
return query(key, new AbstractPrimitive(orElse)).number();
103+
AbstractPrimitive orElsePrimitive = orElse != null ? new AbstractPrimitive(orElse) : null;
104+
AbstractElement e = primitive(key, orElsePrimitive);
105+
return e != null ? e.number() : null;
79106
}
80107

81108
public AbstractObject object(int index) throws AbstractCoercingException {
82-
return get(index).object();
109+
AbstractElement e = get(index);
110+
return e != null ? e.object() : null;
83111
}
84112

85113
public AbstractArray array(int index) throws AbstractCoercingException {
86-
return get(index).array();
114+
AbstractElement e = get(index);
115+
return e != null ? e.array() : null;
87116
}
88117

89118
public AbstractPrimitive primitive(int index) throws AbstractCoercingException {
90-
return get(index).primitive();
119+
AbstractElement e = get(index);
120+
return e != null ? e.primitive() : null;
91121
}
92122

93123
public String string(int index) throws AbstractCoercingException {
94-
return get(index).string();
124+
AbstractElement e = get(index);
125+
return e != null ? e.string() : null;
95126
}
96127

97128
public Boolean bool(int index) throws AbstractCoercingException {
98-
return get(index).bool();
129+
AbstractElement e = get(index);
130+
return e != null ? e.bool() : null;
99131
}
100132

101133
public Number number(int index) throws AbstractCoercingException {
102-
return get(index).number();
134+
AbstractElement e = get(index);
135+
return e != null ? e.number() : null;
103136
}
104137

105138
public AbstractObject object(int index, AbstractObject orElse) throws AbstractCoercingException {
@@ -214,6 +247,8 @@ public AbstractElement[] toArray() {
214247
}
215248

216249
public AbstractElement get(int i) {
250+
if(i >= elements.size())
251+
return null;
217252
return elements.get(i);
218253
}
219254

@@ -223,6 +258,8 @@ public AbstractElement get(int index, AbstractElement orElse) {
223258
}
224259

225260
public AbstractElement query(String query) {
261+
if(query == null)
262+
throw new IllegalArgumentException("query can not be null");
226263
String[] q = query.split("\\.", 2);
227264
try {
228265
int index = Integer.parseInt(q[0]);
@@ -370,6 +407,8 @@ public boolean equals(Object obj) {
370407
}
371408

372409
public AbstractArray addAll(AbstractArray array) {
410+
if(array == null)
411+
throw new IllegalArgumentException("array may not be null");
373412
elements.addAll(array.elements);
374413
return this;
375414
}

src/test/java/org/javawebstack/abstractdata/AbstractArrayTest.java

Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,200 @@
11
package org.javawebstack.abstractdata;
22

3+
import org.javawebstack.abstractdata.exception.AbstractCoercingException;
34
import org.junit.jupiter.api.Test;
45

56
import static org.junit.jupiter.api.Assertions.*;
67

78
class AbstractArrayTest {
89

10+
@Test
11+
void testIsArrayReturnsTrue() {
12+
assertTrue(new AbstractArray().isArray());
13+
}
14+
15+
@Test
16+
void testObjectThrowsInStrictMode() {
17+
assertThrows(AbstractCoercingException.class, () -> new AbstractArray().object(true));
18+
}
19+
20+
@Test
21+
void testObjectCoercingWhenNotInStrictMode() {
22+
AbstractArray a = new AbstractArray();
23+
a.add(0);
24+
a.add("1");
25+
AbstractObject o = assertDoesNotThrow(() -> a.object(false));
26+
assertTrue(o.hasNumber("0"));
27+
assertTrue(o.hasString("1"));
28+
}
29+
30+
@Test
31+
void testObjectQueryReturnsNullWhenKeyIsNotFound() {
32+
assertNull(new AbstractArray().add(new AbstractObject()).object("1"));
33+
}
34+
35+
@Test
36+
void testObjectQueryReturnsValueWhenKeyIsFound() {
37+
assertNotNull(new AbstractArray().add(new AbstractObject()).object("0"));
38+
}
39+
40+
@Test
41+
void testArrayQueryReturnsNullWhenKeyIsNotFound() {
42+
assertNull(new AbstractArray().add(new AbstractArray()).array("1"));
43+
}
44+
45+
@Test
46+
void testArrayQueryReturnsValueWhenKeyIsFound() {
47+
assertNotNull(new AbstractArray().add(new AbstractArray()).array("0"));
48+
}
49+
50+
@Test
51+
void testPrimitiveQueryReturnsNullWhenKeyIsNotFound() {
52+
assertNull(new AbstractArray().add(0).primitive("1"));
53+
}
54+
55+
@Test
56+
void testPrimitiveQueryReturnsValueWhenKeyIsFound() {
57+
assertNotNull(new AbstractArray().add(0).primitive("0"));
58+
}
59+
60+
@Test
61+
void testStringQueryReturnsNullWhenKeyIsNotFound() {
62+
assertNull(new AbstractArray().add("abc").string("1"));
63+
}
64+
65+
@Test
66+
void testStringQueryReturnsValueWhenKeyIsFound() {
67+
assertEquals("abc", new AbstractArray().add("abc").string("0"));
68+
}
69+
70+
@Test
71+
void testNumberQueryReturnsNullWhenKeyIsNotFound() {
72+
assertNull(new AbstractArray().add(123).number("1"));
73+
}
74+
75+
@Test
76+
void testNumberQueryReturnsValueWhenKeyIsFound() {
77+
assertEquals(123, new AbstractArray().add(123).number("0"));
78+
}
79+
80+
@Test
81+
void testBoolQueryReturnsNullWhenKeyIsNotFound() {
82+
assertNull(new AbstractArray().add(true).bool("1"));
83+
}
84+
85+
@Test
86+
void testBoolQueryReturnsValueWhenKeyIsFound() {
87+
assertEquals(true, new AbstractArray().add(true).bool("0"));
88+
}
89+
90+
@Test
91+
void testQueryOrElseReturnsValueIfFoundAndNonNull() {
92+
AbstractElement expected = new AbstractPrimitive(123);
93+
AbstractElement orElse = new AbstractPrimitive("456");
94+
AbstractArray array = new AbstractArray().add(expected);
95+
assertEquals(expected, array.query("0", orElse));
96+
}
97+
98+
@Test
99+
void testQueryOrElseReturnsOrElseIfFoundAndNull() {
100+
AbstractElement orElse = new AbstractPrimitive("456");
101+
AbstractArray array = new AbstractArray().addNull();
102+
assertEquals(orElse, array.query("0", orElse));
103+
}
104+
105+
@Test
106+
void testQueryOrElseReturnsOrElseIfNotFound() {
107+
AbstractElement unexpected = new AbstractPrimitive(123);
108+
AbstractElement orElse = new AbstractPrimitive("456");
109+
AbstractArray array = new AbstractArray().add(unexpected);
110+
assertEquals(orElse, array.query("1", orElse));
111+
}
112+
113+
@Test
114+
void testObjectQueryOrElseReturnsOrElseIfNotFound() {
115+
AbstractObject orElse = new AbstractObject().set("a", 1);
116+
assertSame(orElse, new AbstractArray().object("0", orElse));
117+
}
118+
119+
@Test
120+
void testArrayQueryOrElseReturnsOrElseIfNotFound() {
121+
AbstractArray orElse = new AbstractArray().add(123);
122+
assertSame(orElse, new AbstractArray().array("0", orElse));
123+
}
124+
125+
@Test
126+
void testPrimitiveQueryOrElseReturnsOrElseIfNotFound() {
127+
AbstractPrimitive orElse = new AbstractPrimitive(123);
128+
assertSame(orElse, new AbstractArray().primitive("0", orElse));
129+
}
130+
131+
@Test
132+
void testStringQueryOrElseReturnsOrElseIfNotFound() {
133+
String orElse = "abc";
134+
assertEquals(orElse, new AbstractArray().string("0", orElse));
135+
}
136+
137+
@Test
138+
void testNumberQueryOrElseReturnsOrElseIfNotFound() {
139+
Number orElse = 123;
140+
assertEquals(orElse, new AbstractArray().number("0", orElse));
141+
}
142+
143+
@Test
144+
void testBoolQueryOrElseReturnsOrElseIfNotFound() {
145+
Boolean orElse = true;
146+
assertEquals(orElse, new AbstractArray().bool("0", orElse));
147+
}
148+
149+
@Test
150+
void testQueryThrowsOnNull() {
151+
assertThrows(IllegalArgumentException.class, () -> new AbstractArray().query(null));
152+
}
153+
154+
@Test
155+
void testQueryWithNonIntPathReturnsNull() {
156+
assertNull(new AbstractArray().add(1).query("abc"));
157+
}
158+
159+
@Test
160+
void testQueryWithNonExistingIndexReturnsNull() {
161+
assertNull(new AbstractArray().add(0).query("1"));
162+
}
163+
164+
@Test
165+
void testQueryWithOnlyOneSegmentReturnsValue() {
166+
assertNotNull(new AbstractArray().add(1).query("0"));
167+
}
168+
169+
@Test
170+
void testClearClearsTheArrayAndReturnsThis() {
171+
AbstractArray array = new AbstractArray().add(1).add(2).add(3);
172+
assertEquals(3, array.size());
173+
assertSame(array, array.clear());
174+
assertEquals(0, array.size());
175+
}
176+
177+
@Test
178+
void testQueryWithObjectValueQueriesObject() {
179+
AbstractArray array = new AbstractArray();
180+
array.add(new AbstractObject().set("a", 1));
181+
assertEquals(1, array.query("0.a").number().intValue());
182+
}
183+
184+
@Test
185+
void testQueryWithArrayValueQueriesArray() {
186+
AbstractArray array = new AbstractArray();
187+
array.add(new AbstractArray().add(1));
188+
assertEquals(1, array.query("0.0").number().intValue());
189+
}
190+
191+
@Test
192+
void testQueryWithPrimitiveValueReturnsNull() {
193+
AbstractArray array = new AbstractArray();
194+
array.add("abc");
195+
assertNull(array.query("0.abc"));
196+
}
197+
9198
@Test
10199
void testEqualsNormal() {
11200
AbstractArray first = new AbstractArray();

0 commit comments

Comments
 (0)