Skip to content

Commit

Permalink
feat: read file content tests
Browse files Browse the repository at this point in the history
  • Loading branch information
arifBurakDemiray committed Sep 21, 2023
1 parent c09a1fd commit 89e2a77
Showing 1 changed file with 68 additions and 0 deletions.
68 changes: 68 additions & 0 deletions sdk-java/src/test/java/ly/count/sdk/java/internal/UtilsTests.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package ly.count.sdk.java.internal;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collection;
Expand All @@ -18,6 +21,8 @@ public class UtilsTests {

Log logger;

static final String TEST_FILE_NAME = "testFile";

@Before
public void setupEveryTest() {
logger = new Log(Config.LoggingLevel.VERBOSE, null);
Expand Down Expand Up @@ -234,4 +239,67 @@ public void isValidDataType() {
Assert.assertFalse(Utils.isValidDataType(new ArrayList<>()));
Assert.assertFalse(Utils.isValidDataType(new HashMap<>()));
}

/**
* It checks if the "readFileContent" method is called.
* And if the created file is read correctly.
*/
@Test
public void readFileContent() throws IOException {
String fileName = "testFile";
String fileContent = "testContent";

File file = new File(fileName);
file.createNewFile();
FileWriter writer = new FileWriter(file);
writer.write(fileContent);
writer.close();

String result = Utils.readFileContent(file);
//delete file
file.delete();
Assert.assertEquals(fileContent, result);
}

/**
* If the file does not exist,
* the method should return an empty string.
*/
@Test
public void readFileContent_fileNotExist() throws IOException {
String fileName = "testFile";
String fileContent = "testContent";

File file = new File(fileName);

String result = Utils.readFileContent(file);

Assert.assertNotEquals(fileContent, result);
Assert.assertEquals("", result);
}

/**
* If the file is not readable for some reason,
* the method should throw exception.
*/
@Test(expected = IOException.class)
public void readFileContent_fileNotReadable() throws IOException {
try {
String fileContent = "testContent";

File file = new File(TEST_FILE_NAME);
file.createNewFile();
FileWriter writer = new FileWriter(file);
writer.write(fileContent);
writer.close();
file.setReadable(false);

Utils.readFileContent(file);
} finally {
File file = new File(TEST_FILE_NAME);
if (file.exists()) {
file.delete();
}
}
}
}

0 comments on commit 89e2a77

Please sign in to comment.