001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019: package org.apache.openjpa.persistence.exception;
020:
021: import java.sql.SQLException;
022:
023: import javax.persistence.EntityManager;
024: import javax.persistence.OptimisticLockException;
025: import org.apache.openjpa.persistence.test.SingleEMFTestCase;
026:
027: /**
028: * Tests proper JPA exceptions are raised by the implementation.
029: */
030: public class TestException extends SingleEMFTestCase {
031: public void setUp() {
032: super .setUp(PObject.class);
033: }
034:
035: /**
036: * Tests that when Optimistic transaction consistency is violated, the
037: * exception thrown is an instance of javax.persistence.OptimisticException.
038: */
039: public void testThrowsJPADefinedOptimisticException() {
040: EntityManager em1 = emf.createEntityManager();
041: EntityManager em2 = emf.createEntityManager();
042: assertNotEquals(em1, em2);
043:
044: em1.getTransaction().begin();
045: PObject pc = new PObject();
046: em1.persist(pc);
047: em1.getTransaction().commit();
048: Object id = pc.getId();
049: em1.clear();
050:
051: em1.getTransaction().begin();
052: em2.getTransaction().begin();
053:
054: PObject pc1 = em1.find(PObject.class, id);
055: PObject pc2 = em2.find(PObject.class, id);
056:
057: assertTrue(pc1 != pc2);
058:
059: pc1.setName("Modified in TXN1");
060: em1.flush();
061: try {
062: pc2.setName("Modified in TXN2");
063: em2.flush();
064: fail("Expected optimistic exception on flush");
065: } catch (Throwable t) {
066: if (!isExpectedException(t, OptimisticLockException.class)) {
067: print(t);
068: fail(t.getCause().getClass() + " is not "
069: + OptimisticLockException.class);
070: }
071: }
072:
073: em1.getTransaction().commit();
074: try {
075: em2.getTransaction().commit();
076: fail("Expected optimistic exception on commit");
077: } catch (Throwable t) {
078: if (!isExpectedException(t, OptimisticLockException.class)) {
079: print(t);
080: fail(t.getCause().getClass() + " is not "
081: + OptimisticLockException.class);
082: }
083: }
084: }
085:
086: boolean isExpectedException(Throwable t, Class expectedType) {
087: if (t == null)
088: return false;
089: if (expectedType.isAssignableFrom(t.getClass()))
090: return true;
091: if (t.getCause() == t)
092: return false;
093: return isExpectedException(t.getCause(), expectedType);
094: }
095:
096: void print(Throwable t) {
097: print(t, 0);
098: }
099:
100: void print(Throwable t, int tab) {
101: if (t == null)
102: return;
103: for (int i = 0; i < tab * 4; i++)
104: System.out.print(" ");
105: String sqlState = (t instanceof SQLException) ? "(SQLState="
106: + ((SQLException) t).getSQLState() + ":"
107: + t.getMessage() + ")" : "";
108: System.out.println(t.getClass().getName() + sqlState);
109: if (t.getCause() == t)
110: return;
111: print(t.getCause(), tab + 1);
112: }
113: }
|