1. /* ===========================================================
  2. * JFreeChart : a free chart library for the Java(tm) platform
  3. * ===========================================================
  4. *
  5. * (C) Copyright 2000-2005, 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 License
  20. * along with this library; if not, write to the Free Software Foundation,
  21. * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
  22. *
  23. * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
  24. * in the United States and other countries.]
  25. *
  26. * ---------------------------------
  27. * StandardPieURLGeneratorTests.java
  28. * ---------------------------------
  29. * (C) Copyright 2003-2005, by Object Refinery Limited and Contributors.
  30. *
  31. * Original Author: David Gilbert (for Object Refinery Limited);
  32. * Contributor(s): -;
  33. *
  34. * $Id: StandardPieURLGeneratorTests.java,v 1.4 2005/03/09 13:44:45 mungady Exp $
  35. *
  36. * Changes
  37. * -------
  38. * 21-Mar-2003 : Version 1 (DG);
  39. * 06-Jan-2003 : Added a test for URL generation (DG);
  40. *
  41. */
  42. package org.jfree.chart.urls.junit;
  43. import java.io.ByteArrayInputStream;
  44. import java.io.ByteArrayOutputStream;
  45. import java.io.ObjectInput;
  46. import java.io.ObjectInputStream;
  47. import java.io.ObjectOutput;
  48. import java.io.ObjectOutputStream;
  49. import junit.framework.Test;
  50. import junit.framework.TestCase;
  51. import junit.framework.TestSuite;
  52. import org.jfree.chart.urls.StandardPieURLGenerator;
  53. import org.jfree.data.general.DefaultPieDataset;
  54. /**
  55. * Tests for the {@link StandardPieURLGenerator} class.
  56. */
  57. public class StandardPieURLGeneratorTests extends TestCase {
  58. /**
  59. * Returns the tests as a test suite.
  60. *
  61. * @return The test suite.
  62. */
  63. public static Test suite() {
  64. return new TestSuite(StandardPieURLGeneratorTests.class);
  65. }
  66. /**
  67. * Constructs a new set of tests.
  68. *
  69. * @param name the name of the tests.
  70. */
  71. public StandardPieURLGeneratorTests(String name) {
  72. super(name);
  73. }
  74. /**
  75. * Serialize an instance, restore it, and check for equality.
  76. */
  77. public void testSerialization() {
  78. StandardPieURLGenerator g1 = new StandardPieURLGenerator(
  79. "index.html?", "cat"
  80. );
  81. StandardPieURLGenerator g2 = null;
  82. try {
  83. ByteArrayOutputStream buffer = new ByteArrayOutputStream();
  84. ObjectOutput out = new ObjectOutputStream(buffer);
  85. out.writeObject(g1);
  86. out.close();
  87. ObjectInput in = new ObjectInputStream(
  88. new ByteArrayInputStream(buffer.toByteArray())
  89. );
  90. g2 = (StandardPieURLGenerator) in.readObject();
  91. in.close();
  92. }
  93. catch (Exception e) {
  94. System.out.println(e.toString());
  95. }
  96. assertEquals(g1, g2);
  97. }
  98. /**
  99. * Test that the generated URL is as expected.
  100. */
  101. public void testURL() {
  102. DefaultPieDataset dataset = new DefaultPieDataset();
  103. dataset.setValue("Alpha", new Double(5.0));
  104. dataset.setValue("Beta", new Double(5.5));
  105. StandardPieURLGenerator g1 = new StandardPieURLGenerator(
  106. "chart.jsp", "category"
  107. );
  108. String url = g1.generateURL(dataset, "Beta", 0);
  109. assertEquals("chart.jsp?category=Beta&pieIndex=0", url);
  110. }
  111. }