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.util;
18  
19  // Junit imports
20  import junit.awtui.TestRunner;
21  import junit.framework.Test;
22  import junit.framework.TestSuite;
23  
24  // Cactus imports
25  import org.apache.cactus.ServletTestCase;
26  import org.apache.cactus.WebRequest;
27  import org.apache.cactus.WebResponse;
28  
29  // Jetspeed imports
30  import org.apache.jetspeed.capability.CapabilityMap;
31  import org.apache.jetspeed.om.profile.Profile;
32  import org.apache.jetspeed.services.Profiler;
33  import org.apache.jetspeed.services.resources.JetspeedResources;
34  import org.apache.jetspeed.services.rundata.JetspeedRunData;
35  import org.apache.jetspeed.test.TurbineTestUtilities;
36  
37  // Turbine imports
38  //import org.apache.turbine.om.security.User;
39  import org.apache.turbine.services.pull.TurbinePull;
40  import org.apache.turbine.services.velocity.TurbineVelocity;
41  import org.apache.turbine.util.RunData;
42  import org.apache.turbine.util.RunDataFactory;
43  
44  // Velocity
45  import org.apache.velocity.context.Context;
46  
47  /***
48   *
49   * @author <a href="paulsp@apache.org">Paul Spencer</a>
50   * @version $Id: TestMimeType.java,v 1.1 2004/04/07 22:02:42 jford Exp $
51   */
52  
53  public class TestMimeType extends ServletTestCase
54  {
55      private static final String TEST_ANON_USER_NAME = "";
56      private static final String TEST_CONTEXT = null;
57      private static final String TEST_DEFAULT_PAGE = "default";
58      private static final String TEST_HOST = "localhost";
59      private static final String TEST_SERVLET = "/portal";
60      private static final String TEST_GROUP = "apache";
61      private static final String TEST_PAGE = "news";
62      private static final String TEST_USER = "turbine";
63      private static final String TEST_USER_PASSWORD = "turbine";
64      
65      private RunData rundata = null;
66  
67      /***
68       * Defines the testcase name for JUnit.
69       *
70       * @param name the testcase's name.
71       */
72      public TestMimeType(String name)
73      {
74          super(name);
75      }
76  
77      /***
78       * Start the tests.
79       *
80       * @param args the arguments. Not used
81       */
82      public static void main(String args[])
83      {
84          TestRunner.main(new String[] { TestMimeType.class.getName() });
85      }
86      
87      /***
88       * Creates the test suite.
89       *
90       * @return a test suite (<code>TestSuite</code>) that includes all methods
91       *         starting with "test"
92       */
93      public static Test suite()
94      {
95          // All methods starting with "test" will be executed in the test suite.
96          return new TestSuite(TestMimeType.class);
97      }
98      
99      /***
100      * Sets up the test case.
101      *
102      */
103     protected void setUp () throws Exception 
104     {
105     }
106     
107     /***
108      *  Test: DefaultURL
109      *  With the default URL "/"
110      *    1) A page is generated 
111      *    2) The user is anonymous
112      *    3) Group and Role are not set
113      */
114     public void beginDefaultURL(WebRequest theRequest)
115     {
116         System.out.println("URL = " + theRequest.getURL());
117         theRequest.setURL(TEST_HOST, TEST_CONTEXT, TEST_SERVLET, "", null); 
118         System.out.println("post set URL = " + theRequest.getURL());
119     }
120 
121     /***
122      * Execute the test
123      *
124      * @throws Exception
125      */
126     public void testDefaultURL() throws Exception
127     {
128         // Create the RunData object to be used during testing.        
129         rundata = RunDataFactory.getRunData(request, response, config);
130         assertNotNull("Got rundata", rundata);
131         TurbineTestUtilities.setupRunData(rundata);
132 
133         // Verify we have a CapabilityMap
134         CapabilityMap cm = ((JetspeedRunData) rundata).getCapability();
135         assertNotNull("Got Capability", cm);
136 
137         // Verify we have a profile
138         Profile profile = Profiler.getProfile(rundata);
139         assertNotNull("Got profile from Profiler", profile);
140         System.out.println("DocumentName = " + profile.getDocument().getName()); 
141 
142         // Get and populate the context
143         Context context = TurbineVelocity.getContext(rundata);
144         assertNotNull("Got context", context);
145         TurbinePull.populateContext(context, rundata);
146 
147         // Verify tool are in the context
148         assertNotNull("Got jlink from context", context.get("jlink"));
149 
150         // Generatate and output thre page
151         TurbineTestUtilities.generatePage(rundata);
152         assertEquals("Verifying page's character set",
153             rundata.getCharSet(), 
154             JetspeedResources.getString(JetspeedResources.CONTENT_ENCODING_KEY, "utf-8"));
155 
156         TurbineTestUtilities.outputPage(rundata);
157 
158         // Return the used RunData to the factory for recycling.
159         RunDataFactory.putRunData(rundata);
160     }
161 
162     public void endDefaultURL(WebResponse theResponse)
163     {
164         System.out.println("text length = " + theResponse.getText().length());
165     }
166 }