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.finders;
29  
30  import junit.framework.TestCase;
31  
32  import org.jcontainer.dna.Configuration;
33  import org.jcontainer.dna.Logger;
34  import org.objectledge.configuration.ConfigurationFactory;
35  import org.objectledge.context.Context;
36  import org.objectledge.filesystem.FileSystem;
37  import org.objectledge.logging.LoggerFactory;
38  import org.objectledge.logging.LoggingConfigurator;
39  import org.objectledge.pipeline.Valve;
40  import org.objectledge.templating.Template;
41  import org.objectledge.templating.Templating;
42  import org.objectledge.templating.velocity.VelocityTemplating;
43  import org.objectledge.test.actions.foo.TestAction;
44  import org.objectledge.web.mvc.components.Component;
45  import org.objectledge.xml.XMLGrammarCache;
46  import org.objectledge.xml.XMLValidator;
47  import org.picocontainer.MutablePicoContainer;
48  import org.picocontainer.defaults.DefaultPicoContainer;
49  
50  /***
51   * 
52   * @author <a href="mailto:rafal@caltha.pl">Rafal Krzewski</a>
53   * @version $Id: MVCFinderTest.java,v 1.16 2005/10/10 14:06:45 rafal Exp $
54   */
55  public class MVCFinderTest extends TestCase
56  {
57      private MVCFinder finder;
58      
59      private Templating templating;
60      
61      /***
62       * Constructor for MVCFinderTest.
63       * @param arg0
64       */
65      public MVCFinderTest(String arg0)
66      {
67          super(arg0);
68      }
69  
70      public void setUp()
71          throws Exception
72      {
73          FileSystem fs = FileSystem.getStandardFileSystem("src/test/resources/finder");
74          XMLValidator validator = new XMLValidator(new XMLGrammarCache());        
75          ConfigurationFactory configFactory = new ConfigurationFactory(fs, validator, ".");
76          Configuration config = configFactory.
77              getConfig(NameSequenceFactory.class, NameSequenceFactory.class);
78          NameSequenceFactory nameSequenceFactory = new NameSequenceFactory(config);
79          config = configFactory.getConfig(Templating.class, VelocityTemplating.class);
80          LoggerFactory loggerFactory = new LoggerFactory(new LoggingConfigurator());
81          Logger logger = loggerFactory.getLogger(Templating.class);
82          templating = new VelocityTemplating(config, logger, fs);
83          logger = loggerFactory.getLogger(MVCFinder.class);
84          MutablePicoContainer container = new DefaultPicoContainer();
85          container.registerComponentImplementation(Context.class); 
86          finder = new MVCFinder(container, logger, templating, nameSequenceFactory);
87      }
88  
89      public void testFindBuilderTemplate()
90      {
91          MVCTemplateFinder.Result result = finder.findBuilderTemplate("foo.Bar");
92          assertEquals( "views/foo/Bar", result.getTemplate().getName());
93          assertEquals( "foo.Bar", result.getOriginalView());
94          assertEquals( "foo.Bar", result.getActualView());
95          result = finder.findBuilderTemplate(null);
96          assertNull(result.getTemplate());
97          assertNull(result.getOriginalView());
98          assertNull(result.getActualView());
99          result = finder.findBuilderTemplate("foo.NonExistentView");
100         assertEquals("views/foo/Default", result.getTemplate().getName());
101         assertEquals( "foo.NonExistentView", result.getOriginalView());
102         assertEquals( "foo.Default", result.getActualView());
103         result = finder.findBuilderTemplate("NonExistentView");
104         assertEquals("views/Default", result.getTemplate().getName());
105         assertEquals( "NonExistentView", result.getOriginalView());
106         assertEquals( "Default", result.getActualView());
107         result = finder.findBuilderTemplate("nonexistentpackage.NonExistentView");
108         assertEquals("views/Default", result.getTemplate().getName());
109         assertEquals( "nonexistentpackage.NonExistentView", result.getOriginalView());
110         assertEquals( "Default", result.getActualView());
111     }
112 
113     public void testGetAction()
114         throws Exception
115     {
116         Valve action = finder.getAction("foo.TestAction");
117         assertEquals(TestAction.class, action.getClass());
118         action = finder.getAction("foo.NonExistentAction");
119         assertNull(action);
120     }
121 
122     public void testFindBuilder()
123     {
124         MVCClassFinder.Result result = finder.findBuilder("foo.Bar");
125         assertEquals(org.objectledge.test.views.foo.Bar.class, result.getBuilder().getClass());
126         result = finder.findBuilder(null);
127         assertNull(result.getBuilder());
128         result = finder.findBuilder("foo.NotExistentClass");
129         assertEquals(org.objectledge.test.views.foo.Default.class, result.getBuilder().getClass());
130         result = finder.findBuilder("NotExistentClass");
131         assertEquals(org.objectledge.test.views.Default.class, result.getBuilder().getClass());
132         result = finder.findBuilder("nonexistentpackage.NotExistentClass");
133         assertEquals(org.objectledge.test.views.Default.class, result.getBuilder().getClass());
134     }
135     
136     public void testFindEnclosingViewName()
137     {
138         String viewName = ("foo.Bar");
139         MVCClassFinder.Result result = finder.findBuilder(viewName);
140         assertEquals(org.objectledge.test.views.foo.Bar.class, result.getBuilder().getClass());
141         viewName = finder.findEnclosingViewName(viewName);
142         result = finder.findBuilder(viewName);
143         assertEquals(org.objectledge.test.views.foo.Default.class, result.getBuilder().getClass());
144         viewName = finder.findEnclosingViewName(viewName);
145         result = finder.findBuilder(viewName);
146         assertEquals(org.objectledge.test.views.Default.class, result.getBuilder().getClass());
147         viewName = finder.findEnclosingViewName(viewName);
148         result = finder.findBuilder(viewName);
149         assertNull(result.getBuilder());        
150     }
151 
152     public void testGetComponentTemplate()
153     {
154         Template template = finder.getComponentTemplate("one.Component");
155         assertEquals("components/one/Component", template.getName());
156         template = finder.getComponentTemplate(null);
157         assertNull(template);
158         template = finder.getComponentTemplate("one.NonexistentComponent");
159         assertNull(template);
160     }
161 
162     public void testGetComponent()
163     {
164         Component component = finder.getComponent("one.Component");
165         assertEquals(org.objectledge.test.components.one.Component.class, component.getClass());
166         component = finder.getComponent(null);
167         assertNull(component);
168         component = finder.getComponent("one.NonexistentComponent");
169         assertNull(component);
170     }
171 }