Skip to content

Commit

Permalink
fix: use newline to write
Browse files Browse the repository at this point in the history
  • Loading branch information
arifBurakDemiray committed Oct 10, 2023
1 parent a67ec2b commit 520d89e
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
*/
public class MigrationHelper {
protected static final String MIGRATION_VERSION_FILE_NAME = "migration_version";
private CtxCore ctx;
protected CtxCore ctx;
private final List<Supplier<Boolean>> migrations;
protected int appliedMigrationVersion = -1;
protected Log logger;
Expand Down Expand Up @@ -66,20 +66,26 @@ private void readMigrationVersion() {
}
}

private void updateMigrationVersion() {
protected void updateMigrationVersion() {
logger.i("[MigrationHelper] updateMigrationVersion, Updating migration version to version:[ " + appliedMigrationVersion + " ]");
File file = new File(ctx.getSdkStorageRootDirectory(), SDKStorage.FILE_NAME_PREFIX + SDKStorage.FILE_NAME_SEPARATOR + MIGRATION_VERSION_FILE_NAME);

try (BufferedWriter writer = new BufferedWriter(new FileWriter(file))) {
writer.write(String.valueOf(appliedMigrationVersion));
try { // Write the version to the file
writeVersionToFile(file);
logger.v("[MigrationHelper] writeFileContent, Wrote applied migration version to file");
} catch (IOException e) {
// Handle the error if writing fails
logger.e("[MigrationHelper] writeFileContent, Failed to write applied migration version to file: " + e.getMessage());
}
}

private boolean migration_DeleteConfigFile_00() {
protected void writeVersionToFile(File file) throws IOException {
try (BufferedWriter writer = new BufferedWriter(new FileWriter(file))) {
writer.write(String.valueOf(appliedMigrationVersion));
}
}

protected boolean migration_DeleteConfigFile_00() {
if (appliedMigrationVersion >= 0) {
logger.d("[MigrationHelper] migration_DeleteConfigFile_00, Migration already applied");
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

import static ly.count.sdk.java.internal.SDKStorage.FILE_NAME_PREFIX;
import static ly.count.sdk.java.internal.SDKStorage.FILE_NAME_SEPARATOR;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
Expand Down Expand Up @@ -85,10 +87,29 @@ public void applyMigrations_migrationAlreadyApplied() {
verify(migrationHelper.logger, times(1)).d("[MigrationHelper] migration_DeleteConfigFile_00, Migration already applied");
}

/**
* "updateMigrationVersion" with IO exception
* receives mock context and simulated function to throw IOException
* logger should log expected log
*/
@Test
public void updateMigrationVersion_IOException() throws IOException {
//prepare mock object
migrationHelper = spy(migrationHelper);
migrationHelper.ctx = TestUtils.getMockCtxCore();
//simulate function to throw exception
doThrow(new IOException("Simulated IOException")).when(migrationHelper).writeVersionToFile(any(File.class));
// Call the method that you want to test
migrationHelper.updateMigrationVersion();

// Verify that the logger's error method was called with the expected message
verify(migrationHelper.logger).e("[MigrationHelper] writeFileContent, Failed to write applied migration version to file: Simulated IOException");
}

private void writeToMvFile(Integer version) {
File file = new File(TestUtils.getTestSDirectory(), FILE_NAME_PREFIX + FILE_NAME_SEPARATOR + MigrationHelper.MIGRATION_VERSION_FILE_NAME);
try (BufferedWriter writer = new BufferedWriter(new FileWriter(file))) {
writer.write(String.valueOf(version));
writer.write(version + "\n");
} catch (IOException ignored) {
//do nothing
}
Expand Down

0 comments on commit 520d89e

Please sign in to comment.