001: /* ===========================================================
002: * JFreeChart : a free chart library for the Java(tm) platform
003: * ===========================================================
004: *
005: * (C) Copyright 2000-2007, by Object Refinery Limited and Contributors.
006: *
007: * Project Info: http://www.jfree.org/jfreechart/index.html
008: *
009: * This library is free software; you can redistribute it and/or modify it
010: * under the terms of the GNU Lesser General Public License as published by
011: * the Free Software Foundation; either version 2.1 of the License, or
012: * (at your option) any later version.
013: *
014: * This library is distributed in the hope that it will be useful, but
015: * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
016: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
017: * License for more details.
018: *
019: * You should have received a copy of the GNU Lesser General Public
020: * License along with this library; if not, write to the Free Software
021: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
022: * USA.
023: *
024: * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
025: * in the United States and other countries.]
026: *
027: * ---------------------------------
028: * StandardPieURLGeneratorTests.java
029: * ---------------------------------
030: * (C) Copyright 2003-2007, by Object Refinery Limited and Contributors.
031: *
032: * Original Author: David Gilbert (for Object Refinery Limited);
033: * Contributor(s): -;
034: *
035: * $Id: StandardPieURLGeneratorTests.java,v 1.1.2.3 2007/04/17 16:05:27 mungady Exp $
036: *
037: * Changes
038: * -------
039: * 21-Mar-2003 : Version 1 (DG);
040: * 06-Jan-2003 : Added a test for URL generation (DG);
041: * 24-Nov-2006 : New equals() test (DG);
042: * 17-Apr-2007 : Added additional check to testURL() (DG);
043: *
044: */
045:
046: package org.jfree.chart.urls.junit;
047:
048: import java.io.ByteArrayInputStream;
049: import java.io.ByteArrayOutputStream;
050: import java.io.ObjectInput;
051: import java.io.ObjectInputStream;
052: import java.io.ObjectOutput;
053: import java.io.ObjectOutputStream;
054:
055: import junit.framework.Test;
056: import junit.framework.TestCase;
057: import junit.framework.TestSuite;
058:
059: import org.jfree.chart.urls.StandardPieURLGenerator;
060: import org.jfree.data.general.DefaultPieDataset;
061:
062: /**
063: * Tests for the {@link StandardPieURLGenerator} class.
064: */
065: public class StandardPieURLGeneratorTests extends TestCase {
066:
067: /**
068: * Returns the tests as a test suite.
069: *
070: * @return The test suite.
071: */
072: public static Test suite() {
073: return new TestSuite(StandardPieURLGeneratorTests.class);
074: }
075:
076: /**
077: * Constructs a new set of tests.
078: *
079: * @param name the name of the tests.
080: */
081: public StandardPieURLGeneratorTests(String name) {
082: super (name);
083: }
084:
085: /**
086: * Some checks for the equals() method.
087: */
088: public void testEquals() {
089: StandardPieURLGenerator g1 = new StandardPieURLGenerator();
090: StandardPieURLGenerator g2 = new StandardPieURLGenerator();
091: assertTrue(g1.equals(g2));
092:
093: g1 = new StandardPieURLGenerator("prefix", "category", "index");
094: assertFalse(g1.equals(g2));
095: g2 = new StandardPieURLGenerator("prefix", "category", "index");
096: assertTrue(g1.equals(g2));
097:
098: g1 = new StandardPieURLGenerator("prefix2", "category", "index");
099: assertFalse(g1.equals(g2));
100: g2 = new StandardPieURLGenerator("prefix2", "category", "index");
101: assertTrue(g1.equals(g2));
102:
103: g1 = new StandardPieURLGenerator("prefix2", "category2",
104: "index");
105: assertFalse(g1.equals(g2));
106: g2 = new StandardPieURLGenerator("prefix2", "category2",
107: "index");
108: assertTrue(g1.equals(g2));
109:
110: g1 = new StandardPieURLGenerator("prefix2", "category2",
111: "index2");
112: assertFalse(g1.equals(g2));
113: g2 = new StandardPieURLGenerator("prefix2", "category2",
114: "index2");
115: assertTrue(g1.equals(g2));
116:
117: g1 = new StandardPieURLGenerator("prefix2", "category2", null);
118: assertFalse(g1.equals(g2));
119: g2 = new StandardPieURLGenerator("prefix2", "category2", null);
120: assertTrue(g1.equals(g2));
121: }
122:
123: /**
124: * Serialize an instance, restore it, and check for equality.
125: */
126: public void testSerialization() {
127:
128: StandardPieURLGenerator g1 = new StandardPieURLGenerator(
129: "index.html?", "cat");
130: StandardPieURLGenerator g2 = null;
131:
132: try {
133: ByteArrayOutputStream buffer = new ByteArrayOutputStream();
134: ObjectOutput out = new ObjectOutputStream(buffer);
135: out.writeObject(g1);
136: out.close();
137:
138: ObjectInput in = new ObjectInputStream(
139: new ByteArrayInputStream(buffer.toByteArray()));
140: g2 = (StandardPieURLGenerator) in.readObject();
141: in.close();
142: } catch (Exception e) {
143: e.printStackTrace();
144: }
145: assertEquals(g1, g2);
146:
147: }
148:
149: /**
150: * Test that the generated URL is as expected.
151: */
152: public void testURL() {
153: DefaultPieDataset dataset = new DefaultPieDataset();
154: dataset.setValue("Alpha '1'", new Double(5.0));
155: dataset.setValue("Beta", new Double(5.5));
156: StandardPieURLGenerator g1 = new StandardPieURLGenerator(
157: "chart.jsp", "category");
158: String url = g1.generateURL(dataset, "Beta", 0);
159: assertEquals("chart.jsp?category=Beta&pieIndex=0", url);
160: url = g1.generateURL(dataset, "Alpha '1'", 0);
161: assertEquals("chart.jsp?category=Alpha+%271%27&pieIndex=0",
162: url);
163: }
164:
165: }
|