Skip to content

Commit 34f899c

Browse files
committed
Add unit tests for ParsePush.State copying
1 parent 149bb1c commit 34f899c

File tree

2 files changed

+49
-10
lines changed

2 files changed

+49
-10
lines changed

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

+10-10
Original file line numberDiff line numberDiff line change
@@ -59,21 +59,21 @@ public Builder() {
5959
// do nothing
6060
}
6161

62-
private Builder(State state) {
63-
this.channelSet = state.channelSet == null
62+
public Builder(State state) {
63+
this.channelSet = state.channelSet() == null
6464
? null
65-
: Collections.unmodifiableSet(new HashSet<>(state.channelSet));
66-
this.query = state.queryState == null
65+
: Collections.unmodifiableSet(new HashSet<>(state.channelSet()));
66+
this.query = state.queryState() == null
6767
? null
68-
: new ParseQuery<>(new ParseQuery.State.Builder<ParseInstallation>(state.queryState));
69-
this.expirationTime = state.expirationTime;
70-
this.expirationTimeInterval = state.expirationTimeInterval;
71-
this.pushToIOS = state.pushToIOS;
72-
this.pushToAndroid = state.pushToAndroid;
68+
: new ParseQuery<>(new ParseQuery.State.Builder<ParseInstallation>(state.queryState()));
69+
this.expirationTime = state.expirationTime();
70+
this.expirationTimeInterval = state.expirationTimeInterval();
71+
this.pushToIOS = state.pushToIOS();
72+
this.pushToAndroid = state.pushToAndroid();
7373
// Since in state.build() we check data is not null, we do not need to check it again here.
7474
JSONObject copyData = null;
7575
try {
76-
copyData = new JSONObject(state.data.toString());
76+
copyData = new JSONObject(state.data().toString());
7777
} catch (JSONException e) {
7878
// Swallow this silently since it is impossible to happen
7979
}

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

+39
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@
88
*/
99
package com.parse;
1010

11+
import org.json.JSONException;
1112
import org.json.JSONObject;
1213
import org.junit.Test;
14+
import org.mockito.internal.util.collections.Sets;
1315
import org.skyscreamer.jsonassert.JSONAssert;
1416
import org.skyscreamer.jsonassert.JSONCompareMode;
1517

@@ -20,8 +22,13 @@
2022
import java.util.Set;
2123

2224
import static org.junit.Assert.assertEquals;
25+
import static org.junit.Assert.assertFalse;
26+
import static org.junit.Assert.assertNotSame;
27+
import static org.junit.Assert.assertSame;
2328
import static org.junit.Assert.assertTrue;
2429
import static org.junit.Assert.fail;
30+
import static org.mockito.Mockito.mock;
31+
import static org.mockito.Mockito.when;
2532

2633
public class ParsePushStateTest {
2734

@@ -56,6 +63,38 @@ public void testDefaultsWithData() throws Exception {
5663

5764
//endregion
5865

66+
@Test
67+
public void testCopy() throws JSONException {
68+
ParsePush.State state = mock(ParsePush.State.class);
69+
when(state.expirationTime()).thenReturn(1L);
70+
when(state.expirationTimeInterval()).thenReturn(2L);
71+
Set channelSet = Sets.newSet("one", "two");
72+
when(state.channelSet()).thenReturn(channelSet);
73+
JSONObject data = new JSONObject();
74+
data.put("foo", "bar");
75+
when(state.data()).thenReturn(data);
76+
when(state.pushToAndroid()).thenReturn(true);
77+
when(state.pushToIOS()).thenReturn(false);
78+
ParseQuery.State<ParseInstallation> queryState =
79+
new ParseQuery.State.Builder<>(ParseInstallation.class).build();
80+
when(state.queryState()).thenReturn(queryState);
81+
82+
ParsePush.State copy = new ParsePush.State.Builder(state).build();
83+
assertSame(1L, copy.expirationTime());
84+
assertSame(2L, copy.expirationTimeInterval());
85+
Set channelSetCopy = copy.channelSet();
86+
assertNotSame(channelSet, channelSetCopy);
87+
assertTrue(channelSetCopy.size() == 2 && channelSetCopy.contains("one"));
88+
JSONObject dataCopy = copy.data();
89+
assertNotSame(data, dataCopy);
90+
assertEquals("bar", dataCopy.get("foo"));
91+
assertTrue(copy.pushToAndroid());
92+
assertFalse(copy.pushToIOS());
93+
ParseQuery.State<ParseInstallation> queryStateCopy = copy.queryState();
94+
assertNotSame(queryState, queryStateCopy);
95+
assertEquals("_Installation", queryStateCopy.className());
96+
}
97+
5998
//region testExpirationTime
6099

61100
@Test

0 commit comments

Comments
 (0)