Skip to content

Commit 039744b

Browse files
committed
merging expiremental into master
Merge branch 'expiremental' of git://github.com/matyb/java-koans Conflicts: koans-lib/src/com/sandwich/koan/constant/KoanConstants.java koans-lib/src/com/sandwich/koan/path/PathToEnlightenment.java koans-lib/src/com/sandwich/koan/path/xmltransformation/XmlToPathTransformer.java koans-tests/test/com/sandwich/koan/path/CommandLineTestCase.java koans-tests/test/com/sandwich/koan/runner/AppReadinessForDeploymentTest.java koans/src/PathToEnlightment.xml
2 parents ed751d4 + de85df9 commit 039744b

46 files changed

Lines changed: 1335 additions & 838 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎README‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ Running Instructions:
33
https://github.com/matyb/java-koans/archives/master
44
2. Open a terminal and cd to the 'koans/src' directory from within the archive
55
3. Run run.bat or run.sh (whichever is applicable for your OS)
6-
4. Follow the instructions on screen, and rerun the run file in the last step
76

87
Developing a Koan:
98
1. Follow any of the existing koans as an example to create a new class w/ koan methods (indicated by the @Koan annotation)

‎koans-lib/.classpath‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
<?xml version="1.0" encoding="UTF-8"?>
2-
<classpath>
3-
<classpathentry kind="src" path="src"/>
4-
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
5-
<classpathentry kind="output" path="bin"/>
6-
</classpath>
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="src" path="src"/>
4+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
5+
<classpathentry kind="output" path="bin"/>
6+
</classpath>

‎koans-lib/src/com/sandwich/koan/KoanMethod.java‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ public boolean displayIncompleteException() {
3535
return displayIncompleteException;
3636
}
3737

