PropertyEditorTestSuite.java
Upload User: jiancairen
Upload Date: 2007-08-27
Package Size: 26458k
Code Size: 6k
Category:

Java Develop

Development Platform:

Java

  1. /*
  2.  * Copyright 2002-2004 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. package org.springframework.beans.propertyeditors;
  17. import java.beans.PropertyEditor;
  18. import java.util.Locale;
  19. import java.util.Properties;
  20. import java.io.File;
  21. import junit.framework.TestCase;
  22. import org.springframework.beans.TestBean;
  23. /**
  24.  * Test the conversion of Strings to java.util.Properties objects,
  25.  * and other property editors.
  26.  * @author Rod Johnson
  27.  * @author Juergen Hoeller
  28.  */
  29. public class PropertyEditorTestSuite extends TestCase {
  30. public void testOneProperty() {
  31. String s = "foo=bar";
  32. PropertiesEditor pe= new PropertiesEditor();
  33. pe.setAsText(s);
  34. Properties p= (Properties) pe.getValue();
  35. assertTrue("contains one entry", p.entrySet().size() == 1);
  36. assertTrue("foo=bar", p.get("foo").equals("bar"));
  37. }
  38. public void testTwoProperties() {
  39. String s = "foo=bar with whitespacen" +
  40. "me=mi";
  41. PropertiesEditor pe= new PropertiesEditor();
  42. pe.setAsText(s);
  43. Properties p= (Properties) pe.getValue();
  44. assertTrue("contains two entries", p.entrySet().size() == 2);
  45. assertTrue("foo=bar with whitespace", p.get("foo").equals("bar with whitespace"));
  46. assertTrue("me=mi", p.get("me").equals("mi"));
  47. }
  48. public void testHandlesEqualsInValue() {
  49. String s = "foo=barn" +
  50. "me=min" +
  51. "x=y=z";
  52. PropertiesEditor pe= new PropertiesEditor();
  53. pe.setAsText(s);
  54. Properties p= (Properties) pe.getValue();
  55. assertTrue("contains two entries", p.entrySet().size() == 3);
  56. assertTrue("foo=bar", p.get("foo").equals("bar"));
  57. assertTrue("me=mi", p.get("me").equals("mi"));
  58. assertTrue("x='y=z'", p.get("x").equals("y=z"));
  59. }
  60. public void testHandlesEmptyProperty() {
  61. String s = "foo=barnme=minx=";
  62. PropertiesEditor pe= new PropertiesEditor();
  63. pe.setAsText(s);
  64. Properties p= (Properties) pe.getValue();
  65. assertTrue("contains two entries", p.entrySet().size() == 3);
  66. assertTrue("foo=bar", p.get("foo").equals("bar"));
  67. assertTrue("me=mi", p.get("me").equals("mi"));
  68. assertTrue("x='y=z'", p.get("x").equals(""));
  69. }
  70. public void testHandlesEmptyPropertyWithoutEquals() {
  71. String s = "foonme=minx=x";
  72. PropertiesEditor pe= new PropertiesEditor();
  73. pe.setAsText(s);
  74. Properties p= (Properties) pe.getValue();
  75. assertTrue("contains three entries", p.entrySet().size() == 3);
  76. assertTrue("foo is empty", p.get("foo").equals(""));
  77. assertTrue("me=mi", p.get("me").equals("mi"));
  78. }
  79. /**
  80.  * Comments begin with #
  81.  *
  82.  */
  83. public void testIgnoresCommentLinesAndEmptyLines() {
  84. String s = "#Ignore this commentn" +
  85. "foo=barn" +
  86. "#Another=comment more junk n" +
  87. "me=min" +
  88. "x=xn" +
  89. "n";
  90. PropertiesEditor pe= new PropertiesEditor();
  91. pe.setAsText(s);
  92. Properties p= (Properties) pe.getValue();
  93. assertTrue("contains three entries", p.entrySet().size() == 3);
  94. assertTrue("foo is bar", p.get("foo").equals("bar"));
  95. assertTrue("me=mi", p.get("me").equals("mi"));
  96. }
  97. /**
  98.  * We'll typically align by indenting with tabs or
  99.  * spaces. These should be ignored if at the beginning of a line.
  100.  * We must ensure that comment lines beginning with whitespace
  101.  * are still ignored: the standard syntax doesn't allow this.
  102.  */
  103. public void testIgnoresLeadingSpacesAndTabs() {
  104. String s = "    #Ignore this commentn" +
  105. "ttfoo=barn" +
  106. "t#Another comment more junk n" +
  107. " me=min" +
  108. "x=xn" +
  109. "n";
  110. PropertiesEditor pe= new PropertiesEditor();
  111. pe.setAsText(s);
  112. Properties p= (Properties) pe.getValue();
  113. //p.list(System.out);
  114. assertTrue("contains 3 entries, not " + p.size(), p.size() == 3);
  115. assertTrue("foo is bar", p.get("foo").equals("bar"));
  116. assertTrue("me=mi", p.get("me").equals("mi"));
  117. }
  118. public void testNull() {
  119. PropertiesEditor pe= new PropertiesEditor();
  120. try {
  121. pe.setAsText(null);
  122. fail("Should reject null");
  123. }
  124. catch (IllegalArgumentException ex) {
  125. // OK
  126. }
  127. }
  128. public void testEmptyString() {
  129. PropertiesEditor pe= new PropertiesEditor();
  130. pe.setAsText("");
  131. Properties p= (Properties) pe.getValue();
  132. assertTrue("empty string means empty properties", p.isEmpty());
  133. }
  134. public void testClassEditor() {
  135. PropertyEditor classEditor = new ClassEditor();
  136. classEditor.setAsText("org.springframework.beans.TestBean");
  137. assertEquals(TestBean.class, classEditor.getValue());
  138. assertEquals("org.springframework.beans.TestBean", classEditor.getAsText());
  139. }
  140. public void testClassEditorWithArray() {
  141. PropertyEditor classEditor = new ClassEditor();
  142. classEditor.setAsText("org.springframework.beans.TestBean[]");
  143. assertEquals(TestBean[].class, classEditor.getValue());
  144. assertEquals("org.springframework.beans.TestBean[]", classEditor.getAsText());
  145. }
  146. public void testFileEditor() {
  147. PropertyEditor fileEditor = new FileEditor();
  148. fileEditor.setAsText("C:/test/myfile.txt");
  149. assertEquals(new File("C:/test/myfile.txt"), fileEditor.getValue());
  150. assertEquals((new File("C:/test/myfile.txt")).getAbsolutePath(), fileEditor.getAsText());
  151. }
  152. public void testLocaleEditor() {
  153. PropertyEditor localeEditor = new LocaleEditor();
  154. localeEditor.setAsText("en_CA");
  155. assertEquals(Locale.CANADA, localeEditor.getValue());
  156. assertEquals("en_CA", localeEditor.getAsText());
  157. }
  158. }