1 |
|
package org.kuali.student.common.dictionary.service.impl; |
2 |
|
|
3 |
|
import java.io.File; |
4 |
|
import java.io.FileNotFoundException; |
5 |
|
import java.io.FileOutputStream; |
6 |
|
import java.io.OutputStream; |
7 |
|
import java.io.PrintStream; |
8 |
|
import java.util.ArrayList; |
9 |
|
import java.util.Date; |
10 |
|
import java.util.HashMap; |
11 |
|
import java.util.HashSet; |
12 |
|
import java.util.LinkedHashSet; |
13 |
|
import java.util.List; |
14 |
|
import java.util.Map; |
15 |
|
import java.util.Set; |
16 |
|
|
17 |
|
import org.kuali.student.common.dictionary.dto.ObjectStructureDefinition; |
18 |
|
import org.springframework.context.ApplicationContext; |
19 |
|
import org.springframework.context.support.ClassPathXmlApplicationContext; |
20 |
|
|
|
|
| 93.2% |
Uncovered Elements: 5 (74) |
Complexity: 11 |
Complexity Density: 0.19 |
|
21 |
|
public class DictionaryTesterHelper |
22 |
|
{ |
23 |
|
|
24 |
|
private String outputFileName; |
25 |
|
private File file; |
26 |
|
private OutputStream outputStream; |
27 |
|
private PrintStream out; |
28 |
|
private Set<String> startingClasses; |
29 |
|
private String dictFileName; |
30 |
|
private boolean processSubstructures = false; |
31 |
|
|
|
|
| 88.9% |
Uncovered Elements: 1 (9) |
Complexity: 2 |
Complexity Density: 0.22 |
|
32 |
14
|
public DictionaryTesterHelper (String outputFileName,... |
33 |
|
Set<String> startingClasses, |
34 |
|
String dictFileName, |
35 |
|
boolean processSubstructures) |
36 |
|
{ |
37 |
14
|
this.outputFileName = outputFileName; |
38 |
14
|
this.startingClasses = startingClasses; |
39 |
14
|
this.dictFileName = dictFileName; |
40 |
14
|
this.processSubstructures = processSubstructures; |
41 |
|
|
42 |
14
|
this.file = new File (this.outputFileName); |
43 |
14
|
try |
44 |
|
{ |
45 |
14
|
outputStream = new FileOutputStream (file, false); |
46 |
|
} |
47 |
|
catch (FileNotFoundException ex) |
48 |
|
{ |
49 |
0
|
throw new IllegalArgumentException (ex); |
50 |
|
} |
51 |
14
|
this.out = new PrintStream (outputStream); |
52 |
|
} |
53 |
|
|
54 |
|
private transient Map<String, ObjectStructureDefinition> objectStructures; |
55 |
|
|
|
|
| 90.7% |
Uncovered Elements: 4 (43) |
Complexity: 4 |
Complexity Density: 0.11 |
|
56 |
14
|
public List<String> doTest ()... |
57 |
|
{ |
58 |
14
|
ApplicationContext ac = new ClassPathXmlApplicationContext ( |
59 |
|
"classpath:" + dictFileName); |
60 |
14
|
objectStructures = new HashMap (); |
61 |
14
|
Map<String, ObjectStructureDefinition> beansOfType = |
62 |
|
(Map<String, ObjectStructureDefinition>) ac. |
63 |
|
getBeansOfType (ObjectStructureDefinition.class); |
64 |
14
|
for (ObjectStructureDefinition objStr: beansOfType.values ()) |
65 |
|
{ |
66 |
220
|
objectStructures.put (objStr.getName (), objStr); |
67 |
220
|
System.out.println ("Loading object structure: " + objStr.getName ()); |
68 |
|
} |
69 |
|
|
70 |
14
|
for (String className: startingClasses) |
71 |
|
{ |
72 |
33
|
ObjectStructureDefinition os = null; |
73 |
33
|
os = objectStructures.get (className); |
74 |
33
|
if (os == null) |
75 |
|
{ |
76 |
0
|
throw new RuntimeException ("className is not defined in dictionary: " + className); |
77 |
|
} |
78 |
33
|
DictionaryValidator validator = new DictionaryValidator (os, |
79 |
|
new HashSet (), |
80 |
|
false); |
81 |
33
|
List<String> errors = validator.validate (); |
82 |
33
|
if (errors.size () > 0) |
83 |
|
{ |
84 |
0
|
return errors; |
85 |
|
} |
86 |
|
} |
87 |
|
|
88 |
|
|
89 |
14
|
Set<String> allStructures = new LinkedHashSet (); |
90 |
14
|
for (String className: startingClasses) |
91 |
|
{ |
92 |
33
|
allStructures.addAll (getComplexSubStructures (className)); |
93 |
|
} |
94 |
14
|
Set<String> classesToProcess = null; |
95 |
14
|
if (this.processSubstructures) |
96 |
|
{ |
97 |
4
|
classesToProcess = startingClasses; |
98 |
|
|
99 |
|
} |
100 |
|
else |
101 |
|
{ |
102 |
10
|
classesToProcess = allStructures; |
103 |
|
|
104 |
|
} |
105 |
|
|
106 |
14
|
out.println ("(!) This page was automatically generated on " + new Date ()); |
107 |
14
|
out.println ("DO NOT UPDATE MANUALLY!"); |
108 |
14
|
out.println (""); |
109 |
14
|
out.print ("This page represents a formatted view of [" + dictFileName |
110 |
|
+ "|https://test.kuali.org/svn/student/trunk/ks-lum/ks-lum-impl/src/main/resources/" |
111 |
|
+ dictFileName + "]"); |
112 |
14
|
out.println ( |
113 |
|
" and is compared to the following java classes (and their sub-classes) for discrepancies:"); |
114 |
14
|
for (String className: startingClasses) |
115 |
|
{ |
116 |
33
|
out.println ("# " + className); |
117 |
|
} |
118 |
14
|
out.println (""); |
119 |
14
|
out.println ("----"); |
120 |
14
|
out.println ("{toc}"); |
121 |
14
|
out.println ("----"); |
122 |
14
|
for (String className: classesToProcess) |
123 |
|
{ |
124 |
|
|
125 |
61
|
doTestOnClass (className, ac); |
126 |
|
} |
127 |
14
|
out.close (); |
128 |
14
|
return new ArrayList (); |
129 |
|
} |
130 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
131 |
33
|
private Set<String> getComplexSubStructures (String className)... |
132 |
|
{ |
133 |
33
|
return new ComplexSubstructuresHelper ().getComplexStructures (className); |
134 |
|
} |
135 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (11) |
Complexity: 2 |
Complexity Density: 0.22 |
|
136 |
61
|
private void doTestOnClass (String className, ApplicationContext ac)... |
137 |
|
{ |
138 |
61
|
ObjectStructureDefinition os = os = objectStructures.get (className); |
139 |
61
|
String simpleName = calcSimpleName (className); |
140 |
61
|
if (os == null) |
141 |
|
{ |
142 |
|
|
143 |
2
|
out.println ("h1. " + simpleName); |
144 |
2
|
out.println ("{anchor:" + simpleName + "}"); |
145 |
2
|
out.println ("h2. Error could not find a corresponding dictionary definition"); |
146 |
2
|
return; |
147 |
|
} |
148 |
59
|
DictionaryFormatter formatter = |
149 |
|
new DictionaryFormatter (className, |
150 |
|
className, |
151 |
|
os, |
152 |
|
new HashSet (), |
153 |
|
1, |
154 |
|
this.processSubstructures); |
155 |
59
|
out.println (formatter.formatForWiki ()); |
156 |
|
} |
157 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 2 |
Complexity Density: 0.67 |
|
158 |
61
|
private String calcSimpleName (String name)... |
159 |
|
{ |
160 |
61
|
if (name.lastIndexOf (".") != -1) |
161 |
|
{ |
162 |
55
|
name = name.substring (name.lastIndexOf (".") + 1); |
163 |
|
} |
164 |
61
|
return name; |
165 |
|
} |
166 |
|
|
167 |
|
} |