Coverage Report - org.kuali.student.datadictionary.util.Dictionary2BeanComparer
 
Classes in this File Line Coverage Branch Coverage Complexity
Dictionary2BeanComparer
64%
36/56
46%
15/32
4
 
 1  
 /*
 2  
  * Copyright 2011 The Kuali Foundation
 3  
  *
 4  
  * Licensed under the Educational Community License, Version 1.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/ecl1.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.student.datadictionary.util;
 17  
 
 18  
 import java.util.ArrayList;
 19  
 import java.util.Arrays;
 20  
 import java.util.List;
 21  
 import java.util.Stack;
 22  
 import org.kuali.rice.kns.datadictionary.AttributeDefinition;
 23  
 import org.kuali.rice.kns.datadictionary.DataObjectEntry;
 24  
 
 25  
 
 26  
 public class Dictionary2BeanComparer {
 27  
 
 28  
     private String className;
 29  
     private DataObjectEntry osDict;
 30  
 
 31  4
     public Dictionary2BeanComparer(String className, DataObjectEntry osDict) {
 32  4
         this.className = className;
 33  4
         this.osDict = osDict;
 34  4
     }
 35  
 
 36  
     public List<String> compare() {
 37  4
         if (className == null) {
 38  0
             return Arrays.asList(osDict.getFullClassName() + " does not have a corresponding java class");
 39  
         }
 40  4
         Class<?> clazz = null;
 41  
         try {
 42  4
             clazz = Class.forName(className);
 43  0
         } catch (ClassNotFoundException ex) {
 44  0
             return Arrays.asList(className + " does not have a corresponding java class");
 45  4
         }
 46  4
         Stack<AttributeDefinition> parents = new Stack<AttributeDefinition>();
 47  4
         DataObjectEntry osBean = new Bean2DictionaryConverter(clazz, parents).convert();
 48  4
         return compare(osDict, osBean);
 49  
 
 50  
     }
 51  
 
 52  
     private List<String> compare(DataObjectEntry osDict,
 53  
             DataObjectEntry osBean) {
 54  4
         List<String> discrepancies = new ArrayList();
 55  4
         compareAddDiscrepancy(discrepancies, "Java class name", osDict.getFullClassName(), osBean.getFullClassName());
 56  4
         compareAddDiscrepancy(discrepancies, "Entry class", osDict.getEntryClass(), osBean.getEntryClass());
 57  4
         for (AttributeDefinition adDict : osDict.getAttributes()) {
 58  29
             AttributeDefinition adBean = findField(adDict.getName(), osBean);
 59  29
             if (adBean == null) {
 60  
 //                if (!adDict.isDynamic()) {
 61  0
                 discrepancies.add("Field " + adDict.getName() + " does not exist in the corresponding java class");
 62  
 //                }
 63  0
                 continue;
 64  
             }
 65  29
             compareAddDiscrepancy(discrepancies, adDict.getName() + " dataType", adDict.getDataType(), adBean.getDataType());
 66  
             // TODO: deal with collections
 67  
 //            compareAddDiscrepancy(discrepancies, adDict.getName() + " maxOccurs", adDict.getMaximumNumberOfElements(), adBean.getMaximumNumberOfElements());
 68  29
         }
 69  4
         for (AttributeDefinition fdBean : osBean.getAttributes()) {
 70  73
             AttributeDefinition fdDict = findField(fdBean.getName(), osDict);
 71  73
             if (fdDict == null) {
 72  44
                 discrepancies.add("Field " + fdBean.getName() + " missing from the dictictionary");
 73  44
                 continue;
 74  
             }
 75  29
         }
 76  4
         return discrepancies;
 77  
     }
 78  
 
 79  
     private AttributeDefinition findField(String name, DataObjectEntry ode) {
 80  102
         for (AttributeDefinition fd : ode.getAttributes()) {
 81  782
             if (name.equals(fd.getName())) {
 82  58
                 return fd;
 83  
             }
 84  
         }
 85  44
         return null;
 86  
     }
 87  
 
 88  
     private void compareAddDiscrepancy(List<String> discrepancies, String field, boolean value1,
 89  
             boolean value2) {
 90  0
         String discrep = compare(field, value1, value2);
 91  0
         if (discrep != null) {
 92  0
             discrepancies.add(discrep);
 93  
         }
 94  0
     }
 95  
 
 96  
     private void compareAddDiscrepancy(List<String> discrepancies, String field, Object value1,
 97  
             Object value2) {
 98  37
         String discrep = compare(field, value1, value2);
 99  37
         if (discrep != null) {
 100  0
             discrepancies.add(discrep);
 101  
         }
 102  37
     }
 103  
 
 104  
     private String compare(String field, boolean value1, boolean value2) {
 105  0
         if (value1) {
 106  0
             if (value2) {
 107  0
                 return null;
 108  
             }
 109  
         }
 110  0
         if (!value1) {
 111  0
             if (!value2) {
 112  0
                 return null;
 113  
             }
 114  
         }
 115  0
         return field + " inconsistent: dictionary='" + value1 + "', java class='" + value2 + "'";
 116  
     }
 117  
 
 118  
     private String compare(String field, Object value1, Object value2) {
 119  37
         if (value1 == null) {
 120  0
             if (value2 == null) {
 121  0
                 return null;
 122  
             }
 123  
         } else {
 124  37
             if (value1.equals(value2)) {
 125  37
                 return null;
 126  
             }
 127  
         }
 128  0
         return field + " inconsistent: dictionary='" + value1 + "'], java class='" + value2 + "'";
 129  
     }
 130  
 }
 131