@Override /* package */ void validateSaveEventually() throws ParseException { if (isDirty(KEY_PASSWORD)) { // TODO(mengyan): Unify the exception we throw when validate fails throw new ParseException( ParseException.OTHER_CAUSE, "Unable to saveEventually on a ParseUser with dirty password"); } }
@Override public Integer call() throws Exception { JSONObject cached = ParseKeyValueCache.jsonFromKeyValueCache(cacheKey, state.maxCacheAge()); if (cached == null) { throw new ParseException(ParseException.CACHE_MISS, "results not cached"); } try { return cached.getInt("count"); } catch (JSONException e) { throw new ParseException(ParseException.CACHE_MISS, "the cache contains corrupted json"); } } }, Task.BACKGROUND_EXECUTOR);
@Override public List<T> call() throws Exception { JSONObject cached = ParseKeyValueCache.jsonFromKeyValueCache(cacheKey, state.maxCacheAge()); if (cached == null) { throw new ParseException(ParseException.CACHE_MISS, "results not cached"); } try { return networkController.convertFindResponse(state, cached); } catch (JSONException e) { throw new ParseException(ParseException.CACHE_MISS, "the cache contains corrupted json"); } } }, Task.BACKGROUND_EXECUTOR);
@Override public void run() { tcs.trySetError(new ParseException(ParseException.TIMEOUT, "Location fetch timed out.")); manager.removeUpdates(listener); } }, timeout, TimeUnit.MILLISECONDS));
@Override public T then(Task<List<T>> task) throws Exception { if (task.isFaulted()) { throw task.getError(); } if (task.getResult() != null && task.getResult().size() > 0) { return task.getResult().get(0); } throw new ParseException(ParseException.OBJECT_NOT_FOUND, "no results found for query"); } });
@Override public String then(Task<Cursor> task) throws Exception { Cursor cursor = task.getResult(); cursor.moveToFirst(); if (cursor.isAfterLast()) { /* * This is a pointer that came from Parse that references an object that has * never been saved in the offline store before. This just means there's no data * in the store that needs to be merged into the object. */ cursor.close(); throw new ParseException(ParseException.CACHE_MISS, "This object is not available in the offline cache."); } // we should fetch its data and record its UUID for future reference. String jsonString = cursor.getString(0); String newUUID = cursor.getString(1); cursor.close(); synchronized (lock) { /* * It's okay to put this object into the uuid map. No one will try to fetch * it, because it's already in the fetchedObjects map. And no one will try to * save to it without fetching it first, so everything should be just fine. */ objectToUuidMap.put(object, Task.forResult(newUUID)); uuidToObjectMap.put(newUUID, object); } return jsonString; } });
throw new ParseException(error);
} else if (result.has("error")) { JSONObject error = result.getJSONObject("error"); tcs.setError(new ParseException(error.getInt("code"), error.getString("error")));
throw new ParseException(ParseException.INVALID_QUERY, "whereWithinGeoBox queries cannot cross the International Date Line."); throw new ParseException(ParseException.INVALID_QUERY, "The southwest corner of a geo box must be south of the northeast corner."); throw new ParseException(ParseException.INVALID_QUERY, "Geo box queries larger than 180 degrees in longitude are not supported. " + "Please check point order.");
@Test public void testGetInBackgroundWithCallbackFail() throws Exception { ParseException exception = new ParseException(ParseException.CONNECTION_FAILED, "error"); ParseConfigController controller = mockParseConfigControllerWithException(exception); ParseCorePlugins.getInstance().registerConfigController(controller); final Semaphore done = new Semaphore(0); ParseConfig.getInBackground(new ConfigCallback() { @Override public void done(ParseConfig config, ParseException e) { assertEquals(ParseException.CONNECTION_FAILED, e.getCode()); assertEquals("error", e.getMessage()); done.release(); } }); // Make sure the callback is called assertTrue(done.tryAcquire(1, 10, TimeUnit.SECONDS)); verify(controller, times(1)).getAsync(anyString()); }
@Test public void testSubscribeInBackgroundFail() throws Exception { ParsePushChannelsController controller = mock(ParsePushChannelsController.class); ParseException exception = new ParseException(ParseException.OTHER_CAUSE, "error"); when(controller.subscribeInBackground(anyString())).thenReturn(Task.<Void>forError(exception)); ParseCorePlugins.getInstance().registerPushChannelsController(controller); Task<Void> pushTask = ParsePush.subscribeInBackground("test"); pushTask.waitForCompletion(); verify(controller, times(1)).subscribeInBackground("test"); assertTrue(pushTask.isFaulted()); assertSame(exception, pushTask.getError()); }
@Test public void testUnsubscribeInBackgroundFail() throws Exception { ParsePushChannelsController controller = mock(ParsePushChannelsController.class); ParseException exception = new ParseException(ParseException.OTHER_CAUSE, "error"); when(controller.unsubscribeInBackground(anyString())) .thenReturn(Task.<Void>forError(exception)); ParseCorePlugins.getInstance().registerPushChannelsController(controller); Task<Void> pushTask = ParsePush.unsubscribeInBackground("test"); pushTask.waitForCompletion(); verify(controller, times(1)).unsubscribeInBackground("test"); assertTrue(pushTask.isFaulted()); assertSame(exception, pushTask.getError()); }
@Test public void testSubscribeInBackgroundWithCallbackFail() throws Exception { ParsePushChannelsController controller = mock(ParsePushChannelsController.class); final ParseException exception = new ParseException(ParseException.OTHER_CAUSE, "error"); when(controller.subscribeInBackground(anyString())).thenReturn(Task.<Void>forError(exception)); ParseCorePlugins.getInstance().registerPushChannelsController(controller); ParsePush push = new ParsePush(); final Semaphore done = new Semaphore(0); final Capture<Exception> exceptionCapture = new Capture<>(); ParsePush.subscribeInBackground("test", new SaveCallback() { @Override public void done(ParseException e) { exceptionCapture.set(e); done.release(); } }); assertSame(exception, exceptionCapture.get()); assertTrue(done.tryAcquire(1, 10, TimeUnit.SECONDS)); verify(controller, times(1)).subscribeInBackground("test"); }
@Test public void testMissingRequiredFieldWhenSaveAsync() throws Exception { String sessionToken = "sessionToken"; Task<Void> toAwait = Task.forResult(null); ParseCurrentInstallationController controller = mockCurrentInstallationController(); ParseObjectController objController = mock(ParseObjectController.class); // mock return task when Installation was deleted on the server Task<ParseObject.State> taskError = Task.forError(new ParseException(ParseException.MISSING_REQUIRED_FIELD_ERROR, "")); // mock return task when Installation was re-saved to the server Task<ParseObject.State> task = Task.forResult(null); when(objController.saveAsync( any(ParseObject.State.class), any(ParseOperationSet.class), eq(sessionToken), any(ParseDecoder.class))) .thenReturn(taskError) .thenReturn(task); ParseCorePlugins.getInstance() .registerObjectController(objController); ParseInstallation installation = ParseInstallation.getCurrentInstallation(); assertNotNull(installation); installation.put("key", "value"); ParseTaskUtils.wait(installation.saveAsync(sessionToken, toAwait)); verify(controller).getAsync(); verify(objController, times(2)).saveAsync( any(ParseObject.State.class), any(ParseOperationSet.class), eq(sessionToken), any(ParseDecoder.class)); }
@Test public void testUnsubscribeInBackgroundWithCallbackFail() throws Exception { ParsePushChannelsController controller = mock(ParsePushChannelsController.class); final ParseException exception = new ParseException(ParseException.OTHER_CAUSE, "error"); when(controller.unsubscribeInBackground(anyString())) .thenReturn(Task.<Void>forError(exception)); ParseCorePlugins.getInstance().registerPushChannelsController(controller); ParsePush push = new ParsePush(); final Semaphore done = new Semaphore(0); final Capture<Exception> exceptionCapture = new Capture<>(); ParsePush.unsubscribeInBackground("test", new SaveCallback() { @Override public void done(ParseException e) { exceptionCapture.set(e); done.release(); } }); assertSame(exception, exceptionCapture.get()); assertTrue(done.tryAcquire(1, 10, TimeUnit.SECONDS)); verify(controller, times(1)).unsubscribeInBackground("test"); }
@Test public void testGetCurrentConfigFail() { ParseException exception = new ParseException(ParseException.CONNECTION_FAILED, "error"); ParseConfigController controller = new ParseConfigController(mock(ParseHttpClient.class), mockParseCurrentConfigControllerWithException(exception)); ParseCorePlugins.getInstance().registerConfigController(controller); ParseConfig configAgain = ParseConfig.getCurrentConfig(); // Make sure we get an empty ParseConfig assertEquals(0, configAgain.getParams().size()); }
ParseException exception = new ParseException(ParseException.OTHER_CAUSE, "error"); when(controller.sendInBackground(any(ParsePush.State.class), anyString())) .thenReturn(Task.<Void>forError(exception));
@Test public void testGetSyncFail() { ParseException exception = new ParseException(ParseException.CONNECTION_FAILED, "error"); ParseConfigController controller = mockParseConfigControllerWithException(exception); ParseCorePlugins.getInstance().registerConfigController(controller); try { ParseConfig.get(); fail("Should throw an exception"); } catch (ParseException e) { verify(controller, times(1)).getAsync(anyString()); assertEquals(ParseException.CONNECTION_FAILED, e.getCode()); assertEquals("error", e.getMessage()); } }
@Test public void testSignUpAsyncWithNoCurrentUserAndSignUpFailure() { // Register a mock currentUserController to make getCurrentUser work ParseCurrentUserController currentUserController = mock(ParseCurrentUserController.class); when(currentUserController.getAsync(anyBoolean())) .thenReturn(Task.<ParseUser>forResult(null)); ParseCorePlugins.getInstance().registerCurrentUserController(currentUserController); // Register a mock userController to make logIn work ParseUserController userController = mock(ParseUserController.class); ParseException signUpException = new ParseException(ParseException.OTHER_CAUSE, "test"); when(userController.signUpAsync( any(ParseUser.State.class), any(ParseOperationSet.class), anyString())) .thenReturn(Task.<ParseUser.State>forError(signUpException)); ParseCorePlugins.getInstance().registerUserController(userController); ParseUser user = new ParseUser(); user.put("key", "value"); user.setUsername("userName"); user.setPassword("password"); Task<Void> signUpTask = user.signUpAsync(Task.<Void>forResult(null)); // Make sure we sign up the user verify(userController, times(1)).signUpAsync( any(ParseUser.State.class), any(ParseOperationSet.class), anyString()); // Make sure user's data is correct assertEquals("value", user.getString("key")); // Make sure we never set the current user verify(currentUserController, never()).setAsync(user); // Make sure task is failed assertTrue(signUpTask.isFaulted()); assertSame(signUpException, signUpTask.getError()); }
@SuppressWarnings("ThrowableResultOfMethodCallIgnored") @Test public void testGetInBackgroundFail() throws Exception { ParseException exception = new ParseException(ParseException.CONNECTION_FAILED, "error"); ParseConfigController controller = mockParseConfigControllerWithException(exception); ParseCorePlugins.getInstance().registerConfigController(controller); Task<ParseConfig> configTask = ParseConfig.getInBackground(); configTask.waitForCompletion(); verify(controller, times(1)).getAsync(anyString()); assertThat(configTask.getError(), instanceOf(ParseException.class)); assertEquals(ParseException.CONNECTION_FAILED, ((ParseException) configTask.getError()).getCode()); assertEquals("error", configTask.getError().getMessage()); }