File: load_from_jar_spec.rb

package info (click to toggle)
jruby 9.4.8.0%2Bds-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 89,244 kB
  • sloc: ruby: 548,574; java: 276,189; yacc: 25,873; ansic: 6,178; xml: 6,111; sh: 1,855; sed: 94; makefile: 78; jsp: 48; tcl: 40; exp: 12
file content (70 lines) | stat: -rw-r--r-- 2,787 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
require File.dirname(__FILE__) + "/../spec_helper"
require 'java'

describe "Loading scripts from jar files" do
  it "should correctly report $LOADED_FEATURES" do
    jar = File.expand_path("test/jruby/dir with spaces/test_jar.jar")
    expect(require("#{jar}!abc/foo")).to eq(true)
    expect($LOADED_FEATURES.pop).to match(%r{dir with spaces/test_jar.jar!abc/foo.rb})

    # JRuby has supported a test_jar.jar!/abc/foo.rb alias for 'abc/foo.rb' entry in the test.jar for quite some time
    # (more correct would be test.jar!foo.rb)
    expect(require("#{jar}!/abc/foo")).to eq(true)
    expect($LOADED_FEATURES.pop).to eq("#{jar}!/abc/foo.rb")
  end

  it "blegh" do
    jar = File.expand_path("test/jruby/dir with spaces/test_jar.jar")
    load jar
    expect(File.size?("uri:classloader:/inside_jar.rb")).to eq(108)
  end

  # JRUBY-4774, WARBLER-15
  it "works with classpath URLs that have spaces in them" do
    url_loader = java.net.URLClassLoader.new([java.net.URL.new("file:" + File.expand_path("test/jruby/dir with spaces/test_jar.jar"))].to_java(java.net.URL))
    container = org.jruby.embed.ScriptingContainer.new(org.jruby.embed.LocalContextScope::SINGLETHREAD)
    container.setClassLoader(url_loader)
    expect(container.runScriptlet("begin; require 'abc/foo'; rescue LoadError; false; end")).to be_truthy
  end

  it "works when the jar path contains '#' symbols" do
    expect(require("jar:file:" + File.expand_path("test/jruby/dir with spaces/test#hash#symbol##jar.jar") + "!/abc/foo.rb")).to eq(true)
    expect($LOADED_FEATURES.pop).to match(/foo\.rb$/)

    expect(require("file:" + File.expand_path("test/jruby/dir with spaces/test#hash#symbol##jar.jar") + "!/abc/foo.rb")).to eq(true)
    expect($LOADED_FEATURES.pop).to match(/foo\.rb$/)
  end

  it "works when the load path is a jar and the path contains '#' symbols" do
    begin
      $LOAD_PATH.unshift "jar:file:" + File.expand_path("test/jruby/dir with spaces/test#hash#symbol##jar.jar") + "!/abc"

      expect(require("foo")).to eq(true)
      expect($LOADED_FEATURES.pop).to match(/foo\.rb$/)
    ensure
      $LOAD_PATH.shift
    end

    begin
      $LOAD_PATH.unshift "file:" + File.expand_path("test/jruby/dir with spaces/test#hash#symbol##jar.jar") + "!"

      expect(require("abc/foo")).to eq(true)
      expect($LOADED_FEATURES.pop).to match(/foo\.rb$/)
    ensure
      $LOAD_PATH.shift
    end
  end
end

describe "Opening files from a jar file" do
  # jruby/jruby#3399
  it "silently fails to seek on those files" do
    File.open("uri:classloader:jruby/kernel.rb") do |kernel_file|
      expect(kernel_file.pos).to eq(0)
      kernel_file.read(5)
      expect(kernel_file.pos).to eq(0)
      expect(kernel_file.seek(50)).to eq(0)
      expect(kernel_file.pos).to eq(0)
    end
  end
end