Coverage Report - org.kuali.rice.test.persistence.PersistenceTestHelper
 
Classes in this File Line Coverage Branch Coverage Complexity
PersistenceTestHelper
0%
0/23
0%
0/10
0
 
 1  
 /**
 2  
  * Copyright 2005-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/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  
 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  
  * Helps with BO persistence tests
 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  
         // this should not be rocket science but we have to deal
 67  
         // but it appears mysql (driver?) is truncating time component of datetimes
 68  
         // so we can only portably test timestamps without times...
 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  
         /*for (Map.Entry e: fields.entrySet()) {
 90  
             println(e.getKey().getClass());
 91  
             println(e.getValue().getClass());
 92  
             println(e.getKey());
 93  
             println(e.getValue());
 94  
         }
 95  
         for (Map.Entry e: row.entrySet()) {
 96  
             println(e.getKey().getClass());
 97  
             println(e.getValue().getClass());
 98  
             println(e.getKey());
 99  
             println(e.getValue());
 100  
         }*/
 101  0
         Assert.assertEquals(new HashMap(fields), new HashMap(row))
 102  
     }
 103  
 }