Package com.force.sdk.jpa.entities

Examples of com.force.sdk.jpa.entities.TestEntity


     * can call persist/remove/merge/refresh without a transaction.
     * @hierarchy javasdk
     * @userStory xyz
     */           
    public void testExtendedBehavior() {
        TestEntity t = new TestEntity();
        JPATestUtils.initializeTestEntity(t);
        //new/transient
        ParentTestEntity p = JPATestUtils.setMasterDetailRelationship(t);
        t.setParent(p);
        Assert.assertFalse(emExt.contains(t), "Entity is not transient.");
        emExt.persist(p);
        emExt.persist(t);
//        Assert.assertNull(t.getId(), "Entity was already committed.");//fails
        EntityTransaction tx = emExt.getTransaction();
        tx.begin();
        //managed       
        tx.commit();
        Assert.assertTrue((emExt.contains(t) && emExt.contains(p)), "Entities are not managed.");
        Assert.assertNotNull(t.getId(), "ID was not generated.");
        Assert.assertNotNull(emExt.find(TestEntity.class, t.getId()), "The entity was not stored to the database.");
        String firstTE = t.getId();
        //now remove first TE and replace it with another TE
        emExt.remove(t);
        //another EM should still see the entity
//        Assert.assertNotNull(em.find(TestEntity.class, firstTE), "The entity is already deleted.");//fails
        TestEntity second = new TestEntity();
        JPATestUtils.initializeTestEntity(second);
        second.setParentMasterDetail(p);
        second.setParent(p);
        emExt.persist(second);
        Assert.assertTrue(emExt.contains(second), "Second entity is managed.");
//        Assert.assertNull(second.getId(), "Second was already committed.");//fails
        tx = emExt.getTransaction();
        tx.begin();
        Assert.assertTrue((emExt.contains(second) && emExt.contains(p)), "Entities are not managed.");
//        Assert.assertFalse(emExt.contains(t), "Removed entity is still managed.");//fails
        tx.commit();
        Assert.assertFalse(firstTE.equals(second.getId()), "The first entity was not replaced by a new.");
        //now make changes on TE, then refresh (reverts changes) and merge outside of a tx
        String origName = second.getName();
        second.setName("testExtendedBehavior");
        emExt.refresh(second);
        Assert.assertEquals(second.getName(), origName, "Refresh did not reset the name.");
        second.setName("newname");
//        emExt.merge(second);//should make the new name persistent upon next commit //FAILS due to tx required - this is very odd
        Assert.assertEquals(em.find(TestEntity.class, second.getId()).getName(), origName,
                "Merge instantly committed the changes."); //should still see the old name
        tx = emExt.getTransaction();
        tx.begin();
        tx.rollback();
        Assert.assertEquals(emExt.find(TestEntity.class, second.getId()).getName(), origName, "Name update was not rolled back.");
    }
