@Test(expectedExceptions = OptimisticLockException.class) public void testOptimisticLockException() { ComponentSpec cs = createComponentSpec( Long.toString(1), "chris", "cs", "type1", "chris", Collections.<Tag>emptyList(), Collections.<String, String>emptyMap()); em.getTransaction().begin(); em.persist(cs); em.getTransaction().commit(); em.clear(); Assert.assertFalse(em.contains(cs)); em.getTransaction().begin(); ComponentSpec orig = em.find(ComponentSpec.class, Long.toString(1)); orig.setComponentName("revisedName"); em.getTransaction().commit(); em.clear(); ComponentSpec current = em.find(ComponentSpec.class, Long.toString(1)); Assert.assertEquals(current.getObjVersion(), 1); Assert.assertEquals(cs.getObjVersion(), 0); em.getTransaction().begin(); em.merge(cs); em.getTransaction() .commit(); // optimistic lock exception should be thrown as there has been an intervening // commit }
private void checkComponentSpec( ComponentSpec cs, String owner, String type, String creator, String name, Collection<Tag> tags, Map<String, String> views) { Assert.assertEquals(owner, cs.getOwner()); Assert.assertEquals(type, cs.getComponentType()); Assert.assertEquals(creator, cs.getCreatorUserId()); Assert.assertEquals(name, cs.getComponentName()); Assert.assertNotNull(cs.getDateCreated()); Assert.assertNotNull(cs.getLastModified()); Assert.assertEquals(cs.getViewStateCollection().size(), views.size()); for (Entry<String, String> e : views.entrySet()) { Assert.assertTrue(findViewState(cs.getViewStateCollection(), e.getKey(), e.getValue())); } Assert.assertEquals(cs.getTagAssociationCollection().size(), tags.size()); for (Tag t : tags) { Assert.assertTrue(findTags(cs.getTagAssociationCollection(), t)); } }
private ComponentSpec createComponentSpec( String id, String userId, String name, String type, String owner, List<Tag> tags, Map<String, String> views) { ComponentSpec cs = new ComponentSpec(); cs.setComponentId(id); cs.setCreatorUserId(userId); cs.setComponentName(name); cs.setComponentType(type); cs.setOwner(owner); cs.setReferencedComponents(new ArrayList<ComponentSpec>()); List<ViewState> viewStates = new ArrayList<ViewState>(); for (Entry<String, String> e : views.entrySet()) { ViewState vs = new ViewState(); ViewStatePK viewStatePK = new ViewStatePK(); viewStatePK.setComponentId(cs.getComponentId()); viewStatePK.setViewType(e.getKey()); vs.setViewStatePK(viewStatePK); vs.setViewInfo(e.getValue()); viewStates.add(vs); } cs.setViewStateCollection(viewStates); cs.setTagAssociationCollection(new ArrayList<TagAssociation>()); for (Tag aTag : tags) { TagAssociation association = new TagAssociation(); TagAssociationPK tagPK = new TagAssociationPK(); tagPK.setComponentId(cs.getComponentId()); tagPK.setTagId(aTag.getTagId()); association.setTagAssociationPK(tagPK); association.setTagProperty(aTag.getTagProperty()); cs.getTagAssociationCollection().add(association); } return cs; }
@Test public void testComponentSpec() { em.getTransaction().begin(); long time = System.currentTimeMillis(); final Map<String, String> initialViews = Collections.singletonMap("v1", "v1value"); Tag t1 = new Tag(); t1.setTagId("tag1"); Tag t2 = new Tag(); t2.setTagId("tag2"); final List<Tag> tags = Arrays.asList(t1, t2); for (Tag t : tags) { em.persist(t); } ComponentSpec cs = createComponentSpec( Long.toString(time), "chris", "cs", "type1", "chris", tags, initialViews); em.persist(cs); ComponentSpec cs1 = createComponentSpec( Long.toString(time + 1), "chris", "cs1", "type2", "chris", Collections.<Tag>emptyList(), initialViews); em.persist(cs1); cs.getReferencedComponents().add(cs1); em.getTransaction().commit(); em.clear(); cs = em.find(ComponentSpec.class, Long.toString(time)); checkComponentSpec(cs, "chris", "type1", "chris", "cs", tags, initialViews); Assert.assertEquals(cs.getReferencedComponents().size(), 1); Assert.assertEquals( cs.getReferencedComponents().iterator().next().getComponentId(), Long.toString(time + 1)); checkComponentSpec( cs.getReferencedComponents().iterator().next(), "chris", "type2", "chris", "cs1", Collections.<Tag>emptyList(), initialViews); em.getTransaction().begin(); ComponentSpec cs2 = createComponentSpec( Long.toString(time + 2), "chris", "cs2", "type3", "chris", Collections.<Tag>emptyList(), Collections.<String, String>emptyMap()); cs = em.find(ComponentSpec.class, Long.toString(time)); cs.getReferencedComponents().add(0, cs2); em.getTransaction().commit(); em.clear(); // check cascade persist and relationship ordering Assert.assertNotNull(em.find(ComponentSpec.class, Long.toString(time + 2))); cs = em.find(ComponentSpec.class, Long.toString(time)); Assert.assertEquals(cs.getReferencedComponents().size(), 2); Iterator<ComponentSpec> it = cs.getReferencedComponents().iterator(); Assert.assertEquals(it.next().getComponentId(), Long.toString(time + 2)); Assert.assertEquals(it.next().getComponentId(), Long.toString(time + 1)); }