1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.ibatis.io;
17
18 import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
19 import static org.junit.jupiter.api.Assertions.assertEquals;
20 import static org.junit.jupiter.api.Assertions.assertTrue;
21 import static org.junit.jupiter.api.Assertions.fail;
22
23 import java.io.File;
24 import java.io.FileNotFoundException;
25 import java.io.FileWriter;
26 import java.io.IOException;
27 import java.nio.charset.StandardCharsets;
28 import java.nio.file.Files;
29
30 import org.junit.jupiter.api.AfterEach;
31 import org.junit.jupiter.api.BeforeEach;
32 import org.junit.jupiter.api.Test;
33
34 class ExternalResourcesTest {
35
36 private File sourceFile;
37 private File destFile;
38 private File badFile;
39 private File tempFile;
40
41
42
43
44 @BeforeEach
45 void setUp() throws Exception {
46 tempFile = Files.createTempFile("migration", "properties").toFile();
47 tempFile.canWrite();
48 sourceFile = Files.createTempFile("test1", "sql").toFile();
49 destFile = Files.createTempFile("test2", "sql").toFile();
50 }
51
52 @Test
53 void testcopyExternalResource() {
54 assertDoesNotThrow(() -> {
55 ExternalResources.copyExternalResource(sourceFile, destFile);
56 });
57
58 }
59
60 @Test
61 void testcopyExternalResource_fileNotFound() {
62
63 try {
64 badFile = new File("/tmp/nofile.sql");
65 ExternalResources.copyExternalResource(badFile, destFile);
66 } catch (IOException e) {
67 assertTrue(e instanceof FileNotFoundException);
68 }
69
70 }
71
72 @Test
73 void testcopyExternalResource_emptyStringAsFile() {
74
75 try {
76 badFile = new File(" ");
77 ExternalResources.copyExternalResource(badFile, destFile);
78 } catch (Exception e) {
79 assertTrue(e instanceof FileNotFoundException);
80 }
81
82 }
83
84 @Test
85 void getConfiguredTemplate() {
86 String templateName = "";
87
88 try (FileWriter fileWriter = new FileWriter(tempFile, StandardCharsets.UTF_8)) {
89 fileWriter.append("new_command.template=templates/col_new_template_migration.sql");
90 fileWriter.flush();
91 templateName = ExternalResources.getConfiguredTemplate(tempFile.getAbsolutePath(), "new_command.template");
92 assertEquals("templates/col_new_template_migration.sql", templateName);
93 } catch (Exception e) {
94 fail("Test failed with execption: " + e.getMessage());
95 }
96 }
97
98 @AfterEach
99 void cleanUp() {
100 sourceFile.delete();
101 destFile.delete();
102 tempFile.delete();
103 }
104 }