Author: vborja
Date: Tue Feb 26 12:04:34 2008
New Revision: 631365

URL: http://svn.apache.org/viewvc?rev=631365&view=rev
Log:
Renamed JUnitTestFilter to JavaTestFilter so that it can be used for TestNG 
classes.
Updated documentation on how buildr selects test cases.
Added default maven repo to buildfile
Update changelog, mispelled Caleb Powell's name.

Added:
    incubator/buildr/trunk/lib/java/org/apache/buildr/JavaTestFilter.class
    incubator/buildr/trunk/lib/java/org/apache/buildr/JavaTestFilter.java
Removed:
    incubator/buildr/trunk/lib/java/org/apache/buildr/JUnitTestFilter.class
    incubator/buildr/trunk/lib/java/org/apache/buildr/JUnitTestFilter.java
Modified:
    incubator/buildr/trunk/CHANGELOG
    incubator/buildr/trunk/buildfile
    incubator/buildr/trunk/doc/pages/testing.textile
    incubator/buildr/trunk/lib/java/test_frameworks.rb

Modified: incubator/buildr/trunk/CHANGELOG
URL: 
http://svn.apache.org/viewvc/incubator/buildr/trunk/CHANGELOG?rev=631365&r1=631364&r2=631365&view=diff
==============================================================================
--- incubator/buildr/trunk/CHANGELOG (original)
+++ incubator/buildr/trunk/CHANGELOG Tue Feb 26 12:04:34 2008
@@ -12,7 +12,8 @@
 * Added: Consolidated API for RJB and JRuby, replacing the now deprecated 
JavaWrapper.
 * Added: JRuby 1.1 support (Victor Hugo Borja, Nick Sieger).
 * Added: IDEA 7 task: use buildr idea7x (Shane Witbeck).
-* Changed: Upgraded to Antwrap 0.7.0, thanks to Caleb Powel for relicensing 
under Apache License.
+* Changed: JUnit/TestNG test cases are selected by superClass or annotations, 
not by class-name pattern.
+* Changed: Upgraded to Antwrap 0.7.0, thanks to Caleb Powell for relicensing 
under Apache License.
 * Changed: Upgraded to Rake 0.8, RSpec 1.1, RJB 1.1, Facets 2.2, OpenJPA 1.0.1.
 * Changed: Resources are now copied to target/resources instead of 
target/classes, and target/test/resources instead of target/test-resources.
 * Changed: Test cases are now compiled into target/test/classes instead of 
target/test-classes.

Modified: incubator/buildr/trunk/buildfile
URL: 
http://svn.apache.org/viewvc/incubator/buildr/trunk/buildfile?rev=631365&r1=631364&r2=631365&view=diff
==============================================================================
--- incubator/buildr/trunk/buildfile (original)
+++ incubator/buildr/trunk/buildfile Tue Feb 26 12:04:34 2008
@@ -1,5 +1,7 @@
 require 'buildr/jetty'
 
+repositories.remote << "http://www.ibiblio.org/maven2/";
+
 options = :javac, { :source=>'1.4', :target=>'1.4', :debug=>false }
 define 'java' do
   
compile.from(FileList['lib/java/**/*.java']).into('lib/java').using(*options).with(Buildr::JUnit::REQUIRES)

Modified: incubator/buildr/trunk/doc/pages/testing.textile
URL: 
http://svn.apache.org/viewvc/incubator/buildr/trunk/doc/pages/testing.textile?rev=631365&r1=631364&r2=631365&view=diff
==============================================================================
--- incubator/buildr/trunk/doc/pages/testing.textile (original)
+++ incubator/buildr/trunk/doc/pages/testing.textile Tue Feb 26 12:04:34 2008
@@ -38,9 +38,8 @@
 The default test framework for Java projects is JUnit 4.
 
 When you use JUnit, the dependencies includes JUnit, and Buildr picks up all
-test classes from the project by looking for classes that either start with
[EMAIL PROTECTED]@, end with @Test@ or end with @[EMAIL PROTECTED]  For 
example, @TestMyCode@ and
[EMAIL PROTECTED]@.
+test classes from the project by looking for classes that either subclass 
[EMAIL PROTECTED]@ or include methods annotated with @[EMAIL PROTECTED]
 
 There are benefits to running test cases in separate VMs.  The default forking
 mode is @:once@, and you can change it by setting the @:fork@ option.
