summaryrefslogtreecommitdiff
path: root/app/src/main
diff options
context:
space:
mode:
Diffstat (limited to 'app/src/main')
-rw-r--r--app/src/main/java/se/leap/bitmaskclient/base/utils/FileHelper.java41
-rw-r--r--app/src/main/java/se/leap/bitmaskclient/base/utils/InputStreamHelper.java24
2 files changed, 60 insertions, 5 deletions
diff --git a/app/src/main/java/se/leap/bitmaskclient/base/utils/FileHelper.java b/app/src/main/java/se/leap/bitmaskclient/base/utils/FileHelper.java
index eb1c255c..f1d86876 100644
--- a/app/src/main/java/se/leap/bitmaskclient/base/utils/FileHelper.java
+++ b/app/src/main/java/se/leap/bitmaskclient/base/utils/FileHelper.java
@@ -2,6 +2,8 @@ package se.leap.bitmaskclient.base.utils;
import android.content.Context;
+import androidx.annotation.VisibleForTesting;
+
import java.io.BufferedReader;
import java.io.File;
import java.io.FileWriter;
@@ -9,19 +11,50 @@ import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
+import de.blinkt.openvpn.core.NativeUtils;
+
/**
* Created by cyberta on 18.03.18.
*/
public class FileHelper {
+
+ public interface FileHelperInterface {
+ File createFile(File dir, String fileName);
+ void persistFile(File file, String content) throws IOException;
+ }
+
+ public static class DefaultFileHelper implements FileHelperInterface {
+ @Override
+ public File createFile(File dir, String fileName) {
+ return new File(dir, fileName);
+ }
+
+ @Override
+ public void persistFile(File file, String content) throws IOException {
+ FileWriter writer = new FileWriter(file);
+ writer.write(content);
+ writer.close();
+ }
+ }
+
+ private static FileHelperInterface instance = new DefaultFileHelper();
+
+ @VisibleForTesting
+ public FileHelper(FileHelperInterface helperInterface) {
+ if (!NativeUtils.isUnitTest()) {
+ throw new IllegalStateException("FileHelper injected with FileHelperInterface outside of an unit test");
+ }
+
+ instance = helperInterface;
+ }
+
public static File createFile(File dir, String fileName) {
- return new File(dir, fileName);
+ return instance.createFile(dir, fileName);
}
public static void persistFile(File file, String content) throws IOException {
- FileWriter writer = new FileWriter(file);
- writer.write(content);
- writer.close();
+ instance.persistFile(file, content);
}
public static String readPublicKey(Context context) {
diff --git a/app/src/main/java/se/leap/bitmaskclient/base/utils/InputStreamHelper.java b/app/src/main/java/se/leap/bitmaskclient/base/utils/InputStreamHelper.java
index 8e6273a7..6dfe0861 100644
--- a/app/src/main/java/se/leap/bitmaskclient/base/utils/InputStreamHelper.java
+++ b/app/src/main/java/se/leap/bitmaskclient/base/utils/InputStreamHelper.java
@@ -8,14 +8,36 @@ import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
+import de.blinkt.openvpn.core.NativeUtils;
+
/**
* Created by cyberta on 18.03.18.
*/
public class InputStreamHelper {
+ public interface InputStreamHelperInterface {
+ InputStream getInputStreamFrom(String filePath) throws FileNotFoundException;
+
+ }
+
+ private static InputStreamHelperInterface instance = new DefaultInputStreamHelper();
+
+ private static class DefaultInputStreamHelper implements InputStreamHelperInterface {
+ @Override
+ public InputStream getInputStreamFrom(String filePath) throws FileNotFoundException {
+ return new FileInputStream(filePath);
+ }
+ }
+
+ public InputStreamHelper(InputStreamHelperInterface helperInterface) {
+ if (!NativeUtils.isUnitTest()) {
+ throw new IllegalStateException("InputStreamHelper injected with InputStreamHelperInterface outside of an unit test");
+ }
+ instance = helperInterface;
+ }
//allows us to mock FileInputStream
public static InputStream getInputStreamFrom(String filePath) throws FileNotFoundException {
- return new FileInputStream(filePath);
+ return instance.getInputStreamFrom(filePath);
}
public static String loadInputStreamAsString(InputStream is) {