1   /*
2    * Copyright 2000-2001,2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.apache.jetspeed.capability;
18  
19  // Junit imports
20  import junit.framework.Test;
21  import junit.framework.TestSuite;
22  
23  import org.apache.turbine.util.TurbineConfig;
24  import org.apache.turbine.util.StringUtils;
25  
26  // Jetspeed imports
27  import org.apache.jetspeed.test.JetspeedTestCase;
28  import org.apache.jetspeed.capability.CapabilityMapFactory;
29  import org.apache.jetspeed.capability.CapabilityMap;
30  import org.apache.jetspeed.services.Registry;
31  import org.apache.jetspeed.om.registry.ClientRegistry;
32  import org.apache.jetspeed.om.registry.ClientEntry;
33  import org.apache.jetspeed.util.MimeType;
34  
35  /***
36   * Unit test for capbility package
37   *
38   * @author <a href="raphael@apache.org">Raphaël Luta</a>
39   * @version $Id: TestCapabilityMap.java,v 1.1 2004/04/07 22:02:42 jford Exp $
40   */
41  
42  public class TestCapabilityMap extends JetspeedTestCase
43  {
44      
45      /***
46       * Defines the testcase name for JUnit.
47       *
48       * @param name the testcase's name.
49       */
50      public TestCapabilityMap( String name )
51      {
52          super( name );
53      }
54  
55      /***
56       * Start the tests.
57       *
58       * @param args the arguments. Not used
59       */
60      public static void main(String args[])
61      {
62          junit.awtui.TestRunner.main( new String[] { TestCapabilityMap.class.getName() } );
63      }
64  
65      public void setup()
66      {
67          System.out.println("Setup: Testing CapabilityMap functionality");
68      }
69      /***
70       * Creates the test suite.
71       *
72       * @return a test suite (<code>TestSuite</code>) that includes all methods
73       *         starting with "test"
74       */
75      public static Test suite()
76      {
77          // All methods starting with "test" will be executed in the test suite.
78          return new TestSuite( TestCapabilityMap.class );
79      }
80  
81      public void testRegistry() throws Exception
82      {
83          try
84          {
85              // Make sure the Registry works
86              ClientRegistry cr = (ClientRegistry)Registry.get(Registry.CLIENT);
87              ClientEntry ce = (ClientEntry)cr.getEntry("ie5");
88              assertNotNull(ce);
89          }
90          catch (Exception e)
91          {
92              String errmsg = "Error in test: " + e.toString();
93             // e.printStackTrace();
94             assertNotNull(errmsg, null);
95          }
96      }
97  
98      public void testDefaultMap() throws Exception
99      {
100         try
101         {
102             // first test default capailitymap
103             CapabilityMap cm = CapabilityMapFactory.getDefaultCapabilityMap();
104             assertNotNull(cm);
105             assertTrue(cm.toString().startsWith("ns4"));
106         }
107         catch (Exception e)
108         {
109             String errmsg = "Error in test: " + e.toString();
110            // e.printStackTrace();
111            assertNotNull(errmsg, null);
112         }
113     }
114 
115     public void testStandardMap() throws Exception
116     {
117         try
118         {
119             // then test different standard browsers
120             getUserAgent("ie5","Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0)");
121             getUserAgent("ns4","Mozilla/4.78 (Windows 2000; U) Opera 6.01  [fr]");
122             getUserAgent("mozilla","Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.0rc1) Gecko/20020417");
123             getUserAgent("nokia_generic","Nokia3330/1.0 (03.05)");
124             getUserAgent("lynx","Lynx/2.8.4rel.1 libwww-FM/2.14 SSL-MM/1.4.1 OpenSSL/0.9.6b");
125         }
126         catch (Exception e)
127         {
128             String errmsg = "Error in test: " + e.toString();
129            // e.printStackTrace();
130            assertNotNull(errmsg, null);
131         }
132     }
133 
134     public void testCapabilityCheck() throws Exception
135     {
136         try
137         {
138             // test simple capabilities
139             CapabilityMap cm = CapabilityMapFactory.getCapabilityMap("Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.0rc1) Gecko/20020417");
140             assertTrue(cm.hasCapability("HTML_DOM_1"));
141             assertTrue(cm.hasCapability("HTML_IFRAME"));
142             assertTrue(cm.supportsMimeType(MimeType.XML));
143             assertTrue(cm.supportsMediaType("html"));
144         }
145         catch (Exception e)
146         {
147             String errmsg = "Error in test: " + e.toString();
148            // e.printStackTrace();
149            assertNotNull(errmsg, null);
150         }
151     }
152 
153     private void getUserAgent(String name, String ua)
154     {
155         CapabilityMap cm = CapabilityMapFactory.getCapabilityMap(ua);
156         assertNotNull(cm);
157         assertNotNull(name);
158         assertTrue(cm.toString().startsWith(name));
159     }
160 
161     /*
162       Configuration object to run Turbine outside a servlet container
163       ( uses turbine.properties )
164     */
165     private static TurbineConfig config = null;
166 
167     /*
168       Sets up TurbineConfig using the system property:
169       <pre>turbine.properties</pre>
170     */
171     static
172     {
173         try
174         {
175            config = new TurbineConfig( "webapp", "/WEB-INF/conf/TurbineResources.properties");
176            config.init();
177         }
178         catch (Exception e)
179         {
180             fail(StringUtils.stackTrace(e));
181         }
182     }
183 }