Skip to content
This repository has been archived by the owner on Dec 2, 2020. It is now read-only.

Refactors facts about Java version #55

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions lib/facter/java_major_version.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Fact: java_major_version
#
# Purpose: get Java's major version
#
# Resolution:
# Tests for presence of java, returns nil if not present
# returns output of "java -version" and splits on \n + '"'
# eg.
#
# Caveats:
# none
#
# Notes:
# None
Facter.add(:java_major_version) do
setcode do
java_version = Facter.value(:java_version)
java_patch_level = java_version.strip.split('_')[0].split('.')[1] unless java_version.nil?
end
end
8 changes: 2 additions & 6 deletions lib/facter/java_patch_level.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@
Facter.add(:java_patch_level) do
setcode do
java_version = Facter.value(:java_version)
if java_version.nil?
"JAVA_NOT_INSTALLED"
else
java_patch_level = java_version.strip.split('_')[1]
end
java_patch_level = java_version.strip.split('_')[1] unless java_version.nil?
end
end
end
11 changes: 6 additions & 5 deletions lib/facter/java_version.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Fact: java_version
#
# Purpose: store java versions in the config DB
# Purpose: get full java version string
#
# Resolution:
# Tests for presence of java, returns nil if not present
Expand All @@ -12,10 +12,11 @@
# Notes:
# None
Facter.add(:java_version) do
# This will fail on OS X when Java hasn't been installed yet.
next unless Kernel.system "/usr/libexec/java_home --failfast &>/dev/null"
setcode do
# This will fail on OS X when Java hasn't been installed yet.
next unless system "/usr/libexec/java_home --failfast &>/dev/null"
t_java = Facter::Util::Resolution.exec("java -version 2>&1")
java_version = t_java.to_s.lines.first.strip.split(/version/)[1].gsub(/"/, "").strip
if Facter::Util::Resolution.which('java')
Facter::Util::Resolution.exec('java -Xmx8m -version 2>&1').lines.first.split(/"/)[1].strip
end
end
end
31 changes: 31 additions & 0 deletions spec/unit/facter/java_major_version.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
require "spec_helper"

describe Facter::Util::Fact do
before {
Facter.clear
}

describe "java_major_version" do
context "if java is not installed" do
context 'returns java major version extracted from java_version fact' do
before :each do
allow(Facter.fact(:java_version)).to receive(:value).and_return("1.7.0_71")
end
it do
Facter.fact(:java_major_version).value.should == "7"
end
end
end

context "if java is not installed" do
context 'returns nil' do
before :each do
allow(Facter.fact(:java_version)).to receive(:value).and_return(nil)
end
it do
Facter.fact(:java_major_version).value.should == nil
end
end
end
end
end
27 changes: 27 additions & 0 deletions spec/unit/facter/java_major_version_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
require "spec_helper"

describe Facter::Util::Fact do
before {
Facter.clear
}

describe "java_major_version" do
context 'returns java major version extracted from java_version fact' do
before :each do
allow(Facter.fact(:java_version)).to receive(:value).and_return("1.7.0_71")
end
it do
Facter.fact(:java_major_version).value.should == "7"
end
end

context 'returns nil when java_version fact not present' do
before :each do
allow(Facter.fact(:java_version)).to receive(:value).and_return(nil)
end
it do
Facter.fact(:java_major_version).value.should be_nil
end
end
end
end
28 changes: 12 additions & 16 deletions spec/unit/facter/java_patch_level_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,21 @@
}

describe "java_patch_level" do
context "if java is installed" do
context 'returns java patch version extracted from java_version fact' do
before :each do
allow(Facter.fact(:java_version)).to receive(:value).and_return("1.7.0_71")
end
it do
Facter.fact(:java_patch_level).value.should == "71"
end
context 'returns java patch version extracted from java_version fact' do
before :each do
allow(Facter.fact(:java_version)).to receive(:value).and_return("1.7.0_71")
end
it do
Facter.fact(:java_patch_level).value.should == "71"
end
end

context "if java is installed" do
context 'returns java patch version extracted from java_version fact' do
before :each do
allow(Facter.fact(:java_version)).to receive(:value).and_return(nil)
end
it do
Facter.fact(:java_patch_level).value.should == "JAVA_NOT_INSTALLED"
end
context "returns nil when java_version fact not present" do
before :each do
allow(Facter.fact(:java_version)).to receive(:value).and_return(nil)
end
it do
Facter.fact(:java_patch_level).value.should be_nil
end
end
end
Expand Down
17 changes: 9 additions & 8 deletions spec/unit/facter/java_version_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
describe Facter::Util::Fact do
before {
Facter.clear
allow(Facter::Util::Resolution).to receive(:exec).with(anything()).and_return(nil)
allow(Facter.fact(:kernel)).to receive(:value).and_return("Darwin")
}

describe "java_version" do
Expand All @@ -15,17 +13,20 @@
Java(TM) SE Runtime Environment (build 1.7.0_71-b14)
Java HotSpot(TM) 64-Bit Server VM (build 24.71-b01, mixed mode)
EOS
allow(Facter::Util::Resolution).to receive(:exec).with("java -version 2>&1").
and_return(java_version_output)
allow(Facter::Util::Resolution).to receive(:which).
with("java").and_return(true)
expect(Kernel).to receive(:system).
with("/usr/libexec/java_home --failfast &>/dev/null").and_return(true)
allow(Facter::Util::Resolution).to receive(:exec).
with("java -Xmx8m -version 2>&1").and_return(java_version_output)
Facter.fact(:java_version).value.should == "1.7.0_71"
end
end

context 'returns nil when java present' do
context 'returns nil when java not present' do
it do
java_version_output = "bash: java: command not found"
allow(Facter::Util::Resolution).to receive(:exec).with("java -version 2>&1").
and_return(java_version_output)
allow(Facter::Util::Resolution).to receive(:which).with("java").
and_return(false)
Facter.fact(:java_version).value.should be_nil
end
end
Expand Down