lib/java/test.rb in buildr-0.21.0 vs lib/java/test.rb in buildr-0.22.0

- old
+ new

@@ -19,14 +19,15 @@ super @classpath = [] @paths = [] @include = ["*Test", "*Suite"] @exclude = [] + @options = {} enhance do |task| unless test_cases.empty? puts "Running tests in #{name}" if verbose - passed, failed = Java.junit(test_cases, :classpath=>classpath + @paths) + passed, failed = Java.junit(test_cases, @options.merge(:classpath=>classpath + @paths)) fail "The following tests failed:\n#{failed.join("\n")}" unless failed.empty? end end end @@ -74,14 +75,29 @@ # with(*specs) => self # # Specify artifacts (specs, tasks, files, etc) to include in the classpath when running # the test cases. def with(*files) - @classpath |= artifacts(files.flatten).uniq + @classpath |= Buildr.artifacts(files.flatten).uniq self end + # Returns the JUnit options. + attr_reader :options + + # :call-seq: + # using(options) => self + # + # Sets the JUnit options from a hash and returns self. Right now supports passing properties to JUnit. + # + # For example: + # test.junit.using :properties=>{ "root"=>base_dir } + def using(options) + options.each { |k,v| @options[k.to_sym] = v } + self + end + private def test_cases() unless @cases @cases = @paths.map do |path| @@ -166,10 +182,13 @@ # Use the Test/Suite suffix for classes that implement test cases, avoid this suffix for other # classes (e.g. stubs, helper objects). # # You can also include only specific test cases, or exclude otherwise included test cases # using #include and #exclude. + # + # The Java property baseDir is set to the classes directory, you can use it to pick up test resources + # that you cannot access using getResources(). def junit() @project.task("test:junit") end # :call-seq: @@ -223,10 +242,11 @@ end end + class Project # :call-seq: # test(*prereqs) => TestTask # test(*prereqs) { |task| .. } => TestTask @@ -266,11 +286,10 @@ # Similar to the regular resources task but using different paths. resources = Java::ResourcesTask.define_task("test:resources") resources.filter.from project.path_to("src/test/resources") # Similar to the regular compile task but using different paths. compile = Java::CompileTask.define_task("test:compile"=>[project.compile, project.test.prepare, project.test.resources]) - compile.enhance { project.test.resources.invoke } project.path_to("src/test/java").tap { |dir| compile.from dir if File.exist?(dir) } compile.into project.path_to("target/test-classes") resources.filter.into compile.target # Define the JUnit task here, otherwise we get a normal task. Java::JUnitTask.define_task("test:junit") @@ -283,19 +302,25 @@ # Copy the regular compile classpath over, and also include the generated classes. project.test.with project.compile.classpath, project.compile.target project.test.junit.with project.test.compile.classpath # Tell the JUnit task where to pick the test cases from. project.test.junit.from project.test.compile.target + project.test.junit.options[:properties] ||= {} + project.test.junit.options.tap do |options| + options[:properties] ||= {} + options[:properties]["baseDir"] ||= project.test.compile.target.to_s + end end end # This rule takes a suffix and runs that test case in the current project. For example; # rake test:MyTest # will run the test case class com.example.MyTest, if found in the current project. rule /^test:.*$/ do |task| test = task.name.scan(/test:(.*)/)[0][0] Project.projects.select { |project| project.base_dir == Rake.application.original_dir }. - map { |project| project.test }.each { |task| task.include("*#{test}").invoke } + map { |project| project.test }.each { |task| task.junit.instance_eval { @include = ["*#{test}"] ; @exclude.clear } }. + each(&:invoke) end # Require tests after build unless TEST option is off. # For example: # rake build