Skip to content

Commit c372dcb

Browse files
committed
merge expiremental down to master
1 parent 039744b commit c372dcb

7 files changed

Lines changed: 151 additions & 250 deletions

File tree

‎koans-lib/src/com/sandwich/koan/constant/KoanConstants.java‎

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.sandwich.koan.constant;
22

3+
import java.io.File;
4+
35
import com.sandwich.util.io.IOConstants;
46

57

@@ -12,19 +14,29 @@ private KoanConstants(){}
1214

1315
public static boolean DEBUG = false;
1416
// public static boolean DEBUG = true;
17+
public static final char EXIT_CHARACTER = 'Q';
1518
public static final boolean ENABLE_ENCOURAGEMENT= false;
1619

20+
public static String PROJ_MAIN_FOLDER = "koans";
21+
public static String PROJ_TESTS_FOLDER = "koans-tests";
22+
23+
public static String SOURCE_FOLDER = "src";
24+
public static String BIN_FOLDER = "bin";
25+
public static String DATA_FOLDER = "data";
26+
public static String TESTS_FOLDER = "test";
27+
public static String LIB_FOLDER = "lib";
28+
1729
public static final String __ = "REPLACE ME";
1830
public static final String PATH_XML_NAME = "PathToEnlightment.xml";
1931
// public static final String PATH_XML = "src/"; // TODO: Adapt so dev and deployed can use same path
20-
public static final String PATH_XML = "";
21-
public static final String PATH_XML_LOCATION = PATH_XML+PATH_XML_NAME;
32+
public static final String FILESYSTEM_SEPARATOR = System.getProperty("file.separator");
33+
public static final String PATH_XML_LOCATION = new File("").getAbsolutePath().replace("koans-tests", "koans").replace(FILESYSTEM_SEPARATOR+"src", "")
34+
+ FILESYSTEM_SEPARATOR + "src" + FILESYSTEM_SEPARATOR + PATH_XML_NAME;
2235

2336
public static final String DESCRIPTION = "description";
2437
public static final String ARGUMENTS = "args";
2538

2639
public static final String EOL = System.getProperty("line.separator");
27-
public static final String FILESYSTEM_SEPARATOR= System.getProperty("file.separator");
2840
public static final String EOLS = "[\n\r"+EOL+"]";
2941

3042
public static final String PERIOD = IOConstants.PERIOD;

‎koans-lib/src/com/sandwich/koan/path/PathToEnlightenment.java‎

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package com.sandwich.koan.path;
22

3+
import java.io.FileNotFoundException;
34
import java.lang.reflect.Method;
4-
import java.util.Arrays;
5-
import java.util.Collections;
5+
import java.util.ArrayList;
66
import java.util.HashMap;
77
import java.util.Iterator;
88
import java.util.LinkedHashMap;
@@ -13,24 +13,36 @@
1313
import com.sandwich.koan.KoanMethod;
1414
import com.sandwich.koan.constant.KoanConstants;
1515
import com.sandwich.koan.path.xmltransformation.XmlToPathTransformer;
16+
import com.sandwich.koan.path.xmltransformation.XmlToPathTransformerImpl;
1617

