Save This Page
Home » jcommon-1.0.13 » org.jfree » data » category » junit » [javadoc | source]
    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    * KeyToGroupMapTests.java
   28    * -----------------------
   29    * (C) Copyright 2004, 2005, by Object Refinery Limited and Contributors.
   30    *
   31    * Original Author:  David Gilbert (for Object Refinery Limited);
   32    * Contributor(s):   -;
   33    *
   34    * $Id: KeyToGroupMapTests.java,v 1.3 2005/03/29 12:57:13 mungady Exp $
   35    *
   36    * Changes
   37    * -------
   38    * 29-Apr-2004 : Version 1 (DG);
   39    *
   40    */
   41   
   42   package org.jfree.data.category.junit;
   43   
   44   import java.io.ByteArrayInputStream;
   45   import java.io.ByteArrayOutputStream;
   46   import java.io.ObjectInput;
   47   import java.io.ObjectInputStream;
   48   import java.io.ObjectOutput;
   49   import java.io.ObjectOutputStream;
   50   
   51   import junit.framework.Test;
   52   import junit.framework.TestCase;
   53   import junit.framework.TestSuite;
   54   
   55   import org.jfree.data.KeyToGroupMap;
   56   
   57   /**
   58    * Tests for the {@link KeyToGroupMap} class.
   59    */
   60   public class KeyToGroupMapTests extends TestCase {
   61   
   62       /**
   63        * Returns the tests as a test suite.
   64        *
   65        * @return The test suite.
   66        */
   67       public static Test suite() {
   68           return new TestSuite(KeyToGroupMapTests.class);
   69       }
   70   
   71       /**
   72        * Constructs a new set of tests.
   73        *
   74        * @param name  the name of the tests.
   75        */
   76       public KeyToGroupMapTests(String name) {
   77           super(name);
   78       }
   79   
   80       /**
   81        * Tests the mapKeyToGroup() method.
   82        */
   83       public void testMapKeyToGroup() {
   84           KeyToGroupMap m1 = new KeyToGroupMap("G1");
   85           
   86           // map a key to the default group
   87           m1.mapKeyToGroup("K1", "G1");
   88           assertEquals("G1", m1.getGroup("K1"));
   89           
   90           // map a key to a new group
   91           m1.mapKeyToGroup("K2", "G2");
   92           assertEquals("G2", m1.getGroup("K2"));
   93           
   94           // clear a mapping
   95           m1.mapKeyToGroup("K2", null);
   96           assertEquals("G1", m1.getGroup("K2"));  // after clearing, reverts to 
   97                                                   // default group
   98           
   99           // check handling of null key
  100           boolean pass = false;
  101           try {
  102               m1.mapKeyToGroup(null, "G1");   
  103           }
  104           catch (IllegalArgumentException e) {
  105               pass = true;   
  106           }
  107           assertTrue(pass);
  108       }
  109       
  110       /**
  111        * Tests that the getGroupCount() method returns the correct values under 
  112        * various circumstances.
  113        */
  114       public void testGroupCount() {
  115           KeyToGroupMap m1 = new KeyToGroupMap("Default Group");
  116           
  117           // a new map always has 1 group (the default group)
  118           assertEquals(1, m1.getGroupCount());
  119           
  120           // if the default group is not mapped to, it should still count towards
  121           // the group count...
  122           m1.mapKeyToGroup("C1", "G1");
  123           assertEquals(2, m1.getGroupCount());
  124           
  125           // now when the default group is mapped to, it shouldn't increase the
  126           // group count...
  127           m1.mapKeyToGroup("C2", "Default Group");
  128           assertEquals(2, m1.getGroupCount());
  129       
  130           // complicate things a little...
  131           m1.mapKeyToGroup("C3", "Default Group");
  132           m1.mapKeyToGroup("C4", "G2");
  133           m1.mapKeyToGroup("C5", "G2");
  134           m1.mapKeyToGroup("C6", "Default Group");
  135           assertEquals(3, m1.getGroupCount());
  136           
  137           // now overwrite group "G2"...
  138           m1.mapKeyToGroup("C4", "G1");
  139           m1.mapKeyToGroup("C5", "G1");
  140           assertEquals(2, m1.getGroupCount()); 
  141       }
  142       
  143       /**
  144        * Tests that the getKeyCount() method returns the correct values under 
  145        * various circumstances.
  146        */
  147       public void testKeyCount() {
  148           KeyToGroupMap m1 = new KeyToGroupMap("Default Group");
  149           
  150           // a new map always has 1 group (the default group)
  151           assertEquals(0, m1.getKeyCount("Default Group"));
  152           
  153           // simple case
  154           m1.mapKeyToGroup("K1", "G1");
  155           assertEquals(1, m1.getKeyCount("G1"));
  156           m1.mapKeyToGroup("K1", null);
  157           assertEquals(0, m1.getKeyCount("G1"));
  158           
  159           // if there is an explicit mapping to the default group, it is counted
  160           m1.mapKeyToGroup("K2", "Default Group");
  161           assertEquals(1, m1.getKeyCount("Default Group"));
  162       
  163           // complicate things a little...
  164           m1.mapKeyToGroup("K3", "Default Group");
  165           m1.mapKeyToGroup("K4", "G2");
  166           m1.mapKeyToGroup("K5", "G2");
  167           m1.mapKeyToGroup("K6", "Default Group");
  168           assertEquals(3, m1.getKeyCount("Default Group"));
  169           assertEquals(2, m1.getKeyCount("G2"));
  170           
  171           // now overwrite group "G2"...
  172           m1.mapKeyToGroup("K4", "G1");
  173           m1.mapKeyToGroup("K5", "G1");
  174           assertEquals(2, m1.getKeyCount("G1")); 
  175           assertEquals(0, m1.getKeyCount("G2")); 
  176       }
  177       
  178       /**
  179        * Tests the getGroupIndex() method.
  180        */
  181       public void testGetGroupIndex() {
  182           KeyToGroupMap m1 = new KeyToGroupMap("Default Group");
  183          
  184           // the default group is always at index 0
  185           assertEquals(0, m1.getGroupIndex("Default Group"));
  186           
  187           // a non-existent group should return -1
  188           assertEquals(-1, m1.getGroupIndex("G3"));
  189           
  190           // indices are assigned in the order that groups are originally mapped
  191           m1.mapKeyToGroup("K3", "G3");
  192           m1.mapKeyToGroup("K1", "G1");
  193           m1.mapKeyToGroup("K2", "G2");
  194           assertEquals(1, m1.getGroupIndex("G3"));
  195           assertEquals(2, m1.getGroupIndex("G1"));
  196           assertEquals(3, m1.getGroupIndex("G2"));
  197       }
  198       
  199       /**
  200        * Tests the getGroup() method.
  201        */
  202       public void testGetGroup() {
  203           KeyToGroupMap m1 = new KeyToGroupMap("Default Group");
  204           
  205           // a key that hasn't been mapped should return the default group
  206           assertEquals("Default Group", m1.getGroup("K1"));
  207           
  208           m1.mapKeyToGroup("K1", "G1");
  209           assertEquals("G1", m1.getGroup("K1"));
  210           m1.mapKeyToGroup("K1", "G2");
  211           assertEquals("G2", m1.getGroup("K1"));
  212           m1.mapKeyToGroup("K1", null);
  213           assertEquals("Default Group", m1.getGroup("K1"));
  214           
  215           // a null argument should throw an exception
  216           boolean pass = false;
  217           try {
  218               Comparable g = m1.getGroup(null); 
  219               System.out.println(g);
  220           }
  221           catch (IllegalArgumentException e) {
  222               pass = true;   
  223           }
  224           assertTrue(pass);
  225       }
  226       
  227       /**
  228        * Confirm that the equals method can distinguish all the required fields.
  229        */
  230       public void testEquals() {     
  231           KeyToGroupMap m1 = new KeyToGroupMap("Default Group");
  232           KeyToGroupMap m2 = new KeyToGroupMap("Default Group");
  233           assertTrue(m1.equals(m2));
  234           assertTrue(m2.equals(m1));
  235           
  236           m1.mapKeyToGroup("K1", "G1");
  237           assertFalse(m1.equals(m2));
  238           m2.mapKeyToGroup("K1", "G1");
  239           assertTrue(m1.equals(m2));
  240       }
  241   
  242       /**
  243        * Confirm that cloning works.
  244        */
  245       public void testCloning() {
  246           KeyToGroupMap m1 = new KeyToGroupMap("Test");
  247           m1.mapKeyToGroup("K1", "G1");
  248           KeyToGroupMap m2 = null;
  249           try {
  250               m2 = (KeyToGroupMap) m1.clone();
  251           }
  252           catch (CloneNotSupportedException e) {
  253               System.err.println("Failed to clone.");
  254           }
  255           assertTrue(m1 != m2);
  256           assertTrue(m1.getClass() == m2.getClass());
  257           assertTrue(m1.equals(m2));
  258           
  259           // a small check for independence
  260           m1.mapKeyToGroup("K1", "G2");
  261           assertFalse(m1.equals(m2));
  262           m2.mapKeyToGroup("K1", "G2");
  263           assertTrue(m1.equals(m2));
  264       }
  265   
  266       /**
  267        * Serialize an instance, restore it, and check for equality.
  268        */
  269       public void testSerialization() {
  270   
  271           KeyToGroupMap m1 = new KeyToGroupMap("Test");
  272           KeyToGroupMap m2 = null;
  273   
  274           try {
  275               ByteArrayOutputStream buffer = new ByteArrayOutputStream();
  276               ObjectOutput out = new ObjectOutputStream(buffer);
  277               out.writeObject(m1);
  278               out.close();
  279   
  280               ObjectInput in = new ObjectInputStream(
  281                   new ByteArrayInputStream(buffer.toByteArray())
  282               );
  283               m2 = (KeyToGroupMap) in.readObject();
  284               in.close();
  285           }
  286           catch (Exception e) {
  287               System.out.println(e.toString());
  288           }
  289           assertEquals(m1, m2);
  290   
  291       }
  292   
  293   }

Save This Page
Home » jcommon-1.0.13 » org.jfree » data » category » junit » [javadoc | source]