diff --git a/sunspot_solr/lib/sunspot/solr/java.rb b/sunspot_solr/lib/sunspot/solr/java.rb index b098041e2..71ecaaf5e 100644 --- a/sunspot_solr/lib/sunspot/solr/java.rb +++ b/sunspot_solr/lib/sunspot/solr/java.rb @@ -1,12 +1,25 @@ -require 'rbconfig' +require "rbconfig" module Sunspot module Solr module Java - NULL_DEVICE = RbConfig::CONFIG['host_os'] =~ /mswin|mingw/ ? 'NUL' : '/dev/null' + class << self + def ensure_install! + if installed? + true + else + raise Sunspot::Solr::Server::JavaMissing, "You need a Java Runtime Environment to run the Solr server" + end + end - def self.installed? - !system("java -version >#{NULL_DEVICE} 2>&1").nil? + def installed? + system("java", "-version", [:out, :err] => null_device) + $?.exitstatus.zero? + end + + def null_device + RbConfig::CONFIG["host_os"] =~ /mswin|mingw/ ? "NUL" : "/dev/null" + end end end end diff --git a/sunspot_solr/lib/sunspot/solr/server.rb b/sunspot_solr/lib/sunspot/solr/server.rb index 9f711429a..dbb3ea8b0 100644 --- a/sunspot_solr/lib/sunspot/solr/server.rb +++ b/sunspot_solr/lib/sunspot/solr/server.rb @@ -23,9 +23,8 @@ class Server #:nodoc: attr_writer :pid_dir, :pid_file, :solr_home, :solr_executable - def initialize(*args) - ensure_java_installed - super(*args) + def initialize + Sunspot::Solr::Java.ensure_install! end # @@ -195,16 +194,6 @@ def create_solr_directories private - def ensure_java_installed - unless defined?(@java_installed) - @java_installed = Sunspot::Solr::Java.installed? - unless @java_installed - raise JavaMissing.new("You need a Java Runtime Environment to run the Solr server") - end - end - @java_installed - end - def logging_config_path return @logging_config_path if defined?(@logging_config_path) @logging_config_path = diff --git a/sunspot_solr/spec/java_spec.rb b/sunspot_solr/spec/java_spec.rb new file mode 100644 index 000000000..7fbdf0f3a --- /dev/null +++ b/sunspot_solr/spec/java_spec.rb @@ -0,0 +1,58 @@ +require "spec_helper" + +describe Sunspot::Solr::Java do + + describe ".ensure_install!" do + subject { described_class.ensure_install! } + + context "when Java is installed" do + before { expect(described_class).to receive(:installed?) { true } } + it { should be true } + end + + context "when Java is not installed" do + before { expect(described_class).to receive(:installed?) { false } } + it "should raise a JavaMissing error" do + expect { subject }. + to raise_error Sunspot::Solr::Server::JavaMissing, /You need a Java/ + end + end + end + + describe ".installed?" do + subject { described_class.installed? } + + context "when Java can be found" do + let(:command) { system("echo") } + before do + expect(described_class).to receive(:system). + with("java", "-version", [:out, :err] => "/dev/null") { system("echo", out: "/dev/null") } + end + it { should be true } + end + + context "when Java cannot be found" do + before do + expect(described_class).to receive(:system). + with("java", "-version", [:out, :err] => "/dev/null") { system("some-command-not-found") } + end + it { should be false } + end + end + + describe ".null_device" do + subject { described_class.null_device } + + before { stub_const("RbConfig::CONFIG", { "host_os" => host_os }) } + + context "when the OS is Windows" do + let(:host_os) { "mswin32" } + it { should eq "NUL" } + end + + context "when the OS is not Windows" do + let(:host_os) { "darwin15.2.0" } + it { should eq "/dev/null" } + end + end +end