/** * Reads the contents of a file into a {@link JSONObject}. The file is always closed. */ public static JSONObject readFileToJSONObject(File file) throws IOException, JSONException { String content = readFileToString(file, "UTF-8"); return new JSONObject(content); }
public static String readFileToString(File file, String encoding) throws IOException { return readFileToString(file, Charset.forName(encoding)); }
/** * 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 testReadFileToString() throws Exception { File file = temporaryFolder.newFile("file.txt"); BufferedOutputStream out = null; try { out = new BufferedOutputStream(new FileOutputStream(file)); out.write(TEST_STRING.getBytes("UTF-8")); } finally { ParseIOUtils.closeQuietly(out); } assertEquals(TEST_STRING, ParseFileUtils.readFileToString(file, "UTF-8")); }
@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")); }
@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")); }
assertTrue(cachedFile.exists()); assertTrue(file.exists()); assertEquals("content", ParseFileUtils.readFileToString(cachedFile, "UTF-8"));
@Test public void testSaveAsyncSuccessWithByteArray() throws Exception { JSONObject json = new JSONObject(); json.put("name", "new_file_name"); json.put("url", "http://example.com"); String content = json.toString(); ParseHttpResponse mockResponse = new ParseHttpResponse.Builder() .setStatusCode(200) .setTotalSize((long) content.length()) .setContent(new ByteArrayInputStream(content.getBytes())) .build(); ParseHttpClient restClient = mock(ParseHttpClient.class); when(restClient.execute(any(ParseHttpRequest.class))).thenReturn(mockResponse); File root = temporaryFolder.getRoot(); ParseFileController controller = new ParseFileController(restClient, root); byte[] data = "hello".getBytes(); ParseFile.State state = new ParseFile.State.Builder() .name("file_name") .mimeType("mime_type") .build(); Task<ParseFile.State> task = controller.saveAsync(state, data, null, null, null); ParseFile.State result = ParseTaskUtils.wait(task); verify(restClient, times(1)).execute(any(ParseHttpRequest.class)); assertEquals("new_file_name", result.name()); assertEquals("http://example.com", result.url()); File file = new File(root, "new_file_name"); assertTrue(file.exists()); assertEquals("hello", ParseFileUtils.readFileToString(file, "UTF-8")); }
@Test public void testFetchAsyncCached() throws Exception { ParseHttpClient fileClient = mock(ParseHttpClient.class); File root = temporaryFolder.getRoot(); ParseFileController controller = new ParseFileController(null, root).fileClient(fileClient); File file = new File(root, "cached_file_name"); ParseFileUtils.writeStringToFile(file, "hello", "UTF-8"); ParseFile.State state = new ParseFile.State.Builder() .name("cached_file_name") .build(); Task<File> task = controller.fetchAsync(state, null, null, null); File result = ParseTaskUtils.wait(task); verify(fileClient, times(0)).execute(any(ParseHttpRequest.class)); assertEquals(file, result); assertEquals("hello", ParseFileUtils.readFileToString(result, "UTF-8")); }
@Test public void testFetchAsyncSuccess() throws Exception { byte[] data = "hello".getBytes(); ParseHttpResponse mockResponse = new ParseHttpResponse.Builder() .setStatusCode(200) .setTotalSize((long) data.length) .setContent(new ByteArrayInputStream(data)) .build(); ParseHttpClient fileClient = mock(ParseHttpClient.class); when(fileClient.execute(any(ParseHttpRequest.class))).thenReturn(mockResponse); // Make sure cache dir does not exist File root = new File(temporaryFolder.getRoot(), "cache"); assertFalse(root.exists()); ParseFileController controller = new ParseFileController(null, root).fileClient(fileClient); ParseFile.State state = new ParseFile.State.Builder() .name("file_name") .url("url") .build(); Task<File> task = controller.fetchAsync(state, null, null, null); File result = ParseTaskUtils.wait(task); verify(fileClient, times(1)).execute(any(ParseHttpRequest.class)); assertTrue(result.exists()); assertEquals("hello", ParseFileUtils.readFileToString(result, "UTF-8")); assertFalse(controller.getTempFile(state).exists()); }
new JSONObject(ParseFileUtils.readFileToString(configFile, "UTF-8")); Map<String, Object> decodedDiskConfigObject = (Map<String, Object>) ParseDecoder.get().decode(diskConfigJson);