View Javadoc
1   /*
2    *    Copyright 2009-2021 the original author or authors.
3    *
4    *    Licensed under the Apache License, Version 2.0 (the "License");
5    *    you may not use this file except in compliance with the License.
6    *    You may obtain a copy of the License at
7    *
8    *       http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *    Unless required by applicable law or agreed to in writing, software
11   *    distributed under the License is distributed on an "AS IS" BASIS,
12   *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *    See the License for the specific language governing permissions and
14   *    limitations under the License.
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     * @throws java.lang.Exception
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 }