You are viewing a plain text version of this content. The canonical link for it is here.
Posted to jetspeed-dev@portals.apache.org by ta...@apache.org on 2002/03/26 10:03:19 UTC

cvs commit: jakarta-jetspeed/src/java/org/apache/jetspeed/services/registry TestMarshallRegistry.java

taylor      02/03/26 01:03:19

  Added:       src/java/org/apache/jetspeed/services/registry
                        TestMarshallRegistry.java
  Log:
  - test case for registry
  
  Revision  Changes    Path
  1.1                  jakarta-jetspeed/src/java/org/apache/jetspeed/services/registry/TestMarshallRegistry.java
  
  Index: TestMarshallRegistry.java
  ===================================================================
  package org.apache.jetspeed.services.registry;
  
  import java.io.File;
  import java.io.FileReader;
  
  import java.util.Vector;
  import java.util.Iterator;
  import java.util.HashMap;
  import java.util.Map;
  
  // Junit imports
  import junit.framework.Test;
  import junit.framework.TestSuite;
  import junit.framework.TestCase;
  
  //castor support
  import org.exolab.castor.xml.MarshalException;
  import org.exolab.castor.xml.Unmarshaller;
  import org.exolab.castor.xml.ValidationException;
  import org.exolab.castor.mapping.Mapping;
  import org.exolab.castor.mapping.MappingException;
  import org.xml.sax.InputSource;
  
  import org.apache.jetspeed.om.registry.*;
  
  import org.apache.turbine.services.TurbineServices;
  import org.apache.jetspeed.services.idgenerator.*;
  import org.apache.stratum.configuration.Configuration;
  import org.apache.stratum.configuration.PropertiesConfiguration;
  import org.apache.turbine.util.TurbineConfig;
  import org.apache.turbine.util.StringUtils;
  
  /**
   * TestMarshallRegistry
   *
   * @author <a href="taylor@apache.org">David Sean Taylor</a>
   * @version $Id: TestMarshallRegistry.java,v 1.1 2002/03/26 09:03:19 taylor Exp $
   */
  
  public class TestMarshallRegistry extends TestCase {    
  
      /**
       * Defines the testcase name for JUnit.
       *
       * @param name the testcase's name.
       */
      public TestMarshallRegistry( String name ) {
          super( name );
      }
      
      /**
       * Start the tests.
       *
       * @param args the arguments. Not used
       */
      public static void main(String args[]) {
          junit.awtui.TestRunner.main( new String[] { TestMarshallRegistry.class.getName() } );
      }
   
      public void setup() {
          System.out.println("Setup: Testing marshalling of Registry");
       }
      /**
       * Creates the test suite.
       *
       * @return a test suite (<code>TestSuite</code>) that includes all methods
       *         starting with "test"
       */
      public static Test suite() {
          // All methods starting with "test" will be executed in the test suite.
          return new TestSuite( TestMarshallRegistry.class );
      }
  
      private String getMappingFileName()
      {
          return "../webapp/WEB-INF/conf/registry.xml";
      }
  
      /**
       * Tests unmarshaling registry fragment
       * @throws Exception
       */
      public void testUnmarshallRegistry() throws Exception 
      {
          System.out.println("Testing marshalling of Registry");
  
          String xregFile = "../webapp/WEB-INF/psml/test/testcase-2.xreg";
  
          Mapping mapping = null;
          String mapFile = getMappingFileName();
          File map = new File(mapFile);
          if (map.exists() && map.isFile() && map.canRead())
          {
              try
              {
                  FileReader reader = new FileReader(xregFile);
                  mapping = new Mapping();
                  InputSource is = new InputSource( new FileReader(map) );
                  is.setSystemId( mapFile );
                  mapping.loadMapping( is );
                  Unmarshaller unmarshaller = new Unmarshaller(mapping);
                  RegistryFragment fragment = (RegistryFragment)unmarshaller.unmarshal(reader);
                  assertNotNull(fragment);
                  Vector portlets = fragment.getPortlets();                
  
                  // test abstract
  
                  PortletEntry rss = (PortletEntry)portlets.elementAt(1);
                  assertNotNull(rss);
                  assertTrue(rss.getName().equals("RSS"));
                  assertTrue(!rss.isHidden());
                  assertTrue(rss.getType().equals("abstract"));
                  assertNull(rss.getParent());
                  assertTrue(!rss.isApplication());
                  assertTrue(rss.getClassname().equals("org.apache.jetspeed.portal.portlets.NewRSSPortlet"));
                  // params
                  Iterator it = rss.getParameterNames();    
                  HashMap mapParams = new HashMap();
                  while (it.hasNext())
                  {
                      String name = (String)it.next();
                      Parameter p = rss.getParameter(name);
                      assertNotNull(p);
                      //System.out.println("PARAM:" + p.getName() + " = " + p.getValue());
                      mapParams.put(p.getName(), p);
                  }
                  assertTrue(mapParams.containsKey("showtitle"));
                  assertTrue(mapParams.containsKey("stylesheet"));
                  assertTrue(mapParams.containsKey("itemdisplayed"));
  
                  // test ref 
                  PortletEntry hack = (PortletEntry)portlets.elementAt(2);
                  assertNotNull(hack);
                  assertTrue(hack.getName().equals("XMLHack"));
                  assertTrue(hack.isHidden());
                  assertTrue(hack.getType().equals("ref"));
                  assertTrue(hack.getParent().equals("RSS"));
                  assertTrue(!hack.isApplication());
                  assertTrue(hack.getTitle().equals("XMLHack Title"));
                  assertTrue(hack.getDescription().equals("XMLHack Description"));
                  assertTrue(hack.getMetaInfo().getTitle().equals("XMLHack Title"));
                  assertTrue(hack.getMetaInfo().getDescription().equals("XMLHack Description"));
                  assertTrue(hack.getClassname().equals("org.apache.jetspeed.portal.portlets.NewRSSPortlet"));
  
                  mapParams.clear();
                  it = hack.getParameterNames();
                  while (it.hasNext())
                  {
                      String name = (String)it.next();
                      Parameter p = hack.getParameter(name);
                      assertNotNull(p);
                      //System.out.println("HACK PARAM:" + p.getName() + " = " + p.getValue());
                      mapParams.put(p.getName(), p);
                  }
                  assertTrue(mapParams.containsKey("showtitle"));
                  assertTrue(mapParams.containsKey("stylesheet"));
                  assertTrue(mapParams.containsKey("itemdisplayed"));
                  assertTrue(mapParams.containsKey("HACK"));
                  Parameter p = (Parameter)mapParams.get("HACK");
                  assertTrue(p.getValue().equals("hacker"));
                  Parameter p2 = (Parameter)mapParams.get("showtitle");
                  MetaInfo p2m = p2.getMetaInfo();
                  assertTrue(p2m.getTitle().equals("Show title description ?"));
                  assertTrue(p2.getTitle().equals("Show title description ?"));
                  Map pMap = hack.getParameterMap();
                  String v15 = (String)pMap.get("itemdisplayed");
                  assertTrue(v15.equals("15"));
  
                  // test falling back on meta info               
  
                  PortletEntry mp = (PortletEntry)portlets.elementAt(5);
                  assertNotNull(mp);
                  assertTrue(mp.getName().equals("MetaTest"));
                  assertTrue(mp.getType().equals("ref"));
                  assertTrue(mp.getParent().equals("AdminScreen"));
                  assertTrue(mp.getClassname().equals("org.apache.jetspeed.portal.portlets.TurbineScreenPortlet"));
                  String title = mp.getTitle();
                  assertNotNull(title);
                  assertTrue(title.equals("Turbine Screen in a portlet"));
                  assertTrue(mp.getDescription().equals("We put the Admin Screen in a portlet."));
                  MetaInfo meta = mp.getMetaInfo();
                  assertNotNull(meta);
                  assertTrue(mp.getMetaInfo().getTitle().equals("Turbine Screen in a portlet"));
                  assertTrue(meta.getDescription().equals("We put the Admin Screen in a portlet."));
  
                  // media type
                  Iterator mpi = mp.listMediaTypes();
                  assertNotNull(mpi);
                  HashMap mpMap = new HashMap();
                  while(mpi.hasNext())
                  {
                      String s = (String)mpi.next();
                      mpMap.put(s, s);
                  }
                  assertTrue(mpMap.containsKey("html"));
                  assertTrue(mpMap.containsKey("wml"));
                  assertTrue(mpMap.containsKey("xml"));
                  assertTrue(mp.hasMediaType("wml"));
                  
                  PortletEntry mt = (PortletEntry)portlets.elementAt(6);
                  assertNotNull(mt);
                  assertTrue(mt.getName().equals("NoMediaType"));
                  assertTrue(mt.getType().equals("ref"));
                  Iterator mti = mt.listMediaTypes();
                  assertNotNull(mti);
                  assertTrue(mti.hasNext());
                  String s = (String)mti.next();
                  assertTrue(s.equals("html"));
               
                  assertTrue(hack.getURL().equals("http://www.xmlhack.com/rss.php"));
                  assertTrue(hack.isCachedOnURL());
  
                  PortletEntry keytest = (PortletEntry)portlets.elementAt(7);
                  assertNotNull(keytest);
                  assertTrue(keytest.getName().equals("KeyTest"));
                  assertTrue(keytest.getType().equals("ref"));
                  it = keytest.getParameterNames();
                  mpMap.clear();
                  while (it.hasNext())
                  {
                      String name = (String)it.next();
                      CachedParameter cp = keytest.getCachedParameter(name);
                      mpMap.put(cp.getName(), cp);
                      //System.out.println("param = " + cp.getName() + "," + cp.getValue());
                      //System.out.println("keys = " + cp.isCachedOnName() + "," + cp.isCachedOnValue());
  
                  }
                  assertTrue(keytest.isCachedOnURL() == false);
                  CachedParameter cp = (CachedParameter)mpMap.get("newsid");
                  assertNotNull(cp);
                  assertTrue(cp.isCachedOnName() == false);
                  assertTrue(cp.isCachedOnValue() == true);
                  cp = (CachedParameter)mpMap.get("sportsid");
                  assertNotNull(cp);
                  assertTrue(cp.isCachedOnName() == true);
                  assertTrue(cp.isCachedOnValue() == false);                             
                  cp = (CachedParameter)mpMap.get("contact2");
                  assertNotNull(cp);
                  assertTrue(cp.isCachedOnName() == true);
                  assertTrue(cp.isCachedOnValue() == true);
                  cp = (CachedParameter)mpMap.get("showtitle");
                  assertNotNull(cp);
                  assertTrue(cp.isCachedOnName() == true);
                  assertTrue(cp.isCachedOnValue() == true);
              }
              catch (Exception e)
              {
                  String errmsg = "Error in xreg mapping creation: " + e.toString();
                 // e.printStackTrace();
                 assertNotNull(errmsg, null);
              }
          }
          else
          {
              String errmsg = "Registy Mapping not found or not a file or unreadable: ";
              System.err.println(errmsg);
              assertNotNull(errmsg, null);
          }  
      }
  
      
      /**
       * Tests IdentityElement unmarshaling entryset base stuff
       * @throws Exception
       */
  
  
      /*
        Configuration object to run Turbine outside a servlet container
        ( uses turbine.properties )
      */
      private static TurbineConfig config = null;
      
      /*    
        Sets up TurbineConfig using the system property:
        <pre>turbine.properties</pre>
      */
      static
      {
          try
          {
             config = new TurbineConfig( "../webapp", "/WEB-INF/conf/TurbineResources.properties");
             config.init();
          }
          catch (Exception e)
          {
              fail(StringUtils.stackTrace(e));
          }
      }
  
  
      public void testUnmarshallURL() throws Exception 
      {
          System.out.println("Testing marshalling of Registry, URL");
  
          String xregFile = "../webapp/WEB-INF/psml/test/url-testcase.xreg";
  
          Mapping mapping = null;
          String mapFile = getMappingFileName();
          File map = new File(mapFile);
          if (map.exists() && map.isFile() && map.canRead())
          {
              try
              {
                  FileReader reader = new FileReader(xregFile);
                  mapping = new Mapping();
                  InputSource is = new InputSource( new FileReader(map) );
                  is.setSystemId( mapFile );
                  mapping.loadMapping( is );
                  Unmarshaller unmarshaller = new Unmarshaller(mapping);
                  ContentURL url = (ContentURL)unmarshaller.unmarshal(reader);
                  assertNotNull(url);
  
                  System.out.println(url.getURL());
                  assertTrue(url.getURL().equals("http://www.xmlhack.com/rss.php"));
                  assertTrue(url.isCacheKey());
              }
              catch (Exception e)
              {
                  String errmsg = "Error in xreg mapping creation: " + e.toString();
                  System.err.println(errmsg);
                  assertNotNull(errmsg, null);
              }
          }
          else
          {
              String errmsg = "Registy Mapping not found or not a file or unreadable: ";
              System.err.println(errmsg);
              assertNotNull(errmsg, null);
          }  
  
      }
  }
  
  

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>