| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
package org.kuali.rice.test.persistence |
| 17 | |
|
| 18 | |
import org.kuali.rice.krad.service.KRADServiceLocator |
| 19 | |
import org.kuali.rice.krad.service.BusinessObjectService |
| 20 | |
import javax.sql.DataSource |
| 21 | |
import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader |
| 22 | |
import org.kuali.rice.krad.bo.PersistableBusinessObject |
| 23 | |
import java.sql.Timestamp |
| 24 | |
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate |
| 25 | |
import org.joda.time.DateTime |
| 26 | |
import org.junit.Assert |
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 31 | |
class PersistenceTestHelper { |
| 32 | |
def BusinessObjectService boService; |
| 33 | |
def DataSource datasource; |
| 34 | |
|
| 35 | |
PersistenceTestHelper(String dsName) { |
| 36 | 0 | boService = (BusinessObjectService) KRADServiceLocator.getBusinessObjectService() |
| 37 | 0 | datasource = (DataSource) GlobalResourceLoader.getService(dsName) |
| 38 | 0 | if (!datasource) { |
| 39 | 0 | throw new RuntimeException("DataSource bean not found: " + dsName) |
| 40 | |
} |
| 41 | |
} |
| 42 | |
|
| 43 | |
def bool(value, column) { |
| 44 | 0 | [ (column): value ? "Y" : "N" ] |
| 45 | |
} |
| 46 | |
def default_field(bo) { |
| 47 | 0 | bool(bo.dflt, 'DFLT_IND') |
| 48 | |
} |
| 49 | |
def active_field(bo) { |
| 50 | 0 | bool(bo.active, 'ACTV_IND') |
| 51 | |
} |
| 52 | |
def edit_field(bo) { |
| 53 | 0 | bool(bo.edit, 'EDIT_FLAG') |
| 54 | |
} |
| 55 | |
|
| 56 | |
def basic_fields(PersistableBusinessObject bo) { |
| 57 | 0 | [ OBJ_ID: bo.objectId, |
| 58 | 0 | VER_NBR: new BigDecimal(bo.versionNumber) ] |
| 59 | |
} |
| 60 | |
|
| 61 | |
def standard_fields(PersistableBusinessObject bo) { |
| 62 | 0 | active_field(bo) + basic_fields(bo) |
| 63 | |
} |
| 64 | |
|
| 65 | |
def genDbTimestamp() { |
| 66 | |
|
| 67 | |
|
| 68 | |
|
| 69 | 0 | new Timestamp(new Date().time) |
| 70 | |
} |
| 71 | |
|
| 72 | |
def toDbTimestamp(DateTime datetime) { |
| 73 | 0 | return toDbTimestamp(datetime.millis) |
| 74 | |
} |
| 75 | |
|
| 76 | |
def toDbTimestamp(long millis) { |
| 77 | 0 | def timestamp = new java.sql.Timestamp(millis) |
| 78 | 0 | timestamp.nanos = 0 |
| 79 | 0 | timestamp |
| 80 | |
} |
| 81 | |
|
| 82 | 0 | def assertRow(Map fields, table, pk="id", ignore=["LAST_UPDT_DT"]) { |
| 83 | 0 | def pk_val = fields[pk] |
| 84 | 0 | if (!pk_val) { |
| 85 | 0 | throw new RuntimeException("No primary key value found for field: " + pk) |
| 86 | |
} |
| 87 | 0 | Map row = new SimpleJdbcTemplate(datasource).queryForMap("select * from " + table + " where " + pk + "=?", pk_val) |
| 88 | 0 | row.keySet().removeAll(ignore) |
| 89 | |
|
| 90 | |
|
| 91 | |
|
| 92 | |
|
| 93 | |
|
| 94 | |
|
| 95 | |
|
| 96 | |
|
| 97 | |
|
| 98 | |
|
| 99 | |
|
| 100 | |
|
| 101 | 0 | Assert.assertEquals(new HashMap(fields), new HashMap(row)) |
| 102 | |
} |
| 103 | |
} |