# 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.

#  ___   ___     _  _  ___ _____    ___ ___ ___ _____    _____ _  _ ___ ___ 
# |   \ / _ \   | \| |/ _ \_   _|  | __|   \_ _|_   _|  |_   _| || |_ _/ __|
# | |) | (_) |  | .` | (_) || |    | _|| |) | |  | |      | | | __ || |\__ \
# |___/ \___/   |_|\_|\___/ |_|    |___|___/___| |_|      |_| |_||_|___|___/
#                                                                          
#  ___ ___ _    ___ 
# | __|_ _| |  | __|  CHANGES TO THIS FILE WILL BE LOST
# | _| | || |__| _|   AUTO-GENERATED BY BUILDR on <%= Time.now %>
# |_| |___|____|___|  PREFER TO EDIT JtestR CONFIGURATION FILE:
#
# <%= user_config %>

begin

  <%= jruby_gem %>
  <%= dependencies.map(&:to_s).inspect %>.each { |dep| $CLASSPATH << dep }
  <%= runner.gems.inspect %>.each { |ary| JRuby.gem(*ary.flatten) }
  <%= runner.requires.inspect %>.each { |rb| Kernel.require rb }
  
  jtestr = JtestR::TestRunner.new
  
  class << jtestr
    def config(&block)
      @config_blocks ||= []
      @config_blocks << block if block
      @config_blocks
    end
    
    def load_configuration
      super
      config.each { |block| @configuration.instance_eval(&block) }
    end
  end
  
  jtestr.config do 
    classpath [] # already loaded
    add_common_classpath false
    
    <% ts = ( @jtestr_tests[:junit] & tests ).map { |c| 'Java.' + c } %>
    junit [<%= ts.join(', ') %>]
    
    <% ts = ( @jtestr_tests[:testng] & tests ).map { |c| 'Java.' + c } %>
    testng [<%= ts.join(', ') %>]
    
    <% ts = @jtestr_tests[:testunit] & tests %>
    test_unit <%= ts.inspect %>
      
    <% ts = @jtestr_tests[:story] & tests %>
    story <%= ts.inspect %>
      
    <% ts = @jtestr_tests[:rspec] & tests %>
    rspec <%= ts.inspect %>
      
    <% ts = @jtestr_tests[:expect] & tests %>
    expectations <%= ts.inspect %>
      
    ignore __FILE__
  
    if File.file?(<%= user_config.inspect %>)
      ignore <%= user_config.inspect %>
      load <%= user_config.inspect %>
    end
  end # config

  fake_out = StringIO.new

  <% if runner.output == false %>
     output = fake_out
  <% elsif runner.output.kind_of?(String) %>
     output = File.open(<%= result.output.inspect %>, 'w')
  <% else %>
     output = STDOUT
  <% end %>
  
  
  args = [ '<%= spec_dir %>', # the directory to search for jtestr files
           JtestR::SimpleLogger::ERR, # log level
           JtestR::GenericResultHandler::QUIET, #output level 
           fake_out, # output STDOUT
           [], # groups to run 
           Buildr::TestFramework::TestResult::RSpecResultHandler, # result handler
           []  # classpath
         ]
  
  argv = <%= runner.rspec.inspect %> || []
  argv.push *<%= tests.inspect %>
  Buildr::TestFramework::TestResult::RSpecResultHandler.init(argv, output, output)
  
  jtestr.run *args

rescue => e
  Buildr::TestFramework::TestResult::Error.dump_yaml('<%= runner.result %>', e) rescue \
  puts "-[--- ERROR ---]-", e.class, e.message, *e.backtrace
end

exit 0 # let buildr find the erros from the result yaml
  
# Local Variables:
# mode: ruby
# End: