Continued work on TemplateLanguage configuration and new file extensions: Renamed...
[freemarker.git] / freemarker-core-test / src / test / java / org / apache / freemarker / core / templateresolver / FileTemplateLoaderTest.java
1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19 package org.apache.freemarker.core.templateresolver;
20
21 import static org.hamcrest.Matchers.*;
22 import static org.junit.Assert.*;
23
24 import java.io.File;
25 import java.io.IOException;
26
27 import org.apache.commons.io.FileUtils;
28 import org.apache.commons.lang3.SystemUtils;
29 import org.apache.freemarker.core.Configuration;
30 import org.apache.freemarker.core.TemplateNotFoundException;
31 import org.apache.freemarker.core.templateresolver.impl.FileTemplateLoader;
32 import org.apache.freemarker.test.TestConfigurationBuilder;
33 import org.junit.After;
34 import org.junit.Before;
35 import org.junit.Test;
36
37 import com.google.common.io.Files;
38
39 public class FileTemplateLoaderTest {
40
41 private File templateRootDir;
42
43 private Configuration cfg;
44
45 @Before
46 public void setup() throws IOException {
47 templateRootDir = Files.createTempDir();
48 File sub1Dir = new File(templateRootDir, "sub1");
49 File sub2Dir = new File(sub1Dir, "sub2");
50 if (!sub2Dir.mkdirs()) {
51 throw new IOException("Failed to invoke subdirectories");
52 }
53 File tFile = new File(sub2Dir, "t.f3ah");
54 FileUtils.write(tFile, "foo");
55
56 cfg = new TestConfigurationBuilder().templateLoader(new FileTemplateLoader(templateRootDir)).build();
57 }
58
59 @Test
60 public void testSuccessful() throws Exception {
61 for (int i = 0; i < 2; i++) {
62 assertEquals("foo", cfg.getTemplate("sub1/sub2/t.f3ah").toString());
63 }
64 }
65
66 @Test
67 public void testSuccessful2() throws Exception {
68 ((FileTemplateLoader) cfg.getTemplateLoader()).setEmulateCaseSensitiveFileSystem(true);
69 for (int i = 0; i < 2; i++) {
70 cfg.clearTemplateCache();
71 assertEquals("foo", cfg.getTemplate("sub1/sub2/t.f3ah").toString());
72 }
73 }
74
75
76 @Test
77 public void testNotFound() throws Exception {
78 for (int i = 0; i < 2; i++) {
79 try {
80 cfg.getTemplate("sub1X/sub2/t.f3ah");
81 fail();
82 } catch (TemplateNotFoundException e) {
83 assertThat(e.getMessage(), containsString("sub1X"));
84 assertNull(e.getCause());
85 }
86 }
87 }
88
89 @Test
90 public void testCaseSensitivity() throws Exception {
91 for (boolean emuCaseSensFS : new boolean[] { false, true }) {
92 for (String nameWithBadCase : new String[] {
93 "SUB1/sub2/t.f3ah", "sub1/SUB2/t.f3ah", "sub1/sub2/T.F3AH" }) {
94 ((FileTemplateLoader) cfg.getTemplateLoader()).setEmulateCaseSensitiveFileSystem(emuCaseSensFS);
95 cfg.clearTemplateCache();
96
97 if ((SystemUtils.IS_OS_WINDOWS || SystemUtils.IS_OS_MAC_OSX) && !emuCaseSensFS) {
98 assertEquals("foo", cfg.getTemplate(nameWithBadCase).toString());
99 } else {
100 assertEquals("foo", cfg.getTemplate(nameWithBadCase.toLowerCase()).toString());
101 try {
102 cfg.getTemplate(nameWithBadCase);
103 fail();
104 } catch (TemplateNotFoundException e) {
105 assertThat(e.getMessage(), containsString(nameWithBadCase));
106 assertNull(e.getCause());
107 }
108 }
109 }
110 }
111 }
112
113 @Test
114 public void testDefault() throws IOException {
115 assertFalse(new FileTemplateLoader(templateRootDir).getEmulateCaseSensitiveFileSystem());
116 }
117
118 @After
119 public void tearDown() throws IOException {
120 FileUtils.deleteDirectory(templateRootDir);
121 }
122
123 }