Skip to content

Commit 7c4f2b3

Browse files
committed
Convert ParseConfig JSON constructor to factory method
1 parent 898c41f commit 7c4f2b3

File tree

5 files changed

+12
-15
lines changed

5 files changed

+12
-15
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,14 +103,14 @@ public Task<ParseConfig> then(Task<Void> task) throws Exception {
103103
});
104104
}
105105

106-
// TODO(grantland): /* package */ static ParseConfig decode(JSONObject json, ParseDecoder decoder) {
107-
/* package */ ParseConfig(JSONObject json, ParseDecoder decoder) {
106+
@SuppressWarnings("unchecked")
107+
/* package */ static ParseConfig decode(JSONObject json, ParseDecoder decoder) {
108108
Map<String, Object> decodedObject = (Map<String, Object>) decoder.decode(json);
109109
Map<String, Object> decodedParams = (Map<String, Object>) decodedObject.get("params");
110110
if (decodedParams == null) {
111111
throw new RuntimeException("Object did not contain the 'params' key.");
112112
}
113-
this.params = Collections.unmodifiableMap(decodedParams);
113+
return new ParseConfig(decodedParams);
114114
}
115115

116116
/* package */ ParseConfig(Map<String, Object> params) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public Task<ParseConfig> getAsync(String sessionToken) {
3535
public Task<ParseConfig> then(Task<JSONObject> task) throws Exception {
3636
JSONObject result = task.getResult();
3737

38-
final ParseConfig config = new ParseConfig(result, ParseDecoder.get());
38+
final ParseConfig config = ParseConfig.decode(result, ParseDecoder.get());
3939
return currentConfigController.setCurrentConfigAsync(config).continueWith(new Continuation<Void, ParseConfig>() {
4040
@Override
4141
public ParseConfig then(Task<Void> task) throws Exception {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public ParseConfig call() throws Exception {
6868
} catch (IOException | JSONException e) {
6969
return null;
7070
}
71-
return new ParseConfig(json, ParseDecoder.get());
71+
return ParseConfig.decode(json, ParseDecoder.get());
7272
}
7373

7474
/* package */ void clearCurrentConfigForTesting() {

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,25 +70,25 @@ public void testDefaultConstructor() {
7070
}
7171

7272
@Test
73-
public void testConstructWithValidJsonObject() throws Exception {
73+
public void testDecodeWithValidJsonObject() throws Exception {
7474
final Map<String, Object> params = new HashMap<>();
7575
params.put("string", "value");
7676
JSONObject json = new JSONObject();
7777
json.put("params", NoObjectsEncoder.get().encode(params));
7878

79-
ParseConfig config = new ParseConfig(json, ParseDecoder.get());
79+
ParseConfig config = ParseConfig.decode(json, ParseDecoder.get());
8080
assertEquals(1, config.params.size());
8181
assertEquals("value", config.params.get("string"));
8282
}
8383

8484
@Test(expected = RuntimeException.class)
85-
public void testConstructWithInvalidJsonObject() throws Exception {
85+
public void testDecodeWithInvalidJsonObject() throws Exception {
8686
final Map<String, Object> params = new HashMap<>();
8787
params.put("string", "value");
8888
JSONObject json = new JSONObject();
8989
json.put("error", NoObjectsEncoder.get().encode(params));
9090

91-
new ParseConfig(json, ParseDecoder.get());
91+
ParseConfig.decode(json, ParseDecoder.get());
9292
}
9393

9494
//endregion

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

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,6 @@
1212
import org.junit.Rule;
1313
import org.junit.Test;
1414
import org.junit.rules.TemporaryFolder;
15-
import org.junit.runner.RunWith;
16-
import org.robolectric.RobolectricGradleTestRunner;
17-
import org.robolectric.annotation.Config;
1815

1916
import java.io.File;
2017
import java.util.ArrayList;
@@ -87,7 +84,7 @@ public void testSaveToDiskSuccess() throws Exception {
8784
JSONObject sampleConfigJson = new JSONObject() {{
8885
put("params", NoObjectsEncoder.get().encode(sampleConfigParameters));
8986
}};
90-
ParseConfig config = new ParseConfig(sampleConfigJson, ParseDecoder.get());
87+
ParseConfig config = ParseConfig.decode(sampleConfigJson, ParseDecoder.get());
9188

9289
// Save to disk
9390
File configFile = new File(temporaryFolder.getRoot(), "config");
@@ -164,7 +161,7 @@ public void testGetFromDiskSuccess() throws Exception {
164161
JSONObject sampleConfigJson = new JSONObject() {{
165162
put("params", NoObjectsEncoder.get().encode(sampleConfigParameters));
166163
}};
167-
ParseConfig config = new ParseConfig(sampleConfigJson, ParseDecoder.get());
164+
ParseConfig config = ParseConfig.decode(sampleConfigJson, ParseDecoder.get());
168165

169166
// Save to disk
170167
File configFile = new File(temporaryFolder.getRoot(), "config");
@@ -272,7 +269,7 @@ public void testGetCurrentConfigAsyncSuccessCurrentConfigNotSetDiskConfigExist()
272269
JSONObject sampleConfigJson = new JSONObject() {{
273270
put("params", NoObjectsEncoder.get().encode(sampleConfigParameters));
274271
}};
275-
ParseConfig diskConfig = new ParseConfig(sampleConfigJson, ParseDecoder.get());
272+
ParseConfig diskConfig = ParseConfig.decode(sampleConfigJson, ParseDecoder.get());
276273
currentConfigController.saveToDisk(diskConfig);
277274

278275
// Verify before set, disk config exist and in memory config is null

0 commit comments

Comments
 (0)