1   // 
2   //Copyright (c) 2003, Caltha - Gajda, Krzewski, Mach, Potempski Sp.J. 
3   //All rights reserved. 
4   //   
5   //Redistribution and use in source and binary forms, with or without modification,  
6   //are permitted provided that the following conditions are met: 
7   //   
8   //* Redistributions of source code must retain the above copyright notice,  
9   //this list of conditions and the following disclaimer. 
10  //* Redistributions in binary form must reproduce the above copyright notice,  
11  //this list of conditions and the following disclaimer in the documentation  
12  //and/or other materials provided with the distribution. 
13  //* Neither the name of the Caltha - Gajda, Krzewski, Mach, Potempski Sp.J.  
14  //nor the names of its contributors may be used to endorse or promote products  
15  //derived from this software without specific prior written permission. 
16  // 
17  //THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"  
18  //AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED  
19  //WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
20  //IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,  
21  //INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,  
22  //BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, 
23  //OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,  
24  //WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)  
25  //ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE  
26  //POSSIBILITY OF SUCH DAMAGE. 
27  //
28  
29  package org.objectledge.templating.velocity;
30  
31  import java.io.StringReader;
32  import java.io.StringWriter;
33  
34  import javax.xml.parsers.DocumentBuilder;
35  import javax.xml.parsers.DocumentBuilderFactory;
36  
37  import org.apache.log4j.LogManager;
38  import org.apache.log4j.Logger;
39  import org.jcontainer.dna.Configuration;
40  import org.jcontainer.dna.impl.Log4JLogger;
41  import org.objectledge.context.Context;
42  import org.objectledge.filesystem.ClasspathFileSystemProvider;
43  import org.objectledge.filesystem.FileSystem;
44  import org.objectledge.filesystem.FileSystemProvider;
45  import org.objectledge.filesystem.LocalFileSystemProvider;
46  import org.objectledge.logging.LedgeDOMConfigurator;
47  import org.objectledge.pipeline.SimplePipeline;
48  import org.objectledge.pipeline.Valve;
49  import org.objectledge.templating.MergingException;
50  import org.objectledge.templating.Template;
51  import org.objectledge.templating.TemplateNotFoundException;
52  import org.objectledge.templating.Templating;
53  import org.objectledge.templating.TemplatingContext;
54  import org.objectledge.templating.TemplatingContextLoaderValve;
55  import org.objectledge.templating.tools.ContextToolFactory;
56  import org.objectledge.templating.tools.ContextToolPopulatorValve;
57  import org.objectledge.templating.tools.ContextToolRecyclerValve;
58  import org.objectledge.templating.tools.ContextTools;
59  import org.objectledge.utils.LedgeTestCase;
60  import org.w3c.dom.Document;
61  import org.xml.sax.InputSource;
62  
63  /**
64   * Velocity Templating test.
65   * 
66   * @author <a href="mailto:pablo@caltha.pl">Pawel Potempski</a>
67   */
68  public class VelocityTemplatingTest extends LedgeTestCase
69  {
70      private Templating templating;
71  
72      protected void setUp() throws Exception
73      {
74          super.setUp();
75  		FileSystemProvider lfs = new LocalFileSystemProvider("local", "src/test/resources");
76  		FileSystemProvider cfs = new ClasspathFileSystemProvider("classpath", 
77  			getClass().getClassLoader());
78  		FileSystem fs = new FileSystem(new FileSystemProvider[] { lfs, cfs }, 4096, 4096);
79  		try
80  		{
81              InputSource source = new InputSource(fs.getInputStream(
82                  "config/org.objectledge.logging.LoggingConfigurator.xml"));
83              DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
84              Document logConfig = builder.parse(source);
85              LedgeDOMConfigurator configurator = new LedgeDOMConfigurator(fs);
86              configurator.doConfigure(logConfig.getDocumentElement(), 
87                  LogManager.getLoggerRepository());
88  
89              Configuration config = getConfig(fs,
90                  "config/org.objectledge.templating.velocity.VelocityTemplating.xml");
91                  
92  			Logger logger = Logger.getLogger(VelocityTemplating.class);
93  			templating = new VelocityTemplating(config, new Log4JLogger(logger), fs);
94  		}
95  		catch(Exception e)
96  		{
97  			throw new RuntimeException(e);
98  		}
99      }
100 
101     /***
102     	 * CreateContext method test. 
103      */
104     public void testCreateContext()
105     {
106         assertNotNull(templating);
107         TemplatingContext context = templating.createContext();
108         assertNotNull(context);
109         context.put("foo","bar");
110         assertEquals("bar",context.get("foo"));
111         assertTrue(context.containsKey("foo"));
112         assertEquals(1, context.getKeys().length);
113         context.remove("foo");
114         assertNull(context.get("foo"));
115 		assertEquals(0, context.getKeys().length);
116     }
117 
118     /***
119      * CreateContext method test. 
120      */
121     public void testTemplateExists()
122     {
123     	assertEquals(templating.templateExists("foo"), false);
124     	assertEquals(templating.templateExists("bar"), true);
125     }
126 
127     /***
128      * CreateContext method test. 
129      */
130     public void testGetTemplate()
131     {
132     	try
133     	{
134     		assertNotNull(templating.getTemplate("bar"));
135     		// cache test
136 			assertNotNull(templating.getTemplate("bar"));
137     	}
138     	catch(TemplateNotFoundException e)
139     	{
140     		fail(e.getMessage());
141     	}
142 		try
143 		{
144 			templating.getTemplate("foo");
145 			fail("Should throw TemplateNotFoundException");
146 		}
147 		catch(TemplateNotFoundException e)
148 		{
149 			//do nothing
150 		}
151     }
152 
153     /***
154      * CreateContext method test. 
155      */
156     public void testEvaluate()
157     {
158 		try
159 		{
160 			TemplatingContext context = templating.createContext();
161 			context.put("foo","bar");
162 			StringWriter target = new StringWriter();
163 			StringReader source = new StringReader("foo $foo");
164 			templating.merge(context,source,target,"test"); 
165 			assertEquals("foo bar",target.toString());
166 		}
167 		catch(MergingException e)
168 		{
169 			fail(e.getMessage());
170 		}
171     }
172 
173     /***
174      * CreateContext method test. 
175      */
176     public void testMerge()
177     {
178 		try
179 		{
180 			Template template = templating.getTemplate("bar");
181 			TemplatingContext context = templating.createContext();
182 			context.put("foo","bar");
183 			assertEquals("foo bar",template.merge(context));
184 		}
185 		catch(TemplateNotFoundException e)
186 		{
187 			fail(e.getMessage());
188 		}
189 		catch(MergingException e)
190 		{
191 			fail(e.getMessage());
192 		}
193     }
194 
195     /***
196      * CreateContext method test. 
197      */
198     public void testGetTemplateEncoding()
199     {
200     	assertEquals("UTF-8", templating.getTemplateEncoding());
201     }
202     
203 	public void testPipelineComponents()
204 	{
205 		try
206 		{
207 		    Context context = new Context();
208 	    	Valve[] valves = new Valve[3];
209 	    	ContextTools contextTools = new ContextTools(new ContextToolFactory[0]);
210 	    	valves[0] = new TemplatingContextLoaderValve(templating);
211 			valves[1] = new ContextToolPopulatorValve(contextTools);
212 			valves[2] = new ContextToolRecyclerValve(contextTools);
213 		
214 			Valve pipe = new SimplePipeline(valves);
215 			pipe.process(context);
216 		}
217 		catch(Exception e)
218 		{
219 			fail(e.getMessage());
220 		}
221     }
222 }