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