View Full Code Here


     * Tests the correct lifecycle behavior of transactions in an application managed extended persistence context.
     * @hierarchy javasdk
     * @userStory xyz
     */           
    public void testTxBehavior() {
        TestEntity t = new TestEntity();
        JPATestUtils.initializeTestEntity(t);
        //new/transient
        ParentTestEntity p = JPATestUtils.setMasterDetailRelationship(t);
        t.setParent(p);
        Assert.assertFalse(em.contains(t), "Entity is not transient.");
        EntityTransaction tx = em.getTransaction();
        if (tx.isActive())tx.commit();
        try {
            em.persist(p);
            //TODO
//            Assert.fail("Persist should require a transaction.");//fails
        } catch (IllegalStateException ise) {
            StringWriter sw = new StringWriter();
            PrintWriter pw = new PrintWriter(sw);
            ise.printStackTrace(pw);
            Assert.assertTrue(ise.getMessage()
                    .contains("Transaction is not active. You need to define a transaction around this"),
                    "Caught wrong exception:\n" + sw.toString());
        }
        tx = em.getTransaction();
        tx.begin();
//        em.persist(p);//fails since persist above is successful
        em.persist(t);
        tx.commit();
        t.setName("refresh");
        try {
            em.refresh(t);
            Assert.fail("Refresh should require a transaction.");
        } catch (Exception e) {
            //TODO: this currently throws java.lang.IllegalArgumentException:
            // Entity "com.force.sdk.jpa.entities.TestEntity@4631f1f8" isnt managed and so you cant use this method
            // We need to change that
//            Assert.assertTrue(e.getMessage()
//                    .contains("Transaction is not active. You need to define a transaction around this"),
//                    "Caught wrong exception:\n" + e.getStackTrace());           
        }
        t.setName("testTxBehavior");
        try {
            em.merge(t);
            //TODO
//            Assert.fail("Merge should require a transaction.");//fails
        } catch (Exception e) {
View Full Code Here

     * @hierarchy javasdk
     * @userStory xyz
     */           
    public void testFetchtypeEager(EntityManager e, boolean isExtended) {
        String eId = prepareTest(e, isExtended);
        TestEntity entity = e.find(TestEntity.class, eId);
        String errMsg = "Tx scoped: ";
        if (isExtended) {
            errMsg = "Extended: ";
        }
        Assert.assertTrue(e.contains(entity), errMsg + " Entity is not managed.");
        Assert.assertTrue(e.contains(entity.getParent()), errMsg + " Parent is not managed.");
        ParentTestEntity parent = entity.getParent();
        String pid = parent.getId();
        EntityTransaction tx = e.getTransaction();
        tx.begin();
        tx.commit();
        if (isExtended) {
View Full Code Here

        String eId = prepareTest(e, isExtended);
        String errMsg = "Tx scoped: ";
        if (isExtended) {
            errMsg = "Extended: ";
        }
        TestEntity entity = e.find(TestEntity.class, eId);
        //managed
        Assert.assertTrue(e.contains(entity), errMsg + " Entity is not managed.");
        EntityTransaction tx = e.getTransaction();
        tx.begin();
        tx.commit();
View Full Code Here

            errMsg = "Extended: ";
        }
        //now refresh and create an entity for removal
        EntityTransaction tx = e.getTransaction();
        tx.begin();
        TestEntity removed = new TestEntity();
        //new
        JPATestUtils.initializeTestEntity(removed);
        ParentTestEntity parentRemoved = JPATestUtils.setMasterDetailRelationship(removed);
        e.persist(parentRemoved);
        e.persist(removed);
        TestEntity managed = e.find(TestEntity.class, eId);
        //managed
        Assert.assertTrue(e.contains(managed) && e.contains(removed) && e.contains(parentRemoved),
                errMsg + " Not all entities are in managed state.");
        e.remove(removed);
        e.remove(managed);
View Full Code Here

    /**
     * Helper that creates test entities in tx scoped persistence context and verifies correct life cycle.
     */
    private String prepareTest(EntityManager e, boolean isExtended) {
        TestEntity t = new TestEntity();
        JPATestUtils.initializeTestEntity(t);
        //new/transient
        ParentTestEntity p = JPATestUtils.setMasterDetailRelationship(t);
        t.setParent(p);
        EntityTransaction tx = e.getTransaction();
        String errMsg = "Tx scoped: ";
        if (isExtended) {
            errMsg = "Extended: ";
        }
        Assert.assertFalse(e.contains(t), errMsg + "Entity is not transient.");
        tx.begin();
        e.persist(p);
        e.persist(t);
        //managed
        Assert.assertTrue((e.contains(t) && e.contains(p)), errMsg + " Entity is not managed.");
        tx.commit();
        Assert.assertNotNull(t.getId(), errMsg + " ID was not generated.");
        Assert.assertNotNull(e.find(TestEntity.class, t.getId()), errMsg + " The entity was not stored to the database.");
        if (isExtended) {
            //in extended pers. ctx the entity stays managed after commit
            Assert.assertTrue(e.contains(t), errMsg + " Entity is not managed but should be for extended pers. ctx.");
        } else {
            Assert.assertFalse(e.contains(t), errMsg + " Entity is not detached.");
        }
        return t.getId();
    }
View Full Code Here

    }
   
    @DataProvider
    public Object[][] updateData() throws NumberFormatException, MalformedURLException {
        Object [][] entities = new Object[][]{
                {new TestEntity(), "shortType",     (short) 0, (short) 77},
                {new TestEntity(), "intType",       0, 77},
                {new TestEntity(), "longType",      (long) 0, (long) 77},
                {new TestEntity(), "doubleType",    (double) 0, (double) 77},
                {new TestEntity(), "floatType",     (float) 0, (float) 77},
                {new TestEntity(), "charType",    'A', '7'},
               
                {new TestEntity(), "shortObject",       Short.valueOf((short) 0), Short.valueOf((short) 77)},
                {new TestEntity(), "integerObject",     Integer.valueOf(0), Integer.valueOf(77)},
                {new TestEntity(), "longObject",        Long.valueOf(0), Long.valueOf(77)},
                {new TestEntity(), "doubleObject",      new Double(0), new Double(77)},
                {new TestEntity(), "floatObject",       new Float(0), new Float(77)},
                {new TestEntity(), "characterObject",   'A', '7'},
                {new TestEntity(), "bigDecimalObject"new BigDecimal(0), new BigDecimal(77)},
                {new TestEntity(), "bigIntegerObject",  BigInteger.valueOf(0), BigInteger.valueOf(77)},
                {new TestEntity(), "percent",           0, (77)},
                {new TestEntity(), "stringObject",      "0", "77"},
                {new TestEntity(), "phone",             "415-123-0000", "415-123-0007"},
                {new TestEntity(), "email",             "0foobar@salesforce.com", "77foobar@salesforce.com"},
                {new TestEntity(), "url",               new URL("http://localhost:0000"), new URL("http://localhost:7000")},
                {new TestEntity(), "characterObject",   'A', '7'},
                {new TestEntity(), "booleanObject",     Boolean.TRUE, Boolean.FALSE},
               
                {new ParentTestEntity(), "name",        "parent entity", "updated name"},
        };
        return entities;
    }
View Full Code Here

    @BeforeMethod
    public void initTestData() {
        deleteAll(TestEntity.class);
        deleteAll(ParentTestEntity.class);
       
        TestEntity entity1 = new TestEntity();
        JPATestUtils.initializeTestEntity(entity1);
        entity1.setName("entity1");
        entity1.setBoolType(true);
        entity1.setIntType(1);
        TestEntity entity2 = new TestEntity();
        JPATestUtils.initializeTestEntity(entity2);
        entity2.setName("entity2");
        entity2.setBoolType(false);
        entity2.setIntType(2);
        TestEntity entity3 = new TestEntity();
        JPATestUtils.initializeTestEntity(entity3);
        entity3.setName("entityXX");
        entity3.setBoolType(false);
        entity3.setIntType(3);
        ParentTestEntity parent1 = new ParentTestEntity();
        parent1.setName("Parent1");
        entity1.setParent(parent1);
        entity1.setParentMasterDetail(parent1);
        ParentTestEntity parent2 = new ParentTestEntity();
        parent2.setName("Parent2");
        entity2.setParent(parent2);
        entity2.setParentMasterDetail(parent2);
        ParentTestEntity parent3 = new ParentTestEntity();
        parent3.setName("Parent3");
        entity3.setParent(parent3);
        entity3.setParentMasterDetail(parent3);
       
        addTestDataInTx(Lists.newArrayList(parent1, entity1, parent2, entity2, parent3, entity3));
       
        // These object are needed for Map based related objects
        deleteAll(CascadeMapChildTestEntity.class);
View Full Code Here

    @Test
    public void testEagerSubQueries() {
        deleteAll(ParentTestEntity.class);
        deleteAll(TestEntity.class);

        TestEntity entity1 = new TestEntity();
        JPATestUtils.initializeTestEntity(entity1);
        entity1.setName("entity1");
        entity1.setBoolType(true);
        TestEntity entity2 = new TestEntity();
        JPATestUtils.initializeTestEntity(entity2);
        entity2.setName("entity2");
        entity2.setBoolType(false);
        ParentTestEntity parent = new ParentTestEntity();
        parent.init();
        entity1.setParent(parent);
        entity1.setParentMasterDetail(parent);
        entity2.setParent(parent);
        entity2.setParentMasterDetail(parent);

        addTestDataInTx(Lists.newArrayList(parent, entity1, entity2));

        String nativeQuery = "select id, name, "
                                 + "(select id, " + getFieldName(em, TestEntity.class, "boolType")
View Full Code Here

            em.persist(parent);
            parentMD = new ParentTestEntity();
            parentMD.init();
            em.persist(parentMD);
            for (Digit d : Digit.values()) {
                TestEntity entity = new TestEntity();
                JPATestUtils.initializeTestEntity(entity, d);
                entity.setParent(parent);
                entity.setParentMasterDetail(parentMD);
                em.persist(entity);
            }
            em.flush();
            tx.commit();
            tx = null;
View Full Code Here

TOP

Related Classes of com.force.sdk.jpa.entities.TestEntity

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.