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 testEntityBeanCMP() throws Exception {
    Context ctx = new InitialContext();

    getLog().debug("testEntityBeanCMP");
    getLog().debug(++test + "- " + "Looking up the home nextgen.EnterpriseEntity...ok");

    EnterpriseEntityHome enterpriseEntityHome =
        (EnterpriseEntityHome) ctx.lookup("nextgen.EnterpriseEntity");
    getLog().debug(++test + "- " + "Calling find on EnterpriseEntityHome with name Marc...");
    EnterpriseEntity enterpriseEntity = null;
    try {
      enterpriseEntity = enterpriseEntityHome.findByPrimaryKey("Marc");
    } catch (Exception e) {
      getLog().debug("findByPrimaryKey(Marc) failed", e);
    }
    if (enterpriseEntity == null) {
      getLog().debug("not found OK");
      getLog().debug(++test + "- " + "Calling create on EnterpriseEntityHome with name Marc...");
      enterpriseEntity = enterpriseEntityHome.create("Marc");
    }

    if (enterpriseEntity != null)
      getLog()
          .debug(
              "ok, enterpriseEntity"
                  + enterpriseEntity
                  + ", hashCode="
                  + enterpriseEntity.hashCode());

    getLog().debug(++test + "- " + "Calling for duplicate create and DuplicateKeyException...");
    try {
      Object e = enterpriseEntityHome.create("Marc");
      getLog().debug("I Really should not make it here, e=" + e + ", hashCode=" + e.hashCode());
      throw new Exception("DuplicateKey not seen");
    } catch (DuplicateKeyException dke) {
      getLog().debug("DuplicateKeyException ok");
    }

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

    getLog().debug(++test + "- " + "Getting a new reference with findByPK...");
    EnterpriseEntity enterpriseEntity2 = null;
    try {
      enterpriseEntity2 = enterpriseEntityHome.findByPrimaryKey("Marc");
    } catch (Exception re) {
      getLog().debug("Exception: ", re);
    }
    assertTrue("enterpriseEntity2 != null", enterpriseEntity2 != null);
    getLog().debug("ok");
    getLog().debug(++test + "- " + "Calling Business Method A on enterpriseEntity... ");
    getLog().debug(enterpriseEntity.callBusinessMethodA());

    getLog()
        .debug(
            ++test
                + "- "
                + "Calling Business Method A (again to ejbLoad if TypeC) on enterpriseEntity... ");
    getLog().debug(enterpriseEntity.callBusinessMethodA());

    getLog()
        .debug(
            ++test
                + "- "
                + "Calling Business Method B (EJBObject from entity) on enterpriseEntity...");
    getLog().debug(enterpriseEntity.callBusinessMethodB());

    getLog().debug(++test + "- " + "Calling Business Method B(String) on EnterpriseEntity... ");
    getLog().debug(enterpriseEntity.callBusinessMethodB("of wisdom"));

    getLog().debug(++test + "- " + "Calling getOtherField (non pk) on enterpriseEntity...");
    getLog().debug("value: " + enterpriseEntity.getOtherField());

    getLog().debug(++test + "- " + "Calling setOtherField(4) on enterpriseEntity...");
    enterpriseEntity.setOtherField(4);
    getLog().debug("OK");

    getLog().debug(++test + "- " + "Calling getOtherField() on enterpriseEntity (should be 4)...");
    int value = enterpriseEntity.getOtherField();
    assertTrue("enterpriseEntity.getOtherField() == 4", value == 4);
    getLog().debug("value is " + value + ", OK");

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

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

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

    getLog()
        .debug(
            ++test
                + "- "
                + "equals (another object) (true for this case)... "
                + enterpriseEntity.equals(enterpriseEntity2));

    getLog().debug("***Testing the various local EJBObject class calls");
    getLog().debug(++test + "- " + "Get Primary Key ... " + enterpriseEntity.getPrimaryKey());

    getLog().debug(++test + "- " + "Get Handle ... ");
    Handle entityHandle = enterpriseEntity.getHandle();
    assertTrue("entityHandle != null", entityHandle != null);
    getLog().debug("OK");
    getLog().debug(++test + "- " + "Serialize handle and deserialize....");
    MarshalledObject mo3 = new MarshalledObject(entityHandle);
    Handle entityHandle3 = (Handle) mo3.get();
    EnterpriseEntity enterpriseEntity3 = (EnterpriseEntity) entityHandle3.getEJBObject();
    if (enterpriseEntity3 != null) getLog().debug("OK");
    getLog().debug(++test + "- " + "Calling businessMethodA on it...");
    getLog().debug(enterpriseEntity3.callBusinessMethodB());
    getLog()
        .debug(
            ++test
                + "- "
                + "They should be identical..."
                + enterpriseEntity.isIdentical(enterpriseEntity3));
    getLog().debug(++test + "- " + "Calling entityHome.remove(Handle)...");
    enterpriseEntityHome.remove(enterpriseEntity3.getHandle());
    getLog().debug("OK");

    getLog().debug(++test + "- " + "Calling enterpriseEntity.remove() (should fail)...");
    try {
      enterpriseEntity.remove();
      fail("enterpriseEntity.remove() did not fail");
    } catch (Exception e) {
      getLog().debug("OK");
    }

    getLog().debug(++test + "- " + "Calling EnterpriseEntity.create() for marc6...");
    EnterpriseEntity marc6 = enterpriseEntityHome.create("marc6");
    getLog().debug("ok");

    getLog().debug(++test + "- " + "Calling method createEntity on enterpriseEntity... ");
    EnterpriseEntity marc2 = marc6.createEntity("marc2");
    getLog().debug("OK");

    getLog().debug(++test + "- " + "removing by PK on home (marc2)...");
    enterpriseEntityHome.remove(marc2.getPrimaryKey());
    getLog().debug("ok");

    getLog().debug(++test + "- " + "Calling enterpriseEntity.remove()  (marc6)...");
    marc6.remove();
    getLog().debug("ok");

    getLog().debug(++test + "- " + "Calling EnterpriseEntity.create<METHOD>() for marc7...");
    EnterpriseEntity marc7 = enterpriseEntityHome.createMETHOD("marc7");
    getLog().debug("ok");

    getLog().debug(++test + "- " + "Calling enterpriseEntity.remove()  (marc7)...");
    marc7.remove();
    getLog().debug("ok");

    getLog().debug("");
    getLog().debug("");
    getLog().debug("");
  }