001/* 002 * Copyright 2005-2014 The Kuali Foundation 003 * 004 * Licensed under the Educational Community License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.opensource.org/licenses/ecl2.php 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.kuali.rice.krad.data.jpa; 018 019import org.eclipse.persistence.exceptions.DatabaseException; 020import org.junit.Test; 021import org.kuali.rice.krad.bo.PersistableBusinessObjectBase; 022import org.kuali.rice.krad.service.KRADServiceLocator; 023import org.kuali.rice.krad.test.KRADTestCase; 024import org.kuali.rice.test.BaselineTestCase; 025 026import javax.persistence.Column; 027import javax.persistence.Entity; 028import javax.persistence.Id; 029import javax.persistence.Table; 030 031import static org.junit.Assert.*; 032 033/** 034 * Tests the DisableVersion annotation. 035 * 036 * @author Kuali Rice Team (rice.collab@kuali.org) 037 */ 038@BaselineTestCase.BaselineMode(BaselineTestCase.Mode.CLEAR_DB) // no rollback so we can avoid transactional cache mucking up our results 039public class DisableVersioningTest extends KRADTestCase { 040 041 private DisableVersion createDisableVersion(String rndId, String propertyVal) { 042 return new DisableVersion(rndId, propertyVal, new Long(0)); 043 } 044 045 private DisableNoVersion createDisableNoVersion(String rndId, String propertyVal) { 046 return new DisableNoVersion(rndId, propertyVal); 047 } 048 049 private DisableNoVersionRemoveMapping createDisableNoVersionRemoveMapping(String rndId, String propertyVal) { 050 return new DisableNoVersionRemoveMapping(rndId, propertyVal); 051 } 052 053 private String getRandomId() { 054 String time = String.valueOf(System.currentTimeMillis()); 055 return (time.substring(time.length() - 9)); 056 } 057 058 @Test 059 public void testDisableVersioning() { 060 // get a random value for our id 061 String rndId = getRandomId(); 062 String property = "testPropertyValue"; 063 064 // persist to the datasource 065 KRADServiceLocator.getDataObjectService().save(createDisableVersion(rndId, property)); 066 // retrieve the object back from the datasource 067 DisableVersion resultDV = KRADServiceLocator.getDataObjectService().find(DisableVersion.class, rndId); 068 // validate 069 assertNotNull("DisableVersion is null", resultDV); 070 assertEquals("DisableVersion id does not match given value", rndId, resultDV.getId()); 071 assertEquals("DisableVersion property does not match given value", property, resultDV.getProperty()); 072 assertEquals(new Long(0), resultDV.getVersionNumber()); 073 // now set the version number to a value and make sure it persists properly 074 resultDV.setVersionNumber(new Long(50)); 075 resultDV = KRADServiceLocator.getDataObjectService().save(resultDV); 076 assertEquals(new Long(50), resultDV.getVersionNumber()); 077 078 // now, since DisableNoVersion has no version number column, it should throw an exception when we attempt to 079 // persist it since there is no ver_nbr column in the database 080 try { 081 KRADServiceLocator.getDataObjectService().save(createDisableNoVersion(rndId, property)); 082 fail("Database exception should have been thrown when saving with no version number column"); 083 } catch (DatabaseException e) {} 084 085 // DisableVersionRemoveMapping *should* work though because we are removing the VER_NBR column mapping which 086 // should help because we have no such column in the database 087 KRADServiceLocator.getDataObjectService().save(createDisableNoVersionRemoveMapping(rndId, property)); 088 // retrieve the object back from the datasource 089 DisableNoVersionRemoveMapping resultDNV = KRADServiceLocator.getDataObjectService().find(DisableNoVersionRemoveMapping.class, rndId); 090 // validate 091 assertNotNull("DisableNoVersionRemoveMapping is null", resultDNV); 092 assertEquals("DisableNoVersionRemoveMapping id does not match given value", rndId, resultDNV.getId()); 093 assertEquals("DisableNoVersionRemoveMapping property does not match given value", property, resultDNV.getProperty()); 094 095 } 096 097 @Entity 098 @Table(name="KRTST_TEST_DISABLE_VER_T") 099 @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}