@@ -90,7 +89,8 @@
 project back to using JUnit)
 
 TestNG works much like JUnit, it gets included in the dependency list, Buildr
-picks test classes that start or end with @Test@, and generates test reports in
+picks test classes that contain methods annotated with 
[EMAIL PROTECTED]@, and generates test reports in
 the @reports/testng@ directory.  At the moment we don't have consolidated HTML
 reports for TestNG.
 

Added: incubator/buildr/trunk/lib/java/org/apache/buildr/JavaTestFilter.class
URL: 
http://svn.apache.org/viewvc/incubator/buildr/trunk/lib/java/org/apache/buildr/JavaTestFilter.class?rev=631365&view=auto
==============================================================================
Binary files /tmp/tmpFQFbWy and /tmp/tmp0NNxGE differ

Added: incubator/buildr/trunk/lib/java/org/apache/buildr/JavaTestFilter.java
URL: 
http://svn.apache.org/viewvc/incubator/buildr/trunk/lib/java/org/apache/buildr/JavaTestFilter.java?rev=631365&view=auto
==============================================================================
--- incubator/buildr/trunk/lib/java/org/apache/buildr/JavaTestFilter.java 
(added)
+++ incubator/buildr/trunk/lib/java/org/apache/buildr/JavaTestFilter.java Tue 
Feb 26 12:04:34 2008
@@ -0,0 +1,116 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with this
+ * work for additional information regarding copyright ownership.  The ASF
+ * licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ */
+
+
+package org.apache.buildr;
+
+import java.lang.reflect.Method;
+import java.io.File;
+import java.io.IOException;
+import java.net.URL;
+import java.net.URLClassLoader;
+import java.net.MalformedURLException;
+import java.util.Iterator;
+import java.util.Vector;
+
+public class JavaTestFilter {
+
+  private ClassLoader _loader;
+  private Vector methodAnnotations = new Vector();
+  private Vector classAnnotations = new Vector();
+  private Vector interfaces = new Vector();
+
+  public JavaTestFilter(String[] paths) throws IOException {
+    URL[] urls = new URL[paths.length];
+    for (int i = 0 ; i < paths.length ; ++i) {
+      File file = new File(paths[i]).getCanonicalFile();
+      if (file.exists())
+        urls[i] = file.toURL();
+      else
+        throw new IOException("No file or directory with the name " + file);
+    }
+    _loader = new URLClassLoader(urls, getClass().getClassLoader());
+  }
+
+  public JavaTestFilter addInterfaces(String[] names) throws 
ClassNotFoundException {
+    for (int i = names.length; i -- > 0;) {
+      String name = names[i];
+      interfaces.add(_loader.loadClass(name));
+    }
+    return this;
+  }
+
+  public JavaTestFilter addClassAnnotations(String[] names) throws 
ClassNotFoundException {
+    for (int i = names.length; i -- > 0;) {
+      String name = names[i];
+      classAnnotations.add(_loader.loadClass(name));
+    }
+    return this;
+  }
+
+  public JavaTestFilter addMethodAnnotations(String[] names) throws 
ClassNotFoundException {
+    for (int i = names.length; i -- > 0;) {
+      String name = names[i];
+      methodAnnotations.add(_loader.loadClass(name));
+    }
+    return this;
+  }
+  
+  private boolean isTest(Class cls) {
+    if (interfaces != null) {
+      for (Iterator it = interfaces.iterator(); it.hasNext(); ) {
+        Class iface = (Class) it.next();
+        if (iface.isAssignableFrom(cls)) { return true; }
+      }
+    }
+    if (classAnnotations != null) { 
+      for (Iterator it = classAnnotations.iterator(); it.hasNext(); ) {
+        Class annotation = (Class) it.next();
+        if (cls.isAnnotationPresent(annotation)) { return true; }
+      }
+    } 
+    if (methodAnnotations != null) {
+      Method[] methods = cls.getMethods();
+      for (int j = methods.length ; j-- > 0 ;) {
+        for (Iterator it = methodAnnotations.iterator(); it.hasNext(); ) {
+          Class annotation = (Class) it.next();
+          if (methods[j].isAnnotationPresent(annotation)) { return true; }
+        }
+      }
+    }
+    return false;
+  }
+
+
+  public String[] filter(String[] names) throws ClassNotFoundException {
+    Vector testCases = new Vector();
+    for (int i = names.length ; i-- > 0 ;) {
+      Class cls = _loader.loadClass(names[i]);
+      if (isTest(cls)) { testCases.add(names[i]); }
+    }
+    String[] result = new String[testCases.size()];
+    testCases.toArray(result);
+    return result;
+  }
+
+}
+
+/* 
+ * Local Variables:
+ * indent-tabs-mode: nil
+ * c-basic-offset: 2
+ * End:
+ */
\ No newline at end of file

