diff --git a/lib/logstash/java_integration.rb b/lib/logstash/java_integration.rb index f3493abc5..670ceaae6 100644 --- a/lib/logstash/java_integration.rb +++ b/lib/logstash/java_integration.rb @@ -81,11 +81,17 @@ module java::util::Collection def compact duped = Java::JavaUtil::ArrayList.new(self) duped.compact! + duped end def compact! + size_before = self.size self.removeAll(java::util::Collections.singleton(nil)) - self + if size_before == self.size + nil + else + self + end end # support the Ruby intersection method on Java Collection diff --git a/rakelib/test.rake b/rakelib/test.rake index 1e5a34082..db4083d14 100644 --- a/rakelib/test.rake +++ b/rakelib/test.rake @@ -29,6 +29,11 @@ namespace "test" do exit(Spec::Core::Runner.run(["--fail-fast", Rake::FileList["spec/**/*_spec.rb"]])) end + desc "run core specs on a single file" + task "core-single-file", [:specfile] => ["setup"] do |t,args| + exit(RSpec::Core::Runner.run([Rake::FileList[args.specfile]])) + end + desc "run all installed plugins specs" task "plugins" => ["setup"] do # grab all spec files using the live plugins gem specs. this allows correclty also running the specs diff --git a/spec/lib/logstash/java_integration_spec.rb b/spec/lib/logstash/java_integration_spec.rb index a86ce6b38..e4b36a2bd 100644 --- a/spec/lib/logstash/java_integration_spec.rb +++ b/spec/lib/logstash/java_integration_spec.rb @@ -211,6 +211,36 @@ describe "Java integration" do end end end + + context "when compacting" do + context "#compact with nils" do + let(:initial_array) { [1,2,3,nil,nil,6] } + it "should remove nil values from a copy" do + expect(subject.compact).to eq([1,2,3,6]) + expect(subject).to eq([1,2,3,nil,nil,6]) + end + end + + context "#compact! with nils" do + let(:initial_array) { [1,2,3,nil,nil,6] } + it "should remove nil values" do + expect(subject.compact!).to eq([1,2,3,6]) + expect(subject).to eq([1,2,3,6]) + end + + it "should return the original" do + expect(subject.compact!.object_id).to eq(subject.object_id) + end + end + + context "#compact! without nils" do + let(:initial_array) { [1,2,3,6] } + it "should return nil" do + expect(subject.compact!).to be nil + expect(subject).to eq([1,2,3,6]) + end + end + end end context "Enumerable implementation" do