Author: tom.baeyens(a)jboss.com
Date: 2009-11-03 17:11:03 -0500 (Tue, 03 Nov 2009)
New Revision: 5879
Modified:
projects/bbq/projects/bbq-core/java/org/bbq/Bbq.java
projects/bbq/projects/bbq-core/java/org/bbq/commands/ClassPath.java
projects/bbq/projects/bbq-core/java/org/bbq/commands/Exec.java
projects/bbq/projects/bbq-core/java/org/bbq/commands/Javac.java
projects/bbq/projects/bbq-core/java/org/bbq/commands/Junit.java
projects/bbq/projects/bbq-core/java/org/bbq/system/FileList.java
projects/bbq/projects/bbq-core/java/org/bbq/system/FileScan.java
projects/bbq/projects/bbq-test-project/build/Build.java
projects/bbq/projects/bbq-test-project/test/com/myproject/AddTest.java
Log:
added junit
Modified: projects/bbq/projects/bbq-core/java/org/bbq/Bbq.java
===================================================================
--- projects/bbq/projects/bbq-core/java/org/bbq/Bbq.java 2009-11-03 09:50:18 UTC (rev
5878)
+++ projects/bbq/projects/bbq-core/java/org/bbq/Bbq.java 2009-11-03 22:11:03 UTC (rev
5879)
@@ -23,13 +23,13 @@
import java.io.File;
import java.lang.reflect.Method;
-import java.lang.reflect.Modifier;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Properties;
import org.bbq.commands.Javac;
import org.bbq.system.Console;
+import org.bbq.system.Os;
import org.bbq.system.Path;
@@ -46,13 +46,19 @@
properties.setProperty("javac.dest.dir", "gen/classes");
properties.setProperty("build.src.dir", "build");
properties.setProperty("build.classes.dir",
"gen/build-classes");
- properties.setProperty("local.repo.dir",
System.getProperty("user.home")+"/.bbq");
+ String userBbqDir = System.getProperty("user.home")+"/.bbq";
+ if (Os.isWindows()) {
+ userBbqDir = userBbqDir.replace('\\', '/');
+ }
+ properties.setProperty("local.repo.dir", userBbqDir);
+ String bbqClassPath =
Path.absolute(System.getProperty("java.class.path"));
+ properties.setProperty("bbq.classpath", bbqClassPath);
+
// optionally bbq.properties files could be read
// from ${user.home}/.bbq/bbq.properties and ./bbq.properties
String buildSrcDir = properties.getProperty("build.src.dir");
String buildClassesDir = properties.getProperty("build.classes.dir");
- String buildClassPath =
Path.absolute(System.getProperty("java.class.path"));
File buildSrcFile = new File(buildSrcDir);
if (!buildSrcFile.exists()) {
@@ -67,18 +73,19 @@
new Javac()
.srcDir(buildSrcDir)
.destDir(buildClassesDir)
- .classPath(buildClassPath)
+ .classPath(bbqClassPath)
.execute();
try {
URL[] urls = {new File(buildClassesDir).toURL()};
ClassLoader classLoader = new URLClassLoader(urls , Bbq.class.getClassLoader());
Class<?> buildClass = Class.forName("Build", true, classLoader);
+ Object buildObject = buildClass.newInstance();
// if the user specified build operations as arguments
if (args!=null && args.length>0) {
for (String operation: args) {
Method method = buildClass.getDeclaredMethod(operation);
- Object result = method.invoke(null, (Object[])null);
+ Object result = method.invoke(buildObject, (Object[])null);
if (result!=null) {
Console.log("result of "+operation+": "+result);
}
@@ -86,9 +93,7 @@
} else { // means user didn't pass build operations
Console.log("available build operations:");
for (Method method: buildClass.getDeclaredMethods()) {
- if ( Modifier.isStatic(method.getModifiers())
- && (method.getParameterTypes().length==0)
- ) {
+ if (method.getParameterTypes().length==0) {
Console.log(" "+method.getName());
}
}
@@ -97,4 +102,8 @@
e.printStackTrace();
}
}
+
+ public static String getProperty(String key) {
+ return properties.getProperty(key);
+ }
}
Modified: projects/bbq/projects/bbq-core/java/org/bbq/commands/ClassPath.java
===================================================================
--- projects/bbq/projects/bbq-core/java/org/bbq/commands/ClassPath.java 2009-11-03
09:50:18 UTC (rev 5878)
+++ projects/bbq/projects/bbq-core/java/org/bbq/commands/ClassPath.java 2009-11-03
22:11:03 UTC (rev 5879)
@@ -55,6 +55,7 @@
public ClassPath add(ClassPath other) {
elements.addAll(other.elements);
+ artifacts.addAll(other.artifacts);
return this;
}
@@ -68,6 +69,10 @@
}
return toString(";");
}
+
+ public String toString() {
+ return toString(";");
+ }
public String toString(String separator) {
StringBuilder stringBuilder = new StringBuilder();
Modified: projects/bbq/projects/bbq-core/java/org/bbq/commands/Exec.java
===================================================================
--- projects/bbq/projects/bbq-core/java/org/bbq/commands/Exec.java 2009-11-03 09:50:18 UTC
(rev 5878)
+++ projects/bbq/projects/bbq-core/java/org/bbq/commands/Exec.java 2009-11-03 22:11:03 UTC
(rev 5879)
@@ -67,7 +67,7 @@
BufferedReader bufferedReader = new BufferedReader(new
InputStreamReader(process.getErrorStream()));
for (String line = bufferedReader.readLine(); line!=null; line =
bufferedReader.readLine()) {
- Console.log(" |"+line);
+ Console.log(" [output] "+line);
}
} catch (Exception e) {
Modified: projects/bbq/projects/bbq-core/java/org/bbq/commands/Javac.java
===================================================================
--- projects/bbq/projects/bbq-core/java/org/bbq/commands/Javac.java 2009-11-03 09:50:18
UTC (rev 5878)
+++ projects/bbq/projects/bbq-core/java/org/bbq/commands/Javac.java 2009-11-03 22:11:03
UTC (rev 5879)
@@ -35,14 +35,15 @@
*/
public class Javac {
- FileList sourceFiles = new FileList();
+ FileList sourceFiles = null;
boolean verbose;
String destDir = Bbq.properties.getProperty("javac.dest.dir");
ClassPath classPath = new ClassPath();
public Javac srcDir(String srcDir) {
- FileList sourceFiles = new FileScan(srcDir)
- .filterEnd(".java")
+ FileList sourceFiles = new FileScan()
+ .startsWith(srcDir)
+ .endsWith(".java")
.execute();
if (this.sourceFiles==null) {
@@ -98,9 +99,9 @@
String description = "javac -d "+destDir;
if (!classPath.isEmpty()) {
cmd.add("-classpath");
- cmd.add(classPath.toString());
+ cmd.add(classPath.resolve());
for (String classPathElement: classPath.elements) {
- description += Os.LINE_SEPARATOR+" classpath-element:
"+classPathElement;
+ description += Os.LINE_SEPARATOR+" [classpath] "+classPathElement;
}
}
if (verbose) {
@@ -108,7 +109,7 @@
}
for (String fileName: sourceFiles.list()) {
cmd.add(fileName);
- description += Os.LINE_SEPARATOR+" source-file: "+fileName;
+ description += Os.LINE_SEPARATOR+" [source] "+fileName;
}
// execute the compile command
Modified: projects/bbq/projects/bbq-core/java/org/bbq/commands/Junit.java
===================================================================
--- projects/bbq/projects/bbq-core/java/org/bbq/commands/Junit.java 2009-11-03 09:50:18
UTC (rev 5878)
+++ projects/bbq/projects/bbq-core/java/org/bbq/commands/Junit.java 2009-11-03 22:11:03
UTC (rev 5879)
@@ -21,29 +21,29 @@
*/
package org.bbq.commands;
-import java.util.ArrayList;
-import java.util.List;
-
+import org.bbq.Bbq;
+import org.bbq.system.Console;
import org.bbq.system.FileList;
import org.bbq.system.FileScan;
+import org.junit.runner.Description;
import org.junit.runner.JUnitCore;
-import org.junit.runner.Request;
+import org.junit.runner.Result;
+import org.junit.runner.notification.Failure;
+import org.junit.runner.notification.RunListener;
-import junit.textui.TestRunner;
-
-
/**
* @author Tom Baeyens
*/
public class Junit {
FileList sourceTestFiles = null;
- ClassPath classPath = new ClassPath();
+ ClassPath classPath = new ClassPath()
+ .add(Bbq.getProperty("bbq.classpath"));
String name = "bbq test run";
public Junit tests(String srcDir, String suffix) {
FileList sourceTestFiles = new FileScan(srcDir)
- .filterEnd(suffix)
+ .endsWith(suffix)
.execute();
if (this.sourceTestFiles==null) {
@@ -61,24 +61,46 @@
}
public void execute() {
- List<String> cmd = new ArrayList<String>();
+ if (sourceTestFiles==null) {
+ tests("test", "Test.java");
+ }
+ String[] args = new String[sourceTestFiles.size()];
+ for (int i=0; i<sourceTestFiles.size(); i++) {
+ String sourceTestResource = sourceTestFiles.getFilePaths().get(i);
+ String testClassName = sourceTestResource
+ .substring(0, sourceTestResource.length()-5)
+ .replace('/', '.');
+ args[i] = testClassName;
+ }
new Java()
- TODO...
+ .classPath(classPath.resolve())
+ .className(Run.class.getName())
+ .args(args)
.execute();
}
public static class Run {
public static void main(String[] args) {
JUnitCore junitCore = new JUnitCore();
- for (String srcTestFile: sourceTestFiles.list()) {
- String testClassName = srcTestFile
- .substring(0, srcTestFile.length()-5)
- .replace('/', '.');
-
- Class< ? > testClass = null;
- junitCore.run(testClass);
+ junitCore.addListener(new JunitRunListener());
+ try {
+ for (String testClassName: args) {
+ Class<?> testClass = Class.forName(testClassName);
+ junitCore.run(testClass);
+ }
+ } catch (ClassNotFoundException e) {
+ e.printStackTrace();
}
}
}
+
+ public static class JunitRunListener extends RunListener {
+ public void testStarted(Description description) throws Exception {
+ Console.log("starting test: "+description.toString());
+ }
+ public void testFailure(Failure failure) throws Exception {
+ Console.log("TEST FAILED: "+failure.toString());
+ }
+ }
}
Modified: projects/bbq/projects/bbq-core/java/org/bbq/system/FileList.java
===================================================================
--- projects/bbq/projects/bbq-core/java/org/bbq/system/FileList.java 2009-11-03 09:50:18
UTC (rev 5878)
+++ projects/bbq/projects/bbq-core/java/org/bbq/system/FileList.java 2009-11-03 22:11:03
UTC (rev 5879)
@@ -43,4 +43,12 @@
public void add(FileList other) {
filePaths.addAll(other.filePaths);
}
+
+ public int size() {
+ return filePaths.size();
+ }
+
+ public List<String> getFilePaths() {
+ return filePaths;
+ }
}
Modified: projects/bbq/projects/bbq-core/java/org/bbq/system/FileScan.java
===================================================================
--- projects/bbq/projects/bbq-core/java/org/bbq/system/FileScan.java 2009-11-03 09:50:18
UTC (rev 5878)
+++ projects/bbq/projects/bbq-core/java/org/bbq/system/FileScan.java 2009-11-03 22:11:03
UTC (rev 5879)
@@ -31,24 +31,42 @@
*/
public class FileScan {
- String rootDir;
+ String baseDir = null;
List<ScanFilter> scanFilters = new ArrayList<ScanFilter>();
+ public FileScan() {
+ }
+
public FileScan(String dirPath) {
- rootDir = dirPath;
+ baseDir = dirPath;
}
public FileList execute() {
FileList fileList = new FileList();
- scan(rootDir, fileList);
+ scan(null, fileList);
return fileList;
}
private void scan(String dir, FileList fileList) {
- File dirFile = new File(dir);
+ String dirPath = null;
+ if (baseDir!=null) {
+ if (dir!=null) {
+ dirPath = baseDir+"/"+dir;
+ } else {
+ dirPath = baseDir;
+ }
+ } else {
+ if (dir!=null) {
+ dirPath = dir;
+ } else {
+ dirPath = ".";
+ }
+ }
+
+ File dirFile = new File(dirPath);
if (dirFile.exists()) {
for (File child: dirFile.listFiles()) {
- String childPath = ("".equals(dir) ? child.getName() :
dir+"/"+child.getName());
+ String childPath = (dir==null ? child.getName() :
dir+"/"+child.getName());
if (child.isDirectory()) {
scan(childPath, fileList);
} else if (passesFilters(childPath, child)) {
@@ -71,9 +89,9 @@
boolean passes(String relativePath, File file);
}
- private class EndScanFilter implements ScanFilter {
+ private class EndsWithFilter implements ScanFilter {
String text;
- public EndScanFilter(String text) {
+ public EndsWithFilter(String text) {
this.text = text;
}
public boolean passes(String relativePath, File file) {
@@ -81,8 +99,22 @@
}
}
- public FileScan filterEnd(String text) {
- scanFilters.add(new EndScanFilter(text));
+ private class StartsWithFilter implements ScanFilter {
+ String text;
+ public StartsWithFilter(String text) {
+ this.text = text;
+ }
+ public boolean passes(String relativePath, File file) {
+ return relativePath.startsWith(text);
+ }
+ }
+
+ public FileScan endsWith(String text) {
+ scanFilters.add(new EndsWithFilter(text));
return this;
}
+ public FileScan startsWith(String text) {
+ scanFilters.add(new StartsWithFilter(text));
+ return this;
+ }
}
Modified: projects/bbq/projects/bbq-test-project/build/Build.java
===================================================================
--- projects/bbq/projects/bbq-test-project/build/Build.java 2009-11-03 09:50:18 UTC (rev
5878)
+++ projects/bbq/projects/bbq-test-project/build/Build.java 2009-11-03 22:11:03 UTC (rev
5879)
@@ -37,14 +37,17 @@
.add(jbossRepository,
"org/hibernate/hibernate-core/3.3.2.GA/hibernate-core-3.3.2.GA.jar");
ClassPath testCompileClassPath = new ClassPath()
- .add(getCompileClassPath())
+ .add("gen/classes")
+ .add(compileClassPath)
.add(jbossRepository, "junit/junit/4.4/junit-4.4.jar");
- ClassPath testRuntimeClassPath = testCompileClassPath;
+ ClassPath testRuntimeClassPath = new ClassPath()
+ .add("gen/test-classes")
+ .add(testCompileClassPath);
public void compile() {
new Javac()
- .classPath(getCompileClassPath())
+ .classPath(compileClassPath)
.execute();
}
@@ -53,7 +56,7 @@
new Javac()
.srcDir("test")
.destDir("gen/test-classes")
- .classPath(getTestCompileClassPath())
+ .classPath(testCompileClassPath)
.execute();
}
@@ -61,29 +64,10 @@
compileTest();
new Junit()
+ .classPath(testRuntimeClassPath)
.execute();
}
public void publish() {
}
-
-
- public ClassPath getCompileClassPath() {
- if (compileClassPath==null) {
-
- }
- return compileClassPath;
- }
-
-
- public ClassPath getTestCompileClassPath() {
- if (testCompileClassPath==null) {
- }
- return testCompileClassPath;
- }
-
-
- public ClassPath getTestRuntimeClassPath() {
- return testRuntimeClassPath;
- }
}
Modified: projects/bbq/projects/bbq-test-project/test/com/myproject/AddTest.java
===================================================================
--- projects/bbq/projects/bbq-test-project/test/com/myproject/AddTest.java 2009-11-03
09:50:18 UTC (rev 5878)
+++ projects/bbq/projects/bbq-test-project/test/com/myproject/AddTest.java 2009-11-03
22:11:03 UTC (rev 5879)
@@ -21,14 +21,16 @@
*/
package com.myproject;
+import junit.framework.TestCase;
+
/**
* @author Tom Baeyens
*/
-public class AddTest {
+public class AddTest extends TestCase {
public void testAddition() {
Add add = new Add();
- assertEquals(5, add.add(2, 3));
+ assertEquals(6, add.add(2, 3));
}
}