View Javadoc

1   /*
2    * $Id: BaseTemplateEngineTest.java 651946 2008-04-27 13:41:38Z apetrelli $
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  package org.apache.struts2.components.template;
23  
24  import java.io.File;
25  import java.net.URL;
26  import java.util.Map;
27  
28  import junit.framework.TestCase;
29  
30  /***
31   * Test case for BaseTemplateEngine
32   */
33  public class BaseTemplateEngineTest extends TestCase {
34  
35  public void testGetThemePropsThroughFileSystem() throws Exception {
36  
37          URL dummyResourceUrl = getClass().getResource("dummy.properties");
38          File dummyResourceFile = new File(dummyResourceUrl.getFile().replaceAll("%20", " "));
39          String themePropertiesDir = dummyResourceFile.getParent();
40  
41          System.out.println("dummy resource url="+dummyResourceUrl);
42          System.out.println("resource file="+dummyResourceFile);
43          System.out.println("theme properties dir="+themePropertiesDir);
44  
45          assertTrue(dummyResourceFile.exists());
46          assertNotNull(themePropertiesDir);
47  
48          Template template = new Template(themePropertiesDir, "theme1", "template1");
49  
50          TemplateEngine templateEngine = new InnerBaseTemplateEngine("themeThroughFileSystem.properties");
51          Map propertiesMap = templateEngine.getThemeProps(template);
52  
53          assertNotNull(propertiesMap);
54          assertTrue(propertiesMap.size() > 0);
55  
56      }
57  
58      public void testGetThemePropsThroughClasspath() throws Exception {
59  
60          Template template = new Template("org/apache/struts2/components/template", "theme1", "template2");
61          TemplateEngine templateEngine = new InnerBaseTemplateEngine("themeThroughClassPath.properties");
62          Map propertiesMap = templateEngine.getThemeProps(template);
63  
64          assertNotNull(propertiesMap);
65          assertTrue(propertiesMap.size() > 0);
66      }
67  
68      public class InnerBaseTemplateEngine extends BaseTemplateEngine {
69  
70          private String themePropertiesFileName;
71  
72          public InnerBaseTemplateEngine(String themePropertiesFileName) {
73              this.themePropertiesFileName = themePropertiesFileName;
74          }
75  
76          protected String getSuffix() {
77              return "ftl";
78          }
79  
80          public void renderTemplate(TemplateRenderingContext templateContext) throws Exception {
81          }
82  
83          protected String getThemePropertiesFileName() {
84              return this.themePropertiesFileName;
85          }
86      }
87  }