[jboss-cvs] jboss-seam/src/test/integration/src/org/jboss/seam/test/integration ...
Norman Richards
norman.richards at jboss.com
Wed Oct 10 19:08:47 EDT 2007
User: nrichards
Date: 07/10/10 19:08:47
Modified: src/test/integration/src/org/jboss/seam/test/integration
testng.xml
Added: src/test/integration/src/org/jboss/seam/test/integration
EntityExceptionObserver.java EntityTest.java
Thing.java
Log:
original entity tests
Revision Changes Path
1.2 +2 -1 jboss-seam/src/test/integration/src/org/jboss/seam/test/integration/testng.xml
(In the diff below, changes in quantity of whitespace are not shown.)
Index: testng.xml
===================================================================
RCS file: /cvsroot/jboss/jboss-seam/src/test/integration/src/org/jboss/seam/test/integration/testng.xml,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -b -r1.1 -r1.2
--- testng.xml 8 Oct 2007 18:15:48 -0000 1.1
+++ testng.xml 10 Oct 2007 23:08:47 -0000 1.2
@@ -4,6 +4,7 @@
<test name="Integration">
<classes>
+ <class name="org.jboss.seam.test.integration.EntityTest"/>
<class name="org.jboss.seam.test.integration.PageContextTest"/>
<class name="org.jboss.seam.test.integration.MiscTests"/>
</classes>
1.1 date: 2007/10/10 23:08:47; author: nrichards; state: Exp;jboss-seam/src/test/integration/src/org/jboss/seam/test/integration/EntityExceptionObserver.java
Index: EntityExceptionObserver.java
===================================================================
package org.jboss.seam.test.integration;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Observer;
@Name("entityExceptionObserver")
public class EntityExceptionObserver {
private boolean exceptionSeen;
@Observer("org.jboss.seam.exceptionHandled.javax.persistence.OptimisticLockException")
public void handleException(Exception e) {
exceptionSeen=true;
}
public boolean getOptimisticLockExceptionSeen() {
return exceptionSeen;
}
}
1.1 date: 2007/10/10 23:08:47; author: nrichards; state: Exp;jboss-seam/src/test/integration/src/org/jboss/seam/test/integration/EntityTest.java
Index: EntityTest.java
===================================================================
package org.jboss.seam.test.integration;
import org.jboss.seam.mock.SeamTest;
import java.util.HashMap;
import java.util.Map;
import javax.persistence.EntityManager;
import org.hibernate.StaleStateException;
import org.jboss.seam.contexts.Contexts;
import org.jboss.seam.core.Manager;
import org.testng.Assert;
import org.testng.annotations.Test;
public class EntityTest
extends SeamTest
{
@Test
public void entityUpdatedInNestedConversation() throws Exception {
String parentConversation = new FacesRequest("/page.xhtml") {
@Override
protected void invokeApplication() throws Exception {
Thing thing = new Thing();
thing.setName("thing");
EntityManager entityManager = (EntityManager) getValue("#{entityManager}");
entityManager.persist(thing);
Contexts.getConversationContext().set("thing", thing);
Manager.instance().beginConversation();
}
}.run();
new FacesRequest("/page.xhtml", parentConversation) {
}.run();
// nested conversation
String nestedId = new FacesRequest("/page.xhtml", parentConversation) {
@Override
protected void invokeApplication() throws Exception {
Manager.instance().beginNestedConversation();
}
}.run();
// update entity in nested conversation
new FacesRequest("/page.xhtml", nestedId) {
@Override
protected void invokeApplication() throws Exception {
Thing thing = (Thing) Contexts.getConversationContext().get("thing");
thing.setName("foo");
EntityManager entityManager = (EntityManager) getValue("#{entityManager}");
entityManager.flush();
}
}.run();
// end nested conversation
assert new FacesRequest("/page.xhtml", nestedId) {
@Override
protected void invokeApplication() throws Exception {
Manager.instance().endConversation(false);
}
}.run().equals(parentConversation);
// This tests that the activation in the parent conversation
// doesn't fail
new FacesRequest("/page.xhtml",parentConversation) {
@Override
protected void renderResponse() throws Exception {
Thing thing = (Thing) Contexts.getConversationContext().get("thing");
assert thing.getName().equals("foo");
}
}.run();
}
@Test
public void testStale() throws Exception {
final Map<String, Long> holder = new HashMap<String, Long>();
final String conversation1 = new FacesRequest("/page.xhtml") {
@Override
protected void invokeApplication() throws Exception {
Thing thing = new Thing();
thing.setName("thing");
EntityManager entityManager = (EntityManager) getValue("#{entityManager}");
entityManager.persist(thing);
holder.put("id", thing.getId());
Contexts.getConversationContext().set("thing", thing);
Manager.instance().beginConversation();
}
}.run();
new FacesRequest("/page.xhtml", conversation1) {
}.run();
// update in second conversation
new FacesRequest("/page.xhtml") {
@Override
@SuppressWarnings("cast")
protected void invokeApplication() throws Exception {
EntityManager entityManager = (EntityManager) getValue("#{entityManager}");
Thing thing = (Thing) entityManager.find(Thing.class, holder.get("id"));
thing.setName("foo");
entityManager.flush();
}
}.run();
try {
new FacesRequest("/page.xhtml", conversation1) {
EntityExceptionObserver observer;
@Override
protected void invokeApplication() throws Exception {
Thing thing = (Thing) Contexts.getConversationContext().get("thing");
thing.setName("bar");
observer = (EntityExceptionObserver) getValue("#{entityExceptionObserver}");
}
@Override
protected void renderResponse() throws Exception {
Assert.fail("page rendered without redirect, expected StaleStateException!");
}
@Override
protected void afterRequest() {
assert getValue("#{entityExceptionObserver.optimisticLockExceptionSeen}").equals(Boolean.TRUE);
}
}.run();
} catch (StaleStateException e) {
}
}
}
1.1 date: 2007/10/10 23:08:47; author: nrichards; state: Exp;jboss-seam/src/test/integration/src/org/jboss/seam/test/integration/Thing.java
Index: Thing.java
===================================================================
package org.jboss.seam.test.integration;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Version;
@Entity
public class Thing {
public Thing() {}
@GeneratedValue
@Id
private Long id;
@Version
private Integer version;
private String name;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getVersion() {
return version;
}
public void setVersion(Integer version) {
this.version = version;
}
@Override
public String toString() {
return "Thing#" + id + "[v=" + version + "]";
}
}
More information about the jboss-cvs-commits
mailing list