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.psmlmanager;
18  
19  // Java imports
20  import java.io.File;
21  import java.io.FileReader;
22  import java.io.FileWriter;
23  import java.util.Iterator;
24  
25  // Junit imports
26  import junit.framework.Test;
27  import junit.framework.TestSuite;
28  
29  //castor support
30  import org.exolab.castor.xml.Unmarshaller;
31  import org.exolab.castor.xml.Marshaller;
32  import org.exolab.castor.mapping.Mapping;
33  import org.xml.sax.InputSource;
34  
35  // serialization support
36  import org.apache.xml.serialize.Serializer;
37  import org.apache.xml.serialize.XMLSerializer;
38  import org.apache.xml.serialize.OutputFormat;
39  
40  // Jetspeed imports
41  import org.apache.jetspeed.om.SecurityReference;
42  import org.apache.jetspeed.om.profile.ConfigElement;
43  import org.apache.jetspeed.om.profile.Control;
44  import org.apache.jetspeed.om.profile.Controller;
45  import org.apache.jetspeed.om.profile.Entry;
46  import org.apache.jetspeed.om.profile.Layout;
47  import org.apache.jetspeed.om.profile.MetaInfo;
48  import org.apache.jetspeed.om.profile.Parameter;
49  import org.apache.jetspeed.om.profile.Portlets;
50  import org.apache.jetspeed.om.profile.Reference;
51  import org.apache.jetspeed.om.profile.Skin;
52  import org.apache.jetspeed.om.profile.Security;
53  import org.apache.jetspeed.test.JetspeedTestCase;
54  
55  // Turbine imports
56  import org.apache.turbine.util.TurbineConfig;
57  import org.apache.turbine.util.StringUtils;
58  
59  /***
60   * TestMarshalPsml
61   *
62   * @author <a href="taylor@apache.org">David Sean Taylor</a>
63   * @version $Id: TestMarshalPsml.java,v 1.1 2004/04/07 22:02:42 jford Exp $
64   */
65  //public class TestMarshalPsml extends ServletTestCase {
66  public class TestMarshalPsml extends JetspeedTestCase {    
67      
68      /***
69       * Defines the testcase name for JUnit.
70       *
71       * @param name the testcase's name.
72       */
73      public TestMarshalPsml( String name ) {
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          junit.awtui.TestRunner.main( new String[] { TestMarshalPsml.class.getName() } );
84      }
85   
86      public void setup() {
87          System.out.println("Setup: Testing marshalling of PSML");
88       }
89      /***
90       * Creates the test suite.
91       *
92       * @return a test suite (<code>TestSuite</code>) that includes all methods
93       *         starting with "test"
94       */
95      public static Test suite() {
96          // All methods starting with "test" will be executed in the test suite.
97          return new TestSuite( TestMarshalPsml.class );
98      }
99  
100     private String getMappingFileName()
101     {
102         return "webapp/WEB-INF/conf/psml-mapping.xml";
103     }
104 
105     /***
106      * Tests ConfigElement unmarshaling entryset base stuff
107      * @throws Exception
108      */
109 
110     public void testUnmarshalConfigElement() throws Exception 
111     {
112         System.out.println("Testing marshalling of PSML on base *** ConfigElement ***");
113 
114         String psmlFile = "webapp/WEB-INF/psml/test/testcase.psml";
115 
116         Mapping mapping = null;
117         String mapFile = getMappingFileName();
118         File map = new File(mapFile);
119         if (map.exists() && map.isFile() && map.canRead())
120         {
121             try
122             {
123                 FileReader reader = new FileReader(psmlFile);
124                 mapping = new Mapping();
125                 InputSource is = new InputSource( new FileReader(map) );
126                 is.setSystemId( mapFile );
127                 mapping.loadMapping( is );
128                 Unmarshaller unmarshaller = new Unmarshaller(mapping);
129                 ConfigElement rootset = (ConfigElement)unmarshaller.unmarshal(reader);
130                 
131                 assertTrue(rootset.getName().equals("theRootSet"));
132 
133                 Iterator params = rootset.getParameterIterator();
134                 Parameter param = (Parameter)params.next();
135                 assertTrue(param.getName().equals("city"));
136                 assertTrue(param.getValue().equals("Atlanta"));
137                 param = (Parameter)params.next();
138                 assertTrue(param.getName().equals("state"));
139                 assertTrue(param.getValue().equals("Georgia"));
140                 param = (Parameter)params.next();
141                 assertTrue(param.getName().equals("country"));
142                 assertTrue(param.getValue().equals("USA"));
143                 assertTrue(rootset.getParameterValue("city").equals("Atlanta"));
144                 assertTrue(rootset.getParameterValue("country").equals("USA"));
145                 assertTrue(rootset.getParameter("state").getValue().equals("Georgia"));                
146                 
147             }
148             catch (Exception e)
149             {
150                 String errmsg = "Error in psml mapping creation: " + e.toString();
151                 System.err.println(errmsg);
152                 assertNotNull(errmsg, null);
153             }
154         }
155         else
156         {
157             String errmsg = "PSML Mapping not found or not a file or unreadable: " + map;
158             System.err.println(errmsg);
159             assertNotNull(errmsg, null);
160         }  
161     }
162 
163     /***
164      * Tests IdentityElement unmarshaling entryset base stuff
165      * @throws Exception
166      */
167 
168     public void testUnmarshalPsml() throws Exception 
169     {
170         System.out.println("Testing marshalling of PSML on base *** IdentityElement ***");
171 
172         String psmlFile = "webapp/WEB-INF/psml/test/testcase.psml";
173 
174         Mapping mapping = null;
175         String mapFile = getMappingFileName();
176         File map = new File(mapFile);
177         if (map.exists() && map.isFile() && map.canRead())
178         {
179             try
180             {
181                 FileReader reader = new FileReader(psmlFile);
182                 mapping = new Mapping();
183                 InputSource is = new InputSource( new FileReader(map) );
184                 is.setSystemId( mapFile );
185                 mapping.loadMapping( is );
186                 System.out.println("++++++++++++++++++++++++++++++++++++++++++++++++++++");
187                 Unmarshaller unmarshaller = new Unmarshaller(mapping);
188                 Portlets rootset = (Portlets)unmarshaller.unmarshal(reader);
189                 System.out.println("++++++++++++++++++++++++++++++++++++++++++++++++++++");
190 
191                 assertTrue(rootset.getName().equals("theRootSet"));
192                 assertTrue(rootset.getId().equals("01"));
193 
194                 MetaInfo meta = rootset.getMetaInfo();
195                 assertNotNull(meta);
196                 assertTrue(meta.getTitle().equals("Jetspeed"));
197                 assertTrue(meta.getDescription().equals("This is the default page for me"));
198                 assertTrue(meta.getImage().equals("me.png"));
199                 assertTrue(rootset.getTitle().equals("Jetspeed"));
200                 assertTrue(rootset.getDescription().equals("This is the default page for me"));
201                 assertTrue(rootset.getImage().equals("me.png"));
202 
203                 Security security = rootset.getSecurity();
204                 assertNotNull(security);
205                 assertTrue(security.getId().equals("999"));
206 
207                 Iterator params = rootset.getParameterIterator();
208                 Parameter param = (Parameter)params.next();
209                 assertTrue(param.getName().equals("city"));
210                 assertTrue(param.getValue().equals("Atlanta"));
211                 param = (Parameter)params.next();
212                 assertTrue(param.getName().equals("state"));
213                 assertTrue(param.getValue().equals("Georgia"));
214                 param = (Parameter)params.next();
215                 assertTrue(param.getName().equals("country"));
216                 assertTrue(param.getValue().equals("USA"));
217 
218                 assertTrue(rootset.getParameterValue("city").equals("Atlanta"));
219                 assertTrue(rootset.getParameterValue("country").equals("USA"));
220                 assertTrue(rootset.getParameter("state").getValue().equals("Georgia"));                
221 
222                 Skin skin = rootset.getSkin();
223                 assertNotNull(skin);
224                 assertTrue(skin.getName().equals("skinny"));
225                 assertTrue(skin.getState().equals("DETACHED"));
226                 Iterator skinParams = skin.getParameterIterator();
227                 assertNotNull(skinParams);
228                 Parameter skinParam = (Parameter)skinParams.next();
229                 assertTrue(skinParam.getName().equals("a"));
230                 assertTrue(skinParam.getValue().equals("1"));
231                 skinParam = (Parameter)skinParams.next();
232                 assertTrue(skinParam.getName().equals("b"));
233                 assertTrue(skinParam.getValue().equals("2"));
234 
235                 Layout layout = rootset.getLayout();
236                 assertNotNull(layout);
237                 assertTrue(layout.getName().equals("layout1"));
238                 assertTrue(layout.getSize() == 1);
239                 assertTrue(layout.getPosition() == 3);
240 
241                 Iterator layoutParams = layout.getParameterIterator();
242                 assertNotNull(layoutParams);
243                 Parameter layoutParam = (Parameter)layoutParams.next();
244                 assertTrue(layoutParam.getName().equals("a"));
245                 assertTrue(layoutParam.getValue().equals("1"));
246                 layoutParam = (Parameter)layoutParams.next();
247                 assertTrue(layoutParam.getName().equals("b"));
248                 assertTrue(layoutParam.getValue().equals("2"));
249 
250                 Control control = rootset.getControl();
251                 assertNotNull(control);
252                 Iterator controlParams = control.getParameterIterator();
253                 assertNotNull(controlParams);
254                 Parameter controlParam = (Parameter)controlParams.next();
255                 assertTrue(control.getName().equals("TabControl"));
256                 assertTrue(controlParam.getName().equals("a"));
257                 assertTrue(controlParam.getValue().equals("1"));
258                 controlParam = (Parameter)controlParams.next();
259                 assertTrue(controlParam.getName().equals("b"));
260                 assertTrue(controlParam.getValue().equals("2"));
261 
262                 Controller controller = rootset.getController();
263                 assertNotNull(controller);
264                 Iterator controllerParams = controller.getParameterIterator();
265                 assertNotNull(controllerParams);
266                 Parameter controllerParam = (Parameter)controllerParams.next();
267 
268                 assertTrue(controller.getName().equals("TabController"));
269                 assertTrue(controllerParam.getName().equals("a"));
270                 assertTrue(controllerParam.getValue().equals("1"));
271                 controllerParam = (Parameter)controllerParams.next();
272                 assertTrue(controllerParam.getName().equals("b"));
273                 assertTrue(controllerParam.getValue().equals("2"));
274 
275                 Iterator entries = rootset.getEntriesIterator();
276                 assertNotNull(entries);
277                 Entry entry = (Entry)entries.next();
278                 assertTrue(entry.getParent().equals("LoggedInWelcome"));    
279                 assertTrue(entry.getId().equals("03"));    
280 
281                 Layout elayout = entry.getLayout();
282                 assertNotNull(elayout);
283                 Iterator elayoutParams = elayout.getParameterIterator();
284                 assertNotNull(elayoutParams);
285                 Parameter elayoutParam = (Parameter)elayoutParams.next();
286                 assertTrue(elayoutParam.getName().equals("column"));
287                 elayoutParam = (Parameter)elayoutParams.next();
288                 assertTrue(elayoutParam.getName().equals("row"));
289 
290                 Iterator pv = rootset.getPortletsIterator();
291 
292                 Portlets p = (Portlets)pv.next();
293                 assertNotNull(p);
294 
295                 Controller pc = p.getController();
296                 assertNotNull(pc);
297                 assertTrue(pc.getName().equals("TwoColumns"));
298 
299                 Iterator pe = p.getEntriesIterator();
300                 assertNotNull(pe);
301                 Entry e1 = (Entry)pe.next();
302                 assertTrue(e1.getParent().equals("HelloWhatever"));    
303                 assertTrue(e1.getId().equals("99"));    
304 
305                 Entry e2 = (Entry)pe.next();
306                 assertTrue(e2.getParent().equals("HelloVelocity"));    
307                 assertTrue(e2.getId().equals("100"));    
308 
309                 Entry e3 = (Entry)pe.next();
310                 assertTrue(e3.getParent().equals("HelloCleveland"));    
311                 assertTrue(e3.getId().startsWith("P-"));    
312                 System.out.println(e3.getId());
313 
314                 Iterator rv = p.getReferenceIterator();
315                 assertNotNull(rv);
316                 Reference ref = (Reference)rv.next();
317                 assertNotNull(ref);
318                 assertTrue(ref.getName().equals("ReferenceTest"));
319                 assertTrue(ref.getId().equals("300"));
320                 Portlets epr = ref.getPortletsReference();
321                 assertNotNull(epr);
322                 assertEquals("group/apache/page/news/media-type/html", ref.getPath());
323 
324                 /* SHIT
325                   
326                 assertNotNull(epr.getMetaInfo());
327                 assertNotNull(epr.getMetaInfo().getTitle());
328                   
329   assertTrue(epr.getMetaInfo().getTitle().equals("Default Apache News page"));
330   
331                 // DST: - TODO: only use 'test' psml for unit tests -
332                 // otherwise the tests are against moving targets;
333                 // DST: assertTrue(epr.getController().getParameter("mode").getValue().equals("row"));
334                 // DST: assertTrue(epr.getSkin().getParameter("selected-color").getValue().equals("#990000"));
335                 Entry ent = epr.getEntry(0);
336                 assertTrue(ent.getParent().equals("Apacheweek"));
337 
338                 Iterator itt = p.getPortletsIterator();
339                 while (itt.hasNext())
340                 {
341                     Portlets pp = (Portlets)itt.next();
342                     System.out.println(" PORTLETS %%% " + pp.getId());
343                     if (pp instanceof Reference)
344                     {
345                         System.out.println(" PORTLETS %%% REF: " + pp.getId());
346                     }
347                 }
348 */                
349             }
350             catch (Exception e)
351             {
352                 String errmsg = "Error in psml mapping creation: " + e.toString();
353                 e.printStackTrace();
354                 System.err.println(errmsg);
355                 assertNotNull(errmsg, null);
356             }
357         }
358         else
359         {
360             String errmsg = "PSML Mapping not found or not a file or unreadable: ";
361             System.err.println(errmsg);
362             assertNotNull(errmsg, null);
363         }  
364     }
365 
366     /***
367      * Tests unmarshaling security
368      * @throws Exception
369      */
370     public void testUnmarshalSecurity() throws Exception 
371     {
372         System.out.println("Testing marshalling of PSML on base *** Security ***");
373 
374         String psmlFile = "webapp/WEB-INF/psml/test/testsecurity.psml";
375 
376         Mapping mapping = null;
377         String mapFile = getMappingFileName();
378         File map = new File(mapFile);
379         if (map.exists() && map.isFile() && map.canRead())
380         {
381             try
382             {
383                 FileReader reader = new FileReader(psmlFile);
384                 mapping = new Mapping();
385                 InputSource is = new InputSource( new FileReader(map) );
386                 is.setSystemId( mapFile );
387                 mapping.loadMapping( is );
388                 Unmarshaller unmarshaller = new Unmarshaller(mapping);
389                 Security security = (Security)unmarshaller.unmarshal(reader);
390                 assertNotNull(security);
391                 assertTrue(security.getId().equals("1000"));
392 
393             }
394             catch (Exception e)
395             {
396                 String errmsg = "Error in psml mapping creation: " + e.toString();
397                 System.err.println(errmsg);
398                 assertNotNull(errmsg, null);
399             }
400         }
401         else
402         {
403             String errmsg = "PSML Mapping not found or not a file or unreadable.";
404             System.err.println(errmsg);
405             assertNotNull(errmsg, null);
406         }
407    
408     }
409 
410     /***
411      * Tests unmarshaling security
412      * @throws Exception
413      */
414     public void testUnmarshalSecurityRef() throws Exception 
415     {
416         System.out.println("Testing marshalling of PSML on base *** Security-ref ***");
417 
418         String psmlFile = "webapp/WEB-INF/psml/test/testcase_securityref.psml";
419 
420         Mapping mapping = null;
421         String mapFile = getMappingFileName();
422         File map = new File(mapFile);
423         if (map.exists() && map.isFile() && map.canRead())
424         {
425             FileReader reader = new FileReader(psmlFile);
426             mapping = new Mapping();
427             InputSource is = new InputSource( new FileReader(map) );
428             is.setSystemId( mapFile );
429             mapping.loadMapping( is );
430 
431             System.out.println("++++++++++++++++++++++++++++++++++++++++++++++++++++");
432             Unmarshaller unmarshaller = new Unmarshaller(mapping);
433             Portlets rootset = (Portlets)unmarshaller.unmarshal(reader);
434             System.out.println("++++++++++++++++++++++++++++++++++++++++++++++++++++");
435             
436             assertTrue(rootset.getName().equals("theRootSet"));
437             assertTrue(rootset.getId().equals("01"));
438             
439             SecurityReference securityRef = rootset.getSecurityRef();
440             assertNotNull("got SecurityRef", securityRef);
441             assertEquals( "Name of parent", "all_users", securityRef.getParent());
442                 
443         }
444         else
445         {
446             String errmsg = "PSML Mapping not found or not a file or unreadable.";
447             System.err.println(errmsg);
448             assertNotNull(errmsg, null);
449         }
450    
451     }
452   /*
453     Configuration object to run Turbine outside a servlet container
454     ( uses turbine.properties )
455     */
456     private static TurbineConfig config = null;
457     
458     /***
459     Sets up TurbineConfig using the system property:
460     <pre>turbine.properties</pre>
461     */
462     static
463     {
464         try
465         {
466             config = new TurbineConfig( "webapp", "/WEB-INF/conf/TurbineResources.properties");
467             config.init();
468         }
469         catch (Exception e)
470         {
471             fail(StringUtils.stackTrace(e));
472         }
473     }
474 
475     public void testMarshalPsml() throws Exception 
476     {
477         System.out.println("Testing marshalling of PSML on base *** IdentityElement ***");
478 
479         String psmlFile = "webapp/WEB-INF/psml/test/testcaseMarshall.psml";
480 
481         Mapping mapping = null;
482         String mapFile = getMappingFileName();
483         File map = new File(mapFile);
484         if (map.exists() && map.isFile() && map.canRead())
485         {
486             try
487             {
488                 FileReader reader = new FileReader(psmlFile);
489                 mapping = new Mapping();
490                 InputSource is = new InputSource( new FileReader(map) );
491                 is.setSystemId( mapFile );
492                 mapping.loadMapping( is );
493                 System.out.println("++++++++++++++++++++++++++++++++++++++++++++++++++++");
494                 Unmarshaller unmarshaller = new Unmarshaller(mapping);
495                 Portlets rootset = (Portlets)unmarshaller.unmarshal(reader);
496                 System.out.println("++++++++++++++++++++++++++++++++++++++++++++++++++++");
497 
498                 assertTrue(rootset.getName().equals("theRootSet"));
499                 assertTrue(rootset.getId().equals("01"));
500 
501                 Iterator itt = rootset.getPortletsIterator();
502                 while (itt.hasNext())
503                 {
504                     Portlets pp = (Portlets)itt.next();
505                     System.out.println(" PORTLETS %%% " + pp.getId());
506                     if (pp instanceof Reference)
507                     {
508                         System.out.println(" PORTLETS %%% REF: " + pp.getId());
509                     }
510                 }
511 
512                 Iterator itr = rootset.getReferenceIterator();
513                 while (itr.hasNext())
514                 {
515                     Reference r = (Reference)itr.next();
516                     System.out.println(" REFERENCE %%% " + r.getId());
517                 }
518 
519                 OutputFormat format = new OutputFormat();
520                 format.setIndenting(true);
521                 format.setIndent(4);
522     
523                 File f = new File("marshalled.psml");
524                 FileWriter writer = null;
525 
526                 writer = new FileWriter(f);
527 
528                 System.out.println("-----------------------------------------------------------------");
529                 Serializer serializer = new XMLSerializer(writer, format); 
530                 Marshaller marshaller = new Marshaller(serializer.asDocumentHandler());
531                 marshaller.setMapping(mapping);
532                 marshaller.marshal(rootset);
533                 System.out.println("-----------------------------------------------------------------");
534                 System.out.println("done");
535 
536             }
537             catch (Exception e)
538             {
539                 String errmsg = "Error in psml mapping creation: " + e.toString();
540                 e.printStackTrace();
541                 System.err.println(errmsg);
542                 assertNotNull(errmsg, null);
543             }
544         }
545         else
546         {
547             String errmsg = "PSML Mapping not found or not a file or unreadable: ";
548             System.err.println(errmsg);
549             assertNotNull(errmsg, null);
550         }  
551     }
552 
553     public void testMetaInfo() throws Exception 
554     {
555         boolean foundEntry07 = false;
556         boolean foundPortlet02 = false;
557         
558         System.out.println("Testing marshalling of PSML on base *** IdentityElement ***");
559 
560         String psmlFile = "webapp/WEB-INF/psml/test/testcaseMarshall.psml";
561 
562         Mapping mapping = null;
563         String mapFile = getMappingFileName();
564         File map = new File(mapFile);
565         if (map.exists() && map.isFile() && map.canRead())
566         {
567             try
568             {
569                 FileReader reader = new FileReader(psmlFile);
570                 mapping = new Mapping();
571                 InputSource is = new InputSource( new FileReader(map) );
572                 is.setSystemId( mapFile );
573                 mapping.loadMapping( is );
574                 System.out.println("++++++++++++++++++++++++++++++++++++++++++++++++++++");
575                 Unmarshaller unmarshaller = new Unmarshaller(mapping);
576                 Portlets rootset = (Portlets)unmarshaller.unmarshal(reader);
577                 System.out.println("++++++++++++++++++++++++++++++++++++++++++++++++++++");
578 
579                 assertTrue(rootset.getName().equals("theRootSet"));
580                 assertTrue(rootset.getId().equals("01"));
581 
582                 Iterator itt = rootset.getPortletsIterator();
583                 while (itt.hasNext())
584                 {
585                     Portlets pp = (Portlets)itt.next();
586                     System.out.println(" PORTLETS %%% " + pp.getId());
587                     if ( pp.getId().equals("02"))
588                     {
589                         foundPortlet02 = true;
590                         MetaInfo pp02MetaInfo = pp.getMetaInfo();
591                         assertNotNull( "Portlet ID 02 has metaInfo", pp02MetaInfo);
592                         assertEquals( "Portlet ID 02 Title", "Portlet Title", pp02MetaInfo.getTitle());
593                         assertEquals( "Portlet ID 02 Title", "Portlet Description", pp02MetaInfo.getDescription());
594                         assertEquals( "Portlet ID 02 Title", "Portlet Image", pp02MetaInfo.getImage());
595                         Iterator pp02itt = pp.getEntriesIterator();
596                         while (pp02itt.hasNext())
597                         {
598                             Entry pp02Entry = (Entry) pp02itt.next();
599                             assertNotNull( "Portlet Id 02 has entry", pp02Entry);
600                             if (pp02Entry.getId().equals("07"))
601                             {
602                                 foundEntry07 = true;
603                                 MetaInfo entry07MetaInfo = pp02Entry.getMetaInfo();
604                                 assertNotNull( "Entry ID 07 has metaInfo", entry07MetaInfo);
605                                 assertEquals( "Entry ID 07 Title", "Entry Title", entry07MetaInfo.getTitle());
606                                 assertEquals( "Entry ID 07 Title", "Entry Description", entry07MetaInfo.getDescription());
607                                 assertEquals( "Entry ID 07 Title", "Entry Image", entry07MetaInfo.getImage());
608                             }
609                         }
610                     }
611                 }
612                 assertTrue( "Tested Portlet 02", foundPortlet02);
613                 assertTrue( "Tested Entry 07", foundEntry07);
614 
615             }
616             catch (Exception e)
617             {
618                 String errmsg = "Error in psml mapping creation: " + e.toString();
619                 e.printStackTrace();
620                 System.err.println(errmsg);
621                 assertNotNull(errmsg, null);
622             }
623         }
624         else
625         {
626             String errmsg = "PSML Mapping not found or not a file or unreadable: ";
627             System.err.println(errmsg);
628             assertNotNull(errmsg, null);
629         }  
630     }
631 }