View Javadoc
1   /*
2    *    Copyright 2010-2025 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.migration.commands;
17  
18  import static org.junit.jupiter.api.Assertions.assertEquals;
19  import static org.junit.jupiter.api.Assertions.assertThrows;
20  import static org.junit.jupiter.api.Assertions.assertTrue;
21  
22  import java.io.File;
23  import java.io.FileNotFoundException;
24  import java.io.IOException;
25  import java.io.PrintWriter;
26  import java.nio.file.FileSystems;
27  import java.nio.file.NoSuchFileException;
28  import java.nio.file.Path;
29  import java.util.Properties;
30  import java.util.Scanner;
31  
32  import org.apache.ibatis.migration.io.Resources;
33  import org.apache.ibatis.migration.utils.TestUtil;
34  import org.junit.jupiter.api.Test;
35  
36  class BaseCommandTest {
37    @Test
38    void testNonexistentResource() throws Exception {
39      String resource = "org/apache/ibatis/migration/commands/NoSuchFile.sql";
40      IOException e = assertThrows(IOException.class, () -> {
41        Resources.getResourceAsFile(resource);
42      });
43      assertEquals(e.getMessage(), "Could not find resource " + resource);
44    }
45  
46    @Test
47    void testNonexistentFile() throws Exception {
48      String srcPath = TestUtil.getTempDir().getAbsolutePath() + FileSystems.getDefault().getSeparator()
49          + "NoSuchFile.sql";
50      File dest = File.createTempFile("Out", ".sql");
51      dest.deleteOnExit();
52      NoSuchFileException e = assertThrows(NoSuchFileException.class, () -> {
53        BaseCommand.copyTemplate(Path.of(srcPath).toFile(), dest, null);
54      });
55      assertEquals(e.getMessage(), srcPath);
56    }
57  
58    @Test
59    void testCopyResource() throws Exception {
60      File src = Resources.getResourceAsFile("org/apache/ibatis/migration/commands/TestTemplate.sql");
61      File dest = File.createTempFile("Out", ".sql");
62      dest.deleteOnExit();
63      BaseCommand.copyTemplate(src, dest, null);
64      assertTrue(contentOf(dest).contains("// ${var}"));
65    }
66  
67    @Test
68    void testCopyResourceWithVariables() throws Exception {
69      File src = Resources.getResourceAsFile("org/apache/ibatis/migration/commands/TestTemplate.sql");
70      File dest = File.createTempFile("Out", ".sql");
71      dest.deleteOnExit();
72      Properties variables = new Properties();
73      variables.put("var", "Some description");
74      BaseCommand.copyTemplate(src, dest, variables);
75      assertTrue(contentOf(dest).contains("// Some description"));
76    }
77  
78    @Test
79    void testExternalFile() throws Exception {
80      File src = File.createTempFile("ExternalTemplate", ".sql");
81      src.deleteOnExit();
82      try (PrintWriter writer = new PrintWriter(src)) {
83        writer.println("// ${var}");
84      }
85  
86      File dest = File.createTempFile("Out", ".sql");
87      dest.deleteOnExit();
88      BaseCommand.copyTemplate(src, dest, null);
89      assertTrue(contentOf(dest).contains("// ${var}"));
90    }
91  
92    @Test
93    void testExternalFileWithVariables() throws Exception {
94      File src = File.createTempFile("ExternalTemplate", ".sql");
95      src.deleteOnExit();
96      try (PrintWriter writer = new PrintWriter(src)) {
97        writer.println("// ${var}");
98      }
99  
100     File dest = File.createTempFile("Out", ".sql");
101     dest.deleteOnExit();
102     Properties variables = new Properties();
103     variables.put("var", "Some description");
104     BaseCommand.copyTemplate(src, dest, variables);
105     assertTrue(contentOf(dest).contains("// Some description"));
106   }
107 
108   protected static String contentOf(File file) throws FileNotFoundException {
109     try (Scanner scanner = new Scanner(file)) {
110       return scanner.useDelimiter("\\Z").next();
111     }
112   }
113 }