View Javadoc
1   /*
2    * Copyright 2005-2014 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/ecl2.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  
17  package org.kuali.rice.krad.data.jpa;
18  
19  import org.eclipse.persistence.exceptions.DatabaseException;
20  import org.junit.Test;
21  import org.kuali.rice.krad.bo.PersistableBusinessObjectBase;
22  import org.kuali.rice.krad.service.KRADServiceLocator;
23  import org.kuali.rice.krad.test.KRADTestCase;
24  import org.kuali.rice.test.BaselineTestCase;
25  
26  import javax.persistence.Column;
27  import javax.persistence.Entity;
28  import javax.persistence.Id;
29  import javax.persistence.Table;
30  
31  import static org.junit.Assert.*;
32  
33  /**
34   * Tests the DisableVersion annotation.
35   *
36   * @author Kuali Rice Team (rice.collab@kuali.org)
37   */
38  @BaselineTestCase.BaselineMode(BaselineTestCase.Mode.CLEAR_DB) // no rollback so we can avoid transactional cache mucking up our results
39  public class DisableVersioningTest extends KRADTestCase {
40  
41      private DisableVersion createDisableVersion(String rndId, String propertyVal) {
42          return new DisableVersion(rndId, propertyVal, new Long(0));
43      }
44  
45      private DisableNoVersion createDisableNoVersion(String rndId, String propertyVal) {
46          return new DisableNoVersion(rndId, propertyVal);
47      }
48  
49      private DisableNoVersionRemoveMapping createDisableNoVersionRemoveMapping(String rndId, String propertyVal) {
50          return new DisableNoVersionRemoveMapping(rndId, propertyVal);
51      }
52  
53      private String getRandomId() {
54          String time = String.valueOf(System.currentTimeMillis());
55          return (time.substring(time.length() - 9));
56      }
57  
58      @Test
59      public void testDisableVersioning() {
60          // get a random value for our id
61          String rndId = getRandomId();
62          String property = "testPropertyValue";
63  
64          // persist to the datasource
65          KRADServiceLocator.getDataObjectService().save(createDisableVersion(rndId, property));
66          // retrieve the object back from the datasource
67          DisableVersion resultDV = KRADServiceLocator.getDataObjectService().find(DisableVersion.class, rndId);
68          // validate
69          assertNotNull("DisableVersion is null", resultDV);
70          assertEquals("DisableVersion id does not match given value", rndId, resultDV.getId());
71          assertEquals("DisableVersion property does not match given value", property, resultDV.getProperty());
72          assertEquals(new Long(0), resultDV.getVersionNumber());
73          // now set the version number to a value and make sure it persists properly
74          resultDV.setVersionNumber(new Long(50));
75          resultDV = KRADServiceLocator.getDataObjectService().save(resultDV);
76          assertEquals(new Long(50), resultDV.getVersionNumber());
77  
78          // now, since DisableNoVersion has no version number column, it should throw an exception when we attempt to
79          // persist it since there is no ver_nbr column in the database
80          try {
81              KRADServiceLocator.getDataObjectService().save(createDisableNoVersion(rndId, property));
82              fail("Database exception should have been thrown when saving with no version number column");
83          } catch (DatabaseException e) {}
84  
85          // DisableVersionRemoveMapping *should* work though because we are removing the VER_NBR column mapping which
86          // should help because we have no such column in the database
87          KRADServiceLocator.getDataObjectService().save(createDisableNoVersionRemoveMapping(rndId, property));
88          // retrieve the object back from the datasource
89          DisableNoVersionRemoveMapping resultDNV = KRADServiceLocator.getDataObjectService().find(DisableNoVersionRemoveMapping.class, rndId);
90          // validate
91          assertNotNull("DisableNoVersionRemoveMapping is null", resultDNV);
92          assertEquals("DisableNoVersionRemoveMapping id does not match given value", rndId, resultDNV.getId());
93          assertEquals("DisableNoVersionRemoveMapping property does not match given value", property, resultDNV.getProperty());
94  
95      }
96  
97      @Entity
98      @Table(name="KRTST_TEST_DISABLE_VER_T")
99      @DisableVersioning
100     public static class DisableVersion extends PersistableBusinessObjectBase {
101 
102         @Id
103         @Column(name="ID")
104         private String id;
105 
106         @Column(name="STR_PROP")
107         private String property;
108 
109         public DisableVersion() { }
110 
111         public DisableVersion(String id, String property, Long versionNumber) {
112             this.id = id;
113             this.property = property;
114             setVersionNumber(versionNumber);
115         }
116 
117         public String getId() {
118             return id;
119         }
120 
121         public void setId(String id) {
122             this.id = id;
123         }
124 
125         public String getProperty() {
126             return property;
127         }
128 
129         public void setProperty(String property) {
130             this.property = property;
131         }
132     }
133 
134     @Entity
135     @Table(name="KRTST_TEST_DISABLE_NO_VER_T")
136     @DisableVersioning
137     public static class DisableNoVersion extends PersistableBusinessObjectBase {
138 
139         @Id
140         @Column(name="ID")
141         private String id;
142 
143         @Column(name="STR_PROP")
144         private String property;
145 
146         public DisableNoVersion() { }
147 
148         public DisableNoVersion(String id, String property) {
149             this.id = id;
150             this.property = property;
151         }
152 
153         public String getId() {
154             return id;
155         }
156 
157         public void setId(String id) {
158             this.id = id;
159         }
160 
161         public String getProperty() {
162             return property;
163         }
164 
165         public void setProperty(String property) {
166             this.property = property;
167         }
168     }
169 
170     @Entity
171     @Table(name="KRTST_TEST_DISABLE_NO_VER_T")
172     @DisableVersioning
173     @RemoveMapping(name = "versionNumber")
174     public static class DisableNoVersionRemoveMapping extends PersistableBusinessObjectBase {
175 
176         @Id
177         @Column(name="ID")
178         private String id;
179 
180         @Column(name="STR_PROP")
181         private String property;
182 
183         public DisableNoVersionRemoveMapping() { }
184 
185         public DisableNoVersionRemoveMapping(String id, String property) {
186             this.id = id;
187             this.property = property;
188         }
189 
190         public String getId() {
191             return id;
192         }
193 
194         public void setId(String id) {
195             this.id = id;
196         }
197 
198         public String getProperty() {
199             return property;
200         }
201 
202         public void setProperty(String property) {
203             this.property = property;
204         }
205     }
206 }