DefaultPieDatasetTest.java 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. * PieDatasetTest.java
  29. * -------------------
  30. * (C) Copyright 2003-2013, by Object Refinery Limited and Contributors.
  31. *
  32. * Original Author: David Gilbert (for Object Refinery Limited);
  33. * Contributor(s): -;
  34. *
  35. * Changes
  36. * -------
  37. * 18-Aug-2003 : Version 1 (DG);
  38. * 31-Jul-2006 : Added test for new clear() method (DG);
  39. * 01-Aug-2006 : Added testGetKey() and testGetIndex() methods (DG);
  40. *
  41. */
  42. package org.jfree.data.general;
  43. import org.jfree.chart.TestUtilities;
  44. import org.junit.Test;
  45. import static org.junit.Assert.assertTrue;
  46. import static org.junit.Assert.assertEquals;
  47. import static org.junit.Assert.assertNull;
  48. import static org.junit.Assert.assertNotNull;
  49. /**
  50. * Tests for the {@link org.jfree.data.general.PieDataset} class.
  51. */
  52. public class DefaultPieDatasetTest implements DatasetChangeListener {
  53. private DatasetChangeEvent lastEvent;
  54. /**
  55. * Records the last event.
  56. *
  57. * @param event the last event.
  58. */
  59. @Override
  60. public void datasetChanged(DatasetChangeEvent event) {
  61. this.lastEvent = event;
  62. }
  63. /**
  64. * Some tests for the clear() method.
  65. */
  66. @Test
  67. public void testClear() {
  68. DefaultPieDataset d = new DefaultPieDataset();
  69. d.addChangeListener(this);
  70. // no event is generated if the dataset is already empty
  71. d.clear();
  72. assertNull(this.lastEvent);
  73. d.setValue("A", 1.0);
  74. assertEquals(1, d.getItemCount());
  75. this.lastEvent = null;
  76. d.clear();
  77. assertNotNull(this.lastEvent);
  78. assertEquals(0, d.getItemCount());
  79. }
  80. /**
  81. * Some checks for the getKey(int) method.
  82. */
  83. public void testGetKey() {
  84. DefaultPieDataset d = new DefaultPieDataset();
  85. d.setValue("A", 1.0);
  86. d.setValue("B", 2.0);
  87. assertEquals("A", d.getKey(0));
  88. assertEquals("B", d.getKey(1));
  89. boolean pass = false;
  90. try {
  91. d.getKey(-1);
  92. }
  93. catch (IndexOutOfBoundsException e) {
  94. pass = true;
  95. }
  96. assertTrue(pass);
  97. pass = false;
  98. try {
  99. d.getKey(2);
  100. }
  101. catch (IndexOutOfBoundsException e) {
  102. pass = true;
  103. }
  104. assertTrue(pass);
  105. }
  106. /**
  107. * Some checks for the getIndex() method.
  108. */
  109. public void testGetIndex() {
  110. DefaultPieDataset d = new DefaultPieDataset();
  111. d.setValue("A", 1.0);
  112. d.setValue("B", 2.0);
  113. assertEquals(0, d.getIndex("A"));
  114. assertEquals(1, d.getIndex("B"));
  115. assertEquals(-1, d.getIndex("XX"));
  116. boolean pass = false;
  117. try {
  118. d.getIndex(null);
  119. }
  120. catch (IllegalArgumentException e) {
  121. pass = true;
  122. }
  123. assertTrue(pass);
  124. }
  125. /**
  126. * Confirm that cloning works.
  127. */
  128. public void testCloning() throws CloneNotSupportedException {
  129. DefaultPieDataset d1 = new DefaultPieDataset();
  130. d1.setValue("V1", new Integer(1));
  131. d1.setValue("V2", null);
  132. d1.setValue("V3", new Integer(3));
  133. DefaultPieDataset d2 = (DefaultPieDataset) d1.clone();
  134. assertTrue(d1 != d2);
  135. assertTrue(d1.getClass() == d2.getClass());
  136. assertTrue(d1.equals(d2));
  137. }
  138. /**
  139. * Serialize an instance, restore it, and check for equality.
  140. */
  141. public void testSerialization() {
  142. DefaultPieDataset d1 = new DefaultPieDataset();
  143. d1.setValue("C1", new Double(234.2));
  144. d1.setValue("C2", null);
  145. d1.setValue("C3", new Double(345.9));
  146. d1.setValue("C4", new Double(452.7));
  147. DefaultPieDataset d2 = (DefaultPieDataset) TestUtilities.serialised(d1);
  148. assertEquals(d1, d2);
  149. }
  150. }