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  package org.objectledge.web.mvc.builders;
29  
30  import javax.servlet.http.HttpServletRequest;
31  import javax.servlet.http.HttpServletResponse;
32  
33  import org.jcontainer.dna.Configuration;
34  import org.jcontainer.dna.Logger;
35  import org.jmock.Mock;
36  import org.objectledge.configuration.ConfigurationFactory;
37  import org.objectledge.context.Context;
38  import org.objectledge.filesystem.FileSystem;
39  import org.objectledge.logging.LoggerFactory;
40  import org.objectledge.logging.LoggingConfigurator;
41  import org.objectledge.pipeline.ProcessingException;
42  import org.objectledge.templating.Templating;
43  import org.objectledge.templating.TemplatingContext;
44  import org.objectledge.templating.velocity.VelocityTemplating;
45  import org.objectledge.utils.LedgeTestCase;
46  import org.objectledge.web.HttpContext;
47  import org.objectledge.web.mvc.MVCContext;
48  import org.objectledge.web.mvc.finders.MVCFinder;
49  import org.objectledge.web.mvc.finders.NameSequenceFactory;
50  import org.objectledge.web.mvc.security.SecurityHelper;
51  import org.objectledge.xml.XMLGrammarCache;
52  import org.objectledge.xml.XMLValidator;
53  import org.picocontainer.MutablePicoContainer;
54  import org.picocontainer.defaults.DefaultPicoContainer;
55  
56  /***
57   * 
58   * @author <a href="mailto:rafal@caltha.pl">Rafal Krzewski</a>
59   * @version $Id: BuilderExecutorValveTest.java,v 1.18 2005/10/10 14:06:45 rafal Exp $
60   */
61  public class BuilderExecutorValveTest 
62  	extends LedgeTestCase
63  {
64      private Templating templating;
65      
66      private Context context;
67      
68      private MVCContext mvcContext;
69      
70      private BuilderExecutorValve executor;
71      
72      public void setUp(String base)
73          throws Exception
74      {
75          FileSystem fs = FileSystem.getStandardFileSystem("src/test/resources/"+base);
76          XMLValidator validator = new XMLValidator(new XMLGrammarCache());        
77          ConfigurationFactory configFactory = new ConfigurationFactory(fs, validator, ".");
78          Configuration config = configFactory.
79              getConfig(NameSequenceFactory.class, NameSequenceFactory.class);
80          NameSequenceFactory nameSequenceFactory = new NameSequenceFactory(config);
81          config = configFactory.getConfig(Templating.class, VelocityTemplating.class);
82          LoggerFactory loggerFactory = new LoggerFactory(new LoggingConfigurator());
83          Logger logger = loggerFactory.getLogger(Templating.class);
84          templating = new VelocityTemplating(config, logger, fs);
85          logger = loggerFactory.getLogger(MVCFinder.class);
86          MutablePicoContainer container = new DefaultPicoContainer();
87          container.registerComponentImplementation(Context.class);
88          MVCFinder finder = new MVCFinder(container, logger, templating, nameSequenceFactory);
89          SecurityHelper securityHelper = new SecurityHelper();
90          container.registerComponentInstance(MVCFinder.class, finder);
91          context = new Context();
92          context.clearAttributes();
93          ViewEnclosureManager viewEnclosureManager = new ViewEnclosureManager(context);
94          executor = new BuilderExecutorValve(context, finder, finder, securityHelper, viewEnclosureManager, 8, 8);
95          mvcContext = new MVCContext();
96          Mock mockServletRequest = mock(HttpServletRequest.class);
97          Mock mockServletResponse = mock(HttpServletResponse.class);
98          HttpContext httpContext = new HttpContext((HttpServletRequest)mockServletRequest.proxy(), 
99              (HttpServletResponse)mockServletResponse.proxy());
100         context.setAttribute(MVCContext.class, mvcContext);
101         TemplatingContext templatingContext = templating.createContext();
102         templatingContext.put(viewEnclosureManager.getKey(), viewEnclosureManager.getTool());
103         context.setAttribute(TemplatingContext.class, templatingContext);
104         context.setAttribute(HttpContext.class, httpContext);
105     }
106     
107     public void testEnclosure()
108         throws Exception
109     {
110         setUp("builders");
111         mvcContext.setView("foo.Bar");
112         executor.process(context);
113         assertEquals("Default(foo/Default(foo/Bar()))", mvcContext.getBuildResult());
114     }
115     
116     public void testRoute()
117         throws Exception
118     {
119         setUp("builders");
120         mvcContext.setView("Router");
121         executor.process(context);
122         assertEquals("Default(RoutedTo())", mvcContext.getBuildResult());
123     }
124     
125     public void testInfiniteRoute()
126         throws Exception
127     {
128         setUp("builders");
129         mvcContext.setView("RouteToSelf");
130         try
131         {
132             executor.process(context);
133             fail("exception expected");
134         }
135         catch(Exception e)
136         {
137             assertEquals(ProcessingException.class, e.getClass());
138         }
139     }
140     
141     public void testInfiniteEnclosure()
142         throws Exception
143     {
144         setUp("builders");
145         mvcContext.setView("EncloseSelf");
146         try
147         {
148             executor.process(context);
149             fail("exception expected");
150         }
151         catch(Exception e)
152         {
153             assertEquals(ProcessingException.class, e.getClass());
154         }
155     }
156     
157     public void testFailing()
158         throws Exception
159     {
160         setUp("builders");
161         mvcContext.setView("Failing");
162         try
163         {
164             executor.process(context);
165             fail("exception expected");
166         }
167         catch(Exception e)
168         {
169             assertEquals(ProcessingException.class, e.getClass());
170             assertEquals(BuildException.class, e.getCause().getClass());
171         }
172     }
173     
174     public void testTemplateOnlyEnclosure()
175         throws Exception
176     {
177         setUp("builders-templateonly");
178         mvcContext.setView("foo.Bar");
179         executor.process(context);
180         assertEquals("Default(foo/Default(foo/Bar()))", mvcContext.getBuildResult());
181     }
182 }