PageRenderTime 49ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/src/test/java/com/google/code/simplestuff/bean/SimpleBeanTest.java

http://simplestuff.googlecode.com/
Java | 738 lines | 392 code | 129 blank | 217 comment | 1 complexity | 2539e7e9b130b7cccd7127fbdb56f559 MD5 | raw file
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package com.google.code.simplestuff.bean;
  18. import static junit.framework.Assert.assertEquals;
  19. import static junit.framework.Assert.assertFalse;
  20. import static junit.framework.Assert.assertTrue;
  21. import java.io.Serializable;
  22. import java.util.ArrayList;
  23. import java.util.Date;
  24. import java.util.HashSet;
  25. import java.util.List;
  26. import java.util.Set;
  27. import java.util.regex.Matcher;
  28. import java.util.regex.Pattern;
  29. import org.apache.commons.lang.SerializationUtils;
  30. import org.junit.Before;
  31. import org.junit.Test;
  32. import org.junit.internal.runners.JUnit4ClassRunner;
  33. import org.junit.runner.RunWith;
  34. import com.google.code.simplestuff.annotation.BusinessField;
  35. import com.google.code.simplestuff.annotation.BusinessObject;
  36. import com.google.code.simplestuff.bean.SimpleBeanTest.ParentClass.ParentEnum;
  37. /**
  38. * TestCase class that tests the {@link SimpleBean} class. This test implements
  39. * {@link Serializable} just for be able to use the {@link SerializationUtils}
  40. * class with the nested classes defined.
  41. *
  42. *
  43. * @author Vincenzo Vitale (vincenzo.vitale)
  44. * @since Jul 08, 2008
  45. *
  46. */
  47. @SuppressWarnings("serial")
  48. @RunWith(JUnit4ClassRunner.class)
  49. public class SimpleBeanTest implements Serializable {
  50. private static ParentClass testObjectOne;
  51. private static ParentClass testObjectTwo;
  52. @Before
  53. public void setUp() throws Exception {
  54. testObjectOne = new ParentClass();
  55. testObjectOne.setBooleanField(true);
  56. testObjectOne.setBooleanPrimitiveField(true);
  57. testObjectOne.setStringField("Test String");
  58. testObjectOne
  59. .setStringArrayField(new String[] { "One", "Two", "Three" });
  60. testObjectOne.setDateField(new Date());
  61. testObjectOne.setChilds(new HashSet<ChildClass>());
  62. testObjectOne.getChilds().add(
  63. new ChildClass("Child One String field", testObjectOne));
  64. testObjectOne.getChilds().add(
  65. new ChildClass("Child Two String field", testObjectOne));
  66. ArrayList<ParentEnum> enums = new ArrayList<ParentEnum>();
  67. enums.add(ParentEnum.ONE);
  68. enums.add(ParentEnum.THREE);
  69. testObjectOne.setEnums(enums);
  70. testObjectTwo = (ParentClass) SerializationUtils.clone(testObjectOne);
  71. }
  72. /**
  73. * Test method for
  74. * {@link com.tomtom.commons.bean.SimpleBean#equals(java.lang.Object, java.lang.Object)}
  75. * .
  76. */
  77. @Test
  78. public void testEquals() {
  79. assertTrue(testObjectOne.equals(testObjectTwo));
  80. }
  81. /**
  82. * Test method for
  83. * {@link com.tomtom.commons.bean.SimpleBean#equals(java.lang.Object, java.lang.Object)}
  84. * .
  85. */
  86. @Test
  87. public void testEqualsBetweenAppleAndTable() {
  88. assertTrue((new Apple()).equals(new Table()));
  89. }
  90. /**
  91. * Test method for
  92. * {@link com.tomtom.commons.bean.SimpleBean#equals(java.lang.Object, java.lang.Object)}
  93. * .
  94. */
  95. @Test
  96. public void testNotEqualsBetweenAppleAndChair() {
  97. assertFalse((new Apple()).equals(new Chair()));
  98. }
  99. /**
  100. * Test method for
  101. * {@link com.tomtom.commons.bean.SimpleBean#equals(java.lang.Object, java.lang.Object)}
  102. * .
  103. */
  104. @Test
  105. public void testNotEqualsBetweenTableAndChair() {
  106. assertFalse((new Table()).equals(new Chair()));
  107. }
  108. /**
  109. * Test method for
  110. * {@link com.tomtom.commons.bean.SimpleBean#hashCode(java.lang.Object)}.
  111. */
  112. @Test
  113. public void testHashCode() {
  114. assertEquals(testObjectOne.hashCode(), testObjectTwo.hashCode());
  115. }
  116. /**
  117. * Test method for
  118. * {@link com.tomtom.commons.bean.SimpleBean#toString(java.lang.Object)}.
  119. */
  120. @Test
  121. public void testToString() {
  122. assertEquals(replacePattern(testObjectOne.toString(),
  123. "@\\p{Alnum}+\\[", "["), replacePattern(testObjectTwo
  124. .toString(), "@\\p{Alnum}+\\[", "["));
  125. }
  126. /**
  127. * Test that the StackOverflow error is fixed when a proper getter is not
  128. * defined.
  129. */
  130. @Test
  131. public void testStackOverflowProblemFixed() {
  132. NoSuchMethodExceptionBean firstBean = new NoSuchMethodExceptionBean();
  133. firstBean.toString();
  134. }
  135. /**
  136. * This test makes sure that the utility can deal without problems with
  137. * normal not business objects.
  138. */
  139. @Test
  140. public void testWithBusinessObjectAndNoBusinessObjects() {
  141. Object firstBean = SimpleBean.getTestBean(ParentClass.class, null);
  142. Object secondBean = new Object();
  143. List<Object> list = new ArrayList<Object>();
  144. assertFalse(firstBean.equals(secondBean));
  145. assertFalse(firstBean.equals(list));
  146. }
  147. /**
  148. * This test makes sure that the utility can deal without problems with
  149. * normal not business objects.
  150. */
  151. @Test
  152. public void testWithNoBusinessObjects() {
  153. NoBusinessObjectOne noBusinessObjectOne = new NoBusinessObjectOne();
  154. NoBusinessObjectOneExtended noBusinessObjectOneExtended =
  155. new NoBusinessObjectOneExtended();
  156. NoBusinessObjectTwo noBusinessObjectTwo = new NoBusinessObjectTwo();
  157. assertFalse(noBusinessObjectOne.equals(null));
  158. assertTrue(noBusinessObjectOne.equals(noBusinessObjectOneExtended));
  159. assertTrue(noBusinessObjectOneExtended.equals(noBusinessObjectOne));
  160. assertTrue(noBusinessObjectOne.equals(noBusinessObjectOneExtended));
  161. assertFalse(noBusinessObjectTwo.equals(noBusinessObjectOne));
  162. assertFalse(noBusinessObjectTwo.equals(noBusinessObjectOneExtended));
  163. assertFalse(noBusinessObjectOne.equals(noBusinessObjectTwo));
  164. assertFalse(noBusinessObjectOneExtended.equals(noBusinessObjectTwo));
  165. }
  166. /**
  167. * Utility method for replace a pattern in a String.
  168. *
  169. * @param source the source string.
  170. * @param pattern the pattern to match.
  171. * @param replacement The replacement.
  172. * @return the result string.
  173. */
  174. private String replacePattern(String source, String pattern,
  175. String replacement) {
  176. Pattern p = Pattern.compile(pattern);
  177. Matcher m = p.matcher(source);
  178. StringBuffer sb = new StringBuffer();
  179. while (m.find()) {
  180. m.appendReplacement(sb, replacement);
  181. }
  182. m.appendTail(sb);
  183. return sb.toString();
  184. }
  185. /**
  186. * Test method for
  187. * {@link com.tomtom.commons.bean.SimpleBean#TestBean(Class)}.
  188. */
  189. @Test
  190. public void testGetTestBean() {
  191. ParentClass testBean = SimpleBean.getTestBean(ParentClass.class, null);
  192. assertEquals(true, testBean.isBooleanPrimitiveField());
  193. assertEquals(true, testBean.getBooleanField().booleanValue());
  194. assertEquals(10, testBean.getIntPrimitiveField());
  195. assertEquals(10, testBean.getIntField().intValue());
  196. assertEquals('t', testBean.getCharPrimitiveField());
  197. assertEquals('t', testBean.getCharField().charValue());
  198. assertEquals(10, testBean.getLongPrimitiveField());
  199. assertEquals(10.0, testBean.getLongField().doubleValue());
  200. assertEquals(10.0F, testBean.getFloatPrimitiveField());
  201. assertEquals(10.0F, testBean.getFloatField().floatValue());
  202. assertEquals(10, testBean.getBytePrimitiveField());
  203. assertEquals(10, testBean.getByteField().byteValue());
  204. assertEquals("testStringField", testBean.getStringField());
  205. }
  206. /**
  207. * Parent class for testing the {@link SimpleBean}.
  208. *
  209. * @author Vincenzo Vitale (vita)
  210. * @since Apr 26, 2007
  211. *
  212. */
  213. public static class ParentClass extends AbstractBusinessObject implements
  214. Serializable {
  215. public enum ParentEnum{
  216. ONE,
  217. TWO,
  218. THREE;
  219. }
  220. @BusinessField
  221. Boolean booleanField;
  222. @BusinessField
  223. boolean booleanPrimitiveField;
  224. @BusinessField
  225. int intPrimitiveField;
  226. @BusinessField
  227. Integer intField;
  228. @BusinessField
  229. char charPrimitiveField;
  230. @BusinessField
  231. Character charField;
  232. @BusinessField
  233. long longPrimitiveField;
  234. @BusinessField
  235. Long longField;
  236. @BusinessField
  237. float floatPrimitiveField;
  238. @BusinessField
  239. Float floatField;
  240. @BusinessField
  241. byte bytePrimitiveField;
  242. @BusinessField
  243. Byte byteField;
  244. @BusinessField
  245. String stringField;
  246. @BusinessField
  247. String[] stringArrayField;
  248. @BusinessField
  249. Date dateField;
  250. @BusinessField
  251. Set<ChildClass> childs;
  252. @BusinessField
  253. List<ParentEnum> enums;
  254. /**
  255. * @return the enums
  256. */
  257. public List<ParentEnum> getEnums() {
  258. return enums;
  259. }
  260. /**
  261. * @param enums the enums to set
  262. */
  263. public void setEnums(List<ParentEnum> enums) {
  264. this.enums = enums;
  265. }
  266. public Boolean getBooleanField() {
  267. return booleanField;
  268. }
  269. public void setBooleanField(Boolean booleanField) {
  270. this.booleanField = booleanField;
  271. }
  272. public boolean isBooleanPrimitiveField() {
  273. return booleanPrimitiveField;
  274. }
  275. public void setBooleanPrimitiveField(boolean booleanPrimitiveField) {
  276. this.booleanPrimitiveField = booleanPrimitiveField;
  277. }
  278. public Set<ChildClass> getChilds() {
  279. return childs;
  280. }
  281. public void setChilds(Set<ChildClass> childs) {
  282. this.childs = childs;
  283. }
  284. public String[] getStringArrayField() {
  285. return stringArrayField;
  286. }
  287. public void setStringArrayField(String[] stringArrayField) {
  288. this.stringArrayField = stringArrayField;
  289. }
  290. public String getStringField() {
  291. return stringField;
  292. }
  293. public void setStringField(String stringField) {
  294. this.stringField = stringField;
  295. }
  296. public Date getDateField() {
  297. return dateField;
  298. }
  299. public void setDateField(Date dateField) {
  300. this.dateField = dateField;
  301. }
  302. public Long getLongField() {
  303. return longField;
  304. }
  305. public void setLongField(Long longField) {
  306. this.longField = longField;
  307. }
  308. public long getLongPrimitiveField() {
  309. return longPrimitiveField;
  310. }
  311. public void setLongPrimitiveField(long longPrimitiveField) {
  312. this.longPrimitiveField = longPrimitiveField;
  313. }
  314. public int getIntPrimitiveField() {
  315. return intPrimitiveField;
  316. }
  317. public void setIntPrimitiveField(int intPrimitiveField) {
  318. this.intPrimitiveField = intPrimitiveField;
  319. }
  320. public Integer getIntField() {
  321. return intField;
  322. }
  323. public void setIntField(Integer intField) {
  324. this.intField = intField;
  325. }
  326. public char getCharPrimitiveField() {
  327. return charPrimitiveField;
  328. }
  329. public void setCharPrimitiveField(char charPrimitiveField) {
  330. this.charPrimitiveField = charPrimitiveField;
  331. }
  332. public Character getCharField() {
  333. return charField;
  334. }
  335. public void setCharField(Character charField) {
  336. this.charField = charField;
  337. }
  338. public float getFloatPrimitiveField() {
  339. return floatPrimitiveField;
  340. }
  341. public void setFloatPrimitiveField(float floatPrimitiveField) {
  342. this.floatPrimitiveField = floatPrimitiveField;
  343. }
  344. public Float getFloatField() {
  345. return floatField;
  346. }
  347. public void setFloatField(Float floatField) {
  348. this.floatField = floatField;
  349. }
  350. public byte getBytePrimitiveField() {
  351. return bytePrimitiveField;
  352. }
  353. public void setBytePrimitiveField(byte bytePrimitiveField) {
  354. this.bytePrimitiveField = bytePrimitiveField;
  355. }
  356. public Byte getByteField() {
  357. return byteField;
  358. }
  359. public void setByteField(Byte byteField) {
  360. this.byteField = byteField;
  361. }
  362. }
  363. /**
  364. * Child class for testing the {@link SimpleBean}.
  365. *
  366. * @author Vincenzo Vitale (vita)
  367. * @since Apr 26, 2007
  368. *
  369. */
  370. public static class ChildClass extends AbstractBusinessObject implements
  371. Serializable {
  372. @BusinessField
  373. String stringField;
  374. ParentClass parent;
  375. public ParentClass getParent() {
  376. return parent;
  377. }
  378. public void setParent(ParentClass parent) {
  379. this.parent = parent;
  380. }
  381. /**
  382. * @param stringField
  383. * @param parent
  384. */
  385. public ChildClass(String stringField, ParentClass parent) {
  386. super();
  387. this.stringField = stringField;
  388. this.parent = parent;
  389. }
  390. public String getStringField() {
  391. return stringField;
  392. }
  393. public void setStringField(String stringField) {
  394. this.stringField = stringField;
  395. }
  396. }
  397. /**
  398. * Apple class for testing the {@link SimpleBean} and the
  399. * includeClassAsBusinessField behaviour.
  400. *
  401. * @author Vincenzo Vitale (vita)
  402. * @since Apr 26, 2007
  403. *
  404. */
  405. @BusinessObject(includeClassAsBusinessField = false)
  406. public static class Apple extends AbstractBusinessObject implements
  407. Serializable {
  408. @BusinessField
  409. String color = "red";
  410. /**
  411. * @return the color
  412. */
  413. public String getColor() {
  414. return color;
  415. }
  416. /**
  417. * @param color the color to set
  418. */
  419. public void setColor(String color) {
  420. this.color = color;
  421. }
  422. }
  423. /**
  424. * Apple class for testing the {@link SimpleBean} and the
  425. * includeClassAsBusinessField behavior.
  426. *
  427. * @author Vincenzo Vitale (vita)
  428. * @since Apr 26, 2007
  429. *
  430. */
  431. @BusinessObject(includeClassAsBusinessField = false)
  432. public static class Table extends AbstractBusinessObject implements
  433. Serializable {
  434. @BusinessField
  435. private String color = "red";
  436. /**
  437. * @return the color
  438. */
  439. public String getColor() {
  440. return color;
  441. }
  442. /**
  443. * @param color the color to set
  444. */
  445. public void setColor(String color) {
  446. this.color = color;
  447. }
  448. }
  449. /**
  450. * Apple class for testing the {@link SimpleBean} and the
  451. * includeClassAsBusinessField behavior.
  452. *
  453. * @author Vincenzo Vitale (vita)
  454. * @since Apr 26, 2007
  455. *
  456. */
  457. @BusinessObject(includeClassAsBusinessField = true)
  458. public static class Chair extends AbstractBusinessObject implements
  459. Serializable {
  460. @BusinessField
  461. private String color = "red";
  462. /**
  463. * @return the color
  464. */
  465. public String getColor() {
  466. return color;
  467. }
  468. /**
  469. * @param color the color to set
  470. */
  471. public void setColor(String color) {
  472. this.color = color;
  473. }
  474. }
  475. /**
  476. * This bena should generate a {@link NoSuchMethodException} in ternally in
  477. * Simplestuff. There was a bug in the toString method when this was
  478. * happening generating a {@link StackOverflowError}. This test tests it was
  479. * fixed.
  480. *
  481. * @author Vincenzo Vitale (vita)
  482. * @since Apr 26, 2007
  483. *
  484. */
  485. public static class NoSuchMethodExceptionBean extends
  486. AbstractBusinessObject implements Serializable {
  487. @BusinessField
  488. private String color = "red";
  489. /**
  490. * @return the color
  491. */
  492. public String getColorWrongGetter() {
  493. return color;
  494. }
  495. /**
  496. * @param color the color to set
  497. */
  498. public void setColor(String color) {
  499. this.color = color;
  500. }
  501. }
  502. /**
  503. * A generic class not annotated as a Business Object.
  504. *
  505. * @author Vincenzo Vitale (vita)
  506. * @since Jul 22, 2009
  507. *
  508. */
  509. public static class NoBusinessObjectOne implements Serializable {
  510. private String myString = "vicio";
  511. /**
  512. * @return the myString
  513. */
  514. public String getMyString() {
  515. return myString;
  516. }
  517. /**
  518. * @param myString the myString to set
  519. */
  520. public void setMyString(String myString) {
  521. this.myString = myString;
  522. }
  523. @Override
  524. public boolean equals(Object other) {
  525. return SimpleBean.equals(this, other);
  526. }
  527. /*
  528. * (non-Javadoc)
  529. *
  530. * @see java.lang.Object#hashCode()
  531. */
  532. @Override
  533. public int hashCode() {
  534. return SimpleBean.hashCode(this);
  535. }
  536. /*
  537. * (non-Javadoc)
  538. *
  539. * @see java.lang.Object#toString()
  540. */
  541. @Override
  542. public String toString() {
  543. return SimpleBean.toString(this);
  544. }
  545. }
  546. /**
  547. * An extension of the {@link NoBusinessObjectOne} class.
  548. *
  549. * @author Vincenzo Vitale (vita)
  550. * @since Jul 22, 2009
  551. *
  552. */
  553. public static class NoBusinessObjectOneExtended extends NoBusinessObjectOne
  554. implements Serializable {
  555. @Override
  556. public boolean equals(Object other) {
  557. return SimpleBean.equals(this, other);
  558. }
  559. /*
  560. * (non-Javadoc)
  561. *
  562. * @see java.lang.Object#hashCode()
  563. */
  564. @Override
  565. public int hashCode() {
  566. return SimpleBean.hashCode(this);
  567. }
  568. /*
  569. * (non-Javadoc)
  570. *
  571. * @see java.lang.Object#toString()
  572. */
  573. @Override
  574. public String toString() {
  575. return SimpleBean.toString(this);
  576. }
  577. }
  578. /**
  579. * A generic class not annotated as a Business Object.
  580. *
  581. * @author Vincenzo Vitale (vita)
  582. * @since Jul 22, 2009
  583. *
  584. */
  585. public static class NoBusinessObjectTwo implements Serializable {
  586. @Override
  587. public boolean equals(Object other) {
  588. return SimpleBean.equals(this, other);
  589. }
  590. /*
  591. * (non-Javadoc)
  592. *
  593. * @see java.lang.Object#hashCode()
  594. */
  595. @Override
  596. public int hashCode() {
  597. return SimpleBean.hashCode(this);
  598. }
  599. /*
  600. * (non-Javadoc)
  601. *
  602. * @see java.lang.Object#toString()
  603. */
  604. @Override
  605. public String toString() {
  606. return SimpleBean.toString(this);
  607. }
  608. }
  609. }