Skip to content

Commit ed751d4

Browse files
author
matyb
committed
more robust filesystem interaction
1 parent 047fdc6 commit ed751d4

2 files changed

Lines changed: 193 additions & 0 deletions

File tree

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
package com.sandwich.koan.path.xmltransformation;
2+
3+
import java.io.File;
4+
import java.io.FileNotFoundException;
5+
import java.lang.reflect.Method;
6+
import java.util.ArrayList;
7+
import java.util.Collections;
8+
import java.util.LinkedHashMap;
9+
import java.util.List;
10+
import java.util.Map;
11+
12+
import javax.xml.parsers.DocumentBuilder;
13+
import javax.xml.parsers.DocumentBuilderFactory;
14+
15+
import org.w3c.dom.DOMException;
16+
import org.w3c.dom.Document;
17+
import org.w3c.dom.NamedNodeMap;
18+
import org.w3c.dom.Node;
19+
import org.w3c.dom.NodeList;
20+
21+
import com.sandwich.koan.Koan;
22+
import com.sandwich.koan.KoanMethod;
23+
import com.sandwich.koan.path.PathToEnlightenment.FileFormatException;
24+
import com.sandwich.koan.path.PathToEnlightenment.Path;
25+
import com.sandwich.util.KoanComparator;
26+
27+
public class XmlToPathTransformerImpl implements XmlToPathTransformer {
28+
29+
private final File xmlFile;
30+
31+
XmlToPathTransformerImpl() {
32+
xmlFile = null;
33+
}
34+
35+
public XmlToPathTransformerImpl(String xmlFileLocation) throws FileNotFoundException {
36+
this.xmlFile = new File(xmlFileLocation);
37+
if(!xmlFile.exists()){
38+
throw new FileNotFoundException(xmlFile.getAbsolutePath()
39+
+ " was not found. it may have been deleted, renamed.");
40+
}
41+
}
42+
43+
public Path transform(){
44+
try{
45+
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
46+
DocumentBuilder db = dbf.newDocumentBuilder();
47+
Document doc = db.parse(xmlFile);
48+
doc.getDocumentElement().normalize();
49+
NodeList nodeLst = doc.getElementsByTagName("package");
50+
Map<String, Map<Object, List<KoanMethod>>> koans = new LinkedHashMap<String, Map<Object, List<KoanMethod>>>();
51+
for(int i = 0; i < nodeLst.getLength(); i++){
52+
Node node = nodeLst.item(i);
53+
String packageTitle = node.getAttributes().getNamedItem("name").getNodeValue();
54+
String pkg = node.getAttributes().getNamedItem("pkg").getNodeValue();
55+
koans.put(packageTitle, createSuitesAndKoans(pkg, node.getChildNodes()));
56+
}
57+
return new Path(koans);
58+
}catch(Exception x){
59+
throw new RuntimeException(x);
60+
}
61+
}
62+
63+
static Map<Object, List<KoanMethod>> createSuitesAndKoans(String pkg, NodeList childNodes) throws DOMException, ClassNotFoundException, InstantiationException, IllegalAccessException {
64+
Map<Object, List<KoanMethod>> suitesAndKoans = new LinkedHashMap<Object, List<KoanMethod>>();
65+
for(int i = 0; i < childNodes.getLength(); i++){
66+
Node node = childNodes.item(i);
67+
if("suite".equalsIgnoreCase(node.getNodeName())){
68+
Class<?> koanSuiteClass = Class.forName(pkg + '.' + node.getAttributes().getNamedItem("class").getNodeValue());
69+
suitesAndKoans.put(
70+
koanSuiteClass.newInstance(), Collections.unmodifiableList(
71+
createKoanMethods(koanSuiteClass, node.getChildNodes())));
72+
}
73+
}
74+
return Collections.unmodifiableMap(suitesAndKoans);
75+
}
76+
77+
static List<KoanMethod> createKoanMethods(Class<?> koanSuiteClass, NodeList nodes) {
78+
Map<String, KoanElementAttributes> rawKoanLessonByMethodName = extractKoansAndRawLessons(koanSuiteClass.getName(), nodes);
79+
List<KoanMethod> koanMethods = getKoanMethods(koanSuiteClass, rawKoanLessonByMethodName);
80+
KoanComparator koanComparator = new KoanComparator(rawKoanLessonByMethodName.keySet());
81+
Collections.sort(koanMethods, koanComparator);
82+
assertXmlAndCodeAreInSynch(nodes, koanMethods, koanComparator);
83+
return koanMethods;
84+
}
85+
86+
static Map<String, KoanElementAttributes> extractKoansAndRawLessons(
87+
String className, NodeList nodes) {
88+
Map<String, KoanElementAttributes> rawKoanAttributesByMethodName = new LinkedHashMap<String, KoanElementAttributes>();
89+
for(int i = 0; i < nodes.getLength(); i++){
90+
Node node = nodes.item(i);
91+
if("koan".equalsIgnoreCase(node.getNodeName())){
92+
NamedNodeMap attributes = node.getAttributes();
93+
String name = attributes.getNamedItem("name").getNodeValue();
94+
Node lesson = attributes.getNamedItem("lesson");
95+
String rawLesson = lesson == null ? null : lesson.getNodeValue();
96+
Node displayKoanIncompleteExceptionNode = attributes.getNamedItem("displayIncompleteKoanException");
97+
String displayIncompleteKoanException = displayKoanIncompleteExceptionNode == null ?
98+
null : displayKoanIncompleteExceptionNode.getNodeValue();
99+
if(rawKoanAttributesByMethodName.containsKey(name)){
100+
throw new DuplicateKoanException(className, name);
101+
}
102+
rawKoanAttributesByMethodName.put(name, new KoanElementAttributes(
103+
rawLesson, name, displayIncompleteKoanException));
104+
}
105+
}
106+
return rawKoanAttributesByMethodName;
107+
}
108+
109+
static class DuplicateKoanException extends RuntimeException {
110+
private static final long serialVersionUID = 3846796580641690961L;
111+
112+
public DuplicateKoanException(String className, String name){
113+
super("Duplicate koans in the same suite: "+className+" with the name "+name);
114+
}
115+
}
116+
117+
static void assertXmlAndCodeAreInSynch(NodeList csvOrder,
118+
List<KoanMethod> koanMethods, KoanComparator koanComparator) {
119+
if(koanMethods.size() != 1){
120+
koanComparator.assertXmlAndCodeAreInSynch();
121+
} else if (!koanMethods.get(0).getMethod().getName().equals(
122+
csvOrder.item(1).getAttributes().getNamedItem("name").getNodeValue())) {
123+
throw new FileFormatException(koanMethods.get(0).getMethod().getName()+
124+
" was not found in the PathToEnlightment.xml");
125+
}
126+
}
127+
128+
public static List<KoanMethod> getKoanMethods(Class<?> koanSuiteClass, Map<String, KoanElementAttributes> attributesByKoanName) {
129+
List<KoanMethod> koanMethods = new ArrayList<KoanMethod>();
130+
for(final Method koan : koanSuiteClass.getMethods()){
131+
final Koan annotation = koan.getAnnotation(Koan.class);
132+
if(annotation != null){
133+
KoanElementAttributes koanMethodAttributes = attributesByKoanName.get(koan.getName());
134+
koanMethodAttributes = koanMethodAttributes == null ? KoanElementAttributes.NON_EXISTENT : koanMethodAttributes;
135+
String incompleteKoanException = koanMethodAttributes.displayIncompleteKoanException;
136+
boolean displayIncompleteKoanException = incompleteKoanException == null ||
137+
"true".equalsIgnoreCase(incompleteKoanException.trim());
138+
koanMethods.add(new KoanMethod(koanMethodAttributes.lesson, koan, displayIncompleteKoanException));
139+
}
140+
}
141+
return koanMethods;
142+
}
143+
144+
public static class KoanElementAttributes{
145+
String lesson, name, displayIncompleteKoanException;
146+
private static final KoanElementAttributes NON_EXISTENT = new KoanElementAttributes("", "", "true");
147+
public KoanElementAttributes(String lesson, String name, String displayIncompleteKoanException){
148+
this.lesson = lesson;
149+
this.name = name;
150+
this.displayIncompleteKoanException = displayIncompleteKoanException;
151+
}
152+
}
153+
}
154+
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.sandwich.koan.path.xmltransformation;
2+
3+
import java.util.Collections;
4+
import java.util.LinkedHashMap;
5+
import java.util.List;
6+
import java.util.Map;
7+
8+
import com.sandwich.koan.KoanMethod;
9+
import com.sandwich.koan.path.PathToEnlightenment.Path;
10+
11+
public class FakeXmlToPathTransformer extends XmlToPathTransformerImpl {
12+
13+
private Map<Object, List<KoanMethod>> methodsBySuite;
14+
15+
@SuppressWarnings("unchecked")
16+
public FakeXmlToPathTransformer() {
17+
this(Collections.EMPTY_MAP);
18+
}
19+
20+
public FakeXmlToPathTransformer(Map<Object, List<KoanMethod>> methodsBySuite){
21+
this.methodsBySuite = methodsBySuite;
22+
}
23+
24+
public Map<Object, List<KoanMethod>> getMethodsBySuite() {
25+
return methodsBySuite;
26+
}
27+
28+
public void setMethodsBySuite(Map<Object, List<KoanMethod>> methodsBySuite) {
29+
this.methodsBySuite = methodsBySuite;
30+
}
31+
32+
@Override
33+
public Path transform(){
34+
Map<String, Map<Object, List<KoanMethod>>> koans = new LinkedHashMap<String, Map<Object, List<KoanMethod>>>();
35+
koans.put("test", new LinkedHashMap<Object, List<KoanMethod>>(methodsBySuite));
36+
return new Path(koans);
37+
}
38+
39+
}

0 commit comments

Comments
 (0)