Skip to content

Commit

Permalink
Merge pull request #306 from hazendaz/develop
Browse files Browse the repository at this point in the history
Trying again to cleanup some tests
  • Loading branch information
hazendaz committed May 7, 2023
2 parents 00de9a9 + b500da4 commit 114baa0
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ public InitializeCommand(SelectedOptions selectedOptions) {
}

@Override
@SuppressWarnings("serial")
public void execute(String... params) {
final File basePath = paths.getBasePath();
final File scriptPath = paths.getScriptPath();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,7 @@ class BaseCommandTest {
void testNonexistentResource() throws Exception {
String resource = "org/apache/ibatis/migration/commands/NoSuchFile.sql";
IOException e = assertThrows(IOException.class, () -> {
File src = Resources.getResourceAsFile(resource);
File dest = File.createTempFile("Out", ".sql");
try {
BaseCommand.copyTemplate(src, dest, null);
} finally {
dest.delete();
}
Resources.getResourceAsFile(resource);
});
assertEquals(e.getMessage(), "Could not find resource " + resource);
}
Expand All @@ -54,13 +48,10 @@ void testNonexistentResource() throws Exception {
void testNonexistentFileLinuxMac() throws Exception {
String srcPath = TestUtil.getTempDir().getAbsolutePath() + FileSystems.getDefault().getSeparator()
+ "NoSuchFile.sql";
File dest = File.createTempFile("Out", ".sql");
dest.deleteOnExit();
FileNotFoundException e = assertThrows(FileNotFoundException.class, () -> {
File dest = File.createTempFile("Out", ".sql");
try {
BaseCommand.copyTemplate(new File(srcPath), dest, null);
} finally {
dest.delete();
}
BaseCommand.copyTemplate(new File(srcPath), dest, null);
});
assertEquals(e.getMessage(), srcPath + " (No such file or directory)");
}
Expand All @@ -70,13 +61,10 @@ void testNonexistentFileLinuxMac() throws Exception {
void testNonexistentFileWindows() throws Exception {
String srcPath = TestUtil.getTempDir().getAbsolutePath() + FileSystems.getDefault().getSeparator()
+ "NoSuchFile.sql";
File dest = File.createTempFile("Out", ".sql");
dest.deleteOnExit();
FileNotFoundException e = assertThrows(FileNotFoundException.class, () -> {
File dest = File.createTempFile("Out", ".sql");
try {
BaseCommand.copyTemplate(new File(srcPath), dest, null);
} finally {
dest.delete();
}
BaseCommand.copyTemplate(new File(srcPath), dest, null);
});
assertEquals(e.getMessage(), srcPath + " (The system cannot find the file specified)");
}
Expand All @@ -85,62 +73,50 @@ void testNonexistentFileWindows() throws Exception {
void testCopyResource() throws Exception {
File src = Resources.getResourceAsFile("org/apache/ibatis/migration/commands/TestTemplate.sql");
File dest = File.createTempFile("Out", ".sql");
try {
BaseCommand.copyTemplate(src, dest, null);
assertTrue(contentOf(dest).contains("// ${var}"));
} finally {
dest.delete();
}
dest.deleteOnExit();
BaseCommand.copyTemplate(src, dest, null);
assertTrue(contentOf(dest).contains("// ${var}"));
}

@Test
void testCopyResourceWithVariables() throws Exception {
File src = Resources.getResourceAsFile("org/apache/ibatis/migration/commands/TestTemplate.sql");
File dest = File.createTempFile("Out", ".sql");
dest.deleteOnExit();
Properties variables = new Properties();
variables.put("var", "Some description");
try {
BaseCommand.copyTemplate(src, dest, variables);
assertTrue(contentOf(dest).contains("// Some description"));
} finally {
dest.delete();
}
BaseCommand.copyTemplate(src, dest, variables);
assertTrue(contentOf(dest).contains("// Some description"));
}

@Test
void testExternalFile() throws Exception {
File src = File.createTempFile("ExternalTemplate", ".sql");
src.deleteOnExit();
try (PrintWriter writer = new PrintWriter(src)) {
writer.println("// ${var}");
}

File dest = File.createTempFile("Out", ".sql");
try {
BaseCommand.copyTemplate(src, dest, null);
assertTrue(contentOf(dest).contains("// ${var}"));
} finally {
src.delete();
dest.delete();
}
dest.deleteOnExit();
BaseCommand.copyTemplate(src, dest, null);
assertTrue(contentOf(dest).contains("// ${var}"));
}

@Test
void testExternalFileWithVariables() throws Exception {
File src = File.createTempFile("ExternalTemplate", ".sql");
src.deleteOnExit();
try (PrintWriter writer = new PrintWriter(src)) {
writer.println("// ${var}");
}

File dest = File.createTempFile("Out", ".sql");
dest.deleteOnExit();
Properties variables = new Properties();
variables.put("var", "Some description");
try {
BaseCommand.copyTemplate(src, dest, variables);
assertTrue(contentOf(dest).contains("// Some description"));
} finally {
src.delete();
dest.delete();
}
BaseCommand.copyTemplate(src, dest, variables);
assertTrue(contentOf(dest).contains("// Some description"));
}

protected static String contentOf(File file) throws FileNotFoundException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ void onlyPopulatesCommandOnce() {
}

private String valuedOption(Options option, String aValue) {
return option(option) + "=" + aValue;
return option(option) + '=' + aValue;
}

private String option(Options option) {
Expand Down

0 comments on commit 114baa0

Please sign in to comment.