PaintMapTest.java 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. * PaintMapTest.java
  29. * -----------------
  30. * (C) Copyright 2006-2013, by Object Refinery Limited.
  31. *
  32. * Original Author: David Gilbert (for Object Refinery Limited);
  33. * Contributor(s): -;
  34. *
  35. * Changes:
  36. * --------
  37. * 27-Sep-2006 : Version 1 (DG);
  38. * 17-Jan-2007 : Added testKeysOfDifferentClasses() (DG);
  39. *
  40. */
  41. package org.jfree.chart;
  42. import org.junit.Test;
  43. import java.awt.Color;
  44. import java.awt.GradientPaint;
  45. import static org.junit.Assert.assertEquals;
  46. import static org.junit.Assert.assertFalse;
  47. import static org.junit.Assert.fail;
  48. /**
  49. * Some tests for the {@link PaintMap} class.
  50. */
  51. public class PaintMapTest {
  52. /**
  53. * Some checks for the getPaint() method.
  54. */
  55. @Test
  56. public void testGetPaint() {
  57. PaintMap m1 = new PaintMap();
  58. assertEquals(null, m1.getPaint("A"));
  59. m1.put("A", Color.red);
  60. assertEquals(Color.red, m1.getPaint("A"));
  61. m1.put("A", null);
  62. assertEquals(null, m1.getPaint("A"));
  63. // a null key should throw an IllegalArgumentException
  64. try {
  65. m1.getPaint(null);
  66. fail("IllegalArgumentException should have been thrown on passing null value");
  67. }
  68. catch (IllegalArgumentException e) {
  69. assertEquals("Null 'key' argument.", e.getMessage());
  70. }
  71. }
  72. /**
  73. * Some checks for the put() method.
  74. */
  75. @Test
  76. public void testPut() {
  77. PaintMap m1 = new PaintMap();
  78. m1.put("A", Color.red);
  79. assertEquals(Color.red, m1.getPaint("A"));
  80. // a null key should throw an IllegalArgumentException
  81. try {
  82. m1.put(null, Color.blue);
  83. fail("IllegalArgumentException should have been thrown on null key");
  84. }
  85. catch (IllegalArgumentException e) {
  86. assertEquals("Null 'key' argument.", e.getMessage());
  87. }
  88. }
  89. /**
  90. * Some checks for the equals() method.
  91. */
  92. @Test
  93. public void testEquals() {
  94. PaintMap m1 = new PaintMap();
  95. PaintMap m2 = new PaintMap();
  96. assertEquals(m1, m1);
  97. assertEquals(m1, m2);
  98. assertFalse(m1.equals(null));
  99. assertFalse(m1.equals("ABC"));
  100. m1.put("K1", Color.red);
  101. assertFalse(m1.equals(m2));
  102. m2.put("K1", Color.red);
  103. assertEquals(m1, m2);
  104. m1.put("K2", new GradientPaint(1.0f, 2.0f, Color.green, 3.0f, 4.0f,
  105. Color.yellow));
  106. assertFalse(m1.equals(m2));
  107. m2.put("K2", new GradientPaint(1.0f, 2.0f, Color.green, 3.0f, 4.0f,
  108. Color.yellow));
  109. assertEquals(m1, m2);
  110. m1.put("K2", null);
  111. assertFalse(m1.equals(m2));
  112. m2.put("K2", null);
  113. assertEquals(m1, m2);
  114. }
  115. /**
  116. * Some checks for cloning.
  117. */
  118. @Test
  119. public void testCloning() throws CloneNotSupportedException {
  120. PaintMap m1 = new PaintMap();
  121. PaintMap m2 = (PaintMap) m1.clone();
  122. assertEquals(m1, m2);
  123. m1.put("K1", Color.red);
  124. m1.put("K2", new GradientPaint(1.0f, 2.0f, Color.green, 3.0f, 4.0f,
  125. Color.yellow));
  126. m2 = (PaintMap) m1.clone();
  127. assertEquals(m1, m2);
  128. }
  129. /**
  130. * A check for serialization.
  131. */
  132. @Test
  133. public void testSerialization1() {
  134. PaintMap m1 = new PaintMap();
  135. PaintMap m2 = (PaintMap) TestUtilities.serialised(m1);
  136. assertEquals(m1, m2);
  137. }
  138. /**
  139. * A check for serialization.
  140. */
  141. @Test
  142. public void testSerialization2() {
  143. PaintMap m1 = new PaintMap();
  144. m1.put("K1", Color.red);
  145. m1.put("K2", new GradientPaint(1.0f, 2.0f, Color.green, 3.0f, 4.0f,
  146. Color.yellow));
  147. PaintMap m2 = (PaintMap) TestUtilities.serialised(m1);
  148. assertEquals(m1, m2);
  149. }
  150. /**
  151. * This test covers a bug reported in the forum:
  152. *
  153. * http://www.jfree.org/phpBB2/viewtopic.php?t=19980
  154. */
  155. @Test
  156. public void testKeysOfDifferentClasses() {
  157. PaintMap m = new PaintMap();
  158. m.put("ABC", Color.red);
  159. m.put(new Integer(99), Color.blue);
  160. assertEquals(Color.blue, m.getPaint(new Integer(99)));
  161. }
  162. }