38+
public KoanMethod clone(Method method){
39+
return new KoanMethod(lesson, method, displayIncompleteException);
40+
}
41+
3842
@Override public String toString(){
3943
return "{"+getMethod().getName()
4044
+" : "+ (lesson.length() > 20 ? lesson.substring(0, 20) + "..." : lesson)+"}";
Lines changed: 78 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,64 @@
11
package com.sandwich.koan.cmdline;
22

3-
import java.util.HashMap;
3+
import static com.sandwich.koan.constant.KoanConstants.DATA_FOLDER;
4+
import static com.sandwich.koan.constant.KoanConstants.FILESYSTEM_SEPARATOR;
5+
import static com.sandwich.koan.constant.KoanConstants.PROJ_MAIN_FOLDER;
6+
7+
import java.io.File;
8+
import java.util.LinkedHashMap;
49
import java.util.Map;
10+
import java.util.Map.Entry;
511

612
import com.sandwich.koan.constant.ArgumentType;
7-
import com.sandwich.util.Builder;
13+
import com.sandwich.koan.constant.KoanConstants;
14+
import com.sandwich.util.io.DynamicClassLoader;
15+
import com.sandwich.util.io.FileUtils;
16+
17+
public class CommandLineArgumentBuilder extends LinkedHashMap<ArgumentType, CommandLineArgument> {
18+
19+
private static final long serialVersionUID = 7635285665311420603L;
820

9-
public class CommandLineArgumentBuilder implements Builder<Map<ArgumentType, CommandLineArgument>>{
10-
private String[] args;
1121
public CommandLineArgumentBuilder(String...args){
12-
this.args = args;
13-
}
14-
public Map<ArgumentType, CommandLineArgument> build() {
15-
Map<ArgumentType, CommandLineArgument> commandLineArguments = new HashMap<ArgumentType, CommandLineArgument>();
16-
if(args != null){
17-
for(int index = 0; index < args.length;){
18-
String stringArg = args[index];
19-
if(stringArg == null || stringArg.trim().length() == 0){
20-
index++;
22+
for(int index = 0; index < args.length;){
23+
String stringArg = args[index];
24+
if(stringArg == null || stringArg.trim().length() == 0){
25+
index++;
26+
continue;
27+
}
28+
// is incremented in test of ternary
29+
String stringArgPlusOne = args.length <= ++index ? null : args[index];
30+
stringArgPlusOne = stringArgPlusOne == null || stringArgPlusOne.trim().length() == 0
31+
? null : stringArgPlusOne;
32+
ArgumentType argumentType = ArgumentType.findTypeByString(stringArg);
33+
ArgumentType argumentTypePlusOne = ArgumentType.findTypeByString(stringArgPlusOne);
34+
if(argumentType != null){ // matches an anticipated argument string
35+
// so does next value, must be an argument too, reevaluate w/ index incremented only once above
36+
if(argumentTypePlusOne instanceof ArgumentType){
37+
put(argumentType, new CommandLineArgument(argumentType, null));
38+
// intentionally increment past next argument (only one) and
39+
// evaluate in next iteration with its following argument
40+
// - in other words, argumentType will be argumentTypePlusOne in next iteration
2141
continue;
2242
}
23-
// is incremented in test of ternary
24-
String stringArgPlusOne = args.length <= ++index ? null : args[index];
25-
stringArgPlusOne = stringArgPlusOne == null || stringArgPlusOne.trim().length() == 0
26-
? null : stringArgPlusOne;
27-
ArgumentType argumentType = ArgumentType.findTypeByString(stringArg);
28-
ArgumentType argumentTypePlusOne = ArgumentType.findTypeByString(stringArgPlusOne);
29-
if(argumentType != null){ // matches an anticipated argument string
30-
// so does next value, must be an argument too, reevaluate w/ index incremented only once above
31-
if(argumentTypePlusOne instanceof ArgumentType){
32-
commandLineArguments.put(argumentType, new CommandLineArgument(argumentType, null));
33-
// intentionally increment past next argument (only one) and
34-
// evaluate in next iteration with its following argument
35-
// - in other words, argumentType will be argumentTypePlusOne in next iteration
36-
continue;
37-
}
38-
// ok 2nd argument wasn't a recongized argument type - go
39-
// ahead and see if it's a class, if not, it must be a
40-
// method - or bogus
41-
else if(stringArgPlusOne != null){
42-
guessAtMethodAndClass(commandLineArguments, stringArgPlusOne);
43-
}
44-
else{
45-
commandLineArguments.put(argumentType, new CommandLineArgument(argumentType, null));
46-
}
47-
index++;
48-
continue;
49-
}else if(stringArg != null){
50-
// no flag, but a string - likely class or class and method
51-
guessAtMethodAndClass(commandLineArguments, stringArg);
52-
// do not increment again, bump stringArgPlusOne into stringArg from prior increment
53-
continue;
43+
// ok 2nd argument wasn't a recongized argument type - go
44+
// ahead and see if it's a class, if not, it must be a
45+
// method - or bogus
46+
else if(stringArgPlusOne != null){
47+
guessAtMethodAndClass(this, stringArgPlusOne);
5448
}
49+
else{
50+
put(argumentType, new CommandLineArgument(argumentType, null));
51+
}
52+
index++;
53+
continue;
54+
}else if(stringArg != null){
55+
// no flag, but a string - likely class or class and method
56+
guessAtMethodAndClass(this, stringArg);
57+
// do not increment again, bump stringArgPlusOne into stringArg from prior increment
58+
continue;
5559
}
5660
}
57-
return commandLineArguments;
61+
applyAssumedStartupBehaviors();
5862
}
5963

6064
private static void guessAtMethodAndClass(
@@ -66,13 +70,14 @@ private static void guessAtMethodAndClass(
6670
if(potentialClassOrMethod != null){
6771
if(!hasClass){
6872
commandLineArguments.put(ArgumentType.CLASS_ARG,
69-
new CommandLineArgument(ArgumentType.CLASS_ARG, Class.forName(potentialClassOrMethod).getName()));
73+
new CommandLineArgument(ArgumentType.CLASS_ARG,
74+
new DynamicClassLoader().loadClass(potentialClassOrMethod).getName()));
7075
}else if(!hasMethod){
7176
commandLineArguments.put(ArgumentType.METHOD_ARG,
7277
new CommandLineArgument(ArgumentType.METHOD_ARG, potentialClassOrMethod));
7378
}
7479
}
75-
}catch(ClassNotFoundException cnfe2){
80+
}catch(Exception cnfe2){
7681
if(!hasMethod){
7782
commandLineArguments.put(ArgumentType.METHOD_ARG,
7883
new CommandLineArgument(ArgumentType.METHOD_ARG, potentialClassOrMethod));
@@ -82,4 +87,30 @@ private static void guessAtMethodAndClass(
8287
}
8388
}
8489
}
90+
91+
static private boolean isFirstTimeAppHasBeenRun() {
92+
File dataDirectory = new File(FileUtils.makeAbsoluteRelativeTo(
93+
new StringBuilder(PROJ_MAIN_FOLDER).append(FILESYSTEM_SEPARATOR).append(DATA_FOLDER).toString()));
94+
return !dataDirectory.exists();
95+
}
96+
97+
void applyAssumedStartupBehaviors() {
98+
if(isFirstTimeAppHasBeenRun()){
99+
ArgumentType.BACKUP.run(null);
100+
// really hackish - but this will clear console for first run, not necessary after
101+
for(int i = 0; i < 80; i++){
102+
System.out.println();
103+
}
104+
}
105+
if(isEmpty() || !containsKey(ArgumentType.RUN_KOANS) && containsKey(ArgumentType.CLASS_ARG)){
106+
if(KoanConstants.DEBUG){
107+
System.out.println("Planting default run target.");
108+
for(Entry<ArgumentType, CommandLineArgument> argEntry : entrySet()){
109+
System.out.println("Key: '"+argEntry.getKey()+"'");
110+
System.out.println("Value: '"+argEntry.getValue()+"'");
111+
}
112+
}
113+
put(ArgumentType.RUN_KOANS, new CommandLineArgument(ArgumentType.RUN_KOANS, null, true));
114+
}
115+
}
85116
}

‎koans-lib/src/com/sandwich/koan/cmdline/behavior/Backup.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import java.io.IOException;
44

5-
import com.sandwich.util.FileUtils;
5+
import com.sandwich.util.io.FileUtils;
66

77
public class Backup extends KoanFileCopying{
88

‎koans-lib/src/com/sandwich/koan/cmdline/behavior/ClassArg.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ public class ClassArg extends AbstractArgumentBehavior {
66

77
public void run(String koanSuiteClassName) {
88
if(koanSuiteClassName != null && koanSuiteClassName.trim().length() != 0){
9-
PathToEnlightenment.stagePathToEnlightenment(koanSuiteClassName);
9+
PathToEnlightenment.filterBySuite(koanSuiteClassName);
1010
}
1111
}
1212

‎koans-lib/src/com/sandwich/koan/cmdline/behavior/KoanFileCopying.java‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
package com.sandwich.koan.cmdline.behavior;
22

33
import static com.sandwich.koan.constant.KoanConstants.FILESYSTEM_SEPARATOR;
4+
import static com.sandwich.koan.constant.KoanConstants.PROJ_MAIN_FOLDER;
5+
import static com.sandwich.koan.constant.KoanConstants.SOURCE_FOLDER;
6+
import static com.sandwich.koan.constant.KoanConstants.DATA_FOLDER;
7+
48

59
import java.io.IOException;
610

711
public abstract class KoanFileCopying extends AbstractArgumentBehavior {
812

9-
private final String koans = "koans";
10-
private final String src = "src";
11-
1213
public void run(String value) {
1314
try {
14-
copy(new StringBuilder(koans).append(FILESYSTEM_SEPARATOR).append("data").append(FILESYSTEM_SEPARATOR).append(src).toString(),
15-
new StringBuilder(koans).append(FILESYSTEM_SEPARATOR).append(src).toString());
15+
copy(new StringBuilder( PROJ_MAIN_FOLDER).append(FILESYSTEM_SEPARATOR).append(DATA_FOLDER)
16+
.append(FILESYSTEM_SEPARATOR).append(SOURCE_FOLDER).toString(),
17+
new StringBuilder(PROJ_MAIN_FOLDER).append(FILESYSTEM_SEPARATOR).append(SOURCE_FOLDER).toString());
1618
} catch (IOException e) {
1719
System.out.println(getErrorMessage());
1820
System.exit(-1);

‎koans-lib/src/com/sandwich/koan/cmdline/behavior/MethodArg.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ public class MethodArg extends AbstractArgumentBehavior {
66

77
public void run(String koanName) {
88
if(koanName != null && koanName.trim().length() != 0){
9-
PathToEnlightenment.removeAllKoanMethodsExcept(koanName);
9+
PathToEnlightenment.filterByKoan(koanName);
1010
}
1111
}
1212

‎koans-lib/src/com/sandwich/koan/cmdline/behavior/Reset.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import java.io.IOException;
44

5-
import com.sandwich.util.FileUtils;
5+
import com.sandwich.util.io.FileUtils;
66

77
public class Reset extends KoanFileCopying{
88

‎koans-lib/src/com/sandwich/koan/cmdline/behavior/Test.java‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@
22

33
import java.io.IOException;
44

5-
import com.sandwich.util.FileCompiler;
5+
import com.sandwich.koan.constant.KoanConstants;
6+
import com.sandwich.util.io.FileCompiler;
67

78
public class Test extends AbstractArgumentBehavior{
89

910
public void run(String value) throws IOException {
10-
FileCompiler.compileRelative("koans-tests", "test", "bin");
11+
FileCompiler.compileRelative(KoanConstants.PROJ_TESTS_FOLDER, KoanConstants.TESTS_FOLDER, KoanConstants.BIN_FOLDER);
1112
}
1213

1314
}

0 commit comments

Comments
 (0)