Modified: incubator/buildr/trunk/lib/java/test_frameworks.rb
URL: 
http://svn.apache.org/viewvc/incubator/buildr/trunk/lib/java/test_frameworks.rb?rev=631365&r1=631364&r2=631365&view=diff
==============================================================================
--- incubator/buildr/trunk/lib/java/test_frameworks.rb (original)
+++ incubator/buildr/trunk/lib/java/test_frameworks.rb Tue Feb 26 12:04:34 2008
@@ -106,7 +106,7 @@
     end
 
 
-    # Ant-JUnit requires for JUnit and JUnit reports tasks.  Also requires 
JUnitTestFilter.
+    # Ant-JUnit requires for JUnit and JUnit reports tasks.  Also requires 
JavaTestFilter.
     Java.classpath << "org.apache.ant:ant-junit:jar:#{Ant::VERSION}"
     Java.classpath << File.join(File.dirname(__FILE__))
 
@@ -131,7 +131,10 @@
         reject { |name| name =~ /\$/ }
       begin
         Java.load
-        
Java.org.apache.buildr.JUnitTestFilter.new(dependencies.to_java(Java.java.lang.String)).
+        
Java.org.apache.buildr.JavaTestFilter.new(dependencies.to_java(Java.java.lang.String)).
+          
add_interfaces(['junit.framework.TestCase'].to_java(Java.java.lang.String)).
+          
add_class_annotations(['org.junit.Test'].to_java(Java.java.lang.String)).
+          
add_method_annotations(['org.junit.Test'].to_java(Java.java.lang.String)).
           filter(candidates.to_java(Java.java.lang.String)).map(&:to_s)
       rescue =>ex
         puts "#{ex.class}: #{ex.message}" if verbose
@@ -207,9 +210,6 @@
     VERSION = '5.5' unless const_defined?('VERSION')
     # TestNG specification.
     REQUIRES = ["org.testng:testng:jar:jdk15:#{VERSION}"] + JMock::REQUIRES
-    # Pattern for selecting TestNG test classes. Regardless of include/exclude 
patterns, only classes
-    # that match this pattern are used.
-    TESTS_PATTERN = [ /^Test/, /Test$/, /TestCase$/ ]
 
     class << self
 
@@ -222,10 +222,19 @@
     def tests(task, dependencies) #:nodoc:
       return [] unless task.compile.target
       target = Pathname.new(task.compile.target.to_s)
-      Dir["#{target}/**/*.class"].
+      candidates = Dir["#{target}/**/*.class"].
         map { |file| 
Pathname.new(file).relative_path_from(target).to_s.ext('').gsub(File::SEPARATOR,
 '.') }.
-        reject { |name| name =~ /\$/ }.select { |name| cls_name = 
name.split('.').last
-                                                       TESTS_PATTERN.any? { 
|pattern| cls_name =~ pattern } }
+        reject { |name| name =~ /\$/ }
+      begin
+        Java.load
+        
Java.org.apache.buildr.JavaTestFilter.new(dependencies.to_java(Java.java.lang.String)).
+          
add_class_annotations(['org.testng.annotations.Test'].to_java(Java.java.lang.String)).
+          
add_method_annotations(['org.testng.annotations.Test'].to_java(Java.java.lang.String)).
+          filter(candidates.to_java(Java.java.lang.String)).map(&:to_s)
+      rescue =>ex
+        puts "#{ex.class}: #{ex.message}" if verbose
+        raise
+      end
     end
 
     def run(tests, task, dependencies) #:nodoc:


Reply via email to