Skip to content

Commit 2864a95

Browse files
Merge pull request #297 from ParsePlatform/richardross.npe.fix
Properly initialize `Configuration.interceptors` when using old-style APIs.
2 parents 8542d98 + 210c913 commit 2864a95

File tree

2 files changed

+67
-2
lines changed

2 files changed

+67
-2
lines changed

Parse/src/main/java/com/parse/Parse.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,9 +152,14 @@ public Builder enableLocalDataStore() {
152152
return this;
153153
}
154154

155-
private Builder setNetworkInterceptors(Collection<ParseNetworkInterceptor> interceptors) {
156-
if (interceptors != null) {
155+
/* package for tests */ Builder setNetworkInterceptors(Collection<ParseNetworkInterceptor> interceptors) {
156+
if (this.interceptors == null) {
157+
this.interceptors = new ArrayList<>();
158+
} else {
157159
this.interceptors.clear();
160+
}
161+
162+
if (interceptors != null) {
158163
this.interceptors.addAll(interceptors);
159164
}
160165
return this;

Parse/src/test/java/com/parse/ParseClientConfigurationTest.java

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212

1313
import org.junit.Test;
1414

15+
import java.util.ArrayList;
16+
import java.util.Collection;
17+
import java.util.Iterator;
18+
1519
import static org.junit.Assert.assertEquals;
1620
import static org.junit.Assert.assertFalse;
1721
import static org.junit.Assert.assertNull;
@@ -57,4 +61,60 @@ public void testNetworkInterceptors() {
5761
// Expected
5862
}
5963
}
64+
65+
@Test
66+
public void testSetNetworkInterceptors() {
67+
final ParseNetworkInterceptor interceptorA = mock(ParseNetworkInterceptor.class);
68+
final ParseNetworkInterceptor interceptorB = mock(ParseNetworkInterceptor.class);
69+
70+
Collection<ParseNetworkInterceptor> collectionA = new ArrayList<ParseNetworkInterceptor>() {{
71+
add(interceptorA);
72+
add(interceptorB);
73+
}};
74+
75+
Collection<ParseNetworkInterceptor> collectionB = new ArrayList<ParseNetworkInterceptor>() {{
76+
add(interceptorB);
77+
add(interceptorA);
78+
}};
79+
80+
Parse.Configuration.Builder builder = new Parse.Configuration.Builder(null);
81+
82+
builder.setNetworkInterceptors(collectionA);
83+
Parse.Configuration configurationA = builder.build();
84+
85+
builder.setNetworkInterceptors(collectionB);
86+
Parse.Configuration configurationB = builder.build();
87+
88+
assertTrue(collectionsEqual(configurationA.interceptors, collectionA));
89+
assertTrue(collectionsEqual(configurationB.interceptors, collectionB));
90+
}
91+
92+
private static <T> boolean collectionsEqual(Collection<T> a, Collection<T> b) {
93+
if (a.size() != b.size()) {
94+
return false;
95+
}
96+
97+
Iterator<T> iteratorA = a.iterator();
98+
Iterator<T> iteratorB = b.iterator();
99+
for (; iteratorA.hasNext() && iteratorB.hasNext();) {
100+
T objectA = iteratorA.next();
101+
T objectB = iteratorB.next();
102+
103+
if (objectA == null || objectB == null) {
104+
if (objectA != objectB) {
105+
return false;
106+
}
107+
continue;
108+
}
109+
110+
if (!objectA.equals(objectB)) {
111+
return false;
112+
}
113+
}
114+
115+
if (iteratorA.hasNext() || iteratorB.hasNext()) {
116+
return false;
117+
}
118+
return true;
119+
}
60120
}

0 commit comments

Comments
 (0)