KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jetspeed > services > registry > TestMarshallRegistry


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.services.registry;
18
19 import java.io.File JavaDoc;
20 import java.io.FileReader JavaDoc;
21
22 import java.util.Vector JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.HashMap JavaDoc;
25 import java.util.Map JavaDoc;
26
27 // Junit imports
28
import junit.framework.Test;
29 import junit.framework.TestSuite;
30
31 //castor support
32
import org.exolab.castor.xml.Unmarshaller;
33 import org.exolab.castor.mapping.Mapping;
34 import org.xml.sax.InputSource JavaDoc;
35
36 // Jetspeed imports
37
import org.apache.jetspeed.test.JetspeedTestCase;
38 import org.apache.jetspeed.om.registry.CachedParameter;
39 import org.apache.jetspeed.om.registry.Category;
40 import org.apache.jetspeed.om.registry.ContentURL;
41 import org.apache.jetspeed.om.registry.MetaInfo;
42 import org.apache.jetspeed.om.registry.Parameter;
43 import org.apache.jetspeed.om.registry.PortletEntry;
44 import org.apache.jetspeed.om.registry.SecurityAccess;
45 import org.apache.jetspeed.om.registry.SecurityAllow;
46 import org.apache.jetspeed.om.registry.SecurityEntry;
47 import org.apache.jetspeed.om.SecurityReference;
48
49 // Turbine imports
50
import org.apache.turbine.util.TurbineConfig;
51 import org.apache.turbine.util.StringUtils;
52
53 /**
54  * TestMarshallRegistry
55  *
56  * @author <a HREF="taylor@apache.org">David Sean Taylor</a>
57  * @version $Id: TestMarshallRegistry.java,v 1.1 2004/04/07 22:02:42 jford Exp $
58  */

