diff --git a/lib/java_buildpack/application/java_home.rb b/lib/java_buildpack/application/java_home.rb index 6c53ca62de..c104c783aa 100644 --- a/lib/java_buildpack/application/java_home.rb +++ b/lib/java_buildpack/application/java_home.rb @@ -43,6 +43,19 @@ def as_env_var "JAVA_HOME=#{self}" end + # Execute a block with the +JAVA_HOME+ environment variable set + # + # @yield yields to block with the +JAVA_HOME+ environment variable set + def do_with + previous_value = ENV['JAVA_HOME'] + begin + ENV['JAVA_HOME'] = to_s + yield + ensure + ENV['JAVA_HOME'] = previous_value + end + end + end end diff --git a/lib/java_buildpack/base_component.rb b/lib/java_buildpack/base_component.rb index 59dc8fcdbb..84b5db4000 100644 --- a/lib/java_buildpack/base_component.rb +++ b/lib/java_buildpack/base_component.rb @@ -79,10 +79,12 @@ def release protected - # Copy resources from a components resources directory to its +home+ directory - def copy_resources + # Copy resources from a components resources directory to a directory + # + # @param [Pathname] target_directory the directory to copy to. Default to a component's +home+ + def copy_resources(target_directory = home) resources = Pathname.new(File.expand_path('../../../resources', __FILE__)) + @parsable_component_name - FileUtils.cp_r((resources.to_s + '/.'), home) if resources.exist? + FileUtils.cp_r((resources.to_s + '/.'), target_directory) if resources.exist? end # Downloads an item with the given name and version from the given URI, then yields the resultant file to the given @@ -111,7 +113,8 @@ def download(version, uri, description = @component_name, &block) # +@lib_directory+ # @param [String] description an optional description for the download. Defaults to +@component_name+. def download_jar(version, uri, jar_name, target_directory = @lib_directory, description = @component_name) - download(version, uri, description) { |file| FileUtils.cp file.path, File.join(target_directory, jar_name) } + FileUtils.mkdir_p target_directory + download(version, uri, description) { |file| FileUtils.cp file.path, (target_directory + jar_name) } end # The home directory for this component diff --git a/spec/java_buildpack/application/java_home_spec.rb b/spec/java_buildpack/application/java_home_spec.rb new file mode 100644 index 0000000000..a58847f561 --- /dev/null +++ b/spec/java_buildpack/application/java_home_spec.rb @@ -0,0 +1,33 @@ +# Encoding: utf-8 +# Cloud Foundry Java Buildpack +# Copyright 2013 the original author or authors. +# +# Licensed 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. + +require 'spec_helper' +require 'application_helper' + +describe JavaBuildpack::Application::JavaHome do + include_context 'application_helper' + + let(:java_home) { application.component_directory('test-java-home') } + + before do + application.java_home.set java_home + end + + it 'should set JAVA_HOME environment variable' do + application.java_home.do_with { expect(ENV['JAVA_HOME']).to eq("$PWD/#{java_home.relative_path_from app_dir}") } + end + +end