File: test_load_compiled_ruby_class_from_classpath.rb

package info (click to toggle)
jruby 1.5.1-1
  • links: PTS, VCS
  • area: non-free
  • in suites: squeeze
  • size: 46,252 kB
  • ctags: 72,039
  • sloc: ruby: 398,155; java: 169,482; yacc: 3,782; xml: 2,469; ansic: 415; sh: 279; makefile: 78; tcl: 40
file content (114 lines) | stat: -rw-r--r-- 3,488 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
require "test/unit"
require "fileutils"
require 'test/test_helper'
require 'rbconfig'

# Necessary because of http://jira.codehaus.org/browse/JRUBY-1579
require "jruby"

class LoadCompiledRubyClassFromClasspathTest < Test::Unit::TestCase
  include TestHelper

  RubyName = "runner"
  RubySource = "#{RubyName}.rb"
  RubyClass = "#{RubyName}.class"
  JarFile = "#{RubyName}_in_a_jar.jar"
  StarterName = "Starter"
  StarterSource = "#{StarterName}.java"
  StarterClass = "#{StarterName}.class"
  Manifest = "manifest.txt"

  if java.lang.System.getProperty("basedir") # FIXME: disabling this test under maven
    def test_truth; end
  else
  def setup
    remove_test_artifacts
    @original_classpath = ENV["CLASSPATH"]

    # This line means we assume the test is running from the jruby root directory
    @jruby_home = Dir.pwd
    @in_process = JRuby.runtime.instance_config.run_ruby_in_process
    JRuby.runtime.instance_config.run_ruby_in_process = false
  end

  def teardown
    remove_test_artifacts
    ENV["CLASSPATH"] = @original_classpath
    JRuby.runtime.instance_config.run_ruby_in_process = @in_process
  end

  def test_loading_compiled_ruby_class_from_classpath
    create_compiled_class

    append_to_classpath @jruby_home
    result = nil

    FileUtils.cd("..") do
      result = jruby("-r#{RubyName} -e '1'")
    end
    assert_equal 0, $?.exitstatus, "did not get 0 for exit status from running jruby against the class"
    assert_equal "hello from runner", result, "wrong text from runner"
  end

  def test_loading_compiled_ruby_class_from_jar
    return if RbConfig::CONFIG['host_os'] == "SunOS"
    create_compiled_class

    append_to_classpath jruby_jar
    File.open(StarterSource, "w") do |f|
      f.puts <<-EOS
public class #{StarterName} {
  public static void main(String args[]) throws Exception {
     org.jruby.Main.main(new String[] { "-r#{RubyName}", "-e", "" });
  }
}
      EOS
    end
    File.open(Manifest, "w") do |f|
      f.puts "Main-Class: #{StarterName}"
      f.puts "Class-Path: #{jruby_jar}"
    end

    javac = ENV['JAVA_HOME'] ? "#{ENV['JAVA_HOME']}/bin/javac" : "javac"

    `#{javac} -cp #{jruby_jar} #{StarterSource}`
    assert_equal 0, $?.exitstatus, "javac failed to compile #{StarterSource}"
    `jar cvfm #{JarFile} #{Manifest} #{StarterClass} #{RubyClass}`
    assert_equal 0, $?.exitstatus, "jar failed to build #{JarFile} from #{RubyClass}"

    remove_ruby_source_files

    result = `java -jar #{JarFile}`
    assert_equal 0, $?.exitstatus, "did not get 0 for exit status from running java against the jar"
    assert_equal "hello from runner", result, "wrong text from runner"
  end

  private
  def remove_ruby_source_files
    FileUtils.rm_rf [RubySource, RubyClass]
  end

  def remove_test_artifacts
    remove_ruby_source_files
    FileUtils.rm_rf [JarFile, StarterSource, StarterClass, Manifest]
  end

  def create_compiled_class
    File.open(RubySource, "w") { |f| f << "print 'hello from runner'" }
    jruby("-S jrubyc -p #{q}#{q} #{RubySource}")
    assert_equal 0, $?.exitstatus, "jrubyc failed to compile #{RubySource} into #{RubyClass}"
  ensure
    # just in case, remove the rb file
    FileUtils.rm_rf RubySource
  end

  def jruby_jar
    "lib/jruby.jar"
  end

  def append_to_classpath(*paths)
    current_classpath = ENV["CLASSPATH"].nil? ? "" : ENV["CLASSPATH"]
    ENV["CLASSPATH"] = "#{current_classpath}#{File::PATH_SEPARATOR}#{paths.join(File::PATH_SEPARATOR)}"
  end
  end # end FIXME
end