make plugin.import() always work properly for Proxy objects.

This commit is contained in:
Charles Lowell 2012-03-22 12:58:16 -05:00
parent 81db26ba36
commit 6d0d9bb3c5
4 changed files with 71 additions and 9 deletions

View File

@ -108,14 +108,11 @@ module Jenkins
if extension.class.respond_to? :order
ordinal = extension.class.order
else
if extension.is_a? Proxy
t = import(extension)
if t.class.respond_to? :order
ordinal = t.class.order
end
t = import(extension)
if t.class.respond_to? :order
ordinal = t.class.order
end
end
@peer.addExtension(export(extension), ordinal)
end

View File

@ -28,6 +28,7 @@ module Jenkins
super(*super_args) if defined? super
@plugin, @object = plugin, object
@pluginid = @plugin.name
@plugin.linkout object, self
end
# tell Stapler to go look for views from the wrapped object

View File

@ -4,8 +4,10 @@ describe Jenkins::Plugin::Proxies do
Proxies = Jenkins::Plugin::Proxies
before do
Proxies.clear
@plugin = mock(Jenkins::Plugin)
@plugin = mock(Jenkins::Plugin, :name => 'mock-plugin')
@plugin.stub(:linkout) {|*args| @proxies.linkout(*args)}
@proxies = Jenkins::Plugin::Proxies.new(@plugin)
Jenkins.stub(:plugin) {@plugin}
end
describe "exporting a native ruby object" do
@ -163,15 +165,33 @@ describe Jenkins::Plugin::Proxies do
end
end
describe 'importing a java proxy object which was manually created' do
before do
@impl = Object.new
@proxy = proxy_class.new(@plugin, @impl)
end
it 'returns the proxied ruby object' do
@proxies.import(@proxy).should be @impl
end
it 'exports the proxy in lieu of the ruby implementation' do
@proxies.export(@impl).should be @proxy
end
end
private
def proxy_class
cls = Class.new
cls = Class.new(java.lang.Object)
cls.class_eval do
include Jenkins::Plugin::Proxy
attr_reader :plugin, :object
def initialize(plugin = nil, object = nil)
@plugin, @object = plugin, object
super(plugin || Jenkins.plugin, object)
end
end
return cls
end

View File

@ -1,8 +1,52 @@
require 'spec_helper'
require 'rspec-spies'
require 'tmpdir'
describe Jenkins::Plugin do
describe 'registering an extension.' do
before do
@peer = mock(:name => 'org.jenkinsci.ruby.RubyPlugin')
@peer.stub(:addExtension)
@plugin = Jenkins::Plugin.new @peer
@plugin.stub(:export) {|obj| obj}
end
describe 'When the extension class defines its order' do
before do
ext_class = Class.new
def ext_class.order; 10;end
@ext = ext_class.new
@plugin.register_extension @ext
end
it 'uses it' do
@peer.should have_received(:addExtension).with(@ext,10)
end
end
describe 'When the extension is a proxy' do
before do
impl_class = Class.new
def impl_class.order; 5; end
@impl = impl_class.new
@ext = Object.new
@plugin.stub(:import) {@impl}
@plugin.register_extension @ext
end
it 'uses the proxied objects class order' do
@peer.should have_received(:addExtension).with(@ext, 5)
end
end
describe 'when no order is defined' do
before do
@ext = Object.new
@plugin.register_extension @ext
end
it 'uses 0' do
@peer.should have_received(:addExtension).with(@ext, 0)
end
end
end
describe Jenkins::Plugin::Lifecycle do
before do |variable|
@plugin = Jenkins::Plugin.new mock(:name => 'org.jenkinsci.ruby.RubyPlugin')