📄 defaultkeyedvaluestests.java
字号:
/* =========================================================== * JFreeChart : a free chart library for the Java(tm) platform * =========================================================== * * (C) Copyright 2000-2007, by Object Refinery Limited and Contributors. * * Project Info: http://www.jfree.org/jfreechart/index.html * * This library is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation; either version 2.1 of the License, or * (at your option) any later version. * * This library is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public * License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, * USA. * * [Java is a trademark or registered trademark of Sun Microsystems, Inc. * in the United States and other countries.] * * ---------------------------- * DefaultKeyedValuesTests.java * ---------------------------- * (C) Copyright 2003-2007, by Object Refinery Limited. * * Original Author: David Gilbert (for Object Refinery Limited); * Contributor(s): -; * * Changes * ------- * 05-Mar-2003 : Version 1 (DG); * 27-Aug-2003 : Moved SortOrder from org.jfree.data --> org.jfree.util (DG); * 31-Jul-2006 : Added test for new clear() method (DG); * 01-Aug-2006 : Extended testGetIndex() method (DG); * 30-Apr-2007 : Added some new tests (DG); * 03-Oct-2007 : Updated testRemoveValue() (DG); * 21-Nov-2007 : Added testGetIndex2() method (DG); * */package org.jfree.data.junit;import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStream;import java.io.ObjectInput;import java.io.ObjectInputStream;import java.io.ObjectOutput;import java.io.ObjectOutputStream;import java.util.List;import junit.framework.Test;import junit.framework.TestCase;import junit.framework.TestSuite;import org.jfree.data.DefaultKeyedValues;import org.jfree.data.UnknownKeyException;import org.jfree.util.SortOrder;/** * Tests for the {@link DefaultKeyedValues} class. */public class DefaultKeyedValuesTests extends TestCase { /** * Returns the tests as a test suite. * * @return The test suite. */ public static Test suite() { return new TestSuite(DefaultKeyedValuesTests.class); } /** * Constructs a new set of tests. * * @param name the name of the tests. */ public DefaultKeyedValuesTests(String name) { super(name); } /** * Common test setup. */ protected void setUp() { // no setup required } /** * Checks that a new instance is empty. */ public void testConstructor() { DefaultKeyedValues d = new DefaultKeyedValues(); assertEquals(0, d.getItemCount()); } /** * Some checks for the getItemCount() method. */ public void testGetItemCount() { DefaultKeyedValues d = new DefaultKeyedValues(); assertEquals(0, d.getItemCount()); d.addValue("A", 1.0); assertEquals(1, d.getItemCount()); d.addValue("B", 2.0); assertEquals(2, d.getItemCount()); d.clear(); assertEquals(0, d.getItemCount()); } /** * Some checks for the getKeys() method. */ public void testGetKeys() { DefaultKeyedValues d = new DefaultKeyedValues(); List keys = d.getKeys(); assertTrue(keys.isEmpty()); d.addValue("A", 1.0); keys = d.getKeys(); assertEquals(1, keys.size()); assertTrue(keys.contains("A")); d.addValue("B", 2.0); keys = d.getKeys(); assertEquals(2, keys.size()); assertTrue(keys.contains("A")); assertTrue(keys.contains("B")); d.clear(); keys = d.getKeys(); assertEquals(0, keys.size()); } /** * A simple test for the clear() method. */ public void testClear() { DefaultKeyedValues v1 = new DefaultKeyedValues(); v1.addValue("A", 1.0); v1.addValue("B", 2.0); assertEquals(2, v1.getItemCount()); v1.clear(); assertEquals(0, v1.getItemCount()); } /** * Some checks for the getValue() methods. */ public void testGetValue() { DefaultKeyedValues v1 = new DefaultKeyedValues(); try { /* Number n = */ v1.getValue(-1); assertTrue(false); } catch (IndexOutOfBoundsException e) { // expected } try { /* Number n = */ v1.getValue(0); assertTrue(false); } catch (IndexOutOfBoundsException e) { // expected } DefaultKeyedValues v2 = new DefaultKeyedValues(); v2.addValue("K1", new Integer(1)); v2.addValue("K2", new Integer(2)); v2.addValue("K3", new Integer(3)); assertEquals(new Integer(3), v2.getValue(2)); boolean pass = false; try { /* Number n = */ v2.getValue("KK"); } catch (UnknownKeyException e) { pass = true; } assertTrue(pass); } /** * Some checks for the getKey() methods. */ public void testGetKey() { DefaultKeyedValues v1 = new DefaultKeyedValues(); try { /* Comparable k = */ v1.getKey(-1); assertTrue(false); } catch (IndexOutOfBoundsException e) { // expected } try { /* Comparable k = */ v1.getKey(0); assertTrue(false); } catch (IndexOutOfBoundsException e) { // expected } DefaultKeyedValues v2 = new DefaultKeyedValues(); v2.addValue("K1", new Integer(1)); v2.addValue("K2", new Integer(2)); v2.addValue("K3", new Integer(3)); assertEquals("K2", v2.getKey(1)); } /** * Some checks for the getIndex() methods. */ public void testGetIndex() { DefaultKeyedValues v1 = new DefaultKeyedValues(); assertEquals(-1, v1.getIndex("K1")); DefaultKeyedValues v2 = new DefaultKeyedValues(); v2.addValue("K1", new Integer(1)); v2.addValue("K2", new Integer(2)); v2.addValue("K3", new Integer(3)); assertEquals(2, v2.getIndex("K3")); // try null boolean pass = false; try { v2.getIndex(null); } catch (IllegalArgumentException e) { pass = true; } assertTrue(pass); } /** * Another check for the getIndex(Comparable) method. */ public void testGetIndex2() { DefaultKeyedValues v = new DefaultKeyedValues(); assertEquals(-1, v.getIndex("K1")); v.addValue("K1", 1.0); assertEquals(0, v.getIndex("K1")); v.removeValue("K1"); assertEquals(-1, v.getIndex("K1")); } /** * Some checks for the addValue() method. */ public void testAddValue() { DefaultKeyedValues v1 = new DefaultKeyedValues(); v1.addValue("A", 1.0); assertEquals(new Double(1.0), v1.getValue("A")); v1.addValue("B", 2.0); assertEquals(new Double(2.0), v1.getValue("B")); v1.addValue("B", 3.0); assertEquals(new Double(3.0), v1.getValue("B")); assertEquals(2, v1.getItemCount()); v1.addValue("A", null); assertNull(v1.getValue("A")); assertEquals(2, v1.getItemCount()); boolean pass = false; try { v1.addValue(null, 99.9); } catch (IllegalArgumentException e) { pass = true; } assertTrue(pass);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -