Esempio n. 1
0
  public static void main(String[] args) {
    try {
      Properties p = new Properties();

      p.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
      p.put(Context.PROVIDER_URL, "10.10.10.13:1100,10.10.10.14:1100");
      // p.put(Context.PROVIDER_URL, "localhost:1100");
      p.put(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces");
      InitialContext ctx = new InitialContext(p);

      StatelessSessionHome statelessSessionHome =
          (StatelessSessionHome) ctx.lookup("nextgen.StatelessSession");
      EnterpriseEntityHome cmpHome = (EnterpriseEntityHome) ctx.lookup("nextgen.EnterpriseEntity");
      StatelessSession statelessSession = statelessSessionHome.create();
      EnterpriseEntity cmp = null;
      try {
        cmp = cmpHome.findByPrimaryKey("bill");
      } catch (Exception ex) {
        cmp = cmpHome.create("bill");
      }
      int count = 0;
      while (true) {
        System.out.println(statelessSession.callBusinessMethodB());
        try {
          cmp.setOtherField(count++);
        } catch (Exception ex) {
          System.out.println("exception, trying to create it: " + ex);
          cmp = cmpHome.create("bill");
          cmp.setOtherField(count++);
        }
        System.out.println("Entity: " + cmp.getOtherField());
        Thread.sleep(2000);
      }
    } catch (NamingException nex) {
      if (nex.getRootCause() != null) {
        nex.getRootCause().printStackTrace();
      }
    } catch (Exception ex) {
      ex.printStackTrace();
    }
  }
  public void testAllTypesBean() throws Exception {

    try {
      Context ctx = new InitialContext();

      getLog().debug("");
      getLog().debug("");
      getLog().debug("Test AllTypesBean");
      getLog().debug("=================");
      getLog().debug("");
      getLog().debug(++test + "- " + "Looking up the home AllTypes...");
      AllTypesHome allTypesHome = (AllTypesHome) ctx.lookup("AllTypes");
      if (allTypesHome != null) getLog().debug("ok");

      getLog().debug(++test + "- " + "Getting the home handle...");
      HomeHandle homeHandle = allTypesHome.getHomeHandle();
      getLog().debug("OK");

      getLog().debug(++test + "- " + "Getting the home back from the handle...");
      EJBHome aHome = homeHandle.getEJBHome();
      getLog().debug("OK");

      getLog().debug(++test + "- " + "Getting metadata from home...");
      EJBMetaData aMetaData = aHome.getEJBMetaData();
      getLog().debug("OK");

      getLog().debug(++test + "- " + "Getting home from metadata...");
      aHome = aMetaData.getEJBHome();
      getLog().debug("OK");

      getLog().debug(++test + "- " + "Calling findByPrimaryKey on AllTypesHome with name seb...");

      AllTypes allTypes = null;
      try {
        allTypes = allTypesHome.findByPrimaryKey("seb");
      } catch (Exception e) {
        getLog().debug(e.getMessage());
      }
      if (allTypes == null) {

        getLog().debug("not found OK");
        getLog().debug(++test + "- " + "Calling create on AllTypesHome with name seb...");
        allTypes = allTypesHome.create("seb");
      }

      if (allTypes != null) getLog().debug("ok");

      getLog()
          .debug(
              ++test
                  + "- "
                  + "Calling business method A an AllTypes (B2B with external ejb-ref)...");
      getLog().debug("OK, result is" + allTypes.callBusinessMethodA());

      getLog().debug("Getting all the fields");
      getLog().debug(++test + "- " + "boolean " + allTypes.getBoolean() + " Ok");
      getLog().debug(++test + "- " + "byte " + allTypes.getByte() + " Ok");
      getLog().debug(++test + "- " + "short " + allTypes.getShort() + " Ok");
      getLog().debug(++test + "- " + "int " + allTypes.getInt() + " Ok");
      getLog().debug(++test + "- " + "long " + allTypes.getLong() + " Ok");
      getLog().debug(++test + "- " + "float " + allTypes.getFloat() + " Ok");
      getLog().debug(++test + "- " + "double " + allTypes.getDouble() + " Ok");
      getLog().debug("No char test yet, bug in jdk");
      getLog().debug(++test + "- " + "String " + allTypes.getString() + " Ok");
      getLog().debug(++test + "- " + "Date " + allTypes.getDate() + " Ok");
      getLog().debug(++test + "- " + "Timestamp " + allTypes.getTimestamp() + " Ok");

      getLog().debug(++test + "- " + "MyObject ");
      MyObject obj = allTypes.getObject();
      getLog().debug("OK");

      getLog().debug(++test + "- " + "getting handle of stateful...");
      Handle sfHandle = allTypes.getStateful();
      getLog().debug("OK");

      getLog().debug(++test + "- " + "getting the bean back from the handle...");
      StatefulSession sfBean = (StatefulSession) sfHandle.getEJBObject();
      getLog().debug("OK");

      getLog().debug(++test + "- " + "comparing serialized handles...");
      assertTrue(
          Arrays.equals(
              new MarshalledValue(sfHandle).toByteArray(),
              new MarshalledValue(sfBean.getHandle()).toByteArray()));
      getLog().debug("OK");

      getLog().debug(++test + "- " + "calling business method A on stateful: ");
      getLog().debug("OK, result is " + sfBean.callBusinessMethodA());

      getLog().debug(++test + "- " + "adding the stateful bean as an object in AllTypes..");
      allTypes.addObjectToList(sfBean);
      getLog().debug("OK");

      getLog().debug(++test + "- " + "getting handle of stateless...");
      Handle slHandle = allTypes.getStateless();
      getLog().debug("OK");

      getLog().debug(++test + "- " + "getting the bean back from the handle...");
      StatelessSession slBean = (StatelessSession) slHandle.getEJBObject();
      getLog().debug("OK");

      getLog().debug(++test + "- " + "comparing serialized handles...");
      assertTrue(
          Arrays.equals(
              new MarshalledValue(slHandle).toByteArray(),
              new MarshalledValue(slBean.getHandle()).toByteArray()));
      getLog().debug("OK");

      getLog().debug(++test + "- " + "calling business method B on stateless: ");
      getLog().debug("OK, result is " + slBean.callBusinessMethodB());

      getLog().debug(++test + "- " + "adding the stateless bean as an object in AllTypes..");
      allTypes.addObjectToList(slBean);
      getLog().debug("OK");

      getLog().debug(++test + "- " + "getting handle of entity...");
      Handle eeHandle = allTypes.getEntity();
      getLog().debug("OK");

      getLog().debug(++test + "- " + "getting the bean back from the handle...");
      EnterpriseEntity eeBean = (EnterpriseEntity) eeHandle.getEJBObject();
      getLog().debug("OK");

      getLog().debug(++test + "- " + "comparing serialized handles...");
      assertTrue(
          Arrays.equals(
              new MarshalledValue(eeHandle).toByteArray(),
              new MarshalledValue(eeBean.getHandle()).toByteArray()));
      getLog().debug("OK");

      getLog().debug(++test + "- " + "calling business method A on stateless: ");
      getLog().debug("OK, result is" + eeBean.callBusinessMethodA());

      getLog().debug(++test + "- " + "adding the entity bean as an object in AllTypes..");
      allTypes.addObjectToList(eeBean);
      getLog().debug("OK");

      getLog()
          .debug(
              ++test + "- " + "Getting the list of objects back (should contain the 3 beans)...");
      Collection coll = allTypes.getObjectList();
      assertEquals(coll.size(), 3);
      getLog().debug("OK");
      getLog().debug(++test + "- " + "stateful bean ");
      assertTrue(coll.contains(sfBean));
      getLog().debug("OK");
      getLog().debug(++test + "- " + "stateless bean ");
      assertTrue(coll.contains(slBean));
      getLog().debug("OK");
      getLog().debug(++test + "- " + "entity bean ");
      assertTrue(coll.contains(eeBean));
      getLog().debug("OK");

      getLog().debug("Testing automatically generated finders");

      getLog().debug(++test + "- " + "findAll()..");
      coll = allTypesHome.findAll();
      assertTrue(coll.contains(allTypes));
      getLog().debug("OK");

      getLog().debug(++test + "- " + "findByPrimaryKey()...");
      AllTypes result = allTypesHome.findByPrimaryKey("seb");
      assertTrue(result.equals(allTypes));
      getLog().debug("OK");

      getLog().debug(++test + "- " + "findByABoolean()..");
      coll = allTypesHome.findByABoolean(allTypes.getBoolean());
      assertTrue(coll.contains(allTypes));
      getLog().debug("OK");

      getLog().debug(++test + "- " + "findByAByte()..");
      coll = allTypesHome.findByAByte(allTypes.getByte());
      assertTrue(coll.contains(allTypes));
      getLog().debug("OK");

      getLog().debug(++test + "- " + "findByAShort()..");
      coll = allTypesHome.findByAShort(allTypes.getShort());
      assertTrue(coll.contains(allTypes));
      getLog().debug("OK");

      getLog().debug(++test + "- " + "findByAnInt()..");
      coll = allTypesHome.findByAnInt(allTypes.getInt());
      assertTrue(coll.contains(allTypes));
      getLog().debug("OK");

      getLog().debug(++test + "- " + "findByALong()..");
      coll = allTypesHome.findByALong(allTypes.getLong());
      assertTrue(coll.contains(allTypes));
      getLog().debug("OK");

      getLog().debug(++test + "- " + "findByAFloat()..");
      coll = allTypesHome.findByAFloat(allTypes.getFloat());
      assertTrue(coll.contains(allTypes));
      getLog().debug("OK");

      getLog().debug(++test + "- " + "findByADouble()..");
      coll = allTypesHome.findByADouble(allTypes.getDouble());
      assertTrue(coll.contains(allTypes));
      getLog().debug("OK");

      getLog().debug("No Char test yet, bug in jdk");

      getLog().debug(++test + "- " + "findByAString()..");
      coll = allTypesHome.findByAString(allTypes.getString());
      assertTrue(coll.contains(allTypes));
      getLog().debug("OK");

      getLog().debug(++test + "- " + "findByADate()..");
      coll = allTypesHome.findByADate(allTypes.getDate());
      assertTrue(coll.contains(allTypes));
      getLog().debug("OK");

      getLog().debug(++test + "- " + "findByATimestamp()..");
      coll = allTypesHome.findByATimestamp(allTypes.getTimestamp());
      assertTrue(coll.contains(allTypes));
      getLog().debug("OK");

      getLog().debug(++test + "- " + "findByAnObject()..");
      coll = allTypesHome.findByAnObject(allTypes.getObject());
      assertTrue(coll.contains(allTypes));
      getLog().debug("OK");

      getLog().debug(++test + "- " + "findByStatefulSession()..");
      coll =
          allTypesHome.findByStatefulSession(
              (StatefulSession) allTypes.getStateful().getEJBObject());
      getLog().debug("size=" + coll.size());
      for (Iterator i = coll.iterator(); i.hasNext(); ) {
        Object o = i.next();
        getLog().debug("o=" + o);
      }
      assertTrue(coll.contains(allTypes));
      getLog().debug("OK");

      getLog().debug(++test + "- " + "findByStatelessSession()..");
      coll =
          allTypesHome.findByStatelessSession(
              (StatelessSession) allTypes.getStateless().getEJBObject());
      assertTrue(coll.contains(allTypes));
      getLog().debug("OK");

      getLog().debug(++test + "- " + "findByEnterpriseEntity()..");
      coll =
          allTypesHome.findByEnterpriseEntity(
              (EnterpriseEntity) allTypes.getEntity().getEJBObject());
      assertTrue(coll.contains(allTypes));
      getLog().debug("OK");

      getLog().debug("Testing finders defined in jaws.xml");

      getLog().debug(++test + "- " + "findByMinInt()..");
      coll = allTypesHome.findByMinInt(0);
      assertTrue(coll.contains(allTypes));
      getLog().debug("OK");

      getLog().debug(++test + "- " + "findByIntAndDouble()..");
      coll = allTypesHome.findByIntAndDouble(allTypes.getInt(), allTypes.getDouble());
      assertTrue(coll.contains(allTypes));
      getLog().debug("OK");

    } catch (Exception e) {
      getLog().debug("Exception: ", e);
      throw e;
    }
  }
  public void testStatelessBean() throws Exception {

    getLog().debug(++test + "- " + "Trying the context...");

    Context ctx = new InitialContext();
    getLog().debug("OK");

    /// *
    getLog().debug("");
    getLog().debug("Test Stateless Bean");
    getLog().debug("===================");
    getLog().debug("");
    getLog().debug(++test + "- " + "Looking up the home nextgen.StatelessSession...");
    StatelessSessionHome statelessSessionHome =
        (StatelessSessionHome) ctx.lookup("nextgen.StatelessSession");
    if (statelessSessionHome != null) getLog().debug("ok");
    getLog().debug(++test + "- " + "Calling create on StatelessSessionHome...");
    StatelessSession statelessSession = statelessSessionHome.create();
    assertTrue("statelessSessionHome.create() != null", statelessSession != null);
    getLog().debug("ok");

    getLog().debug(++test + "- " + "Calling getEJBHome() on StatelessSession...");
    assertTrue("statelessSession.getEJBHome() != null", statelessSession.getEJBHome() != null);
    getLog().debug("ok");

    getLog().debug(++test + "- " + "Calling Business Method A on StatelessSession... ");
    statelessSession.callBusinessMethodA();
    getLog().debug("ok");
    getLog().debug(++test + "- " + "Calling Business Method B on StatelessSession... ");
    getLog().debug(statelessSession.callBusinessMethodB());
    getLog().debug(++test + "- " + "Calling Business Method B(String) on StatelessSession... ");
    getLog().debug(statelessSession.callBusinessMethodB("of wisdom"));
    getLog().debug(++test + "- " + "Calling Business Method C on StatelessSession... ");
    getLog().debug(statelessSession.callBusinessMethodC());
    getLog().debug(++test + "- " + "Calling Business Method D on StatelessSession... ");
    try {
      statelessSession.callBusinessMethodD();
      fail("callBusinessMethodD, no exception was thrown");
    } catch (BusinessMethodException e) {
      getLog().debug("Caught BusinessMethodException OK");
    }
    getLog()
        .debug(++test + "- " + "Calling Business Method E (getEJBObject) on StatelessSession... ");
    getLog().debug(statelessSession.callBusinessMethodE());

    getLog().debug(++test + "- " + "Calling testClassLoading on StatelessSession... ");
    statelessSession.testClassLoading();
    getLog().debug("OK");

    getLog().debug("***Testing the various local Object class calls");
    getLog().debug(++test + "- " + "toString ... " + statelessSession.toString());

    getLog().debug(++test + "- " + "hashCode ... " + statelessSession.hashCode());

    getLog()
        .debug(
            ++test
                + "- "
                + "equals (same object) ... "
                + statelessSession.equals(statelessSession));

    getLog()
        .debug(
            ++test
                + "- "
                + "equals (another object) (true under same home)... "
                + statelessSession.equals(statelessSessionHome.create()));

    getLog().debug("***Testing the various local EJBObject class calls");

    getLog().debug(++test + "- " + "Get Handle ... ");
    Handle statelessHandle = statelessSession.getHandle();
    assertTrue("statelessHandle != null", statelessHandle != null);
    getLog().debug("OK");
    getLog().debug(++test + "- " + "Serialize handle and deserialize..");
    MarshalledObject mo = new MarshalledObject(statelessHandle);
    Handle handle2 = (Handle) mo.get();
    StatelessSession statelessSession2 = (StatelessSession) handle2.getEJBObject();
    assertTrue("statelessSession2 != null", statelessSession2 != null);
    getLog().debug("OK");
    getLog().debug(++test + "- " + "Calling businessMethodB on it...");
    getLog().debug(statelessSession2.callBusinessMethodB());
    getLog()
        .debug(
            ++test
                + "- "
                + "They should be identical..."
                + statelessSession.isIdentical(statelessSession2));
    getLog().debug("***Testing the various local EJBHome class calls");
    getLog().debug(++test + "- " + "Getting the metaData...");
    EJBMetaData statelessMetaData = statelessSessionHome.getEJBMetaData();
    assertTrue("statelessMetaData != null", statelessMetaData != null);
    getLog().debug("OK");
    getLog()
        .debug(++test + "- " + "Is stateless Session? " + statelessMetaData.isStatelessSession());
    getLog()
        .debug(
            ++test + "- " + "The remote class is " + statelessMetaData.getRemoteInterfaceClass());

    getLog().debug("");
    getLog().debug(++test + "- " + "Calling StatelessSession.remove()...");
    statelessSession.remove();
    getLog().debug("ok");
  }