/** * Fetches a new configuration object from the server. * * @return The {@code ParseConfig} that was fetched. * @throws ParseException Throws an exception if the server is inaccessible. */ public static ParseConfig get() throws ParseException { return ParseTaskUtils.wait(getInBackground()); }
@Override public ParseConfig call() { synchronized (currentConfigMutex) { if (currentConfig == null) { ParseConfig config = getFromDisk(); currentConfig = (config != null) ? config : new ParseConfig(); } } return currentConfig; } }, ParseExecutors.io());
/** * Retrieves a {@code ParseConfig} from a file on disk. * * @return The {@code ParseConfig} that was retrieved. If the file wasn't found, or the contents * of the file is an invalid {@code ParseConfig}, returns null. */ /* package for test */ ParseConfig getFromDisk() { JSONObject json; try { json = ParseFileUtils.readFileToJSONObject(currentConfigFile); } catch (IOException | JSONException e) { return null; } return ParseConfig.decode(json, ParseDecoder.get()); }
/** * Retrieves the most recently-fetched configuration object, either from memory or * disk if necessary. * * @return The most recently-fetched {@code ParseConfig} if it exists, else an empty * {@code ParseConfig} */ public static ParseConfig getCurrentConfig() { try { return ParseTaskUtils.wait(getConfigController().getCurrentConfigController() .getCurrentConfigAsync() ); } catch (ParseException e) { // In order to have backward compatibility, we swallow the exception silently. return new ParseConfig(); } }
@Test public void testGetKeyExist() { final Map<String, Object> params = new HashMap<>(); params.put("key", "value"); ParseConfig config = new ParseConfig(params); assertEquals(config.get("key"), "value"); assertEquals("value", config.get("key", "haha")); }
@Test public void testGetStringKeyNotExist() { final Map<String, Object> params = new HashMap<>(); params.put("key", "value"); ParseConfig config = new ParseConfig(params); assertNull(config.getString("wrongKey")); assertEquals("haha", config.getString("wrongKey", "haha")); }
@Test public void testGetNumberKeyExist() { final Number number = 1; Number numberAgain = 2; final Map<String, Object> params = new HashMap<>(); params.put("key", number); ParseConfig config = new ParseConfig(params); assertEquals(number, config.getNumber("key")); assertEquals(number, config.getNumber("key", numberAgain)); }
@Test public void testGetDateKeyExistValueNotDate() { Date date = new Date(); date.setTime(20); final Map<String, Object> params = new HashMap<>(); params.put("key", 1); ParseConfig config = new ParseConfig(params); assertNull(config.getDate("key")); assertSame(date, config.getDate("key", date)); }
@Test public void testGetParseGeoPointKeyExistValueNull() { ParseGeoPoint geoPoint = new ParseGeoPoint(45.484, 27.029); final Map<String, Object> params = new HashMap<>(); params.put("key", JSONObject.NULL); params.put("keyAgain", null); ParseConfig config = new ParseConfig(params); assertNull(config.getParseGeoPoint("key")); assertNull(config.getParseGeoPoint("key", geoPoint)); assertNull(config.getParseGeoPoint("keyAgain")); assertNull(config.getParseGeoPoint("keyAgain", geoPoint)); }
@Test public void testGetListKeyExistValueNotList() { final List<String> list = new ArrayList<>(); list.add("foo"); list.add("bar"); list.add("baz"); final Map<String, Object> params = new HashMap<>(); params.put("key", 1); ParseConfig config = new ParseConfig(params); assertNull(config.getList("key")); assertSame(list, config.getList("key", list)); }
@Test public void testGetJsonObjectKeyExist() throws Exception { final Map<String, String> value = new HashMap<>(); value.put("key", "value"); final Map<String, Object> params = new HashMap<>(); params.put("key", value); final JSONObject json = new JSONObject(value); ParseConfig config = new ParseConfig(params); assertEquals(json, config.getJSONObject("key"), JSONCompareMode.NON_EXTENSIBLE); assertEquals(json, config.getJSONObject("key", new JSONObject()), JSONCompareMode.NON_EXTENSIBLE); }
@Test public void testGetMapKeyExistValueNotMap() { Map<String, Object> map = new HashMap<>(); map.put("firstAgain", "fooAgain"); map.put("secondAgain", "barAgain"); map.put("thirdAgain", "bazAgain"); final Map<String, Object> params = new HashMap<>(); params.put("key", 1); ParseConfig config = new ParseConfig(params); assertNull(config.getMap("key")); assertSame(map, config.getMap("key", map)); }
@Test public void testGetParseFileKeyExistValueNull() { ParseFile file = new ParseFile( new ParseFile.State.Builder().name("file.txt").url("http://yarr.com/file.txt").build()); final Map<String, Object> params = new HashMap<>(); params.put("key", JSONObject.NULL); params.put("keyAgain", JSONObject.NULL); ParseConfig config = new ParseConfig(params); assertNull(config.getParseFile("key")); assertNull(config.getParseFile("key", file)); assertNull(config.getParseFile("keyAgain")); assertNull(config.getParseFile("keyAgain", file)); }
@Test public void testGetJsonArrayKeyExist() throws Exception { final Map<String, String> map = new HashMap<>(); map.put("key", "value"); final Map<String, String> mapAgain = new HashMap<>(); mapAgain.put("keyAgain", "valueAgain"); final List<Map<String, String>> value = new ArrayList<>(); value.add(map); value.add(mapAgain); final Map<String, Object> params = new HashMap<>(); params.put("key", value); JSONArray jsonArray = new JSONArray(value); ParseConfig config = new ParseConfig(params); assertEquals(jsonArray, config.getJSONArray("key"), JSONCompareMode.NON_EXTENSIBLE); assertEquals(jsonArray, config.getJSONArray("key", new JSONArray()), JSONCompareMode.NON_EXTENSIBLE); }
@Test public void testGetInBackgroundWithCallbackSuccess() throws Exception { final Map<String, Object> params = new HashMap<>(); params.put("string", "value"); ParseConfig config = new ParseConfig(params); ParseConfigController controller = mockParseConfigControllerWithResponse(config); ParseCorePlugins.getInstance().registerConfigController(controller); final Semaphore done = new Semaphore(0); ParseConfig.getInBackground(new ConfigCallback() { @Override public void done(ParseConfig config, ParseException e) { assertEquals(1, config.params.size()); assertEquals("value", config.params.get("string")); done.release(); } }); // Make sure the callback is called assertTrue(done.tryAcquire(1, 10, TimeUnit.SECONDS)); verify(controller, times(1)).getAsync(anyString()); }
@Test public void testGetBooleanKeyExistValueNotBoolean() { final Map<String, Object> params = new HashMap<>(); params.put("key", 1); ParseConfig config = new ParseConfig(params); assertFalse(config.getBoolean("key")); assertFalse(config.getBoolean("key", false)); }
@Test public void testGetCurrentConfigAsyncSuccessCurrentConfigNotSetDiskConfigExist() throws Exception { File configFile = new File(temporaryFolder.getRoot(), "config"); ParseCurrentConfigController currentConfigController = new ParseCurrentConfigController(configFile); // Save sample ParseConfig to disk final Map<String, Object> sampleConfigParameters = new HashMap<String, Object>() {{ put("string", "value"); }}; JSONObject sampleConfigJson = new JSONObject() {{ put("params", NoObjectsEncoder.get().encode(sampleConfigParameters)); }}; ParseConfig diskConfig = ParseConfig.decode(sampleConfigJson, ParseDecoder.get()); currentConfigController.saveToDisk(diskConfig); // Verify before set, disk config exist and in memory config is null assertTrue(configFile.exists()); assertNull(currentConfigController.currentConfig); Task<ParseConfig> getTask = currentConfigController.getCurrentConfigAsync(); ParseTaskUtils.wait(getTask); ParseConfig config = getTask.getResult(); // Verify after set, in memory config is set and value is correct assertSame(config, currentConfigController.currentConfig); assertEquals("value", config.get("string")); assertEquals(1, config.getParams().size()); }
@Test public void testGetLongKeyExist() { final Map<String, Object> params = new HashMap<>(); params.put("key", (long) 998); ParseConfig config = new ParseConfig(params); assertEquals(config.getLong("key"), (long) 998); assertEquals((long) 998, config.getLong("key", (long) 100)); }
@Test public void testGetIntKeyExist() { final Map<String, Object> params = new HashMap<>(); params.put("key", 998); ParseConfig config = new ParseConfig(params); assertEquals(config.getInt("key"), 998); assertEquals(998, config.getInt("key", 100)); }
@Test public void testGetDoubleKeyExist() { final Map<String, Object> params = new HashMap<>(); params.put("key", 998.1); ParseConfig config = new ParseConfig(params); assertEquals(config.getDouble("key"), 998.1, 0.0001); assertEquals(998.1, config.getDouble("key", 100.1), 0.0001); }