CustomEditorTestSuite.java
Upload User: jiancairen
Upload Date: 2007-08-27
Package Size: 26458k
Code Size: 17k
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;
  17. import java.beans.PropertyEditorSupport;
  18. import java.math.BigDecimal;
  19. import java.math.BigInteger;
  20. import java.text.NumberFormat;
  21. import java.util.Locale;
  22. import java.util.StringTokenizer;
  23. import junit.framework.TestCase;
  24. import org.springframework.beans.propertyeditors.CustomBooleanEditor;
  25. import org.springframework.beans.propertyeditors.CustomDateEditor;
  26. import org.springframework.beans.propertyeditors.CustomNumberEditor;
  27. import org.springframework.beans.propertyeditors.StringTrimmerEditor;
  28. /**
  29.  * @author Juergen Hoeller
  30.  * @since 10.06.2003
  31.  */
  32. public class CustomEditorTestSuite extends TestCase {
  33. public void testComplexObject() {
  34. TestBean t = new TestBean();
  35. String newName = "Rod";
  36. String tbString = "Kerry_34";
  37. try {
  38. BeanWrapper bw = new BeanWrapperImpl(t);
  39. bw.registerCustomEditor(ITestBean.class, null, new TestBeanEditor());
  40. MutablePropertyValues pvs = new MutablePropertyValues();
  41. pvs.addPropertyValue(new PropertyValue("age", new Integer(55)));
  42. pvs.addPropertyValue(new PropertyValue("name", newName));
  43. pvs.addPropertyValue(new PropertyValue("touchy", "valid"));
  44. pvs.addPropertyValue(new PropertyValue("spouse", tbString));
  45. bw.setPropertyValues(pvs);
  46. assertTrue("spouse is non-null", t.getSpouse() != null);
  47. assertTrue("spouse name is Kerry and age is 34", t.getSpouse().getName().equals("Kerry") && t.getSpouse().getAge() == 34);
  48. //assertTrue("Event source is correct", l.getEventCount() == 3);
  49. }
  50. catch (BeansException ex) {
  51. fail("Shouldn't throw exception when everything is valid: " + ex.getMessage());
  52. }
  53. }
  54. public void testCustomEditorForSingleProperty() {
  55. TestBean tb = new TestBean();
  56. BeanWrapper bw = new BeanWrapperImpl(tb);
  57. bw.registerCustomEditor(String.class, "name", new PropertyEditorSupport() {
  58. public void setAsText(String text) throws IllegalArgumentException {
  59. setValue("prefix" + text);
  60. }
  61. });
  62. try {
  63. bw.setPropertyValue("name", "value");
  64. bw.setPropertyValue("touchy", "value");
  65. }
  66. catch (BeansException ex) {
  67. fail("Should not throw BeansException: " + ex.getMessage());
  68. }
  69. assertEquals("prefixvalue", bw.getPropertyValue("name"));
  70. assertEquals("prefixvalue", tb.getName());
  71. assertEquals("value", bw.getPropertyValue("touchy"));
  72. assertEquals("value", tb.getTouchy());
  73. }
  74. public void testCustomEditorForAllStringProperties() {
  75. TestBean tb = new TestBean();
  76. BeanWrapper bw = new BeanWrapperImpl(tb);
  77. bw.registerCustomEditor(String.class, null, new PropertyEditorSupport() {
  78. public void setAsText(String text) throws IllegalArgumentException {
  79. setValue("prefix" + text);
  80. }
  81. });
  82. try {
  83. bw.setPropertyValue("name", "value");
  84. bw.setPropertyValue("touchy", "value");
  85. }
  86. catch (BeansException ex) {
  87. fail("Should not throw BeansException: " + ex.getMessage());
  88. }
  89. assertEquals("prefixvalue", bw.getPropertyValue("name"));
  90. assertEquals("prefixvalue", tb.getName());
  91. assertEquals("prefixvalue", bw.getPropertyValue("touchy"));
  92. assertEquals("prefixvalue", tb.getTouchy());
  93. }
  94. public void testCustomEditorForSingleNestedProperty() {
  95. TestBean tb = new TestBean();
  96. tb.setSpouse(new TestBean());
  97. BeanWrapper bw = new BeanWrapperImpl(tb);
  98. bw.registerCustomEditor(String.class, "spouse.name", new PropertyEditorSupport() {
  99. public void setAsText(String text) throws IllegalArgumentException {
  100. setValue("prefix" + text);
  101. }
  102. });
  103. try {
  104. bw.setPropertyValue("spouse.name", "value");
  105. bw.setPropertyValue("touchy", "value");
  106. }
  107. catch (BeansException ex) {
  108. fail("Should not throw BeansException: " + ex.getMessage());
  109. }
  110. assertEquals("prefixvalue", bw.getPropertyValue("spouse.name"));
  111. assertEquals("prefixvalue", tb.getSpouse().getName());
  112. assertEquals("value", bw.getPropertyValue("touchy"));
  113. assertEquals("value", tb.getTouchy());
  114. }
  115. public void testCustomEditorForAllNestedStringProperties() {
  116. TestBean tb = new TestBean();
  117. tb.setSpouse(new TestBean());
  118. BeanWrapper bw = new BeanWrapperImpl(tb);
  119. bw.registerCustomEditor(String.class, null, new PropertyEditorSupport() {
  120. public void setAsText(String text) throws IllegalArgumentException {
  121. setValue("prefix" + text);
  122. }
  123. });
  124. try {
  125. bw.setPropertyValue("spouse.name", "value");
  126. bw.setPropertyValue("touchy", "value");
  127. }
  128. catch (BeansException ex) {
  129. fail("Should not throw BeansException: " + ex.getMessage());
  130. }
  131. assertEquals("prefixvalue", bw.getPropertyValue("spouse.name"));
  132. assertEquals("prefixvalue", tb.getSpouse().getName());
  133. assertEquals("prefixvalue", bw.getPropertyValue("touchy"));
  134. assertEquals("prefixvalue", tb.getTouchy());
  135. }
  136. public void testBooleanPrimitiveEditor() {
  137. BooleanTestBean tb = new BooleanTestBean();
  138. BeanWrapper bw = new BeanWrapperImpl(tb);
  139. try {
  140. bw.setPropertyValue("bool1", "true");
  141. }
  142. catch (BeansException ex) {
  143. fail("Should not throw BeansException: " + ex.getMessage());
  144. }
  145. assertTrue("Correct bool1 value", Boolean.TRUE.equals(bw.getPropertyValue("bool1")));
  146. assertTrue("Correct bool1 value", tb.isBool1());
  147. try {
  148. bw.setPropertyValue("bool1", "false");
  149. }
  150. catch (BeansException ex) {
  151. fail("Should not throw BeansException: " + ex.getMessage());
  152. }
  153. assertTrue("Correct bool1 value", Boolean.FALSE.equals(bw.getPropertyValue("bool1")));
  154. assertTrue("Correct bool1 value", !tb.isBool1());
  155. try {
  156. bw.setPropertyValue("bool1", "argh");
  157. }
  158. catch (BeansException ex) {
  159. // expected
  160. return;
  161. }
  162. fail("Should have thrown BeansException");
  163. }
  164. public void testBooleanObjectEditorWithAllowEmpty() {
  165. BooleanTestBean tb = new BooleanTestBean();
  166. BeanWrapper bw = new BeanWrapperImpl(tb);
  167. bw.registerCustomEditor(Boolean.class, null, new CustomBooleanEditor(true));
  168. try {
  169. bw.setPropertyValue("bool2", "true");
  170. }
  171. catch (BeansException ex) {
  172. fail("Should not throw BeansException: " + ex.getMessage());
  173. }
  174. assertTrue("Correct bool2 value", Boolean.TRUE.equals(bw.getPropertyValue("bool2")));
  175. assertTrue("Correct bool2 value", tb.getBool2().booleanValue());
  176. try {
  177. bw.setPropertyValue("bool2", "false");
  178. }
  179. catch (BeansException ex) {
  180. fail("Should not throw BeansException: " + ex.getMessage());
  181. }
  182. assertTrue("Correct bool2 value", Boolean.FALSE.equals(bw.getPropertyValue("bool2")));
  183. assertTrue("Correct bool2 value", !tb.getBool2().booleanValue());
  184. try {
  185. bw.setPropertyValue("bool2", "");
  186. }
  187. catch (BeansException ex) {
  188. fail("Should not throw BeansException: " + ex.getMessage());
  189. }
  190. assertTrue("Correct bool2 value", bw.getPropertyValue("bool2") == null);
  191. assertTrue("Correct bool2 value", tb.getBool2() == null);
  192. }
  193. public void testBooleanObjectEditorWithoutAllowEmpty() {
  194. BooleanTestBean tb = new BooleanTestBean();
  195. BeanWrapper bw = new BeanWrapperImpl(tb);
  196. bw.registerCustomEditor(Boolean.class, null, new CustomBooleanEditor(false));
  197. try {
  198. bw.setPropertyValue("bool2", "true");
  199. }
  200. catch (BeansException ex) {
  201. fail("Should not throw BeansException: " + ex.getMessage());
  202. }
  203. assertTrue("Correct bool2 value", Boolean.TRUE.equals(bw.getPropertyValue("bool2")));
  204. assertTrue("Correct bool2 value", tb.getBool2().booleanValue());
  205. try {
  206. bw.setPropertyValue("bool2", "false");
  207. }
  208. catch (BeansException ex) {
  209. fail("Should not throw BeansException: " + ex.getMessage());
  210. }
  211. assertTrue("Correct bool2 value", Boolean.FALSE.equals(bw.getPropertyValue("bool2")));
  212. assertTrue("Correct bool2 value", !tb.getBool2().booleanValue());
  213. try {
  214. bw.setPropertyValue("bool2", "");
  215. }
  216. catch (BeansException ex) {
  217. // expected
  218. assertTrue("Correct bool2 value", bw.getPropertyValue("bool2") != null);
  219. assertTrue("Correct bool2 value", tb.getBool2() != null);
  220. return;
  221. }
  222. fail("Should have throw BeansException");
  223. }
  224. public void testNumberEditorWithoutAllowEmpty() {
  225. NumberFormat nf = NumberFormat.getNumberInstance(Locale.GERMAN);
  226. NumberTestBean tb = new NumberTestBean();
  227. BeanWrapper bw = new BeanWrapperImpl(tb);
  228. bw.registerCustomEditor(short.class, null, new CustomNumberEditor(Short.class, nf, false));
  229. bw.registerCustomEditor(Short.class, null, new CustomNumberEditor(Short.class, nf, false));
  230. bw.registerCustomEditor(int.class, null, new CustomNumberEditor(Short.class, nf, false));
  231. bw.registerCustomEditor(Integer.class, null, new CustomNumberEditor(Integer.class, nf, false));
  232. bw.registerCustomEditor(long.class, null, new CustomNumberEditor(Long.class, nf, false));
  233. bw.registerCustomEditor(Long.class, null, new CustomNumberEditor(Long.class, nf, false));
  234. bw.registerCustomEditor(BigInteger.class, null, new CustomNumberEditor(BigInteger.class, nf, false));
  235. bw.registerCustomEditor(float.class, null, new CustomNumberEditor(Float.class, nf, false));
  236. bw.registerCustomEditor(Float.class, null, new CustomNumberEditor(Float.class, nf, false));
  237. bw.registerCustomEditor(double.class, null, new CustomNumberEditor(Double.class, nf, false));
  238. bw.registerCustomEditor(Double.class, null, new CustomNumberEditor(Double.class, nf, false));
  239. bw.registerCustomEditor(BigDecimal.class, null, new CustomNumberEditor(BigDecimal.class, nf, false));
  240. try {
  241. bw.setPropertyValue("short1", "1");
  242. bw.setPropertyValue("short2", "2");
  243. bw.setPropertyValue("int1", "7");
  244. bw.setPropertyValue("int2", "8");
  245. bw.setPropertyValue("long1", "5");
  246. bw.setPropertyValue("long2", "6");
  247. bw.setPropertyValue("bigInteger", "3");
  248. bw.setPropertyValue("float1", "7,1");
  249. bw.setPropertyValue("float2", "8,1");
  250. bw.setPropertyValue("double1", "5,1");
  251. bw.setPropertyValue("double2", "6,1");
  252. bw.setPropertyValue("bigDecimal", "4");
  253. }
  254. catch (BeansException ex) {
  255. fail("Should not throw BeansException: " + ex.getMessage());
  256. }
  257. assertTrue("Correct short1 value", new Short("1").equals(bw.getPropertyValue("short1")));
  258. assertTrue("Correct short1 value", tb.getShort1() == 1);
  259. assertTrue("Correct short2 value", new Short("2").equals(bw.getPropertyValue("short2")));
  260. assertTrue("Correct short2 value", new Short("2").equals(tb.getShort2()));
  261. assertTrue("Correct int1 value", new Integer("7").equals(bw.getPropertyValue("int1")));
  262. assertTrue("Correct int1 value", tb.getInt1() == 7);
  263. assertTrue("Correct int2 value", new Integer("8").equals(bw.getPropertyValue("int2")));
  264. assertTrue("Correct int2 value", new Integer("8").equals(tb.getInt2()));
  265. assertTrue("Correct long1 value", new Long("5").equals(bw.getPropertyValue("long1")));
  266. assertTrue("Correct long1 value", tb.getLong1() == 5);
  267. assertTrue("Correct long2 value", new Long("6").equals(bw.getPropertyValue("long2")));
  268. assertTrue("Correct long2 value", new Long("6").equals(tb.getLong2()));
  269. assertTrue("Correct bigInteger value", new BigInteger("3").equals(bw.getPropertyValue("bigInteger")));
  270. assertTrue("Correct bigInteger value", new BigInteger("3").equals(tb.getBigInteger()));
  271. assertTrue("Correct float1 value", new Float("7.1").equals(bw.getPropertyValue("float1")));
  272. assertTrue("Correct float1 value", new Float("7.1").equals(new Float(tb.getFloat1())));
  273. assertTrue("Correct float2 value", new Float("8.1").equals(bw.getPropertyValue("float2")));
  274. assertTrue("Correct float2 value", new Float("8.1").equals(tb.getFloat2()));
  275. assertTrue("Correct double1 value", new Double("5.1").equals(bw.getPropertyValue("double1")));
  276. assertTrue("Correct double1 value", tb.getDouble1() == 5.1);
  277. assertTrue("Correct double2 value", new Double("6.1").equals(bw.getPropertyValue("double2")));
  278. assertTrue("Correct double2 value", new Double("6.1").equals(tb.getDouble2()));
  279. assertTrue("Correct bigDecimal value", new BigDecimal("4.0").equals(bw.getPropertyValue("bigDecimal")));
  280. assertTrue("Correct bigDecimal value", new BigDecimal("4.0").equals(tb.getBigDecimal()));
  281. }
  282. public void testNumberEditorsWithAllowEmpty() {
  283. NumberFormat nf = NumberFormat.getNumberInstance(Locale.GERMAN);
  284. NumberTestBean tb = new NumberTestBean();
  285. BeanWrapper bw = new BeanWrapperImpl(tb);
  286. bw.registerCustomEditor(long.class, null, new CustomNumberEditor(Long.class, nf, true));
  287. bw.registerCustomEditor(Long.class, null, new CustomNumberEditor(Long.class, nf, true));
  288. try {
  289. bw.setPropertyValue("long1", "5");
  290. bw.setPropertyValue("long2", "6");
  291. }
  292. catch (BeansException ex) {
  293. fail("Should not throw BeansException: " + ex.getMessage());
  294. }
  295. assertTrue("Correct long1 value", new Long("5").equals(bw.getPropertyValue("long1")));
  296. assertTrue("Correct long1 value", tb.getLong1() == 5);
  297. assertTrue("Correct long2 value", new Long("6").equals(bw.getPropertyValue("long2")));
  298. assertTrue("Correct long2 value", new Long("6").equals(tb.getLong2()));
  299. try {
  300. bw.setPropertyValue("long2", "");
  301. }
  302. catch (BeansException ex) {
  303. fail("Should not throw BeansException: " + ex.getMessage());
  304. }
  305. assertTrue("Correct long2 value", bw.getPropertyValue("long2") == null);
  306. assertTrue("Correct long2 value", tb.getLong2() == null);
  307. try {
  308. bw.setPropertyValue("long1", "");
  309. }
  310. catch (BeansException ex) {
  311. // expected
  312. assertTrue("Correct long1 value", new Long("5").equals(bw.getPropertyValue("long1")));
  313. assertTrue("Correct long1 value", tb.getLong1() == 5);
  314. return;
  315. }
  316. fail("Should have thrown BeansException");
  317. }
  318. public void testCustomBooleanEditor() {
  319. CustomBooleanEditor editor = new CustomBooleanEditor(false);
  320. editor.setAsText("true");
  321. assertEquals(Boolean.TRUE, editor.getValue());
  322. assertEquals("true", editor.getAsText());
  323. editor.setAsText("false");
  324. assertEquals(Boolean.FALSE, editor.getValue());
  325. assertEquals("false", editor.getAsText());
  326. editor.setValue(null);
  327. assertEquals(null, editor.getValue());
  328. assertEquals("", editor.getAsText());
  329. }
  330. public void testCustomBooleanEditorWithEmptyAsNull() {
  331. CustomBooleanEditor editor = new CustomBooleanEditor(true);
  332. editor.setAsText("true");
  333. assertEquals(Boolean.TRUE, editor.getValue());
  334. assertEquals("true", editor.getAsText());
  335. editor.setAsText("false");
  336. assertEquals(Boolean.FALSE, editor.getValue());
  337. assertEquals("false", editor.getAsText());
  338. editor.setValue(null);
  339. assertEquals(null, editor.getValue());
  340. assertEquals("", editor.getAsText());
  341. }
  342. public void testCustomDateEditor() {
  343. CustomDateEditor editor = new CustomDateEditor(null, false);
  344. editor.setValue(null);
  345. assertEquals(null, editor.getValue());
  346. assertEquals("", editor.getAsText());
  347. }
  348. public void testCustomDateEditorWithEmptyAsNull() {
  349. CustomDateEditor editor = new CustomDateEditor(null, true);
  350. editor.setValue(null);
  351. assertEquals(null, editor.getValue());
  352. assertEquals("", editor.getAsText());
  353. }
  354. public void testCustomNumberEditor() {
  355. CustomNumberEditor editor = new CustomNumberEditor(Integer.class, false);
  356. editor.setAsText("5");
  357. assertEquals(new Integer(5), editor.getValue());
  358. assertEquals("5", editor.getAsText());
  359. editor.setValue(null);
  360. assertEquals(null, editor.getValue());
  361. assertEquals("", editor.getAsText());
  362. }
  363. public void testCustomNumberEditorWithEmptyAsNull() {
  364. CustomNumberEditor editor = new CustomNumberEditor(Integer.class, true);
  365. editor.setAsText("5");
  366. assertEquals(new Integer(5), editor.getValue());
  367. assertEquals("5", editor.getAsText());
  368. editor.setAsText("");
  369. assertEquals(null, editor.getValue());
  370. assertEquals("", editor.getAsText());
  371. editor.setValue(null);
  372. assertEquals(null, editor.getValue());
  373. assertEquals("", editor.getAsText());
  374. }
  375. public void testStringTrimmerEditor() {
  376. StringTrimmerEditor editor = new StringTrimmerEditor(false);
  377. editor.setAsText("test");
  378. assertEquals("test", editor.getValue());
  379. assertEquals("test", editor.getAsText());
  380. editor.setAsText(" test ");
  381. assertEquals("test", editor.getValue());
  382. assertEquals("test", editor.getAsText());
  383. editor.setAsText("");
  384. assertEquals("", editor.getValue());
  385. assertEquals("", editor.getAsText());
  386. editor.setValue(null);
  387. assertEquals("", editor.getAsText());
  388. }
  389. public void testStringTrimmerEditorWithEmptyAsNull() {
  390. StringTrimmerEditor editor = new StringTrimmerEditor(true);
  391. editor.setAsText("test");
  392. assertEquals("test", editor.getValue());
  393. assertEquals("test", editor.getAsText());
  394. editor.setAsText(" test ");
  395. assertEquals("test", editor.getValue());
  396. assertEquals("test", editor.getAsText());
  397. editor.setAsText("");
  398. assertEquals(null, editor.getValue());
  399. assertEquals("", editor.getAsText());
  400. editor.setValue(null);
  401. assertEquals("", editor.getAsText());
  402. }
  403. private static class TestBeanEditor extends PropertyEditorSupport {
  404. public void setAsText(String text) {
  405. TestBean tb = new TestBean();
  406. StringTokenizer st = new StringTokenizer(text, "_");
  407. tb.setName(st.nextToken());
  408. tb.setAge(Integer.parseInt(st.nextToken()));
  409. setValue(tb);
  410. }
  411. }
  412. }