Skip to content

Commit 047fdc6

Browse files
author
matyb
committed
fixing issue 9 - npe from xml -> path xformation
1 parent 34fd744 commit 047fdc6

8 files changed

Lines changed: 61 additions & 164 deletions

File tree

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

Lines changed: 5 additions & 4 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

46

57

@@ -14,16 +16,15 @@ private KoanConstants(){}
1416

1517
public static final String __ = "REPLACE ME";
1618
public static final String PATH_XML_NAME = "PathToEnlightment.xml";
17-
// public static final String PATH_XML = "src/"; // TODO: Adapt so dev and deployed can use same path
18-
public static final String PATH_XML = "";
19-
public static final String PATH_XML_LOCATION = PATH_XML+PATH_XML_NAME;
19+
public static final String FILESYSTEM_SEPARATOR= System.getProperty("file.separator");
20+
public static final String PATH_XML_LOCATION = new File("").getAbsolutePath().replace("koans-tests", "koans").replace(FILESYSTEM_SEPARATOR+"src", "")
21+
+ FILESYSTEM_SEPARATOR + "src" + FILESYSTEM_SEPARATOR + PATH_XML_NAME;
2022

2123
public static final String DESCRIPTION = "description";
2224
public static final String ARGUMENTS = "args";
2325

2426
public static final String EOL = System.getProperty("line.separator");
2527
public static final String EOLS = "[\n\r"+EOL+"]";
26-
public static final String FILESYSTEM_SEPARATOR= System.getProperty("file.separator");
2728
public static final String PERIOD = ".";
2829
public static final String EXPECTATION_LEFT_ARG= "has expectation as wrong argument!";
2930
public static final String EXPECTED_LEFT = "expected:<";

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

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,31 @@
1212

1313
import com.sandwich.koan.KoanMethod;
1414
import com.sandwich.koan.constant.KoanConstants;
15-
import com.sandwich.koan.path.xmltransformation.XmlToPathTransformer;
16-
import com.sandwich.koan.path.xmltransformation.XmlToPathTransformer.KoanElementAttributes;
15+
import com.sandwich.koan.path.xmltransformation.XmlToPathTransformerImpl;
16+
import com.sandwich.koan.path.xmltransformation.XmlToPathTransformerImpl.KoanElementAttributes;
1717

