1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.jetspeed.locator;
18  
19  import java.util.ArrayList;
20  
21  import junit.framework.Test;
22  import junit.framework.TestCase;
23  import junit.framework.TestSuite;
24  
25  /***
26   * TestTemplateLocator
27   *
28   * @author <a href="mailto:taylor@apache.org">David Sean Taylor</a>
29   * @version $Id: TestTemplateLocator.java 516448 2007-03-09 16:25:47Z ate $
30   */
31  public class TestTemplateLocator extends TestCase
32  {
33      private JetspeedTemplateLocator templateLocator;
34  
35      public TestTemplateLocator(String name) 
36      {
37          super( name );
38      }
39  
40      
41      /***
42       * Start the tests.
43       *
44       * @param args the arguments. Not used
45       */
46      public static void main(String args[]) 
47      {
48          junit.awtui.TestRunner.main( new String[] { TestTemplateLocator.class.getName() } );
49      }
50  
51      public static Test suite()
52      {
53          // All methods starting with "test" will be executed in the test suite.
54          return new TestSuite(TestTemplateLocator.class);
55      }
56              
57      public void testLocateTemplate()
58            throws Exception
59      {
60          // TemplateLocator component = (TemplateLocator)componentManager.getComponent("TemplateLocator");
61          assertNotNull("template service is null", templateLocator);            
62          LocatorDescriptor locator = templateLocator.createLocatorDescriptor("email");
63          locator.setName("test.vm");
64          TemplateDescriptor template = templateLocator.locateTemplate(locator);
65          assertNotNull("template is null", template);
66          System.out.println("template1 = " + template);
67          assertTrue("template1 result", "type/email/name/test.vm".endsWith(template.toString()));
68          
69          LocatorDescriptor locator2 = templateLocator.createLocatorDescriptor("email");
70          locator2.setName("htmltest.vm");
71          locator2.setMediaType("html");        
72          template = templateLocator.locateTemplate(locator2);
73          assertNotNull("template is null", template);                
74          System.out.println("template2 = " + template);            
75          assertTrue("template2 result", "type/email/media-type/html/name/htmltest.vm".endsWith(template.toString()));
76  
77          LocatorDescriptor locator3 = templateLocator.createLocatorDescriptor("email");
78          locator3.setName("entest.vm");
79          locator3.setMediaType("html");
80          locator3.setLanguage("en");                
81          template = templateLocator.locateTemplate(locator3);
82          assertNotNull("template is null", template);        
83          System.out.println("template3 = " + template);            
84          assertTrue("template3 result", "type/email/media-type/html/language/en/name/entest.vm".endsWith(template.toString()));
85  
86          LocatorDescriptor locator4 = templateLocator.createLocatorDescriptor("email");
87          locator4.setName("ustest.vm");
88          locator4.setMediaType("html");
89          locator4.setLanguage("en");
90          locator4.setCountry("US");                
91          template = templateLocator.locateTemplate(locator4);
92          assertNotNull("template is null", template);        
93          System.out.println("template4 = " + template);            
94          assertTrue("template4 result", 
95              "type/email/media-type/html/language/en/country/US/name/ustest.vm".endsWith(template.toString()));
96  
97          // test fallback
98          LocatorDescriptor locator5 = templateLocator.createLocatorDescriptor("email");
99          locator5.setName("entest.vm");
100         locator5.setMediaType("html");
101         locator5.setLanguage("en");
102         locator5.setCountry("UZ");                
103         template = templateLocator.locateTemplate(locator5);
104         assertNotNull("template is null", template);        
105         System.out.println("template5 = " + template);            
106         assertTrue("template5 result", 
107             "type/email/media-type/html/language/en/name/entest.vm".endsWith(template.toString()));
108 
109         // test fallback all the way to email
110         LocatorDescriptor locator6 = templateLocator.createLocatorDescriptor("email");
111         locator6.setName("test.vm");
112         locator6.setMediaType("html");
113         locator6.setLanguage("en");
114         locator6.setCountry("UZ");                
115         template = templateLocator.locateTemplate(locator6);
116         System.out.println("template6 = " + template);            
117         assertTrue("template6 result", 
118             "type/email/name/test.vm".endsWith(template.toString()));
119                     
120     }
121     
122     /* (non-Javadoc)
123      * @see junit.framework.TestCase#setUp()
124      */
125     protected void setUp() throws Exception
126     {
127         ArrayList roots = new ArrayList(1);
128         roots.add("./testdata/templates");
129         ArrayList classes = new ArrayList(2);
130         classes.add(JetspeedTemplateDescriptor.class);
131         classes.add(JetspeedLocatorDescriptor.class);
132         
133         templateLocator = new JetspeedTemplateLocator(roots, classes, "email", "./");
134         templateLocator.start();
135     }
136 }