KeyedObjectTest.java 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /* ===========================================================
  2. * JFreeChart : a free chart library for the Java(tm) platform
  3. * ===========================================================
  4. *
  5. * (C) Copyright 2000-2013, by Object Refinery Limited and Contributors.
  6. *
  7. * Project Info: http://www.jfree.org/jfreechart/index.html
  8. *
  9. * This library is free software; you can redistribute it and/or modify it
  10. * under the terms of the GNU Lesser General Public License as published by
  11. * the Free Software Foundation; either version 2.1 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This library is distributed in the hope that it will be useful, but
  15. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  16. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  17. * License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with this library; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
  22. * USA.
  23. *
  24. * [Oracle and Java are registered trademarks of Oracle and/or its affiliates.
  25. * Other names may be trademarks of their respective owners.]
  26. *
  27. * --------------------
  28. * KeyedObjectTest.java
  29. * --------------------
  30. * (C) Copyright 2004-2013, by Object Refinery Limited.
  31. *
  32. * Original Author: David Gilbert (for Object Refinery Limited);
  33. * Contributor(s): -;
  34. *
  35. * Changes
  36. * -------
  37. * 27-Jan-2004 : Version 1 (DG);
  38. * 28-Sep-2007 : Added testCloning2() (DG);
  39. *
  40. */
  41. package org.jfree.data;
  42. import java.util.ArrayList;
  43. import org.jfree.chart.TestUtilities;
  44. import org.jfree.data.general.DefaultPieDataset;
  45. import static org.junit.Assert.assertEquals;
  46. import static org.junit.Assert.assertFalse;
  47. import static org.junit.Assert.assertTrue;
  48. import org.junit.Test;
  49. /**
  50. * Tests for the {@link KeyedObject} class.
  51. */
  52. public class KeyedObjectTest {
  53. /**
  54. * Confirm that the equals method can distinguish all the required fields.
  55. */
  56. @Test
  57. public void testEquals() {
  58. KeyedObject ko1 = new KeyedObject("Test", "Object");
  59. KeyedObject ko2 = new KeyedObject("Test", "Object");
  60. assertTrue(ko1.equals(ko2));
  61. assertTrue(ko2.equals(ko1));
  62. ko1 = new KeyedObject("Test 1", "Object");
  63. ko2 = new KeyedObject("Test 2", "Object");
  64. assertFalse(ko1.equals(ko2));
  65. ko1 = new KeyedObject("Test", "Object 1");
  66. ko2 = new KeyedObject("Test", "Object 2");
  67. assertFalse(ko1.equals(ko2));
  68. }
  69. /**
  70. * Confirm that cloning works.
  71. */
  72. @Test
  73. public void testCloning() throws CloneNotSupportedException {
  74. KeyedObject ko1 = new KeyedObject("Test", "Object");
  75. KeyedObject ko2 = (KeyedObject) ko1.clone();
  76. assertTrue(ko1 != ko2);
  77. assertTrue(ko1.getClass() == ko2.getClass());
  78. assertTrue(ko1.equals(ko2));
  79. }
  80. /**
  81. * Confirm special features of cloning.
  82. */
  83. @Test
  84. public void testCloning2() throws CloneNotSupportedException {
  85. // case 1 - object is mutable but not PublicCloneable
  86. Object obj1 = new ArrayList();
  87. KeyedObject ko1 = new KeyedObject("Test", obj1);
  88. KeyedObject ko2 = (KeyedObject) ko1.clone();
  89. assertTrue(ko1 != ko2);
  90. assertTrue(ko1.getClass() == ko2.getClass());
  91. assertTrue(ko1.equals(ko2));
  92. // the clone contains a reference to the original object
  93. assertTrue(ko2.getObject() == obj1);
  94. // CASE 2 - object is mutable AND PublicCloneable
  95. obj1 = new DefaultPieDataset();
  96. ko1 = new KeyedObject("Test", obj1);
  97. ko2 = (KeyedObject) ko1.clone();
  98. assertTrue(ko1 != ko2);
  99. assertTrue(ko1.getClass() == ko2.getClass());
  100. assertTrue(ko1.equals(ko2));
  101. // the clone contains a reference to a CLONE of the original object
  102. assertTrue(ko2.getObject() != obj1);
  103. }
  104. /**
  105. * Serialize an instance, restore it, and check for equality.
  106. */
  107. @Test
  108. public void testSerialization() {
  109. KeyedObject ko1 = new KeyedObject("Test", "Object");
  110. KeyedObject ko2 = (KeyedObject) TestUtilities.serialised(ko1);
  111. assertEquals(ko1, ko2);
  112. }
  113. }