59
60 public class TestMarshallRegistry extends JetspeedTestCase
61 {
62     /**
63      * Defines the testcase name for JUnit.
64      *
65      * @param name the testcase's name.
66      */

67     public TestMarshallRegistry(String JavaDoc name)
68     {
69         super(name);
70     }
71
72     /**
73      * Start the tests.
74      *
75      * @param args the arguments. Not used
76      */

77     public static void main(String JavaDoc args[])
78     {
79         junit.awtui.TestRunner.main(new String JavaDoc[] { TestMarshallRegistry.class.getName() });
80     }
81
82     public void setup()
83     {
84         System.out.println("Setup: Testing marshalling of Registry");
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(TestMarshallRegistry.class);
97     }
98
99     private String JavaDoc getMappingFileName()
100     {
101         return "webapp/WEB-INF/conf/registry.xml";
102     }
103
104     /**
105      * Tests unmarshaling registry fragment
106      * @throws Exception
107      */

108     public void testUnmarshallRegistry() throws Exception JavaDoc
109     {
110         System.out.println("Testing marshalling of Registry");
111
112         String JavaDoc xregFile = "webapp/WEB-INF/psml/test/testcase-2.xreg";
113
114         Mapping mapping = null;
115         String JavaDoc mapFile = getMappingFileName();
116         File JavaDoc map = new File JavaDoc(mapFile);
117         if (map.exists() && map.isFile() && map.canRead())
118         {
119             try
120             {
121                 FileReader JavaDoc reader = new FileReader JavaDoc(xregFile);
122                 mapping = new Mapping();
123                 InputSource JavaDoc is = new InputSource JavaDoc(new FileReader JavaDoc(map));
124                 is.setSystemId(mapFile);
125                 mapping.loadMapping(is);
126                 Unmarshaller unmarshaller = new Unmarshaller(mapping);
127                 RegistryFragment fragment = (RegistryFragment) unmarshaller.unmarshal(reader);
128                 assertNotNull(fragment);
129                 Vector JavaDoc portlets = fragment.getPortlets();
130
131                 // test abstract
132

133                 PortletEntry rss = (PortletEntry) portlets.elementAt(1);
134                 assertNotNull(rss);
135                 assertTrue(rss.getName().equals("RSS"));
136                 assertTrue(!rss.isHidden());
137                 assertTrue(rss.getType().equals("abstract"));
138                 assertNull(rss.getParent());
139                 assertTrue(!rss.isApplication());
140                 assertTrue(rss.getClassname().equals("org.apache.jetspeed.portal.portlets.NewRSSPortlet"));
141                 // params
142
Iterator JavaDoc it = rss.getParameterNames();
143                 HashMap JavaDoc mapParams = new HashMap JavaDoc();
144                 while (it.hasNext())
145                 {
146                     String JavaDoc name = (String JavaDoc) it.next();
147                     Parameter p = rss.getParameter(name);
148                     assertNotNull(p);
149                     //System.out.println("PARAM:" + p.getName() + " = " + p.getValue());
150
mapParams.put(p.getName(), p);
151                 }
152                 assertTrue(mapParams.containsKey("showtitle"));
153                 assertTrue(mapParams.containsKey("stylesheet"));
154                 assertTrue(mapParams.containsKey("itemdisplayed"));
155
156                 // test ref
157
PortletEntry hack = (PortletEntry) portlets.elementAt(2);
158                 assertNotNull(hack);
159                 assertTrue(hack.getName().equals("XMLHack"));
160                 assertTrue(hack.isHidden());
161                 assertTrue(hack.getType().equals("ref"));
162                 assertTrue(hack.getParent().equals("RSS"));
163                 assertTrue(!hack.isApplication());
164                 assertTrue(hack.getTitle().equals("XMLHack Title"));
165                 assertTrue(hack.getDescription().equals("XMLHack Description"));
166                 assertTrue(hack.getMetaInfo().getTitle().equals("XMLHack Title"));
167                 assertTrue(hack.getMetaInfo().getDescription().equals("XMLHack Description"));
168                 assertTrue(hack.getClassname().equals("org.apache.jetspeed.portal.portlets.NewRSSPortlet"));
169
170                 mapParams.clear();
171                 it = hack.getParameterNames();
172                 while (it.hasNext())
173                 {
174                     String JavaDoc name = (String JavaDoc) it.next();
175                     Parameter p = hack.getParameter(name);
176                     assertNotNull(p);
177                     //System.out.println("HACK PARAM:" + p.getName() + " = " + p.getValue());
178
mapParams.put(p.getName(), p);
179                 }
180                 assertTrue(mapParams.containsKey("showtitle"));
181                 assertTrue(mapParams.containsKey("stylesheet"));
182                 assertTrue(mapParams.containsKey("itemdisplayed"));
183                 assertTrue(mapParams.containsKey("HACK"));
184                 Parameter p = (Parameter) mapParams.get("HACK");
185                 assertTrue(p.getValue().equals("hacker"));
186                 Parameter p2 = (Parameter) mapParams.get("showtitle");
187                 MetaInfo p2m = p2.getMetaInfo();
188                 assertTrue(p2m.getTitle().equals("Show title description ?"));
189                 assertTrue(p2.getTitle().equals("Show title description ?"));
190                 Map JavaDoc pMap = hack.getParameterMap();
191                 String JavaDoc v15 = (String JavaDoc) pMap.get("itemdisplayed");
192                 assertTrue(v15.equals("15"));
193
194                 // test falling back on meta info
195

196                 PortletEntry mp = (PortletEntry) portlets.elementAt(5);
197                 assertNotNull(mp);
198                 assertTrue(mp.getName().equals("MetaTest"));
199                 assertTrue(mp.getType().equals("ref"));
200                 assertTrue(mp.getParent().equals("AdminScreen"));
201                 assertTrue(mp.getClassname().equals("org.apache.jetspeed.portal.portlets.TurbineScreenPortlet"));
202                 String JavaDoc title = mp.getTitle();
203                 assertNotNull(title);
204                 assertTrue(title.equals("Turbine Screen in a portlet"));
205                 assertTrue(mp.getDescription().equals("We put the Admin Screen in a portlet."));
206                 MetaInfo meta = mp.getMetaInfo();
207                 assertNotNull(meta);
208                 assertTrue(mp.getMetaInfo().getTitle().equals("Turbine Screen in a portlet"));
209                 assertTrue(meta.getDescription().equals("We put the Admin Screen in a portlet."));
210
211                 // media type
212
Iterator JavaDoc mpi = mp.listMediaTypes();
213                 assertNotNull(mpi);
214                 HashMap JavaDoc mpMap = new HashMap JavaDoc();
215                 while (mpi.hasNext())
216                 {
217                     String JavaDoc s = (String JavaDoc) mpi.next();
218                     mpMap.put(s, s);
219                 }
220                 assertTrue(mpMap.containsKey("html"));
221                 assertTrue(mpMap.containsKey("wml"));
222                 assertTrue(mpMap.containsKey("xml"));
223                 assertTrue(mp.hasMediaType("wml"));
224
225                 PortletEntry mt = (PortletEntry) portlets.elementAt(6);
226                 assertNotNull(mt);
227                 assertTrue(mt.getName().equals("NoMediaType"));
228                 assertTrue(mt.getType().equals("ref"));
229                 Iterator JavaDoc mti = mt.listMediaTypes();
230                 assertNotNull(mti);
231                 assertTrue(mti.hasNext());
232                 String JavaDoc s = (String JavaDoc) mti.next();
233                 assertTrue(s.equals("html"));
234
235                 assertTrue(hack.getURL().equals("http://www.xmlhack.com/rss.php"));
236                 assertTrue(hack.isCachedOnURL());
237
238                 PortletEntry keytest = (PortletEntry) portlets.elementAt(7);
239                 assertNotNull(keytest);
240                 assertTrue(keytest.getName().equals("KeyTest"));
241                 assertTrue(keytest.getType().equals("ref"));
242                 it = keytest.getParameterNames();
243                 mpMap.clear();
244                 while (it.hasNext())
245                 {
246                     String JavaDoc name = (String JavaDoc) it.next();
247                     CachedParameter cp = keytest.getCachedParameter(name);
248                     mpMap.put(cp.getName(), cp);
249                     //System.out.println("param = " + cp.getName() + "," + cp.getValue());
250
//System.out.println("keys = " + cp.isCachedOnName() + "," + cp.isCachedOnValue());
251

252                 }
253                 assertTrue(keytest.isCachedOnURL() == false);
254                 CachedParameter cp = (CachedParameter) mpMap.get("newsid");
255                 assertNotNull(cp);
256                 assertTrue(cp.isCachedOnName() == false);
257                 assertTrue(cp.isCachedOnValue() == true);
258                 cp = (CachedParameter) mpMap.get("sportsid");
259                 assertNotNull(cp);
260                 assertTrue(cp.isCachedOnName() == true);
261                 assertTrue(cp.isCachedOnValue() == false);
262                 cp = (CachedParameter) mpMap.get("contact2");
263                 assertNotNull(cp);
264                 assertTrue(cp.isCachedOnName() == true);
265                 assertTrue(cp.isCachedOnValue() == true);
266                 cp = (CachedParameter) mpMap.get("showtitle");
267                 assertNotNull(cp);
268                 assertTrue(cp.isCachedOnName() == true);
269                 assertTrue(cp.isCachedOnValue() == true);
270
271                 // categories
272
PortletEntry feeder = (PortletEntry) portlets.elementAt(3);
273                 assertNotNull(feeder);
274                 assertTrue(feeder.getName().equals("Feeder"));
275                 assertTrue(feeder.getType().equals("ref"));
276                 Iterator JavaDoc fi = feeder.listCategories();
277                 Category cat = (Category) fi.next();
278                 assertTrue(cat.getName().equals("news.world.politics"));
279                 assertTrue(cat.getGroup().equals("news"));
280                 assertTrue(feeder.hasCategory("news.world.politics", "news"));
281                 feeder.addCategory("news.us.economy");
282                 assertTrue(feeder.hasCategory("news.us.economy"));
283                 feeder.removeCategory("news.us.economy");
284                 assertTrue(!feeder.hasCategory("news.us.economy"));
285                 /*
286                 Iterator fi = feeder.listCategories();
287                     Category cat = (Category)fi.next();
288                     System.out.println("cat = " + cat.getName());
289                     System.out.println("cat.group = " + cat.getGroup());
290                 }
291                 */

292             }
293             catch (Exception JavaDoc e)
294             {
295                 String JavaDoc errmsg = "Error in xreg mapping creation: " + e.toString();
296                // e.printStackTrace();
297
assertNotNull(errmsg, null);
298             }
299         }
300         else
301         {
302             String JavaDoc errmsg = "Registy Mapping not found or not a file or unreadable: ";
303             System.err.println(errmsg);
304             assertNotNull(errmsg, null);
305         }
306     }
307
308
309     /**
310      * Tests unmarshaling registry fragment
311      * @throws Exception
312      */

313     public void testUnmarshallSecurityRegistry() throws Exception JavaDoc
314     {
315         boolean entryFound = false;
316         System.out.println("Testing marshalling of Registry security-entry");
317
318         String JavaDoc xregFile = "webapp/WEB-INF/psml/test/testcase-security.xreg";
319
320         Mapping mapping = null;
321         String JavaDoc mapFile = getMappingFileName();
322         File JavaDoc map = new File JavaDoc(mapFile);
323         if (map.exists() && map.isFile() && map.canRead())
324         {
325             FileReader JavaDoc reader = new FileReader JavaDoc(xregFile);
326             mapping = new Mapping();
327             InputSource JavaDoc is = new InputSource JavaDoc(new FileReader JavaDoc(map));
328             is.setSystemId(mapFile);
329             mapping.loadMapping(is);
330             Unmarshaller unmarshaller = new Unmarshaller(mapping);
331             RegistryFragment fragment = (RegistryFragment) unmarshaller.unmarshal(reader);
332             assertNotNull(fragment);
333             Vector JavaDoc securityEntries = fragment.getSecurityEntries();
334             assertTrue("Security Entries exist", !securityEntries.isEmpty());
335
336             // test abstract
337
SecurityEntry securityEntry = (SecurityEntry) securityEntries.firstElement();
338             assertNotNull("Got first Security Entry", securityEntry);
339             assertTrue("Getting expect security entry", securityEntry.getName().equals("basic_testcase"));
340
341             // Get the action
342
Vector JavaDoc securityAccesses = securityEntry.getAccesses();
343             assertNotNull("Got SecurityAccesses", securityAccesses);
344             SecurityAccess securityAccess = (SecurityAccess) securityAccesses.firstElement();
345             assertNotNull("Got SecurityAccess", securityAccess);
346             assertEquals("Verify action", "view", securityAccess.getAction());
347
348             // Get allows
349
Vector JavaDoc securityAllows = securityAccess.getAllows();
350             assertNotNull("Got SecurityAllows", securityAllows);
351             SecurityAllow securityAllow = (SecurityAllow) securityAllows.firstElement();
352             assertNotNull("Got SecurityAllow", securityAllow);
353             assertEquals("Verify role", "clerk", securityAllow.getRole());
354             assertNull("Verify user" , securityAllow.getUser());
355
356             securityAllow = (SecurityAllow) securityAllows.elementAt(1);
357             assertNotNull("Got SecurityAllow", securityAllow);
358             assertNull("Verify role", securityAllow.getRole());
359             assertEquals("Verify user", "joe", securityAllow.getUser());
360
361             securityAllow = (SecurityAllow) securityAllows.elementAt(2);
362             assertNotNull("Got SecurityAllow", securityAllow);
363             assertEquals("Verify role", "manager", securityAllow.getRole());
364             assertEquals("Verify user", "fred", securityAllow.getUser());
365
366             // test allows
367
assertEquals("clerk role can view", true, securityEntry.allowsRole("clerk", "view"));
368             assertEquals("manager role can view", true, securityEntry.allowsRole("manager", "view"));
369             assertEquals("anonymous role can NOT view", false, securityEntry.allowsRole("anonymous", "view"));
370             assertEquals("fred user can view", true, securityEntry.allowsUser("fred", "view"));
371             assertEquals("joe user can view", true, securityEntry.allowsUser("joe", "view"));
372             assertEquals("anonymous user can NOT view", false, securityEntry.allowsUser("anonymous", "view"));
373
374             // Customize has <allow-id user="*"/>
375
assertEquals("clerk role can NOT customize", false, securityEntry.allowsRole("clerk", "customize"));
376             assertEquals("joe user can customize", true, securityEntry.allowsUser("joe", "customize"));
377
378             // Minimize has no <allows-if .../>
379
assertEquals("clerk role can NOT minimize", true, securityEntry.allowsRole("clerk", "minimize"));
380             assertEquals("joe user can NOT minimize", true, securityEntry.allowsUser("joe", "minimize"));
381
382             // Maximixe is not defined
383
assertEquals("clerk role can NOT maximize", false, securityEntry.allowsRole("clerk", "maximize"));
384             assertEquals("joe user can NOT maximize", false, securityEntry.allowsUser("joe", "maximize"));
385
386             // Test the wide-open entry
387
entryFound = false;
388             for (Iterator JavaDoc securityIterator = securityEntries.iterator(); securityIterator.hasNext();)
389             {
390                 securityEntry = (SecurityEntry) securityIterator.next();
391                 if (securityEntry.getName().equals("wide_open") == true)
392                 {
393                     entryFound = true;
394                     assertEquals("any role is NOT allowed", false, securityEntry.allowsRole("customer", "do_any_thing"));
395                     assertEquals("any user is allowed", true, securityEntry.allowsUser("joe_customer", "do_any_thing"));
396                 }
397             }
398             assertTrue("Found wide_open security entry", entryFound);
399
400             // Test the owner-only entry
401
entryFound = false;
402             for (Iterator JavaDoc securityIterator = securityEntries.iterator(); securityIterator.hasNext();)
403             {
404                 securityEntry = (SecurityEntry) securityIterator.next();
405                 if (securityEntry.getName().equals("owner_only") == true)
406                 {
407                     entryFound = true;
408                     assertEquals("User joe_customer is not allowed", false,
409                                  securityEntry.allowsUser("joe_customer", "do_any_thing"));
410                     assertEquals("User joe_customer is not allowed", false,
411                                  securityEntry.allowsUser("joe_customer", "do_any_thing", "joe_owner"));
412                     assertEquals("User joe_owner is allowed", true,
413                                  securityEntry.allowsUser("joe_owner", "do_any_thing", "joe_owner"));
414                 }
415             }
416             assertTrue("Found owner_only security entry", entryFound);
417         }
418     }
419
420     public void testUnmarshallSecurityReference() throws Exception JavaDoc
421     {
422         System.out.println("Testing marshalling of Registry security-ref");
423
424         String JavaDoc xregFile = "webapp/WEB-INF/psml/test/testcase-security.xreg";
425
426         Mapping mapping = null;
427         String JavaDoc mapFile = getMappingFileName();
428         File JavaDoc map = new File JavaDoc(mapFile);
429         if (map.exists() && map.isFile() && map.canRead())
430         {
431             FileReader JavaDoc reader = new FileReader JavaDoc(xregFile);
432             mapping = new Mapping();
433             InputSource JavaDoc is = new InputSource JavaDoc(new FileReader JavaDoc(map));
434             is.setSystemId(mapFile);
435             mapping.loadMapping(is);
436             Unmarshaller unmarshaller = new Unmarshaller(mapping);
437             RegistryFragment fragment = (RegistryFragment) unmarshaller.unmarshal(reader);
438             assertNotNull(fragment);
439             Vector JavaDoc securityEntries = fragment.getSecurityEntries();
440             assertTrue("Security Entries exist", !securityEntries.isEmpty());
441
442             // Test the wide-open entry
443
boolean entryFound = false;
444             Iterator JavaDoc securityIterator = null;
445             for (securityIterator = securityEntries.iterator(); securityIterator.hasNext();)
446             {
447                 SecurityEntry securityEntry = (SecurityEntry) securityIterator.next();
448                 if (securityEntry.getName().equals("wide_open") == true)
449                 {
450                     entryFound = true;
451                     SecurityReference securityReference = securityEntry.getSecurityRef();
452                     assertNotNull("Security entry 'wide_open' has a SecurityReference", securityReference);
453                     assertEquals("Got parent", "admin_only", securityReference.getParent());
454                 }
455             }
456             assertTrue("Found wide_open security entry", entryFound);
457
458             // Test the use_only entry
459
entryFound = false;
460             for (securityIterator = securityEntries.iterator(); securityIterator.hasNext();)
461             {
462                 SecurityEntry securityEntry = (SecurityEntry) securityIterator.next();
463                 if (securityEntry.getName().equals("user_only") == true)
464                 {
465                     entryFound = true;
466                     SecurityReference securityReference = securityEntry.getSecurityRef();
467                     assertNull("Security entry 'user_open' has no SecurityReference", securityReference);
468                 }
469             }
470             assertEquals("Found user_only security entry", true, entryFound);
471         }
472     }
473
474     /**
475      * Tests IdentityElement unmarshaling entryset base stuff
476      * @throws Exception
477      */

478
479
480     /*
481       Configuration object to run Turbine outside a servlet container
482       ( uses turbine.properties )
483     */

484     private static TurbineConfig config = null;
485
486     /*
487       Sets up TurbineConfig using the system property:
488       <pre>turbine.properties</pre>
489     */

490     static
491     {
492         try
493         {
494            config = new TurbineConfig("webapp", "/WEB-INF/conf/TurbineResources.properties");
495            config.init();
496         }
497         catch (Exception JavaDoc e)
498         {
499             fail(StringUtils.stackTrace(e));
500         }
501     }
502
503
504     public void testUnmarshallURL() throws Exception JavaDoc
505     {
506         System.out.println("Testing marshalling of Registry, URL");
507
508         String JavaDoc xregFile = "webapp/WEB-INF/psml/test/url-testcase.xreg";
509
510         Mapping mapping = null;
511         String JavaDoc mapFile = getMappingFileName();
512         File JavaDoc map = new File JavaDoc(mapFile);
513         if (map.exists() && map.isFile() && map.canRead())
514         {
515             try
516             {
517                 FileReader JavaDoc reader = new FileReader JavaDoc(xregFile);
518                 mapping = new Mapping();
519                 InputSource JavaDoc is = new InputSource JavaDoc(new FileReader JavaDoc(map));
520                 is.setSystemId(mapFile);
521                 mapping.loadMapping(is);
522                 Unmarshaller unmarshaller = new Unmarshaller(mapping);
523                 ContentURL url = (ContentURL) unmarshaller.unmarshal(reader);
524                 assertNotNull(url);
525
526                 System.out.println(url.getURL());
527                 assertTrue(url.getURL().equals("http://www.xmlhack.com/rss.php"));
528                 assertTrue(url.isCacheKey());
529             }
530             catch (Exception JavaDoc e)
531             {
532                 String JavaDoc errmsg = "Error in xreg mapping creation: " + e.toString();
533                 System.err.println(errmsg);
534                 assertNotNull(errmsg, null);
535             }
536         }
537         else
538         {
539             String JavaDoc errmsg = "Registy Mapping not found or not a file or unreadable: ";
540             System.err.println(errmsg);
541             assertNotNull(errmsg, null);
542         }
543
544     }
545 }
Popular Tags