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.assertEquals;
19 import static org.junit.jupiter.api.Assertions.assertTrue;
20 import static org.junit.jupiter.api.Assertions.fail;
21
22 import java.io.File;
23 import java.io.FileNotFoundException;
24 import java.io.FileWriter;
25 import java.io.IOException;
26 import java.nio.charset.StandardCharsets;
27
28 import org.junit.jupiter.api.AfterEach;
29 import org.junit.jupiter.api.BeforeEach;
30 import org.junit.jupiter.api.Test;
31
32 class ExternalResourcesTest {
33
34 private File sourceFile;
35 private File destFile;
36 private File badFile;
37 private File tempFile;
38
39
40
41
42 @BeforeEach
43 void setUp() throws Exception {
44 tempFile = File.createTempFile("migration", "properties");
45 tempFile.canWrite();
46 sourceFile = File.createTempFile("test1", "sql");
47 destFile = File.createTempFile("test2", "sql");
48 }
49
50 @Test
51 void testcopyExternalResource() {
52
53 try {
54 ExternalResources.copyExternalResource(sourceFile, destFile);
55 } catch (IOException e) {
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 testGetConfiguredTemplate() {
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 }