LookupPaintScaleTest.java 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. * LookupPaintScaleTest.java
  29. * -------------------------
  30. * (C) Copyright 2006-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. * 05-Jul-2006 : Version 1 (DG);
  38. * 31-Jan-2007 : Additional serialization tests (DG);
  39. * 07-Mar-2007 : Added new tests (DG);
  40. * 09-Mar-2007 : Check independence in testCloning() (DG);
  41. *
  42. */
  43. package org.jfree.chart.renderer;
  44. import static org.junit.Assert.assertEquals;
  45. import static org.junit.Assert.assertFalse;
  46. import static org.junit.Assert.assertTrue;
  47. import java.awt.Color;
  48. import java.awt.GradientPaint;
  49. import org.jfree.chart.TestUtilities;
  50. import org.junit.Test;
  51. /**
  52. * Tests for the {@link LookupPaintScale} class.
  53. */
  54. public class LookupPaintScaleTest {
  55. /**
  56. * A test for the equals() method.
  57. */
  58. @Test
  59. public void testEquals() {
  60. LookupPaintScale g1 = new LookupPaintScale();
  61. LookupPaintScale g2 = new LookupPaintScale();
  62. assertTrue(g1.equals(g2));
  63. assertTrue(g2.equals(g1));
  64. g1 = new LookupPaintScale(1.0, 2.0, Color.red);
  65. assertFalse(g1.equals(g2));
  66. g2 = new LookupPaintScale(1.0, 2.0, Color.red);
  67. assertTrue(g1.equals(g2));
  68. g1.add(1.5, new GradientPaint(1.0f, 2.0f, Color.red, 3.0f, 4.0f,
  69. Color.blue));
  70. assertFalse(g1.equals(g2));
  71. g2.add(1.5, new GradientPaint(1.0f, 2.0f, Color.red, 3.0f, 4.0f,
  72. Color.blue));
  73. assertTrue(g1.equals(g2));
  74. }
  75. /**
  76. * Confirm that cloning works.
  77. */
  78. @Test
  79. public void testCloning() throws CloneNotSupportedException {
  80. LookupPaintScale g1 = new LookupPaintScale();
  81. LookupPaintScale g2 = (LookupPaintScale) g1.clone();
  82. assertTrue(g1 != g2);
  83. assertTrue(g1.getClass() == g2.getClass());
  84. assertTrue(g1.equals(g2));
  85. // check independence
  86. g1.add(0.5, Color.red);
  87. assertFalse(g1.equals(g2));
  88. g2.add(0.5, Color.red);
  89. assertTrue(g1.equals(g2));
  90. // try with gradient paint
  91. g1 = new LookupPaintScale(1.0, 2.0, new GradientPaint(1.0f, 2.0f,
  92. Color.red, 3.0f, 4.0f, Color.green));
  93. g1.add(1.5, new GradientPaint(1.0f, 2.0f, Color.red, 3.0f, 4.0f,
  94. Color.blue));
  95. g2 = (LookupPaintScale) g1.clone();
  96. assertTrue(g1 != g2);
  97. assertTrue(g1.getClass() == g2.getClass());
  98. assertTrue(g1.equals(g2));
  99. }
  100. /**
  101. * Serialize an instance, restore it, and check for equality.
  102. */
  103. @Test
  104. public void testSerialization() {
  105. LookupPaintScale g1 = new LookupPaintScale();
  106. LookupPaintScale g2 = (LookupPaintScale) TestUtilities.serialised(g1);
  107. assertEquals(g1, g2);
  108. g1 = new LookupPaintScale(1.0, 2.0, new GradientPaint(1.0f, 2.0f,
  109. Color.red, 3.0f, 4.0f, Color.yellow));
  110. g1.add(1.5, new GradientPaint(1.1f, 2.2f, Color.red, 3.3f, 4.4f,
  111. Color.blue));
  112. g2 = (LookupPaintScale) TestUtilities.serialised(g1);
  113. assertEquals(g1, g2);
  114. }
  115. private static final double EPSILON = 0.0000000001;
  116. /**
  117. * Some checks for the default constructor.
  118. */
  119. @Test
  120. public void testConstructor1() {
  121. LookupPaintScale s = new LookupPaintScale();
  122. assertEquals(0.0, s.getLowerBound(), EPSILON);
  123. assertEquals(1.0, s.getUpperBound(), EPSILON);
  124. }
  125. /**
  126. * Some checks for the other constructor.
  127. */
  128. @Test
  129. public void testConstructor2() {
  130. LookupPaintScale s = new LookupPaintScale(1.0, 2.0, Color.red);
  131. assertEquals(1.0, s.getLowerBound(), EPSILON);
  132. assertEquals(2.0, s.getUpperBound(), EPSILON);
  133. assertEquals(Color.red, s.getDefaultPaint());
  134. }
  135. /**
  136. * Some general checks for the lookup table.
  137. */
  138. @Test
  139. public void testGeneral() {
  140. LookupPaintScale s = new LookupPaintScale(0.0, 100.0, Color.black);
  141. assertEquals(Color.black, s.getPaint(-1.0));
  142. assertEquals(Color.black, s.getPaint(0.0));
  143. assertEquals(Color.black, s.getPaint(50.0));
  144. assertEquals(Color.black, s.getPaint(100.0));
  145. assertEquals(Color.black, s.getPaint(101.0));
  146. s.add(50.0, Color.blue);
  147. assertEquals(Color.black, s.getPaint(-1.0));
  148. assertEquals(Color.black, s.getPaint(0.0));
  149. assertEquals(Color.blue, s.getPaint(50.0));
  150. assertEquals(Color.blue, s.getPaint(100.0));
  151. assertEquals(Color.black, s.getPaint(101.0));
  152. s.add(50.0, Color.red);
  153. assertEquals(Color.black, s.getPaint(-1.0));
  154. assertEquals(Color.black, s.getPaint(0.0));
  155. assertEquals(Color.red, s.getPaint(50.0));
  156. assertEquals(Color.red, s.getPaint(100.0));
  157. assertEquals(Color.black, s.getPaint(101.0));
  158. s.add(25.0, Color.green);
  159. assertEquals(Color.black, s.getPaint(-1.0));
  160. assertEquals(Color.black, s.getPaint(0.0));
  161. assertEquals(Color.green, s.getPaint(25.0));
  162. assertEquals(Color.red, s.getPaint(50.0));
  163. assertEquals(Color.red, s.getPaint(100.0));
  164. assertEquals(Color.black, s.getPaint(101.0));
  165. s.add(75.0, Color.yellow);
  166. assertEquals(Color.black, s.getPaint(-1.0));
  167. assertEquals(Color.black, s.getPaint(0.0));
  168. assertEquals(Color.green, s.getPaint(25.0));
  169. assertEquals(Color.red, s.getPaint(50.0));
  170. assertEquals(Color.yellow, s.getPaint(75.0));
  171. assertEquals(Color.yellow, s.getPaint(100.0));
  172. assertEquals(Color.black, s.getPaint(101.0));
  173. }
  174. }