View Javadoc

1   /*
2    * $Id: BaseTemplateEngineTest.java 440597 2006-09-06 03:34:39Z wsmoak $
3    *
4    * Copyright 2006 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * 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, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.struts2.components.template;
19  
20  import java.io.File;
21  import java.net.URL;
22  import java.util.Map;
23  
24  import junit.framework.TestCase;
25  
26  /***
27   * Test case for BaseTemplateEngine
28   */
29  public class BaseTemplateEngineTest extends TestCase {
30  
31  public void testGetThemePropsThroughFileSystem() throws Exception {
32  		
33  		URL dummyResourceUrl = getClass().getResource("dummy.properties");
34  		File dummyResourceFile = new File(dummyResourceUrl.getFile());
35  		String themePropertiesDir = dummyResourceFile.getParent();
36  		
37  		System.out.println("dummy resource url="+dummyResourceUrl);
38  		System.out.println("resource file="+dummyResourceFile);
39  		System.out.println("theme properties dir="+themePropertiesDir);
40  		
41  		assertTrue(dummyResourceFile.exists());
42  		assertNotNull(themePropertiesDir);
43  		
44  		Template template = new Template(themePropertiesDir, "theme1", "template1");
45  		
46  		TemplateEngine templateEngine = new InnerBaseTemplateEngine("themeThroughFileSystem.properties");
47  		Map propertiesMap = templateEngine.getThemeProps(template);
48  		
49  		assertNotNull(propertiesMap);
50  		assertTrue(propertiesMap.size() > 0);
51  		
52  	}
53  	
54  	public void testGetThemePropsThroughClasspath() throws Exception {
55  		
56  		Template template = new Template("org/apache/struts2/components/template", "theme1", "template2");
57  		TemplateEngine templateEngine = new InnerBaseTemplateEngine("themeThroughClassPath.properties");
58  		Map propertiesMap = templateEngine.getThemeProps(template);
59  		
60  		assertNotNull(propertiesMap);
61  		assertTrue(propertiesMap.size() > 0);
62  	}
63  	
64  	public class InnerBaseTemplateEngine extends BaseTemplateEngine {
65  		
66  		private String themePropertiesFileName;
67  		
68  		public InnerBaseTemplateEngine(String themePropertiesFileName) {
69  			this.themePropertiesFileName = themePropertiesFileName;
70  		}
71  		
72  		protected String getSuffix() {
73  			return "ftl";
74  		}
75  
76  		public void renderTemplate(TemplateRenderingContext templateContext) throws Exception {
77  		}
78  		
79  		protected String getThemePropertiesFileName() {
80  			return this.themePropertiesFileName;
81  		}
82  	}
83  }