View Javadoc
1   /*
2    * Copyright 2011 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl1.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.krad.data.provider;
17  
18  import org.eclipse.persistence.exceptions.ValidationException;
19  import org.junit.Test;
20  import org.kuali.rice.krad.data.DataObjectService;
21  import org.kuali.rice.krad.data.KradDataServiceLocator;
22  import org.kuali.rice.krad.data.PersistenceOption;
23  import org.kuali.rice.krad.test.KRADTestCase;
24  import org.kuali.rice.krad.test.document.bo.AccountType;
25  import org.kuali.rice.test.data.PerSuiteUnitTestData;
26  import org.kuali.rice.test.data.UnitTestData;
27  import org.kuali.rice.test.data.UnitTestFile;
28  import org.springframework.orm.jpa.JpaSystemException;
29  
30  import javax.persistence.PersistenceException;
31  
32  import static org.junit.Assert.*;
33  
34  /**
35   * A test to make sure we get the "real" cause of a transaction rollback exception.
36   *
37   * @author Kuali Rice Team (rice.collab@kuali.org)
38   */
39  @PerSuiteUnitTestData( {
40          @UnitTestData(
41                  sqlFiles = {
42                          @UnitTestFile(filename = "classpath:testAccountType.sql", delimiter = ";")
43                  })
44  })
45  public class RollbackExceptionErrorReportingTest extends KRADTestCase {
46  
47      private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(RollbackExceptionErrorReportingTest.class);
48  
49      protected DataObjectService getDataObjectService() {
50          return KradDataServiceLocator.getDataObjectService();
51      }
52  
53      /**
54       * Tests that if you try to change a primary key, a {@link org.springframework.orm.jpa.JpaSystemException} is thrown.
55       */
56      @Test
57      public void changePrimaryKeyValue_Exception() {
58          AccountType acctType = getDataObjectService().find(AccountType.class, "CAT");
59          assertNotNull( "Error retrieving CAT account type from data object service", acctType );
60  
61          acctType.setAccountTypeCode("CLR");
62  
63          // test what object getter does
64          try {
65              getDataObjectService().save(acctType, PersistenceOption.FLUSH);
66              fail( "The save method should have failed." );
67          } catch (JpaSystemException ex) {
68              // JpaSystemException should have been thrown since we are illegally trying to change the
69              // primary key on an attached JPA entity
70              LOG.info(ex, ex);
71              assertNotNull("The thrown rollback exception should have had a cause", ex.getCause());
72              assertTrue("Embedded error should have been a javax.persistence.PersistenceException.  But was: " + ex.getCause(), ex.getCause() instanceof PersistenceException);
73              assertNotNull("The embedded rollback exception should have had a cause", ex.getCause().getCause());
74              assertTrue("The embedded rollback exception should have been a validation exception, but was: " + ex.getCause().getCause(), ex.getCause().getCause() instanceof ValidationException);
75          } catch (Exception ex) {
76              fail("It should have failed with JpaSystemException");
77          }
78      }
79  
80  }