View Javadoc
1   /*
2    *    Copyright 2009-2023 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    *       https://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.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     * @throws java.lang.Exception
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 }