1718
public abstract class PathToEnlightenment {
1819

1920
static Path theWay;
2021
static String suiteName;
2122
static String koanMethod;
23+
static XmlToPathTransformer xmlToPathTransformer;
2224

2325
private PathToEnlightenment(){} // non instantiable
2426

2527
static Path createPath(){
2628
try{
27-
return new XmlToPathTransformer(KoanConstants.PATH_XML_LOCATION, suiteName, koanMethod)
28-
.transform();
29+
return getXmlToPathTransformer().transform();
2930
}catch(Throwable t){
3031
throw new RuntimeException(t);
3132
}
3233
}
3334

35+
private static XmlToPathTransformer getXmlToPathTransformer(){
36+
if(xmlToPathTransformer == null){
37+
try {
38+
xmlToPathTransformer = new XmlToPathTransformerImpl(KoanConstants.PATH_XML_LOCATION, suiteName, koanMethod);
39+
} catch (FileNotFoundException e) {
40+
throw new RuntimeException(e);
41+
}
42+
}
43+
return xmlToPathTransformer;
44+
}
45+
3446
public static void filterBySuite(String suite){
3547
suiteName = suite;
3648
}
Lines changed: 2 additions & 148 deletions
Original file line numberDiff line numberDiff line change
@@ -1,156 +1,10 @@
11
package com.sandwich.koan.path.xmltransformation;
22

3-
import java.io.File;
4-
import java.io.FileNotFoundException;
5-
import java.io.IOException;
6-
import java.lang.reflect.Method;
7-
import java.util.ArrayList;
8-
import java.util.Collections;
9-
import java.util.LinkedHashMap;
10-
import java.util.List;
11-
import java.util.Map;
12-
13-
import javax.xml.parsers.DocumentBuilder;
14-
import javax.xml.parsers.DocumentBuilderFactory;
15-
import javax.xml.parsers.ParserConfigurationException;
16-
17-
import org.w3c.dom.DOMException;
18-
import org.w3c.dom.Document;
19-
import org.w3c.dom.NamedNodeMap;
20-
import org.w3c.dom.Node;
21-
import org.w3c.dom.NodeList;
22-
import org.xml.sax.SAXException;
23-
24-
import com.sandwich.koan.Koan;
25-
import com.sandwich.koan.KoanMethod;
263
import com.sandwich.koan.path.PathToEnlightenment.Path;
27-
import com.sandwich.util.KoanComparator;
28-
import com.sandwich.util.io.DynamicClassLoader;
29-
30-
public class XmlToPathTransformer {
31-
32-
private final File xmlFile;
33-
private final String suiteName;
34-
private final String methodName;
35-
36-
public XmlToPathTransformer(){
37-
xmlFile = null;
38-
suiteName = null;
39-
methodName = null;
40-
}
41-
42-
public XmlToPathTransformer(String xmlFileLocation, String suiteName, String methodName) throws FileNotFoundException {
43-
this.xmlFile = new File(xmlFileLocation);
44-
this.suiteName = suiteName;
45-
this.methodName = methodName;
46-
if(!xmlFile.exists()){
47-
throw new FileNotFoundException(xmlFile.getAbsolutePath()
48-
+ " was not found. it may have been deleted, renamed.");
49-
}
50-
}
51-
52-
public Path transform() throws ParserConfigurationException, SAXException,
53-
IOException, DOMException, ClassNotFoundException,
54-
InstantiationException, IllegalAccessException {
55-
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
56-
DocumentBuilder db = dbf.newDocumentBuilder();
57-
Document doc = db.parse(xmlFile);
58-
doc.getDocumentElement().normalize();
59-
NodeList nodeLst = doc.getElementsByTagName("package");
60-
Map<String, Map<Object, List<KoanMethod>>> koans = new LinkedHashMap<String, Map<Object, List<KoanMethod>>>();
61-
for(int i = 0; i < nodeLst.getLength(); i++){
62-
Node node = nodeLst.item(i);
63-
String packageTitle = node.getAttributes().getNamedItem("name").getNodeValue();
64-
String pkg = node.getAttributes().getNamedItem("pkg").getNodeValue();
65-
koans.put(packageTitle, createSuitesAndKoans(pkg, node.getChildNodes()));
66-
}
67-
return new Path(koans);
68-
}
69-
70-
Map<Object, List<KoanMethod>> createSuitesAndKoans(String pkg, NodeList childNodes) throws DOMException, ClassNotFoundException, InstantiationException, IllegalAccessException {
71-
Map<Object, List<KoanMethod>> suitesAndKoans = new LinkedHashMap<Object, List<KoanMethod>>();
72-
for(int i = 0; i < childNodes.getLength(); i++){
73-
Node node = childNodes.item(i);
74-
if("suite".equalsIgnoreCase(node.getNodeName())){
75-
String className = pkg + '.' + node.getAttributes().getNamedItem("class").getNodeValue();
76-
if(suiteName == null || suiteName.equalsIgnoreCase(className)){
77-
DynamicClassLoader dynamicClassLoader = new DynamicClassLoader();
78-
Class<?> koanSuiteClass = dynamicClassLoader.loadClass(className);
79-
suitesAndKoans.put(
80-
koanSuiteClass.newInstance(), Collections.unmodifiableList(
81-
createKoanMethods(koanSuiteClass, node.getChildNodes())));
82-
}
83-
}
84-
}
85-
return suitesAndKoans;
86-
}
87-
88-
List<KoanMethod> createKoanMethods(Class<?> koanSuiteClass, NodeList nodes) {
89-
Map<String, KoanElementAttributes> rawKoanLessonByMethodName = extractKoansAndRawLessons(koanSuiteClass.getName(), nodes);
90-
List<KoanMethod> koanMethods = getKoanMethods(koanSuiteClass, rawKoanLessonByMethodName);
91-
KoanComparator koanComparator = new KoanComparator(rawKoanLessonByMethodName.keySet());
92-
Collections.sort(koanMethods, koanComparator);
93-
return koanMethods;
94-
}
95-
96-
Map<String, KoanElementAttributes> extractKoansAndRawLessons(
97-
String className, NodeList nodes) {
98-
Map<String, KoanElementAttributes> rawKoanAttributesByMethodName = new LinkedHashMap<String, KoanElementAttributes>();
99-
for(int i = 0; i < nodes.getLength(); i++){
100-
Node node = nodes.item(i);
101-
if("koan".equalsIgnoreCase(node.getNodeName())){
102-
NamedNodeMap attributes = node.getAttributes();
103-
String name = attributes.getNamedItem("name").getNodeValue();
104-
Node lesson = attributes.getNamedItem("lesson");
105-
String rawLesson = lesson == null ? null : lesson.getNodeValue();
106-
Node displayKoanIncompleteExceptionNode = attributes.getNamedItem("displayIncompleteKoanException");
107-
String displayIncompleteKoanException = displayKoanIncompleteExceptionNode == null ?
108-
null : displayKoanIncompleteExceptionNode.getNodeValue();
109-
if(rawKoanAttributesByMethodName.containsKey(name)){
110-
throw new DuplicateKoanException(className, name);
111-
}
112-
rawKoanAttributesByMethodName.put(name, new KoanElementAttributes(
113-
rawLesson, name, displayIncompleteKoanException));
114-
}
115-
}
116-
return rawKoanAttributesByMethodName;
117-
}
118-
119-
static class DuplicateKoanException extends RuntimeException {
120-
private static final long serialVersionUID = 3846796580641690961L;
1214

122-
public DuplicateKoanException(String className, String name){
123-
super("Duplicate koans in the same suite: "+className+" with the name "+name);
124-
}
125-
}
5+
public interface XmlToPathTransformer {
1266

127-
public List<KoanMethod> getKoanMethods(Class<?> koanSuiteClass, Map<String, KoanElementAttributes> attributesByKoanName) {
128-
List<KoanMethod> koanMethods = new ArrayList<KoanMethod>();
129-
for(final Method koan : koanSuiteClass.getMethods()){
130-
final Koan annotation = koan.getAnnotation(Koan.class);
131-
if(annotation != null){
132-
if(methodName == null || methodName.equalsIgnoreCase(koan.getName())){
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-
}
142-
return koanMethods;
143-
}
7+
public Path transform();
1448

145-
public static class KoanElementAttributes{
146-
public static final KoanElementAttributes NON_EXISTENT = new KoanElementAttributes(
147-
"", "", "true");
148-
String lesson, name, displayIncompleteKoanException;
149-
public KoanElementAttributes(String lesson, String name, String displayIncompleteKoanException){
150-
this.lesson = lesson;
151-
this.name = name;
152-
this.displayIncompleteKoanException = displayIncompleteKoanException;
153-
}
154-
}
1559
}
15610

0 commit comments

Comments
 (0)