1 Reply Latest reply on Aug 30, 2002 11:44 AM by Jens Schumann

    java.lang.ClassCastException

    Roger Ball Newbie

      I am writing a JUnit test to test the CMP CD EJB example project for JBoss 2.2.4. Everytime I run it I get the runtime exception “java.lang.ClassCastException” at the line “CDCollectionHome home = (CDCollectionHome)PortableRemoteObject.narrow (ref, CDCollectionHome.class);” I have tried to get around it but have not found a solution. Any ideas?

      Roger



      //--------------------------------- TestCD -----------------------------------------------------------------

      package org.jboss.docs.cmp.cd;

      import org.jboss.docs.cmp.cd.interfaces.CDCollectionHome;
      import org.jboss.docs.cmp.cd.interfaces.CDCollection;
      import org.jboss.docs.cmp.cd.interfaces.CD;
      import javax.naming.InitialContext;
      import javax.rmi.PortableRemoteObject;
      import java.util.Hashtable;
      import java.util.Properties;
      import java.io.FileInputStream;

      import junit.framework.*;


      public class TestCD extends TestCase {

      public TestCD(String name) {
      super(name);
      }

      protected void setUp() {

      }

      public static Test suite() {

      return new TestSuite(TestCD.class);
      }

      public void testSingleQuery() {

      String text= "Time further";
      int theLen = 0;

      try
      {
      // Get a naming context
      InitialContext jndiContext = new InitialContext();

      if (jndiContext == null)
      {
      Exception e1 = new Exception("jndiContext is null");
      throw (e1);
      }

      // Get a reference to a CD Bean
      Object ref = jndiContext.lookup("cd/CDCollection");

      if (ref == null)
      {
      Exception e1 = new Exception("ref is null");
      throw (e1);
      }

      System.out.println("The class of " + ref +
      " is " + ref.getClass().getName());

      // Get a reference from this to the Bean's Home interface
      CDCollectionHome home = (CDCollectionHome)PortableRemoteObject.narrow (ref, CDCollectionHome.class);
      //Object home = PortableRemoteObject.narrow(ref, CDCollectionHome.class);

      System.out.println("The class of " + home +
      " is " + home.getClass().getName());

      /**************
      CDCollection cdCollection = home.create();

      if (cdCollection == null)
      {
      Exception e1 = new Exception("cdCollection is null");
      throw (e1);
      }


      CD[] cds = cdCollection.lookupInAnyField(text);
      theLen = cds.length;

      *******/
      }
      catch(Exception e)
      {
      System.out.println("home NULL?... "+e.toString()+ "\r");
      e.printStackTrace();
      }

      //assertTrue(theLen == 1);
      assertEquals(1,theLen);

      /************
      for (int i = 0; i < cds.length; i++)
      {
      System.out.println (cds.getId() + "\t" + cds.getTitle() + "\t" +
      cds.getArtist() + "\t" + cds.getType());
      }
      // forced failure result == 5
      assertTrue(result == 6);
      *************/
      }
      }