1. /* ===========================================================
  2. * JFreeChart : a free chart library for the Java(tm) platform
  3. * ===========================================================
  4. *
  5. * (C) Copyright 2000-2004, 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 under the terms
  10. * of the GNU Lesser General Public License as published by the Free Software Foundation;
  11. * either version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
  14. * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  15. * See the GNU Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public License along with this
  18. * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  19. * Boston, MA 02111-1307, USA.
  20. *
  21. * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
  22. * in the United States and other countries.]
  23. *
  24. * --------------------------
  25. * AbstractRendererTests.java
  26. * --------------------------
  27. * (C) Copyright 2003, 2004, by Object Refinery Limited and Contributors.
  28. *
  29. * Original Author: David Gilbert (for Object Refinery Limited);
  30. * Contributor(s): -;
  31. *
  32. * $Id: AbstractRendererTests.java,v 1.2 2005/02/09 17:24:48 mungady Exp $
  33. *
  34. * Changes
  35. * -------
  36. * 23-Oct-2003 : Version 1 (DG);
  37. * 01-Mar-2004 : Added serialization test (DG);
  38. *
  39. */
  40. package org.jfree.chart.renderer.junit;
  41. import java.awt.BasicStroke;
  42. import java.awt.Color;
  43. import java.awt.Font;
  44. import java.awt.Shape;
  45. import java.awt.Stroke;
  46. import java.awt.geom.Rectangle2D;
  47. import java.io.ByteArrayInputStream;
  48. import java.io.ByteArrayOutputStream;
  49. import java.io.ObjectInput;
  50. import java.io.ObjectInputStream;
  51. import java.io.ObjectOutput;
  52. import java.io.ObjectOutputStream;
  53. import junit.framework.Test;
  54. import junit.framework.TestCase;
  55. import junit.framework.TestSuite;
  56. import org.jfree.chart.event.RendererChangeEvent;
  57. import org.jfree.chart.labels.ItemLabelAnchor;
  58. import org.jfree.chart.labels.ItemLabelPosition;
  59. import org.jfree.chart.renderer.AbstractRenderer;
  60. import org.jfree.chart.renderer.category.BarRenderer;
  61. import org.jfree.ui.TextAnchor;
  62. /**
  63. * Tests for the {@link AbstractRenderer} class.
  64. */
  65. public class AbstractRendererTests extends TestCase {
  66. /**
  67. * Returns the tests as a test suite.
  68. *
  69. * @return The test suite.
  70. */
  71. public static Test suite() {
  72. return new TestSuite(AbstractRendererTests.class);
  73. }
  74. /**
  75. * Constructs a new set of tests.
  76. *
  77. * @param name the name of the tests.
  78. */
  79. public AbstractRendererTests(String name) {
  80. super(name);
  81. }
  82. /**
  83. * Test that setting the series visibility for ALL series does in fact work.
  84. */
  85. public void testSetSeriesVisible() {
  86. BarRenderer r = new BarRenderer();
  87. r.setSeriesVisible(Boolean.TRUE);
  88. assertTrue(r.getItemVisible(0, 0));
  89. }
  90. /**
  91. * Test that setting the paint for ALL series does in fact work.
  92. */
  93. public void testSetPaint() {
  94. BarRenderer r = new BarRenderer();
  95. r.setPaint(Color.orange);
  96. assertEquals(Color.orange, r.getItemPaint(0, 0));
  97. }
  98. /**
  99. * Test that setting the outline paint for ALL series does in fact work.
  100. */
  101. public void testSetOutlinePaint() {
  102. BarRenderer r = new BarRenderer();
  103. r.setOutlinePaint(Color.orange);
  104. assertEquals(Color.orange, r.getItemOutlinePaint(0, 0));
  105. }
  106. /**
  107. * Test that setting the stroke for ALL series does in fact work.
  108. */
  109. public void testSetStroke() {
  110. BarRenderer r = new BarRenderer();
  111. Stroke s = new BasicStroke(10.0f);
  112. r.setStroke(s);
  113. assertEquals(s, r.getItemStroke(0, 0));
  114. }
  115. /**
  116. * Test that setting the outline stroke for ALL series does in fact work.
  117. */
  118. public void testSetOutlineStroke() {
  119. BarRenderer r = new BarRenderer();
  120. Stroke s = new BasicStroke(10.0f);
  121. r.setOutlineStroke(s);
  122. assertEquals(s, r.getItemOutlineStroke(0, 0));
  123. }
  124. /**
  125. * Test that setting the shape for ALL series does in fact work.
  126. */
  127. public void testSetShape() {
  128. BarRenderer r = new BarRenderer();
  129. Shape s = new Rectangle2D.Double(1.0, 2.0, 3.0, 4.0);
  130. r.setShape(s);
  131. assertEquals(s, r.getItemShape(0, 0));
  132. }
  133. /**
  134. * Test that setting the item label visibility for ALL series does in fact work.
  135. */
  136. public void testSetItemLabelsVisible() {
  137. BarRenderer r = new BarRenderer();
  138. r.setItemLabelsVisible(true);
  139. assertTrue(r.isItemLabelVisible(0, 0));
  140. }
  141. /**
  142. * Test that setting the font for ALL series does in fact work (it was broken at
  143. * one point).
  144. */
  145. public void testSetItemLabelFont() {
  146. BarRenderer r = new BarRenderer();
  147. r.setItemLabelFont(new Font("SansSerif", Font.PLAIN, 33));
  148. assertEquals(new Font("SansSerif", Font.PLAIN, 33), r.getItemLabelFont(0, 0));
  149. }
  150. /**
  151. * Test that setting the paint for ALL series does in fact work (it was broken at
  152. * one point).
  153. */
  154. public void testSetItemLabelPaint() {
  155. BarRenderer r = new BarRenderer();
  156. r.setItemLabelPaint(Color.green);
  157. assertEquals(Color.green, r.getItemLabelPaint(0, 0));
  158. }
  159. /**
  160. * Test that setting the positive item label position for ALL series does in fact work.
  161. */
  162. public void testSetPositiveItemLabelPosition() {
  163. BarRenderer r = new BarRenderer();
  164. r.setPositiveItemLabelPosition(
  165. new ItemLabelPosition(ItemLabelAnchor.INSIDE1, TextAnchor.BASELINE_LEFT)
  166. );
  167. assertEquals(
  168. new ItemLabelPosition(ItemLabelAnchor.INSIDE1, TextAnchor.BASELINE_LEFT),
  169. r.getPositiveItemLabelPosition(0, 0)
  170. );
  171. }
  172. /**
  173. * Test that setting the negative item label position for ALL series does in fact work.
  174. */
  175. public void testSetNegativeItemLabelPosition() {
  176. BarRenderer r = new BarRenderer();
  177. r.setNegativeItemLabelPosition(
  178. new ItemLabelPosition(ItemLabelAnchor.INSIDE1, TextAnchor.BASELINE_LEFT)
  179. );
  180. assertEquals(
  181. new ItemLabelPosition(ItemLabelAnchor.INSIDE1, TextAnchor.BASELINE_LEFT),
  182. r.getNegativeItemLabelPosition(0, 0)
  183. );
  184. }
  185. /**
  186. * Tests each setter method to ensure that it sends an event notification.
  187. */
  188. public void testEventNotification() {
  189. RendererChangeDetector detector = new RendererChangeDetector();
  190. BarRenderer r1 = new BarRenderer(); // have to use a subclass of AbstractRenderer
  191. r1.addChangeListener(detector);
  192. // PAINT
  193. detector.setNotified(false);
  194. r1.setPaint(Color.red);
  195. assertTrue(detector.getNotified());
  196. detector.setNotified(false);
  197. r1.setSeriesPaint(0, Color.red);
  198. assertTrue(detector.getNotified());
  199. detector.setNotified(false);
  200. r1.setBasePaint(Color.red);
  201. assertTrue(detector.getNotified());
  202. // OUTLINE PAINT
  203. detector.setNotified(false);
  204. r1.setOutlinePaint(Color.red);
  205. assertTrue(detector.getNotified());
  206. detector.setNotified(false);
  207. r1.setSeriesOutlinePaint(0, Color.red);
  208. assertTrue(detector.getNotified());
  209. detector.setNotified(false);
  210. r1.setBaseOutlinePaint(Color.red);
  211. assertTrue(detector.getNotified());
  212. // STROKE
  213. detector.setNotified(false);
  214. r1.setStroke(new BasicStroke(1.0f));
  215. assertTrue(detector.getNotified());
  216. detector.setNotified(false);
  217. r1.setSeriesStroke(0, new BasicStroke(1.0f));
  218. assertTrue(detector.getNotified());
  219. detector.setNotified(false);
  220. r1.setBaseStroke(new BasicStroke(1.0f));
  221. assertTrue(detector.getNotified());
  222. // OUTLINE STROKE
  223. detector.setNotified(false);
  224. r1.setOutlineStroke(new BasicStroke(1.0f));
  225. assertTrue(detector.getNotified());
  226. detector.setNotified(false);
  227. r1.setSeriesOutlineStroke(0, new BasicStroke(1.0f));
  228. assertTrue(detector.getNotified());
  229. detector.setNotified(false);
  230. r1.setBaseOutlineStroke(new BasicStroke(1.0f));
  231. assertTrue(detector.getNotified());
  232. // SHAPE
  233. detector.setNotified(false);
  234. r1.setShape(new Rectangle2D.Float());
  235. assertTrue(detector.getNotified());
  236. detector.setNotified(false);
  237. r1.setSeriesShape(0, new Rectangle2D.Float());
  238. assertTrue(detector.getNotified());
  239. detector.setNotified(false);
  240. r1.setBaseShape(new Rectangle2D.Float());
  241. assertTrue(detector.getNotified());
  242. // ITEM_LABELS_VISIBLE
  243. detector.setNotified(false);
  244. r1.setItemLabelsVisible(Boolean.TRUE);
  245. assertTrue(detector.getNotified());
  246. detector.setNotified(false);
  247. r1.setSeriesItemLabelsVisible(0, Boolean.TRUE);
  248. assertTrue(detector.getNotified());
  249. detector.setNotified(false);
  250. r1.setBaseItemLabelsVisible(Boolean.TRUE);
  251. assertTrue(detector.getNotified());
  252. // ITEM_LABEL_FONT
  253. detector.setNotified(false);
  254. r1.setItemLabelFont(new Font("Serif", Font.PLAIN, 12));
  255. assertTrue(detector.getNotified());
  256. detector.setNotified(false);
  257. r1.setSeriesItemLabelFont(0, new Font("Serif", Font.PLAIN, 12));
  258. assertTrue(detector.getNotified());
  259. detector.setNotified(false);
  260. r1.setBaseItemLabelFont(new Font("Serif", Font.PLAIN, 12));
  261. assertTrue(detector.getNotified());
  262. // ITEM_LABEL_PAINT
  263. detector.setNotified(false);
  264. r1.setItemLabelPaint(Color.blue);
  265. assertTrue(detector.getNotified());
  266. detector.setNotified(false);
  267. r1.setSeriesItemLabelPaint(0, Color.blue);
  268. assertTrue(detector.getNotified());
  269. detector.setNotified(false);
  270. r1.setBaseItemLabelPaint(Color.blue);
  271. assertTrue(detector.getNotified());
  272. // POSITIVE ITEM LABEL POSITION
  273. detector.setNotified(false);
  274. r1.setPositiveItemLabelPosition(
  275. new ItemLabelPosition(ItemLabelAnchor.CENTER, TextAnchor.CENTER)
  276. );
  277. assertTrue(detector.getNotified());
  278. detector.setNotified(false);
  279. r1.setSeriesPositiveItemLabelPosition(
  280. 0, new ItemLabelPosition(ItemLabelAnchor.CENTER, TextAnchor.CENTER)
  281. );
  282. assertTrue(detector.getNotified());
  283. detector.setNotified(false);
  284. r1.setBasePositiveItemLabelPosition(
  285. new ItemLabelPosition(ItemLabelAnchor.CENTER, TextAnchor.CENTER)
  286. );
  287. assertTrue(detector.getNotified());
  288. // NEGATIVE ITEM LABEL ANCHOR
  289. detector.setNotified(false);
  290. r1.setNegativeItemLabelPosition(
  291. new ItemLabelPosition(ItemLabelAnchor.CENTER, TextAnchor.CENTER)
  292. );
  293. assertTrue(detector.getNotified());
  294. detector.setNotified(false);
  295. r1.setSeriesNegativeItemLabelPosition(
  296. 0, new ItemLabelPosition(ItemLabelAnchor.CENTER, TextAnchor.CENTER)
  297. );
  298. assertTrue(detector.getNotified());
  299. detector.setNotified(false);
  300. r1.setBaseNegativeItemLabelPosition(
  301. new ItemLabelPosition(ItemLabelAnchor.CENTER, TextAnchor.CENTER)
  302. );
  303. assertTrue(detector.getNotified());
  304. }
  305. /**
  306. * Serialize an instance, restore it, and check for equality. In addition, test for a bug that
  307. * was reported where the listener list is 'null' after deserialization.
  308. */
  309. public void testSerialization() {
  310. BarRenderer r1 = new BarRenderer();
  311. BarRenderer r2 = null;
  312. try {
  313. ByteArrayOutputStream buffer = new ByteArrayOutputStream();
  314. ObjectOutput out = new ObjectOutputStream(buffer);
  315. out.writeObject(r1);
  316. out.close();
  317. ObjectInput in = new ObjectInputStream(new ByteArrayInputStream(buffer.toByteArray()));
  318. r2 = (BarRenderer) in.readObject();
  319. in.close();
  320. }
  321. catch (Exception e) {
  322. System.out.println(e.toString());
  323. }
  324. assertEquals(r1, r2);
  325. try {
  326. r2.notifyListeners(new RendererChangeEvent(r2));
  327. }
  328. catch (NullPointerException e) {
  329. assertTrue(false); // failed
  330. }
  331. }
  332. }