1818
public abstract class PathToEnlightenment {
1919

2020
static Path theWay;
21+
static XmlToPathTransformerImpl xmlToPathTransformer;
2122

2223
static Path createPath(){
23-
try{
24-
return new XmlToPathTransformer(KoanConstants.PATH_XML_LOCATION).transform();
25-
}catch(Throwable t){
26-
throw new RuntimeException(t);
24+
try {
25+
return getXmlToPathTransformer().transform();
26+
} catch (Exception e) {
27+
throw new RuntimeException(e);
28+
}
29+
}
30+
31+
private static XmlToPathTransformerImpl getXmlToPathTransformer(){
32+
if(xmlToPathTransformer == null){
33+
try{
34+
xmlToPathTransformer = new XmlToPathTransformerImpl(KoanConstants.PATH_XML_LOCATION);
35+
}catch(Throwable t){
36+
throw new RuntimeException(t);
37+
}
2738
}
39+
return xmlToPathTransformer;
2840
}
2941

3042
public static Path getPathToEnlightment(){
@@ -105,7 +117,7 @@ static void stagePathToEnlightenment(Object koanSuite) {
105117
}
106118
Map<String, Map<Object, List<KoanMethod>>> koans = new HashMap<String, Map<Object,List<KoanMethod>>>();
107119
Map<Object, List<KoanMethod>> suiteAndMethods = new HashMap<Object, List<KoanMethod>>();
108-
List<KoanMethod> methods = XmlToPathTransformer.getKoanMethods(koanSuite.getClass(),
120+
List<KoanMethod> methods = XmlToPathTransformerImpl.getKoanMethods(koanSuite.getClass(),
109121
createStubbedLessonMapForSuite(koanSuite.getClass()));
110122
suiteAndMethods.put(koanSuite, methods);
111123
koans.put(koanSuite.toString(), suiteAndMethods);
Lines changed: 9 additions & 149 deletions
Original file line numberDiff line numberDiff line change
@@ -1,149 +1,9 @@
1-
package com.sandwich.koan.path.xmltransformation;
2-
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;
26-
import com.sandwich.koan.path.PathToEnlightenment.FileFormatException;
27-
import com.sandwich.koan.path.PathToEnlightenment.Path;
28-
import com.sandwich.util.KoanComparator;
29-
30-
public class XmlToPathTransformer {
31-
32-
private final File xmlFile;
33-
34-
public XmlToPathTransformer(String xmlFileLocation) throws FileNotFoundException {
35-
this.xmlFile = new File(xmlFileLocation);
36-
if(!xmlFile.exists()){
37-
throw new FileNotFoundException(xmlFile.getAbsolutePath()
38-
+ " was not found. it may have been deleted, renamed.");
39-
}
40-
}
41-
42-
public Path transform() throws ParserConfigurationException, SAXException,
43-
IOException, DOMException, ClassNotFoundException,
44-
InstantiationException, IllegalAccessException {
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-
}
59-
60-
static Map<Object, List<KoanMethod>> createSuitesAndKoans(String pkg, NodeList childNodes) throws DOMException, ClassNotFoundException, InstantiationException, IllegalAccessException {
61-
Map<Object, List<KoanMethod>> suitesAndKoans = new LinkedHashMap<Object, List<KoanMethod>>();
62-
for(int i = 0; i < childNodes.getLength(); i++){
63-
Node node = childNodes.item(i);
64-
if("suite".equalsIgnoreCase(node.getNodeName())){
65-
Class<?> koanSuiteClass = Class.forName(pkg + '.' + node.getAttributes().getNamedItem("class").getNodeValue());
66-
suitesAndKoans.put(
67-
koanSuiteClass.newInstance(), Collections.unmodifiableList(
68-
createKoanMethods(koanSuiteClass, node.getChildNodes())));
69-
}
70-
}
71-
return Collections.unmodifiableMap(suitesAndKoans);
72-
}
73-
74-
static List<KoanMethod> createKoanMethods(Class<?> koanSuiteClass, NodeList nodes) {
75-
Map<String, KoanElementAttributes> rawKoanLessonByMethodName = extractKoansAndRawLessons(koanSuiteClass.getName(), nodes);
76-
List<KoanMethod> koanMethods = getKoanMethods(koanSuiteClass, rawKoanLessonByMethodName);
77-
KoanComparator koanComparator = new KoanComparator(rawKoanLessonByMethodName.keySet());
78-
Collections.sort(koanMethods, koanComparator);
79-
assertXmlAndCodeAreInSynch(nodes, koanMethods, koanComparator);
80-
return koanMethods;
81-
}
82-
83-
static Map<String, KoanElementAttributes> extractKoansAndRawLessons(
84-
String className, NodeList nodes) {
85-
Map<String, KoanElementAttributes> rawKoanAttributesByMethodName = new LinkedHashMap<String, KoanElementAttributes>();
86-
for(int i = 0; i < nodes.getLength(); i++){
87-
Node node = nodes.item(i);
88-
if("koan".equalsIgnoreCase(node.getNodeName())){
89-
NamedNodeMap attributes = node.getAttributes();
90-
String name = attributes.getNamedItem("name").getNodeValue();
91-
Node lesson = attributes.getNamedItem("lesson");
92-
String rawLesson = lesson == null ? null : lesson.getNodeValue();
93-
Node displayKoanIncompleteExceptionNode = attributes.getNamedItem("displayIncompleteKoanException");
94-
String displayIncompleteKoanException = displayKoanIncompleteExceptionNode == null ?
95-
null : displayKoanIncompleteExceptionNode.getNodeValue();
96-
if(rawKoanAttributesByMethodName.containsKey(name)){
97-
throw new DuplicateKoanException(className, name);
98-
}
99-
rawKoanAttributesByMethodName.put(name, new KoanElementAttributes(
100-
rawLesson, name, displayIncompleteKoanException));
101-
}
102-
}
103-
return rawKoanAttributesByMethodName;
104-
}
105-
106-
static class DuplicateKoanException extends RuntimeException {
107-
private static final long serialVersionUID = 3846796580641690961L;
108-
109-
public DuplicateKoanException(String className, String name){
110-
super("Duplicate koans in the same suite: "+className+" with the name "+name);
111-
}
112-
}
113-
114-
static void assertXmlAndCodeAreInSynch(NodeList csvOrder,
115-
List<KoanMethod> koanMethods, KoanComparator koanComparator) {
116-
if(koanMethods.size() != 1){
117-
koanComparator.assertXmlAndCodeAreInSynch();
118-
} else if (!koanMethods.get(0).getMethod().getName().equals(
119-
csvOrder.item(1).getAttributes().getNamedItem("name").getNodeValue())) {
120-
throw new FileFormatException(koanMethods.get(0).getMethod().getName()+
121-
" was not found in the PathToEnlightment.xml");
122-
}
123-
}
124-
125-
public static List<KoanMethod> getKoanMethods(Class<?> koanSuiteClass, Map<String, KoanElementAttributes> attributesByKoanName) {
126-
List<KoanMethod> koanMethods = new ArrayList<KoanMethod>();
127-
for(final Method koan : koanSuiteClass.getMethods()){
128-
final Koan annotation = koan.getAnnotation(Koan.class);
129-
if(annotation != null){
130-
KoanElementAttributes koanMethodAttributes = attributesByKoanName.get(koan.getName());
131-
String incompleteKoanException = koanMethodAttributes.displayIncompleteKoanException;
132-
boolean displayIncompleteKoanException = incompleteKoanException == null ||
133-
"true".equalsIgnoreCase(incompleteKoanException.trim());
134-
koanMethods.add(new KoanMethod(koanMethodAttributes.lesson, koan, displayIncompleteKoanException));
135-
}
136-
}
137-
return koanMethods;
138-
}
139-
140-
public static class KoanElementAttributes{
141-
String lesson, name, displayIncompleteKoanException;
142-
public KoanElementAttributes(String lesson, String name, String displayIncompleteKoanException){
143-
this.lesson = lesson;
144-
this.name = name;
145-
this.displayIncompleteKoanException = displayIncompleteKoanException;
146-
}
147-
}
148-
}
149-
1+
package com.sandwich.koan.path.xmltransformation;
2+
3+
import com.sandwich.koan.path.PathToEnlightenment.Path;
4+
5+
public interface XmlToPathTransformer {
6+
7+
public Path transform();
8+
9+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public XmlVariableInjector(String lesson, Method koanMethod){
2020
this.lesson = lesson;
2121
methodName = koanMethod.getName();
2222
suiteName = koanMethod.getDeclaringClass().getSimpleName();
23-
suitePath = new File(KoanConstants.PATH_XML+koanMethod.getDeclaringClass().getName().replace('.', '/')).getAbsolutePath()
23+
suitePath = new File(koanMethod.getDeclaringClass().getName().replace('.', '/')).getAbsolutePath()
2424
.replace(koanMethod.getDeclaringClass().getSimpleName(), "");
2525
}
2626

‎koans-tests/test/com/sandwich/koan/path/CommandLineTestCase.java‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,28 +18,35 @@
1818
import com.sandwich.koan.TestUtils.ArgRunner;
1919
import com.sandwich.koan.constant.ArgumentType;
2020
import com.sandwich.koan.path.PathToEnlightenment.Path;
21+
import com.sandwich.koan.path.xmltransformation.FakeXmlToPathTransformer;
2122
import com.sandwich.koan.runner.RunKoans;
2223

2324
public abstract class CommandLineTestCase {
2425

2526
private PrintStream console;
2627
private ByteArrayOutputStream bytes;
27-
28+
2829
@Before
2930
public void setUp() {
3031
bytes = new ByteArrayOutputStream();
3132
console = System.out;
3233
TestUtils.setValue("behavior", new RunKoans(), ArgumentType.RUN_KOANS);
34+
PathToEnlightenment.xmlToPathTransformer = new FakeXmlToPathTransformer();
3335
PathToEnlightenment.theWay = PathToEnlightenment.createPath();
3436
System.setOut(new PrintStream(bytes));
3537
}
3638

3739
@After
3840
public void tearDown() {
39-
PathToEnlightenment.theWay = PathToEnlightenment.createPath();
41+
setRealPath();
4042
System.setOut(console);
4143
}
4244

45+
protected void setRealPath(){
46+
PathToEnlightenment.xmlToPathTransformer = null;
47+
PathToEnlightenment.theWay = PathToEnlightenment.createPath();
48+
}
49+
4350
protected Path stubAllKoans(String packageName, List<?> path){
4451
Path oldKoans = PathToEnlightenment.getPathToEnlightment();
4552
Map<Object, List<KoanMethod>> tempSuitesAndMethods = new LinkedHashMap<Object, List<KoanMethod>>();

‎koans-tests/test/com/sandwich/koan/path/DefaultKoanDescriptionTest.java‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,26 @@
33
import java.util.List;
44
import java.util.Map.Entry;
55

6+
import org.junit.After;
7+
import org.junit.Before;
68
import org.junit.Test;
79

810
import com.sandwich.koan.Koan;
911
import com.sandwich.koan.KoanMethod;
1012
import com.sandwich.koan.constant.KoanConstants;
1113

12-
public class DefaultKoanDescriptionTest {
14+
public class DefaultKoanDescriptionTest extends CommandLineTestCase {
15+
16+
@Before
17+
public void setUp(){
18+
super.setUp();
19+
}
1320

21+
@After
22+
public void tearDown(){
23+
super.tearDown();
24+
}
25+
1426
@Test
1527
public void defaultKoanDescriptions() throws Exception {
1628
StringBuilder exceptionStringBuilder = new StringBuilder(KoanConstants.EOL);

‎koans-tests/test/com/sandwich/koan/runner/AppReadinessForDeploymentTest.java‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ public void testGetKoans() throws Exception {
7575

7676
@Test /** Ensures that koans are ready for packaging & distribution */
7777
public void testKoanSuiteRunner_firstKoanFail() throws Exception {
78+
setRealPath();
7879
final KoanSuiteResult[] result = new KoanSuiteResult[]{null};
7980
final SuitePresenter presenter = new SuitePresenter(){
8081
public void displayResult(KoanSuiteResult actualAppResult) {
@@ -89,6 +90,7 @@ public void displayResult(KoanSuiteResult actualAppResult) {
8990

9091
@Test /** Ensures that koans are ready for packaging & distribution */
9192
public void testKoanSuiteRunner_allKoansFail() throws Exception {
93+
setRealPath();
9294
final KoanSuiteResult[] result = new KoanSuiteResult[]{null};
9395
final SuitePresenter presenter = new SuitePresenter(){
9496
public void displayResult(KoanSuiteResult actualAppResult) {

‎koans/src/PathToEnlightment.xml‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,9 @@
122122
<koan name="shortCircuitAnd" />
123123
<koan name="aboutXOR" />
124124
<koan name="dontMistakeEqualsForEqualsEquals" />
125+
<koan name="aboutBitShiftingRightShift" />
126+
<koan name="aboutBitShiftingLeftShift" />
127+
<koan name="aboutBitShiftingRightUnsigned" />
125128
</suite>
126129
<suite class="AboutArrays">
127130
<koan name="arraysDoNotConsiderElementsWhenEvaluatingEquality"

0 commit comments

Comments
 (0)