openjpa-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ppod...@apache.org
Subject svn commit: r603032 - in /openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/exception: ./ PObject.java TestException.java
Date Mon, 10 Dec 2007 20:16:15 GMT
Author: ppoddar
Date: Mon Dec 10 12:16:11 2007
New Revision: 603032

URL: http://svn.apache.org/viewvc?rev=603032&view=rev
Log:
OPENJPA-462: Adding a Test case to verify correct exception type

Added:
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/exception/
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/exception/PObject.java
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/exception/TestException.java

Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/exception/PObject.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/exception/PObject.java?rev=603032&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/exception/PObject.java
(added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/exception/PObject.java
Mon Dec 10 12:16:11 2007
@@ -0,0 +1,57 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.    
+ */
+package org.apache.openjpa.persistence.exception;
+
+import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.Id;
+import javax.persistence.Version;
+
+/**
+ * A Simple entity for testing. Has a version field for testing optimistic
+ * concurrent usage.
+ * 
+ * @author Pinaki Poddar
+ *
+ */
+@Entity
+public class PObject {
+	@Id
+	@GeneratedValue
+	private long id;
+	private String name;
+	@Version
+	private int version;
+	
+	public String getName() {
+		return name;
+	}
+	
+	public void setName(String name) {
+		this.name = name;
+	}
+	
+	public long getId() {
+		return id;
+	}
+	
+	public int getVersion() {
+		return version;
+	}
+}

Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/exception/TestException.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/exception/TestException.java?rev=603032&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/exception/TestException.java
(added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/exception/TestException.java
Mon Dec 10 12:16:11 2007
@@ -0,0 +1,107 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.    
+ */
+package org.apache.openjpa.persistence.exception;
+
+import java.sql.SQLException;
+
+import javax.persistence.EntityManager;
+import javax.persistence.OptimisticLockException;
+import org.apache.openjpa.persistence.test.SingleEMFTestCase;
+
+/**
+ * Tests proper JPA exceptions are raised by the implementation. 
+ */
+public class TestException extends SingleEMFTestCase {
+    public void setUp() {
+        super.setUp(PObject.class);
+    }
+    
+	/**
+	 * Tests that when Optimistic transaction consistency is violated, the
+	 * exception thrown is an instance of javax.persistence.OptimisticException.
+	 */
+	public void testThrowsJPADefinedOptimisticException() {
+		EntityManager em1 = emf.createEntityManager();
+		EntityManager em2 = emf.createEntityManager();
+		assertNotEquals(em1, em2);
+		
+		em1.getTransaction().begin();
+		PObject pc = new PObject();
+		em1.persist(pc);
+		em1.getTransaction().commit();
+		Object id = pc.getId();
+		em1.clear();
+		
+		em1.getTransaction().begin();
+		em2.getTransaction().begin();
+		
+		PObject pc1 = em1.find(PObject.class, id);
+		PObject pc2 = em2.find(PObject.class, id);
+		
+		assertTrue(pc1 != pc2);
+		
+		pc1.setName("Modified in TXN1");
+		em1.flush();
+		try {
+			pc2.setName("Modified in TXN2");
+			em2.flush();
+			fail("Expected optimistic exception on flush");
+		} catch (Throwable t) {
+			if (!isExpectedException(t, OptimisticLockException.class)) {
+				print(t);
+				fail(t.getCause().getClass() + " is not " + 
+						OptimisticLockException.class);
+			}
+		}
+		
+		em1.getTransaction().commit();
+		try {
+			em2.getTransaction().commit();
+			fail("Expected optimistic exception on commit");
+		} catch (Throwable t) {
+			if (!isExpectedException(t, OptimisticLockException.class)) {
+				print(t);
+				fail(t.getCause().getClass() + " is not " + 
+						OptimisticLockException.class);
+			}
+		}
+	}
+	
+	boolean isExpectedException(Throwable t, Class expectedType) {
+		if (t == null) return false;
+		if (expectedType.isAssignableFrom(t.getClass()))
+				return true;
+		if (t.getCause()==t) return false;
+		return isExpectedException(t.getCause(), expectedType);
+	}
+	
+	void print(Throwable t) {
+		print(t, 0);
+	}
+	
+	void print(Throwable t, int tab) {
+		if (t == null) return;
+		for (int i=0; i<tab*4;i++) System.out.print(" ");
+		String sqlState = (t instanceof SQLException) ? 
+			"(SQLState=" + ((SQLException)t).getSQLState() + ":" + t.getMessage() + ")":"";
+		System.out.println(t.getClass().getName() + sqlState);
+		if (t.getCause()==t) return;
+		print(t.getCause(), tab+1);
+	}
+}



Mime
View raw message