1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.ibatis.migration.commands;
17
18 import static org.junit.jupiter.api.Assertions.assertEquals;
19 import static org.junit.jupiter.api.Assertions.assertThrows;
20 import static org.junit.jupiter.api.Assertions.assertTrue;
21
22 import java.io.File;
23 import java.io.FileNotFoundException;
24 import java.io.IOException;
25 import java.io.PrintWriter;
26 import java.nio.file.FileSystems;
27 import java.nio.file.NoSuchFileException;
28 import java.nio.file.Path;
29 import java.util.Properties;
30 import java.util.Scanner;
31
32 import org.apache.ibatis.migration.io.Resources;
33 import org.apache.ibatis.migration.utils.TestUtil;
34 import org.junit.jupiter.api.Test;
35
36 class BaseCommandTest {
37 @Test
38 void testNonexistentResource() throws Exception {
39 String resource = "org/apache/ibatis/migration/commands/NoSuchFile.sql";
40 IOException e = assertThrows(IOException.class, () -> {
41 Resources.getResourceAsFile(resource);
42 });
43 assertEquals(e.getMessage(), "Could not find resource " + resource);
44 }
45
46 @Test
47 void testNonexistentFile() throws Exception {
48 String srcPath = TestUtil.getTempDir().getAbsolutePath() + FileSystems.getDefault().getSeparator()
49 + "NoSuchFile.sql";
50 File dest = File.createTempFile("Out", ".sql");
51 dest.deleteOnExit();
52 NoSuchFileException e = assertThrows(NoSuchFileException.class, () -> {
53 BaseCommand.copyTemplate(Path.of(srcPath).toFile(), dest, null);
54 });
55 assertEquals(e.getMessage(), srcPath);
56 }
57
58 @Test
59 void testCopyResource() throws Exception {
60 File src = Resources.getResourceAsFile("org/apache/ibatis/migration/commands/TestTemplate.sql");
61 File dest = File.createTempFile("Out", ".sql");
62 dest.deleteOnExit();
63 BaseCommand.copyTemplate(src, dest, null);
64 assertTrue(contentOf(dest).contains("// ${var}"));
65 }
66
67 @Test
68 void testCopyResourceWithVariables() throws Exception {
69 File src = Resources.getResourceAsFile("org/apache/ibatis/migration/commands/TestTemplate.sql");
70 File dest = File.createTempFile("Out", ".sql");
71 dest.deleteOnExit();
72 Properties variables = new Properties();
73 variables.put("var", "Some description");
74 BaseCommand.copyTemplate(src, dest, variables);
75 assertTrue(contentOf(dest).contains("// Some description"));
76 }
77
78 @Test
79 void testExternalFile() throws Exception {
80 File src = File.createTempFile("ExternalTemplate", ".sql");
81 src.deleteOnExit();
82 try (PrintWriter writer = new PrintWriter(src)) {
83 writer.println("// ${var}");
84 }
85
86 File dest = File.createTempFile("Out", ".sql");
87 dest.deleteOnExit();
88 BaseCommand.copyTemplate(src, dest, null);
89 assertTrue(contentOf(dest).contains("// ${var}"));
90 }
91
92 @Test
93 void testExternalFileWithVariables() throws Exception {
94 File src = File.createTempFile("ExternalTemplate", ".sql");
95 src.deleteOnExit();
96 try (PrintWriter writer = new PrintWriter(src)) {
97 writer.println("// ${var}");
98 }
99
100 File dest = File.createTempFile("Out", ".sql");
101 dest.deleteOnExit();
102 Properties variables = new Properties();
103 variables.put("var", "Some description");
104 BaseCommand.copyTemplate(src, dest, variables);
105 assertTrue(contentOf(dest).contains("// Some description"));
106 }
107
108 protected static String contentOf(File file) throws FileNotFoundException {
109 try (Scanner scanner = new Scanner(file)) {
110 return scanner.useDelimiter("\\Z").next();
111 }
112 }
113 }