Skip to content

Commit

Permalink
extracts java checking logic and tests it
Browse files Browse the repository at this point in the history
  • Loading branch information
tonyta committed Jun 12, 2016
1 parent 6c25285 commit e087842
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 17 deletions.
21 changes: 17 additions & 4 deletions 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
Expand Down
15 changes: 2 additions & 13 deletions sunspot_solr/lib/sunspot/solr/server.rb
Expand Up @@ -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

#
Expand Down Expand Up @@ -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 =
Expand Down
58 changes: 58 additions & 0 deletions 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

0 comments on commit e087842

Please sign in to comment.