Author: assaf
Date: Tue Jul 29 12:50:42 2008
New Revision: 680818

URL: http://svn.apache.org/viewvc?rev=680818&view=rev
Log:
Fixed: BUILDR-114 Hash.from_java_properties does not behave like 
java.util.Properties (Lacton).

Modified:
    incubator/buildr/trunk/CHANGELOG
    incubator/buildr/trunk/lib/buildr/core/util.rb
    incubator/buildr/trunk/spec/common_spec.rb

Modified: incubator/buildr/trunk/CHANGELOG
URL: 
http://svn.apache.org/viewvc/incubator/buildr/trunk/CHANGELOG?rev=680818&r1=680817&r2=680818&view=diff
==============================================================================
--- incubator/buildr/trunk/CHANGELOG (original)
+++ incubator/buildr/trunk/CHANGELOG Tue Jul 29 12:50:42 2008
@@ -14,6 +14,8 @@
 * Fixed: BUILDR-110 Error creating buildfile from POM when missing plugin
 configuration (Geoffrey Ruscoe).
 * Fixed: BUILDR-112 Using a user gem repository with 'rake setup' (Lacton).
+* Fixed: BUILDR-114 Hash.from_java_properties does not behave
+like java.util.Properties (Lacton).
 * Docs: BUILDR-111 Troubleshoot tip when Buildr's bin directory shows up in
 RUBYLIB (Geoffrey Ruscoe).
 

Modified: incubator/buildr/trunk/lib/buildr/core/util.rb
URL: 
http://svn.apache.org/viewvc/incubator/buildr/trunk/lib/buildr/core/util.rb?rev=680818&r1=680817&r2=680818&view=diff
==============================================================================
--- incubator/buildr/trunk/lib/buildr/core/util.rb (original)
+++ incubator/buildr/trunk/lib/buildr/core/util.rb Tue Jul 29 12:50:42 2008
@@ -187,10 +187,16 @@
     #   Hash.from_properties(str)
     #   => { 'foo'=>'bar', 'baz'=>'fab' }.to_properties
     def from_java_properties(string)
-      string.gsub(/\\\n/, '').split("\n").select { |line| line =~ /^[^#].*=.*/ 
}.
-        map { |line| line.gsub(/\\[trnf\\]/) { |escaped| {?t=>"\t", ?r=>"\r", 
?n=>"\n", ?f=>"\f", ?\\=>"\\"}[escaped[1]] } }.
-        map { |line| line.split('=') }.
-        inject({}) { |hash, (name, value)| hash.merge(name=>value) }
+      hash = {}
+      input_stream = Java.java.io.StringBufferInputStream.new(string)
+      java_properties = Java.java.util.Properties.new
+      java_properties.load input_stream
+      keys = java_properties.keySet.iterator
+      while keys.hasNext do
+        key = keys.next.toString
+        hash[key] = java_properties.getProperty(key)
+      end
+      hash
     end
 
   end

Modified: incubator/buildr/trunk/spec/common_spec.rb
URL: 
http://svn.apache.org/viewvc/incubator/buildr/trunk/spec/common_spec.rb?rev=680818&r1=680817&r2=680818&view=diff
==============================================================================
--- incubator/buildr/trunk/spec/common_spec.rb (original)
+++ incubator/buildr/trunk/spec/common_spec.rb Tue Jul 29 12:50:42 2008
@@ -573,9 +573,14 @@
 
 name2=with\\nand\f
 
-name3=double\\hash
+name3=double\\\\hash
 PROPS
-    hash.should == {'name1'=>"with\tand\r", 'name2'=>"with\nand\f", 
'name3'=>"double\\hash"}
+    hash.should == {'name1'=>"with\tand", 'name2'=>"with\nand\f", 
'name3'=>'double\hash'}
+  end
+  
+  it 'should ignore whitespace' do
+    hash = Hash.from_java_properties('name1 = value1')
+    hash.should == {'name1'=>'value1'}
   end
 end
 
@@ -589,7 +594,7 @@
   end
 
   it 'should handle \t, \r, \n and \f' do
-    props = {'name1'=>"with\tand\r", 'name2'=>"with\nand\f", 
'name3'=>"double\\hash"}.to_java_properties
+    props = {'name1'=>"with\tand\r", 'name2'=>"with\nand\f", 
'name3'=>'double\hash'}.to_java_properties
     props.split("\n").should include("name1=with\\tand\\r")
     props.split("\n").should include("name2=with\\nand\\f")
     props.split("\n").should include("name3=double\\\\hash")


Reply via email to