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