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 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("");
  }