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.template;
18  
19  // Java imports
20  
21  // Junit imports
22  import junit.awtui.TestRunner;
23  import junit.framework.Test;
24  import junit.framework.TestSuite;
25  
26  // Cactus imports
27  import org.apache.cactus.ServletTestCase;
28  import org.apache.cactus.WebRequest;
29  
30  // Jetspeed imports
31  import org.apache.jetspeed.om.profile.ProfileLocator;
32  import org.apache.jetspeed.services.Profiler;
33  import org.apache.jetspeed.services.resources.JetspeedResources;
34  import org.apache.jetspeed.util.template.JetspeedLink;
35  import org.apache.jetspeed.util.template.JetspeedLinkFactory;
36  import org.apache.jetspeed.test.TurbineTestUtilities;
37  
38  // Turbine imports
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   * TestJespeedLink
48   *
49   * @author <a href="paulsp@apache.org">Paul Spencer</a>
50   * @version $Id: TestJetspeedLink.java,v 1.1 2004/04/07 22:02:42 jford Exp $
51   */
52  public class TestJetspeedLink extends ServletTestCase
53  {
54      private static String TEST_CONTEXT = "/test";
55      private static String TEST_HOST = "localhost";
56      private static String TEST_SERVLET = "/portal";
57      
58      /***
59       * Defines the testcase name for JUnit.
60       *
61       * @param name the testcase's name.
62       */
63      public TestJetspeedLink(String name)
64      {
65          super( name );
66      }
67      
68      /***
69       * Start the tests.
70       *
71       * @param args the arguments. Not used
72       */
73      public static void main(String args[])
74      {
75          TestRunner.main( new String[] { TestJetspeedLink.class.getName() } );
76      }
77      
78      /***
79       * Creates the test suite.
80       *
81       * @return a test suite (<code>TestSuite</code>) that includes all methods
82       *         starting with "test"
83       */
84      public static Test suite()
85      {
86          // All methods starting with "test" will be executed in the test suite.
87          return new TestSuite( TestJetspeedLink.class );
88      }
89      
90      
91      public void beginBaseLink(WebRequest theRequest)
92      {
93          System.out.println("URL = " + theRequest.getURL());
94          theRequest.setURL(TEST_HOST, TEST_CONTEXT, TEST_SERVLET, "", null);
95          System.out.println("post set URL = " + theRequest.getURL());
96      }
97      
98      /***
99       * Test the base link
100      * @throws Exception
101      */
102     public void testBaseLink() throws Exception
103     {
104         ProfileLocator profileLocator = null;
105 
106         // Create the RunData object to be used during testing.
107         RunData rundata = RunDataFactory.getRunData( request, response, config );
108         assertNotNull( "Got rundata", rundata);
109         TurbineTestUtilities.setupRunData(rundata);
110         
111         // Get and populate the context
112         Context context = TurbineVelocity.getContext(rundata);
113         assertNotNull( "Got context", context);
114         TurbinePull.populateContext( context, rundata);
115         
116         // Verify tool are in the context
117         JetspeedLink jsLink = (JetspeedLink) context.get("jslink");
118         assertNotNull( "Got jslink from context", jsLink);
119                 
120         // Generate the link
121         String link = jsLink.toString();
122         System.out.println("Base link = " + link);
123         assertTrue( "Verifing base Link", 
124           link.startsWith("http://" + TEST_HOST + TEST_CONTEXT + TEST_SERVLET));
125 
126         // Generatate and output thre page
127         TurbineTestUtilities.generatePage(rundata);
128         TurbineTestUtilities.outputPage(rundata);
129         
130         // Return the used RunData to the factory for recycling.
131         RunDataFactory.putRunData(rundata);
132     }
133     
134     public void endBaseLink(org.apache.cactus.WebResponse theResponse)
135     {
136         System.out.println("text length = " + theResponse.getText().length());
137     }
138     
139     public void beginGroupLink(WebRequest theRequest)
140     {
141         theRequest.setURL(TEST_HOST, TEST_CONTEXT, TEST_SERVLET
142         , "/" + Profiler.PARAM_GROUP + "/apache" , null);
143         System.out.println("post set URL = " + theRequest.getURL());
144     }
145     
146     /***
147      * Test the Group link
148      * @throws Exception
149      */
150     public void testGroupLink() throws Exception
151     {
152         // Create the RunData object to be used during testing.
153         RunData rundata = RunDataFactory.getRunData( request, response, config );
154         assertNotNull( "Got rundata", rundata);
155         TurbineTestUtilities.setupRunData(rundata);
156         
157         // Get and populate the context
158         Context context = TurbineVelocity.getContext(rundata);
159         assertNotNull( "Got context", context);
160         TurbinePull.populateContext( context, rundata);
161         
162         // Verify tool are in the context
163         JetspeedLink jsLink = (JetspeedLink) context.get("jslink");
164         assertNotNull( "Got jslink from context", jsLink);
165         
166         // Generate the link
167         String link = jsLink.toString();
168         System.out.println("Group link = " + link);
169         assertTrue( "Verifing base Link", 
170           link.startsWith("http://" + TEST_HOST + TEST_CONTEXT + TEST_SERVLET));
171 
172         assertTrue( "Verify link contains /" + Profiler.PARAM_GROUP + "/apache", 
173           (link.indexOf("/" + Profiler.PARAM_GROUP + "/apache") >0));
174 
175         assertEquals( "Verify link does NOT contain /" + Profiler.PARAM_USER, -1,
176            link.indexOf("/" + Profiler.PARAM_USER ));
177 
178         // Generatate and output thre page
179         TurbineTestUtilities.generatePage(rundata);
180         TurbineTestUtilities.outputPage(rundata);
181         
182         // Return the used RunData to the factory for recycling.
183         RunDataFactory.putRunData(rundata);
184         
185     }
186     
187     public void endGroupLink(org.apache.cactus.WebResponse theResponse)
188     {
189         System.out.println("text = " + theResponse.getText().length());
190     }
191     
192     public void beginPageLink(WebRequest theRequest)
193     {
194         theRequest.setURL(TEST_HOST, TEST_CONTEXT, TEST_SERVLET
195         , "/" + Profiler.PARAM_PAGE + "/news" , null);
196         System.out.println("post set URL = " + theRequest.getURL());
197     }
198     
199     /***
200      * Test the User link
201      * @throws Exception
202      */
203     public void testPageLink() throws Exception
204     {
205         // Create the RunData object to be used during testing.
206         RunData rundata = RunDataFactory.getRunData( request, response, config );
207         assertNotNull( "Got rundata", rundata);
208         TurbineTestUtilities.setupRunData(rundata);
209         
210         // Get and populate the context
211         Context context = TurbineVelocity.getContext(rundata);
212         assertNotNull( "Got context", context);
213         TurbinePull.populateContext( context, rundata);
214         
215         // Verify tool are in the context
216         JetspeedLink jsLink = (JetspeedLink) context.get("jslink");
217         assertNotNull( "Got jslink from context", jsLink);
218         
219         // Generate the link
220         String link = jsLink.toString();
221         System.out.println("Page Link = " + link);
222         assertTrue( "Verifing base Link", 
223           link.startsWith("http://" + TEST_HOST + TEST_CONTEXT + TEST_SERVLET));
224 
225         assertTrue( "Verify link contains /" + Profiler.PARAM_USER + "/anon", 
226           (link.indexOf("/" + Profiler.PARAM_USER + "/anon") >0));
227 
228         assertTrue( "Verify link contains /" + Profiler.PARAM_PAGE + "/news", 
229           (link.indexOf("/" + Profiler.PARAM_PAGE + "/news") >0));
230 
231         assertEquals( "Verify link does NOT contain /" + Profiler.PARAM_GROUP, -1,
232            link.indexOf("/" + Profiler.PARAM_GROUP ));
233 
234         // Generatate and output thre page
235         TurbineTestUtilities.generatePage(rundata);
236         TurbineTestUtilities.outputPage(rundata);
237         
238         // Return the used RunData to the factory for recycling.
239         RunDataFactory.putRunData(rundata);
240     }
241     public void endPageLink(org.apache.cactus.WebResponse theResponse)
242     {
243         System.out.println("text = " + theResponse.getText().length());
244     }
245     
246     public void beginGroupPage(WebRequest theRequest)
247     {
248         theRequest.setURL(TEST_HOST, TEST_CONTEXT, TEST_SERVLET
249         , "/" + Profiler.PARAM_GROUP + "/apache/" + Profiler.PARAM_PAGE + "/news" , null);
250         System.out.println("post set URL = " + theRequest.getURL());
251     }
252     
253     /***
254      * Test the User link
255      * @throws Exception
256      */
257     public void testGroupPage() throws Exception
258     {
259         // Create the RunData object to be used during testing.
260         RunData rundata = RunDataFactory.getRunData( request, response, config );
261         assertNotNull( "Got rundata", rundata);
262         TurbineTestUtilities.setupRunData(rundata);
263         
264         // Get and populate the context
265         Context context = TurbineVelocity.getContext(rundata);
266         assertNotNull( "Got context", context);
267         TurbinePull.populateContext( context, rundata);
268         
269         // Verify tool are in the context
270         JetspeedLink jsLink = (JetspeedLink) context.get("jslink");
271         assertNotNull( "Got jslink from context", jsLink);
272         
273         // Generate the link
274         String link = jsLink.toString();
275         System.out.println("Group/Page Link = " + link);
276         assertTrue( "Verifing base Link", 
277           link.startsWith("http://" + TEST_HOST + TEST_CONTEXT + TEST_SERVLET));
278 
279         assertTrue( "Verify link contains /" + Profiler.PARAM_GROUP + "/apache", 
280           (link.indexOf("/" + Profiler.PARAM_GROUP + "/apache") >0));
281 
282         assertEquals( "Verify link does NOT contain /" + Profiler.PARAM_USER, -1,
283            link.indexOf("/" + Profiler.PARAM_USER ));
284 
285         assertTrue( "Verify link contains /" + Profiler.PARAM_PAGE + "/news", 
286           (link.indexOf("/" + Profiler.PARAM_PAGE + "/news") >0));
287         
288         // Generatate and output thre page
289         TurbineTestUtilities.generatePage(rundata);
290         TurbineTestUtilities.outputPage(rundata);
291         
292         // Return the used RunData to the factory for recycling.
293         RunDataFactory.putRunData(rundata);
294     }
295     public void endGroupPage(org.apache.cactus.WebResponse theResponse)
296     {
297         System.out.println("text = " + theResponse.getText().length());
298     }
299 
300     public void beginGroupPage2(WebRequest theRequest)
301     {
302         theRequest.setURL(TEST_HOST, TEST_CONTEXT, TEST_SERVLET
303         , "/" + Profiler.PARAM_GROUP + "/apache/" + Profiler.PARAM_PAGE + "/news" , null);
304         System.out.println("post set URL = " + theRequest.getURL());
305     }
306     
307     /***
308      * Test the User link
309      * @throws Exception
310      */
311     public void testGroupPage2() throws Exception
312     {
313         JetspeedLink jetspeedLink = null;
314         
315         // Create the RunData object to be used during testing.
316         RunData rundata = RunDataFactory.getRunData( request, response, config );
317         assertNotNull( "Got rundata", rundata);
318         TurbineTestUtilities.setupRunData(rundata);
319         
320         // Get and populate the context
321         Context context = TurbineVelocity.getContext(rundata);
322         assertNotNull( "Got context", context);
323         TurbinePull.populateContext( context, rundata);
324         
325         // Verify tool are in the context
326         JetspeedLink jsLink = (JetspeedLink) context.get("jslink");
327         assertNotNull( "Got jslink from context", jsLink);
328         
329         // Generate the link
330         String link = jsLink.toString();
331         System.out.println("Group/Page Link = " + link);
332         
333         // Generate the link from JetspeedLink
334         jetspeedLink = JetspeedLinkFactory.getInstance(rundata);
335         System.out.println("Group/Page jetspeedLink = " + jetspeedLink.toString());
336         assertEquals( "jsLink = jetspeedLink", link, jetspeedLink.toString());
337         JetspeedLinkFactory.putInstance(jsLink);
338         
339         // Make sure 2 calls to toString() return the same value
340         jetspeedLink = JetspeedLinkFactory.getInstance(rundata);
341         assertEquals( "jsLink = jetspeedLink", jetspeedLink.toString(), jetspeedLink.toString());
342         JetspeedLinkFactory.putInstance(jsLink);
343 
344         // Generatate and output thre page
345         TurbineTestUtilities.generatePage(rundata);
346         TurbineTestUtilities.outputPage(rundata);
347         
348         // Return the used RunData to the factory for recycling.
349         RunDataFactory.putRunData(rundata);
350     }
351     public void endGroupPage2(org.apache.cactus.WebResponse theResponse)
352     {
353         System.out.println("text = " + theResponse.getText().length());
354     }
355     
356     
357     public void beginGetLink(WebRequest theRequest)
358     {
359         theRequest.setURL(TEST_HOST, TEST_CONTEXT, TEST_SERVLET
360         , "/" + Profiler.PARAM_GROUP + "/apache/" + Profiler.PARAM_PAGE + "/news/" + JetspeedResources.PATH_PORTLETID_KEY + "/p_1234/" + JetspeedResources.PATH_TEMPLATE_KEY + "/Home/" + Profiler.PARAM_MEDIA_TYPE + "/html/" + Profiler.PARAM_LANGUAGE + "/en/" + Profiler.PARAM_COUNTRY + "/US" , null);
361         System.out.println("post set URL = " + theRequest.getURL());
362     }
363     
364     /***
365      * Test the User link
366      * @throws Exception
367      */
368     public void testGetLink() throws Exception
369     {
370         JetspeedLink jetspeedLink = null;
371         
372         // Create the RunData object to be used during testing.
373         RunData rundata = RunDataFactory.getRunData( request, response, config );
374         assertNotNull( "Got rundata", rundata);
375         TurbineTestUtilities.setupRunData(rundata);
376         
377         // Get and populate the context
378         Context context = TurbineVelocity.getContext(rundata);
379         assertNotNull( "Got context", context);
380         TurbinePull.populateContext( context, rundata);
381         
382         // Verify tool are in the context
383         JetspeedLink jsLink = (JetspeedLink) context.get("jslink");
384         assertNotNull( "Got jslink from context", jsLink);
385         
386         // Generate the link from JetspeedLink
387         jetspeedLink = JetspeedLinkFactory.getInstance(rundata);
388         String baseLink = new String("http://" + TEST_HOST + TEST_CONTEXT + TEST_SERVLET);
389         String currentLink = new String( baseLink 
390             + "/" + Profiler.PARAM_MEDIA_TYPE + "/" + jetspeedLink.getMediaType() 
391             + "/" + Profiler.PARAM_GROUP + "/" + jetspeedLink.getGroupName() 
392             + "/" + Profiler.PARAM_PAGE + "/" + jetspeedLink.getPageName());
393  
394         assertEquals( "Testing getGroupName()", "apache" , jetspeedLink.getGroupName() );
395         assertEquals( "Testing getPageName()", "news.psml" , jetspeedLink.getPageName() );
396         assertEquals( "Testing getPageName()", "html" , jetspeedLink.getMediaType() );
397         assertNull( "Testing getRoleName()", jetspeedLink.getRoleName() );
398         assertNull( "Testing getUserName()", jetspeedLink.getUserName() );
399 
400         // Test conveniance methods
401         assertEquals( "Testing getAction(actionName)", currentLink + "?" + JetspeedResources.PATH_ACTION_KEY + "=myAction", jetspeedLink.getAction("myAction").toString());
402         assertEquals( "Testing getAction(actionName, peid)", currentLink + "/" + JetspeedResources.PATH_PORTLETID_KEY + "/actionpeid?" + JetspeedResources.PATH_ACTION_KEY + "=myAction1", jetspeedLink.getAction("myAction1","actionpeid").toString() );
403         assertEquals( "Testing getGroup(groupName)", baseLink + "/" + Profiler.PARAM_MEDIA_TYPE + "/" + jetspeedLink.getMediaType()+ "/" + Profiler.PARAM_GROUP + "/myGroup", jetspeedLink.getGroup("myGroup").toString());
404         assertEquals( "Testing getGroup(groupName,pageName)", baseLink + "/" + Profiler.PARAM_MEDIA_TYPE + "/" + jetspeedLink.getMediaType() + "/" + Profiler.PARAM_GROUP + "/myGroup1/" + Profiler.PARAM_PAGE + "/myGroupPage", jetspeedLink.getGroup("myGroup1","myGroupPage").toString());
405         assertEquals( "Testing getHomePage()", baseLink , jetspeedLink.getHomePage().toString() );
406         assertEquals( "Testing getRole(roleName)", baseLink + "/" + Profiler.PARAM_MEDIA_TYPE + "/" + jetspeedLink.getMediaType() + "/" + Profiler.PARAM_ROLE + "/myRole", jetspeedLink.getRole("myRole").toString());
407         assertEquals( "Testing getRole(roleName,pageName)", baseLink + "/" + Profiler.PARAM_MEDIA_TYPE + "/" + jetspeedLink.getMediaType()+ "/" + Profiler.PARAM_ROLE + "/myRole1/" + Profiler.PARAM_PAGE + "/myRolePage", jetspeedLink.getRole("myRole1","myRolePage").toString());
408         assertEquals( "Testing getTemplate(templateName)", currentLink  + "/" + JetspeedResources.PATH_TEMPLATE_KEY + "/myTemplate", jetspeedLink.getTemplate("myTemplate").toString());
409         assertEquals( "Testing getTemplate()", currentLink , jetspeedLink.getTemplate().toString() );
410         assertEquals( "Testing getUser(userName)", baseLink + "/" + Profiler.PARAM_MEDIA_TYPE + "/" + jetspeedLink.getMediaType() + "/" + Profiler.PARAM_USER + "/myUser", jetspeedLink.getUser("myUser").toString());
411         assertEquals( "Testing getUser(userName,PageName)", baseLink + "/" + Profiler.PARAM_MEDIA_TYPE + "/" + jetspeedLink.getMediaType() + "/" + Profiler.PARAM_USER + "/myUser1/" + Profiler.PARAM_PAGE + "/myUserPage", jetspeedLink.getUser("myUser1","myUserPage").toString());
412 
413         // Test getLink()
414         assertEquals( "Testing getLink() with default & nulls",
415             baseLink + 
416             "/" + Profiler.PARAM_MEDIA_TYPE +
417             "/" + jetspeedLink.getMediaType() + 
418             "/" + Profiler.PARAM_PAGE + 
419             "/" + jetspeedLink.getPageName(),
420             jetspeedLink.getLink(JetspeedLink.DEFAULT, null, null, JetspeedLink.DEFAULT, null, null).toString() );
421 
422         assertEquals( "Testing getLink() with current & nulls", 
423             baseLink + 
424             "/" + Profiler.PARAM_MEDIA_TYPE + 
425             "/" + jetspeedLink.getMediaType()  + 
426             "/" + Profiler.PARAM_GROUP + 
427             "/" + jetspeedLink.getGroupName()+ 
428             "/" + Profiler.PARAM_PAGE + 
429             "/" + jetspeedLink.getPageName(),
430             jetspeedLink.getLink(JetspeedLink.CURRENT, null, null, JetspeedLink.CURRENT, null, null).toString() );
431 
432         assertEquals( "Testing getLink() with default & nulls + media/language/country", 
433             baseLink + 
434             "/" + Profiler.PARAM_PAGE + 
435             "/" + jetspeedLink.getPageName() + 
436             "/" + Profiler.PARAM_MEDIA_TYPE + 
437             "/" + "test_html"+ 
438             "/" + Profiler.PARAM_LANGUAGE + 
439             "/" + "test_en" + 
440             "/" + Profiler.PARAM_COUNTRY + 
441             "/" + "test_US", 
442             jetspeedLink.getLink(JetspeedLink.DEFAULT, null, null, JetspeedLink.DEFAULT, null, null, null, "test_html", "test_en", "test_US").toString() );
443 
444         JetspeedLinkFactory.putInstance(jsLink);
445         
446         // Generatate and output thre page
447         TurbineTestUtilities.generatePage(rundata);
448         TurbineTestUtilities.outputPage(rundata);
449         
450         // Return the used RunData to the factory for recycling.
451         RunDataFactory.putRunData(rundata);
452     }
453     public void endGetLink(org.apache.cactus.WebResponse theResponse)
454     {
455         System.out.println("text = " + theResponse.getText().length());
456     }
457 }