KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > beans > propertyeditors > PropertiesEditorTests


1 /*
2  * Copyright 2002-2005 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.beans.propertyeditors;
18
19 import java.util.Properties JavaDoc;
20
21 import junit.framework.TestCase;
22
23 /**
24  * Test the conversion of Strings to java.util.Properties objects,
25  * and other property editors.
26  *
27  * @author Rod Johnson
28  * @author Juergen Hoeller
29  */

30 public class PropertiesEditorTests extends TestCase {
31
32     public void testOneProperty() {
33         String JavaDoc s = "foo=bar";
34         PropertiesEditor pe= new PropertiesEditor();
35         pe.setAsText(s);
36         Properties JavaDoc p= (Properties JavaDoc) 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 JavaDoc s = "foo=bar with whitespace\n" +
43             "me=mi";
44         PropertiesEditor pe= new PropertiesEditor();
45         pe.setAsText(s);
46         Properties JavaDoc p= (Properties JavaDoc) 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 JavaDoc s = "foo=bar\n" +
54             "me=mi\n" +
55             "x=y=z";
56         PropertiesEditor pe= new PropertiesEditor();
57         pe.setAsText(s);
58         Properties JavaDoc p= (Properties JavaDoc) 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 JavaDoc s = "foo=bar\nme=mi\nx=";
67         PropertiesEditor pe= new PropertiesEditor();
68         pe.setAsText(s);
69         Properties JavaDoc p= (Properties JavaDoc) 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 JavaDoc s = "foo\nme=mi\nx=x";
78         PropertiesEditor pe= new PropertiesEditor();
79         pe.setAsText(s);
80         Properties JavaDoc p= (Properties JavaDoc) 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     /**
87      * Comments begin with #
88      */

89     public void testIgnoresCommentLinesAndEmptyLines() {
90         String JavaDoc 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 JavaDoc p= (Properties JavaDoc) 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     /**
105      * We'll typically align by indenting with tabs or spaces.
106      * These should be ignored if at the beginning of a line.
107      * We must ensure that comment lines beginning with whitespace are
108      * still ignored: The standard syntax doesn't allow this on JDK 1.3.
109      */

110     public void testIgnoresLeadingSpacesAndTabs() {
111         String JavaDoc 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 JavaDoc p= (Properties JavaDoc) 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 JavaDoc ex) {
132             // OK
133
}
134     }
135     
136     public void testEmptyString() {
137         PropertiesEditor pe = new PropertiesEditor();
138         pe.setAsText("");
139         Properties JavaDoc p= (Properties JavaDoc) pe.getValue();
140         assertTrue("empty string means empty properties", p.isEmpty());
141     }
142
143 }
144
Popular Tags