Bug 1267468 - Move test writeJSONObjectToFile -> FileUtils. r=sebastian
MozReview-Commit-ID: GRVzaHYbEhI
--- a/mobile/android/base/java/org/mozilla/gecko/util/FileUtils.java
+++ b/mobile/android/base/java/org/mozilla/gecko/util/FileUtils.java
@@ -10,16 +10,17 @@ import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.FilenameFilter;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.nio.charset.Charset;
import java.util.Scanner;
+import org.json.JSONObject;
import org.mozilla.gecko.annotation.RobocopTarget;
public class FileUtils {
private static final String LOGTAG = "GeckoFileUtils";
/*
* A basic Filter for checking a filename and age.
**/
static public class NameAndAgeFilter implements FilenameFilter {
@@ -96,16 +97,24 @@ public class FileUtils {
} finally {
if (scanner != null) {
scanner.close();
}
}
}
/**
+ * A generic solution to write a JSONObject to a file.
+ * See {@link #writeStringToFile(File, String)} for more details.
+ */
+ public static void writeJSONObjectToFile(final File file, final JSONObject obj) throws IOException {
+ writeStringToFile(file, obj.toString());
+ }
+
+ /**
* A generic solution to write to a File - the given file will be overwritten.
* See {@link #writeStringToOutputStreamAndCloseStream(OutputStream, String)} for more details.
*/
public static void writeStringToFile(final File file, final String str) throws IOException {
writeStringToOutputStreamAndCloseStream(new FileOutputStream(file, false), str);
}
/**
@@ -120,10 +129,9 @@ public class FileUtils {
throws IOException {
final OutputStreamWriter writer = new OutputStreamWriter(outputStream, Charset.forName("UTF-8"));
try {
writer.write(str);
} finally {
writer.close();
}
}
-
}
--- a/mobile/android/tests/background/junit4/src/org/mozilla/gecko/TestGeckoProfile.java
+++ b/mobile/android/tests/background/junit4/src/org/mozilla/gecko/TestGeckoProfile.java
@@ -123,17 +123,17 @@ public class TestGeckoProfile {
}
@Test
public void testGetClientIdMissingClientIdJSONAttr() throws Exception {
final String validClientId = "905de1c0-0ea6-4a43-95f9-6170035f5a82";
final JSONObject objMissingClientId = new JSONObject();
objMissingClientId.put("irrelevantKey", validClientId);
assertTrue("Created the parent dirs of the client ID file", clientIdFile.getParentFile().mkdirs());
- FileUtil.writeJSONObjectToFile(clientIdFile, objMissingClientId);
+ FileUtils.writeJSONObjectToFile(clientIdFile, objMissingClientId);
final String clientIdForMissingAttr = profile.getClientId();
assertValidClientId(clientIdForMissingAttr);
assertNotEquals("Did not use other attr when JSON attr was missing", validClientId, clientIdForMissingAttr);
}
@Test
public void testGetClientIdInvalidIdFileFormat() throws Exception {
@@ -205,17 +205,17 @@ public class TestGeckoProfile {
}
}
@Test
public void testGetProfileCreationDateFromTimesFile() throws Exception {
final long expectedDate = System.currentTimeMillis();
final JSONObject expectedObj = new JSONObject();
expectedObj.put(PROFILE_CREATION_DATE_JSON_ATTR, expectedDate);
- FileUtil.writeJSONObjectToFile(timesFile, expectedObj);
+ FileUtils.writeJSONObjectToFile(timesFile, expectedObj);
final Context context = RuntimeEnvironment.application;
final long actualDate = profile.getAndPersistProfileCreationDate(context);
assertEquals("Date from disk equals date inserted to disk", expectedDate, actualDate);
final long actualDateFromDisk = readProfileCreationDateFromFile(timesFile);
assertEquals("Date in times.json has not changed after accessing profile creation date",
expectedDate, actualDateFromDisk);
@@ -245,11 +245,11 @@ public class TestGeckoProfile {
private String readClientIdFromFile(final File file) throws Exception {
final JSONObject obj = FileUtil.readJSONObjectFromFile(file);
return obj.getString(CLIENT_ID_JSON_ATTR);
}
private void writeClientIdToFile(final File file, final String clientId) throws Exception {
final JSONObject obj = new JSONObject();
obj.put(CLIENT_ID_JSON_ATTR, clientId);
- FileUtil.writeJSONObjectToFile(file, obj);
+ FileUtils.writeJSONObjectToFile(file, obj);
}
}
--- a/mobile/android/tests/background/junit4/src/org/mozilla/gecko/helpers/FileUtil.java
+++ b/mobile/android/tests/background/junit4/src/org/mozilla/gecko/helpers/FileUtil.java
@@ -22,14 +22,9 @@ public class FileUtil {
while (scanner.hasNext()) {
builder.append(scanner.next());
}
} finally {
scanner.close();
}
return new JSONObject(builder.toString());
}
-
- public static void writeJSONObjectToFile(final File file, final JSONObject obj) throws Exception {
- FileUtils.writeStringToFile(file, obj.toString());
- }
-
}
--- a/mobile/android/tests/background/junit4/src/org/mozilla/gecko/util/TestFileUtils.java
+++ b/mobile/android/tests/background/junit4/src/org/mozilla/gecko/util/TestFileUtils.java
@@ -1,16 +1,17 @@
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, you can obtain one at http://mozilla.org/MPL/2.0/.
*/
package org.mozilla.gecko.util;
+import org.json.JSONObject;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.junit.runner.RunWith;
import org.mozilla.gecko.background.testhelpers.TestRunner;
import java.io.File;
@@ -64,16 +65,37 @@ public class TestFileUtils {
public void testWriteStringToFile() throws Exception {
final String expected = "String to write contains hard characters: !\n \\s..\"'\u00f1";
FileUtils.writeStringToFile(testFile, expected);
assertTrue("Written file exists", testFile.exists());
assertEquals("Read data equals written data", expected, readStringFromFile(testFile, expected.length()));
}
+ @Test
+ public void testWriteJSONObjectToFile() throws Exception {
+ final JSONObject expected = new JSONObject()
+ .put("int", 1)
+ .put("str", "1")
+ .put("bool", true)
+ .put("null", JSONObject.NULL)
+ .put("raw null", null);
+ FileUtils.writeJSONObjectToFile(testFile, expected);
+
+ assertTrue("Written file exists", testFile.exists());
+
+ // JSONObject.equals compares references so we have to assert each key individually. >:(
+ final JSONObject actual = new JSONObject(readStringFromFile(testFile, (int) testFile.length()));
+ assertEquals(1, actual.getInt("int"));
+ assertEquals("1", actual.getString("str"));
+ assertEquals(true, actual.getBoolean("bool"));
+ assertEquals(JSONObject.NULL, actual.get("null"));
+ assertFalse(actual.has("raw null"));
+ }
+
// Since the read methods may not be tested yet.
private static String readStringFromFile(final File file, final int bufferLen) throws IOException {
final char[] buffer = new char[bufferLen];
try (InputStreamReader reader = new InputStreamReader(new FileInputStream(file), Charset.forName("UTF-8"))) {
reader.read(buffer, 0, buffer.length);
}
return new String(buffer);
}