1 16 17 package org.springframework.beans.propertyeditors; 18 19 import java.util.Properties ; 20 21 import junit.framework.TestCase; 22 23 30 public class PropertiesEditorTests extends TestCase { 31 32 public void testOneProperty() { 33 String s = "foo=bar"; 34 PropertiesEditor pe= new PropertiesEditor(); 35 pe.setAsText(s); 36 Properties p= (Properties ) pe.getValue(); 37 assertTrue("contains one entry", p.entrySet().size() == 1); 38 assertTrue("foo=bar", p.get("foo").equals("bar")); 39 } 40 41 public void testTwoProperties() { 42 String s = "foo=bar with whitespace\n" + 43 "me=mi"; 44 PropertiesEditor pe= new PropertiesEditor(); 45 pe.setAsText(s); 46 Properties p= (Properties ) pe.getValue(); 47 assertTrue("contains two entries", p.entrySet().size() == 2); 48 assertTrue("foo=bar with whitespace", p.get("foo").equals("bar with whitespace")); 49 assertTrue("me=mi", p.get("me").equals("mi")); 50 } 51 52 public void testHandlesEqualsInValue() { 53 String s = "foo=bar\n" + 54 "me=mi\n" + 55 "x=y=z"; 56 PropertiesEditor pe= new PropertiesEditor(); 57 pe.setAsText(s); 58 Properties p= (Properties ) pe.getValue(); 59 assertTrue("contains two entries", p.entrySet().size() == 3); 60 assertTrue("foo=bar", p.get("foo").equals("bar")); 61 assertTrue("me=mi", p.get("me").equals("mi")); 62 assertTrue("x='y=z'", p.get("x").equals("y=z")); 63 } 64 65 public void testHandlesEmptyProperty() { 66 String s = "foo=bar\nme=mi\nx="; 67 PropertiesEditor pe= new PropertiesEditor(); 68 pe.setAsText(s); 69 Properties p= (Properties ) pe.getValue(); 70 assertTrue("contains two entries", p.entrySet().size() == 3); 71 assertTrue("foo=bar", p.get("foo").equals("bar")); 72 assertTrue("me=mi", p.get("me").equals("mi")); 73 assertTrue("x='y=z'", p.get("x").equals("")); 74 } 75 76 public void testHandlesEmptyPropertyWithoutEquals() { 77 String s = "foo\nme=mi\nx=x"; 78 PropertiesEditor pe= new PropertiesEditor(); 79 pe.setAsText(s); 80 Properties p= (Properties ) pe.getValue(); 81 assertTrue("contains three entries", p.entrySet().size() == 3); 82 assertTrue("foo is empty", p.get("foo").equals("")); 83 assertTrue("me=mi", p.get("me").equals("mi")); 84 } 85 86 89 public void testIgnoresCommentLinesAndEmptyLines() { 90 String s = "#Ignore this comment\n" + 91 "foo=bar\n" + 92 "#Another=comment more junk /\n" + 93 "me=mi\n" + 94 "x=x\n" + 95 "\n"; 96 PropertiesEditor pe= new PropertiesEditor(); 97 pe.setAsText(s); 98 Properties p= (Properties ) pe.getValue(); 99 assertTrue("contains three entries", p.entrySet().size() == 3); 100 assertTrue("foo is bar", p.get("foo").equals("bar")); 101 assertTrue("me=mi", p.get("me").equals("mi")); 102 } 103 104 110 public void testIgnoresLeadingSpacesAndTabs() { 111 String s = " #Ignore this comment\n" + 112 "\t\tfoo=bar\n" + 113 "\t#Another comment more junk \n" + 114 " me=mi\n" + 115 "x=x\n" + 116 "\n"; 117 PropertiesEditor pe= new PropertiesEditor(); 118 pe.setAsText(s); 119 Properties p= (Properties ) pe.getValue(); 120 assertTrue("contains 3 entries, not " + p.size(), p.size() == 3); 121 assertTrue("foo is bar", p.get("foo").equals("bar")); 122 assertTrue("me=mi", p.get("me").equals("mi")); 123 } 124 125 public void testNull() { 126 PropertiesEditor pe= new PropertiesEditor(); 127 try { 128 pe.setAsText(null); 129 fail("Should reject null"); 130 } 131 catch (IllegalArgumentException ex) { 132 } 134 } 135 136 public void testEmptyString() { 137 PropertiesEditor pe = new PropertiesEditor(); 138 pe.setAsText(""); 139 Properties p= (Properties ) pe.getValue(); 140 assertTrue("empty string means empty properties", p.isEmpty()); 141 } 142 143 } 144 | Popular Tags |