Clover Coverage Report - KS Contract Documentation Generator 0.0.1-SNAPSHOT
Coverage timestamp: Wed Dec 31 1969 19:00:00 EST
../../../../../img/srcFileCovDistChart0.png 0% of files have more coverage
64   162   26   7.11
32   123   0.41   9
9     2.89  
1    
 
  Dictionary2BeanComparer       Line # 27 64 0% 26 105 0% 0.0
 
No Tests
 
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.krad.datadictionary.AttributeDefinition;
23    import org.kuali.rice.krad.datadictionary.ComplexAttributeDefinition;
24    import org.kuali.rice.krad.datadictionary.DataDictionaryDefinitionBase;
25    import org.kuali.rice.krad.datadictionary.DataObjectEntry;
26   
 
27    public class Dictionary2BeanComparer {
28   
29    private String className;
30    private DataObjectEntry osDict;
31   
 
32  0 toggle public Dictionary2BeanComparer(String className, DataObjectEntry osDict) {
33  0 this.className = className;
34  0 this.osDict = osDict;
35    }
36   
 
37  0 toggle public List<String> compare() {
38  0 if (className == null) {
39  0 return Arrays.asList(osDict.getFullClassName() + " does not have a corresponding java class");
40    }
41  0 Class<?> clazz = null;
42  0 try {
43  0 clazz = Thread.currentThread().getContextClassLoader().loadClass(className);
44    // had to change the standard Class.forName below to the above so it uses the right class loader
45    // that is defined in KSDictionaryDocMojo.java
46    // clazz = Class.forName(className);
47    } catch (ClassNotFoundException ex) {
48  0 return Arrays.asList("Cannot compare the dictionary entry to it's object because the object's class cannot be found");
49    }
50  0 Stack<DataDictionaryDefinitionBase> parentFields = new Stack<DataDictionaryDefinitionBase>();
51  0 Stack<Class<?>> parentClasses = new Stack<Class<?>>();
52  0 DataObjectEntry osBean = new Bean2DictionaryConverter(clazz, parentFields, parentClasses).convert();
53  0 return compare(osDict, osBean);
54   
55    }
56   
 
57  0 toggle private List<String> compare(DataObjectEntry osDict,
58    DataObjectEntry osBean) {
59  0 List<String> discrepancies = new ArrayList();
60  0 compareAddDiscrepancy(discrepancies, "Java class name", osDict.getFullClassName(), osBean.getFullClassName());
61  0 compareAddDiscrepancy(discrepancies, "Entry class", osDict.getEntryClass(), osBean.getEntryClass());
62  0 for (AttributeDefinition adDict : osDict.getAttributes()) {
63  0 AttributeDefinition adBean = findAttributeDefinition(adDict.getName(), osBean);
64  0 if (adBean == null) {
65    // if (!adDict.isDynamic()) {
66  0 discrepancies.add("Field " + adDict.getName() + " does not exist in the corresponding java class");
67    // }
68  0 continue;
69    }
70  0 compareAddDiscrepancy(discrepancies, adDict.getName() + " dataType", adDict.getDataType(), adBean.getDataType());
71    // TODO: deal with collections
72    // compareAddDiscrepancy(discrepancies, adDict.getName() + " maxOccurs", adDict.getMaximumNumberOfElements(), adBean.getMaximumNumberOfElements());
73    }
74  0 for (ComplexAttributeDefinition cadDict : osDict.getComplexAttributes()) {
75  0 ComplexAttributeDefinition cadBean = findComplexAttributeDefinition(cadDict.getName(), osBean);
76  0 if (cadBean == null) {
77    // if (!adDict.isDynamic()) {
78  0 discrepancies.add("Field " + cadDict.getName() + " does not exist in the corresponding java class");
79    // }
80  0 continue;
81    }
82    // TODO: deal with collections
83    // compareAddDiscrepancy(discrepancies, adDict.getName() + " maxOccurs", adDict.getMaximumNumberOfElements(), adBean.getMaximumNumberOfElements());
84    }
85  0 for (AttributeDefinition fdBean : osBean.getAttributes()) {
86  0 AttributeDefinition fdDict = findAttributeDefinition(fdBean.getName(), osDict);
87  0 if (fdDict == null) {
88  0 discrepancies.add("Field " + fdBean.getName() + " missing from the dictictionary");
89  0 continue;
90    }
91    }
92  0 for (ComplexAttributeDefinition fdBean : osBean.getComplexAttributes()) {
93  0 ComplexAttributeDefinition fdDict = findComplexAttributeDefinition(fdBean.getName(), osDict);
94  0 if (fdDict == null) {
95  0 discrepancies.add("Field " + fdBean.getName() + " missing from the dictictionary");
96  0 continue;
97    }
98    }
99  0 return discrepancies;
100    }
101   
 
102  0 toggle private AttributeDefinition findAttributeDefinition(String name, DataObjectEntry ode) {
103  0 for (AttributeDefinition fd : ode.getAttributes()) {
104  0 if (name.equals(fd.getName())) {
105  0 return fd;
106    }
107    }
108  0 return null;
109    }
110   
 
111  0 toggle private ComplexAttributeDefinition findComplexAttributeDefinition(String name, DataObjectEntry ode) {
112  0 for (ComplexAttributeDefinition cad : ode.getComplexAttributes()) {
113  0 if (name.equals(cad.getName())) {
114  0 return cad;
115    }
116    }
117  0 return null;
118    }
119   
 
120  0 toggle private void compareAddDiscrepancy(List<String> discrepancies, String field, boolean value1,
121    boolean value2) {
122  0 String discrep = compare(field, value1, value2);
123  0 if (discrep != null) {
124  0 discrepancies.add(discrep);
125    }
126    }
127   
 
128  0 toggle private void compareAddDiscrepancy(List<String> discrepancies, String field, Object value1,
129    Object value2) {
130  0 String discrep = compare(field, value1, value2);
131  0 if (discrep != null) {
132  0 discrepancies.add(discrep);
133    }
134    }
135   
 
136  0 toggle private String compare(String field, boolean value1, boolean value2) {
137  0 if (value1) {
138  0 if (value2) {
139  0 return null;
140    }
141    }
142  0 if (!value1) {
143  0 if (!value2) {
144  0 return null;
145    }
146    }
147  0 return field + " inconsistent: dictionary='" + value1 + "', java class='" + value2 + "'";
148    }
149   
 
150  0 toggle private String compare(String field, Object value1, Object value2) {
151  0 if (value1 == null) {
152  0 if (value2 == null) {
153  0 return null;
154    }
155    } else {
156  0 if (value1.equals(value2)) {
157  0 return null;
158    }
159    }
160  0 return field + " inconsistent: dictionary='" + value1 + "'], java class='" + value2 + "'";
161    }
162    }