Coverage Report - org.kuali.student.common.test.spring.LoadDataBean
 
Classes in this File Line Coverage Branch Coverage Complexity
LoadDataBean
84%
48/57
81%
26/32
5.6
 
 1  
 /**
 2  
  * Copyright 2010 The Kuali Foundation Licensed under the
 3  
  * Educational Community License, Version 2.0 (the "License"); you may
 4  
  * not use this file except in compliance with the License. You may
 5  
  * obtain a copy of the License at
 6  
  *
 7  
  * http://www.osedu.org/licenses/ECL-2.0
 8  
  *
 9  
  * Unless required by applicable law or agreed to in writing,
 10  
  * software distributed under the License is distributed on an "AS IS"
 11  
  * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 12  
  * or implied. See the License for the specific language governing
 13  
  * permissions and limitations under the License.
 14  
  */
 15  
 
 16  
 package org.kuali.student.common.test.spring;
 17  
 
 18  
 import java.io.BufferedReader;
 19  
 import java.io.File;
 20  
 import java.io.FileReader;
 21  
 import java.util.List;
 22  
 import java.util.Map;
 23  
 
 24  
 import javax.persistence.EntityManager;
 25  
 import javax.persistence.PersistenceContext;
 26  
 import javax.persistence.PersistenceException;
 27  
 
 28  
 import org.apache.commons.lang.StringUtils;
 29  
 import org.apache.commons.logging.Log;
 30  
 import org.apache.commons.logging.LogFactory;
 31  
 import org.springframework.context.ApplicationContext;
 32  
 import org.springframework.context.ApplicationContextAware;
 33  
 import org.springframework.context.support.FileSystemXmlApplicationContext;
 34  
 import org.springframework.core.io.ClassPathResource;
 35  
 import org.springframework.transaction.annotation.Transactional;
 36  
 
 37  
 @Transactional
 38  5
 public class LoadDataBean implements ApplicationContextAware{
 39  1
         private static final Log LOG = LogFactory.getLog(LoadDataBean.class);
 40  
 
 41  
 
 42  
 
 43  
         @PersistenceContext
 44  
         EntityManager em;
 45  5
         private boolean loaded = false;
 46  
         private String daoAnnotations;
 47  
 
 48  
         private ApplicationContext applicationContext;
 49  
 
 50  
         public void loadData(){
 51  6
                 if (daoAnnotations == null || loaded || daoAnnotations.trim().isEmpty()) {
 52  4
                         return;
 53  
                 }
 54  
 
 55  
                 // Load all the beans
 56  2
                 String[] classes = daoAnnotations.split(",");
 57  6
                 for (String line : classes) {
 58  
                         try {
 59  4
                                 String[] split = line.split("\\|");
 60  
 
 61  
                                 // Invoke the data loader for this dao
 62  4
                 invokeDataLoader(split[0]);
 63  
 
 64  
                 // Load data bean file for this dao
 65  4
                 if (split.length > 1&& !split[1].isEmpty()) {
 66  2
                                     String testDataFile = split[1];
 67  
 
 68  2
                                         ApplicationContext ac = new FileSystemXmlApplicationContext(
 69  
                                                         testDataFile);
 70  2
                                         for (Object bean : (List<?>) ac.getBean("persistList")) {
 71  4
                                                 if(!em.contains(bean)){
 72  4
                                                         em.persist(bean);
 73  
                                                 }
 74  
                                         }
 75  
                                 }
 76  
                 // Load sql file for this dao
 77  4
                 if (split.length > 2&& !split[2].isEmpty()) {
 78  
 
 79  1
                                         String testDataFile = split[2];
 80  
                                     File sqlFile;
 81  1
                                     if(testDataFile.startsWith("classpath:")){
 82  1
                                             sqlFile = new ClassPathResource(testDataFile.substring("classpath:".length())).getFile();
 83  
                                     }else{
 84  0
                                             sqlFile = new File(testDataFile);
 85  
                                     }
 86  1
                                         BufferedReader in
 87  
                                            = new BufferedReader(new FileReader(sqlFile));
 88  
                                         try {
 89  1
                                             String ln = "";
 90  1
                                             int lnNr = 0;
 91  
 
 92  
                                             try {
 93  22
                             while((ln=in.readLine())!=null){
 94  21
                                 lnNr++;
 95  21
                                     if(!ln.startsWith("/")&&!ln.startsWith("--")&&StringUtils.isNotBlank(ln)){
 96  2
                                             ln=ln.replaceFirst("[;/]\\s*$","");
 97  2
                                             em.createNativeQuery(ln).executeUpdate();
 98  
                                     }
 99  
                             }
 100  0
                         } catch (PersistenceException e) {
 101  0
                             LOG.error("Failed statement at line " + lnNr + ": " + ln);
 102  0
                             throw e;
 103  1
                         }
 104  
                                         } finally {
 105  1
                                             in.close();
 106  1
                                         }
 107  
                                 }
 108  
 
 109  0
                         } catch (Exception e) {
 110  0
                                 throw new RuntimeException(e);
 111  4
                         }
 112  
                 }
 113  
 
 114  2
                 loaded = true;
 115  
 
 116  2
         }
 117  
 
 118  
         protected void invokeDataLoader(String dao){
 119  
             try {
 120  
             //Check if there is a loader class for this dao
 121  4
                 Class<?> daoType = Class.forName(dao).getInterfaces()[0];
 122  
 
 123  4
                 Class<?> clazz = Class.forName(daoType.getName() + "Loader");
 124  2
             DaoLoader daoLoader = (DaoLoader)clazz.newInstance();
 125  
 
 126  
             //Get spring bean for the dao
 127  2
             Map<?,?> daoBeans = applicationContext.getBeansOfType(daoType);
 128  
 
 129  
             //Invoke the loader for this doa bean (there shouldn't be more than one)
 130  2
             if (daoBeans.size() == 1){
 131  2
                 daoLoader.setDao(daoBeans.values().iterator().next());
 132  2
                 daoLoader.run();
 133  
             }
 134  
 
 135  2
             } catch (ClassNotFoundException cnfe) {
 136  2
                     LOG.info(cnfe);
 137  0
             } catch (Exception e) {
 138  0
             LOG.error(e);
 139  4
         }
 140  4
         }
 141  
 
 142  
         /**
 143  
          * @return the daoAnnotations
 144  
          */
 145  
         public String getDaoAnnotations() {
 146  0
                 return daoAnnotations;
 147  
         }
 148  
 
 149  
         /**
 150  
          * @param daoAnnotations
 151  
          *            the daoAnnotations to set
 152  
          */
 153  
         public void setDaoAnnotations(String daoAnnotations) {
 154  3
                 this.daoAnnotations = daoAnnotations;
 155  3
         }
 156  
 
 157  
         public void setApplicationContext(ApplicationContext applicationContext){
 158  3
             this.applicationContext = applicationContext;
 159  3
         }
 160  
 }