/** * Sets the installationId and persists it to disk. */ public void set(String newInstallationId) { synchronized (lock) { if (ParseTextUtils.isEmpty(newInstallationId) || newInstallationId.equals(get())) { return; } setInternal(newInstallationId); } }
@Test public void testSetNullDoesNotPersist() { File installationIdFile = new File(temporaryFolder.getRoot(), "installationId"); InstallationId installationId = new InstallationId(installationIdFile); String installationIdString = installationId.get(); installationId.set(null); assertEquals(installationIdString, installationId.get()); }
/* package */ void updateDeviceInfo(InstallationId installationId) { /* * If we don't have an installationId, use the one that comes from the installationId file on * disk. This should be impossible since we set the installationId in setDefaultValues. */ if (!has(KEY_INSTALLATION_ID)) { performPut(KEY_INSTALLATION_ID, installationId.get()); } String deviceType = "android"; if (!deviceType.equals(get(KEY_DEVICE_TYPE))) { performPut(KEY_DEVICE_TYPE, deviceType); } }
@Test public void testInstallationIdIsCachedInMemory() { File installationIdFile = new File(temporaryFolder.getRoot(), "installationId"); InstallationId installationId = new InstallationId(installationIdFile); String installationIdString = installationId.get(); ParseFileUtils.deleteQuietly(installationIdFile); assertEquals(installationIdString, installationId.get()); }
@Test public void testSetEmptyStringDoesNotPersist() { File installationIdFile = new File(temporaryFolder.getRoot(), "installationId"); InstallationId installationId = new InstallationId(installationIdFile); String installationIdString = installationId.get(); installationId.set(""); assertEquals(installationIdString, installationId.get()); } }
private static void mocksForUpdateBeforeSave() { // Mock currentInstallationController to make setAsync work ParseCurrentInstallationController controller = mock(ParseCurrentInstallationController.class); when(controller.isCurrent(any(ParseInstallation.class))).thenReturn(true); ParseCorePlugins.getInstance().registerCurrentInstallationController(controller); // Mock App Name RuntimeEnvironment.application.getApplicationInfo().name = "parseTest"; ParsePlugins plugins = mock(ParsePlugins.class); // Mock installationId InstallationId installationId = mock(InstallationId.class); when(installationId.get()).thenReturn("installationId"); when(plugins.installationId()).thenReturn(installationId); // Mock application context when(plugins.applicationContext()).thenReturn(RuntimeEnvironment.application); ParsePlugins.set(plugins); }
@Test public void testInstallationIdIsRandom() { File installationIdFile = new File(temporaryFolder.getRoot(), "installationId"); String installationIdString = new InstallationId(installationIdFile).get(); ParseFileUtils.deleteQuietly(installationIdFile); assertFalse(installationIdString.equals(new InstallationId(installationIdFile).get())); }
@Override public Response intercept(Chain chain) throws IOException { Request request = chain.request(); Headers.Builder headersBuilder = request.headers().newBuilder() .set(ParseRESTCommand.HEADER_APPLICATION_ID, configuration.applicationId) .set(ParseRESTCommand.HEADER_APP_BUILD_VERSION, String.valueOf(ManifestInfo.getVersionCode())) .set(ParseRESTCommand.HEADER_APP_DISPLAY_VERSION, ManifestInfo.getVersionName()) .set(ParseRESTCommand.HEADER_OS_VERSION, Build.VERSION.RELEASE) .set(ParseRESTCommand.USER_AGENT, userAgent()); if (request.header(ParseRESTCommand.HEADER_INSTALLATION_ID) == null) { // We can do this synchronously since the caller is already on a background thread headersBuilder.set(ParseRESTCommand.HEADER_INSTALLATION_ID, installationId().get()); } // client key can be null with self-hosted Parse Server if (configuration.clientKey != null) { headersBuilder.set(ParseRESTCommand.HEADER_CLIENT_KEY, configuration.clientKey); } request = request.newBuilder() .headers(headersBuilder.build()) .build(); return chain.proceed(request); } });
@Test public void testSetSameDoesNotWriteToDisk() { File installationIdFile = new File(temporaryFolder.getRoot(), "installationId"); InstallationId installationId = new InstallationId(installationIdFile); String installationIdString = installationId.get(); ParseFileUtils.deleteQuietly(installationIdFile); installationId.set(installationIdString); assertFalse(installationIdFile.exists()); }
@Test public void testSetThenGet() { File installationIdFile = new File(temporaryFolder.getRoot(), "installationId"); InstallationId installationId = new InstallationId(installationIdFile); installationId.set("test_installation_id"); assertEquals("test_installation_id", installationId.get()); }
@Test public void testGetReadsInstallationIdFromFile() throws Exception { File installationIdFile = new File(temporaryFolder.getRoot(), "installationId"); InstallationId installationId = new InstallationId(installationIdFile); ParseFileUtils.writeStringToFile(installationIdFile, "test_installation_id", "UTF-8"); assertEquals("test_installation_id", installationId.get()); }
@Test public void testGetAsyncWithNoInstallation() throws Exception { // Mock installationId InstallationId installationId = mock(InstallationId.class); when(installationId.get()).thenReturn("testInstallationId"); //noinspection unchecked ParseObjectStore<ParseInstallation> store = mock(ParseObjectStore.class); when(store.getAsync()).thenReturn(Task.<ParseInstallation>forResult(null)); // Create test controller CachedCurrentInstallationController controller = new CachedCurrentInstallationController(store, installationId); ParseInstallation currentInstallation = ParseTaskUtils.wait(controller.getAsync()); verify(store, times(1)).getAsync(); // Make sure controller state is update to date assertSame(controller.currentInstallation, currentInstallation); // Make sure device info is updated assertEquals("testInstallationId", currentInstallation.getInstallationId()); assertEquals("android", currentInstallation.get(KEY_DEVICE_TYPE)); }
@Test public void testGetGeneratesInstallationIdAndFile() throws Exception { File installationIdFile = new File(temporaryFolder.getRoot(), "installationId"); InstallationId installationId = new InstallationId(installationIdFile); String installationIdString = installationId.get(); assertNotNull(installationIdString); assertEquals(installationIdString, ParseFileUtils.readFileToString(installationIdFile, "UTF-8")); }
@Test public void testGetAsyncWithNoInstallationRaceCondition() throws ParseException { // Mock installationId InstallationId installationId = mock(InstallationId.class); when(installationId.get()).thenReturn("testInstallationId"); //noinspection unchecked ParseObjectStore<ParseInstallation> store = mock(ParseObjectStore.class); TaskCompletionSource<ParseInstallation> tcs = new TaskCompletionSource(); when(store.getAsync()).thenReturn(tcs.getTask()); // Create test controller CachedCurrentInstallationController controller = new CachedCurrentInstallationController(store, installationId); Task<ParseInstallation> taskA = controller.getAsync(); Task<ParseInstallation> taskB = controller.getAsync(); tcs.setResult(null); ParseInstallation installationA = ParseTaskUtils.wait(taskA); ParseInstallation installationB = ParseTaskUtils.wait(taskB); verify(store, times(1)).getAsync(); assertSame(controller.currentInstallation, installationA); assertSame(controller.currentInstallation, installationB); // Make sure device info is updated assertEquals("testInstallationId", installationA.getInstallationId()); assertEquals("android", installationA.get(KEY_DEVICE_TYPE)); }