@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()); }
@Override public void clearFromDisk() { synchronized (mutex) { currentInstallation = null; } try { installationId.clear(); ParseTaskUtils.wait(store.deleteAsync()); } catch (ParseException e) { // ignored } }
/** * 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 testInstallationIdIsRandom() { File installationIdFile = new File(temporaryFolder.getRoot(), "installationId"); String installationIdString = new InstallationId(installationIdFile).get(); ParseFileUtils.deleteQuietly(installationIdFile); assertFalse(installationIdString.equals(new InstallationId(installationIdFile).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 testSetWritesInstallationIdToFile() throws Exception { File installationIdFile = new File(temporaryFolder.getRoot(), "installationId"); InstallationId installationId = new InstallationId(installationIdFile); installationId.set("test_installation_id"); assertEquals("test_installation_id", ParseFileUtils.readFileToString(installationIdFile, "UTF-8")); }
InstallationId installationId() { synchronized (lock) { if (installationId == null) { //noinspection deprecation installationId = new InstallationId(new File(getParseDir(), INSTALLATION_ID_LOCATION)); } return installationId; } }
@Test public void testSetAsyncWithCurrentInstallation() throws Exception { InstallationId installationId = mock(InstallationId.class); //noinspection unchecked ParseObjectStore<ParseInstallation> store = mock(ParseObjectStore.class); // Create test controller CachedCurrentInstallationController controller = new CachedCurrentInstallationController(store, installationId); ParseInstallation currentInstallation = mock(ParseInstallation.class); when(currentInstallation.getInstallationId()).thenReturn("testInstallationId"); controller.currentInstallation = currentInstallation; ParseTaskUtils.wait(controller.setAsync(currentInstallation)); // Verify that we persist it verify(store, times(1)).setAsync(currentInstallation); // Make sure installationId is updated verify(installationId, times(1)).set("testInstallationId"); }
/** * Loads the installationId from memory, then tries to loads the legacy installationId from disk * if it is present, or creates a new random UUID. */ public String get() { synchronized (lock) { if (installationId == null) { try { installationId = ParseFileUtils.readFileToString(file, "UTF-8"); } catch (FileNotFoundException e) { PLog.i(TAG, "Couldn't find existing installationId file. Creating one instead."); } catch (IOException e) { PLog.e(TAG, "Unexpected exception reading installation id from disk", e); } } if (installationId == null) { setInternal(UUID.randomUUID().toString()); } } return installationId; }
@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()); }
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); }
static ParsePlugins mockParsePlugins(Parse.Configuration configuration) { ParsePlugins parsePlugins = mock(ParsePlugins.class); when(parsePlugins.applicationId()).thenReturn(configuration.applicationId); when(parsePlugins.clientKey()).thenReturn(configuration.clientKey); when(parsePlugins.configuration()).thenReturn(configuration); Context applicationContext = configuration.context.getApplicationContext(); when(parsePlugins.applicationContext()).thenReturn(applicationContext); File parseDir = createFileDir(applicationContext.getDir("Parse", Context.MODE_PRIVATE)); when(parsePlugins.installationId()) .thenReturn( new InstallationId(new File(parseDir, "installationId"))); when(parsePlugins.getParseDir()) .thenReturn(parseDir); when(parsePlugins.getCacheDir()) .thenReturn(createFileDir( new File(applicationContext.getCacheDir(), "com.parse"))); when(parsePlugins.getFilesDir()) .thenReturn(createFileDir( new File(applicationContext.getFilesDir(), "com.parse"))); return parsePlugins; }
@Test public void testGetAsyncFromStore() throws Exception { // Mock installationId InstallationId installationId = mock(InstallationId.class); //noinspection unchecked ParseObjectStore<ParseInstallation> store = mock(ParseObjectStore.class); ParseInstallation installation = mock(ParseInstallation.class); when(installation.getInstallationId()).thenReturn("testInstallationId"); when(store.getAsync()).thenReturn(Task.forResult(installation)); // Create test controller CachedCurrentInstallationController controller = new CachedCurrentInstallationController(store, installationId); ParseInstallation currentInstallation = ParseTaskUtils.wait(controller.getAsync()); verify(store, times(1)).getAsync(); // Make sure installationId is updated verify(installationId, times(1)).set("testInstallationId"); // Make sure controller state is update to date assertSame(installation, controller.currentInstallation); // Make sure the installation we get is correct assertSame(installation, currentInstallation); }
@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()); } }
@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()); }
@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 testClearFromDisk() { // Mock installationId InstallationId installationId = mock(InstallationId.class); //noinspection unchecked ParseObjectStore<ParseInstallation> store = mock(ParseObjectStore.class); when(store.deleteAsync()).thenReturn(Task.<Void>forResult(null)); CachedCurrentInstallationController controller = new CachedCurrentInstallationController(store, installationId); controller.currentInstallation = mock(ParseInstallation.class); controller.clearFromDisk(); assertNull(controller.currentInstallation); // Make sure the in LDS currentInstallation is cleared verify(store, times(1)).deleteAsync(); // Make sure installationId is cleared verify(installationId, times(1)).clear(); }
@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 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 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)); }