001: /* ===========================================================
002: * JFreeChart : a free chart library for the Java(tm) platform
003: * ===========================================================
004: *
005: * (C) Copyright 2000-2006, 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: * RelativeDateFormatTests.java
029: * ----------------------------
030: * (C) Copyright 2006, by Object Refinery Limited and Contributors.
031: *
032: * Original Author: David Gilbert (for Object Refinery Limited);
033: * Contributor(s): -;
034: *
035: * $Id: RelativeDateFormatTests.java,v 1.1.2.1 2006/11/23 15:42:24 mungady Exp $
036: *
037: * Changes
038: * -------
039: * 23-Nov-2006 : Version 1 (DG);
040: *
041: */
042:
043: package org.jfree.chart.util.junit;
044:
045: import java.text.DecimalFormat;
046: import java.text.NumberFormat;
047:
048: import junit.framework.Test;
049: import junit.framework.TestCase;
050: import junit.framework.TestSuite;
051:
052: import org.jfree.chart.util.RelativeDateFormat;
053:
054: /**
055: * Tests for the {@link RelativeDateFormat} class.
056: */
057: public class RelativeDateFormatTests extends TestCase {
058:
059: /**
060: * Returns the tests as a test suite.
061: *
062: * @return The test suite.
063: */
064: public static Test suite() {
065: return new TestSuite(RelativeDateFormatTests.class);
066: }
067:
068: /**
069: * Constructs a new set of tests.
070: *
071: * @param name the name of the tests.
072: */
073: public RelativeDateFormatTests(String name) {
074: super (name);
075: }
076:
077: /**
078: * Check that the equals() method can distinguish all fields.
079: */
080: public void testEquals() {
081: RelativeDateFormat df1 = new RelativeDateFormat();
082: RelativeDateFormat df2 = new RelativeDateFormat();
083: assertEquals(df1, df2);
084:
085: df1.setBaseMillis(123L);
086: assertFalse(df1.equals(df2));
087: df2.setBaseMillis(123L);
088: assertTrue(df1.equals(df2));
089:
090: df1.setDaySuffix("D");
091: assertFalse(df1.equals(df2));
092: df2.setDaySuffix("D");
093: assertTrue(df1.equals(df2));
094:
095: df1.setHourSuffix("H");
096: assertFalse(df1.equals(df2));
097: df2.setHourSuffix("H");
098: assertTrue(df1.equals(df2));
099:
100: df1.setMinuteSuffix("M");
101: assertFalse(df1.equals(df2));
102: df2.setMinuteSuffix("M");
103: assertTrue(df1.equals(df2));
104:
105: df1.setSecondSuffix("S");
106: assertFalse(df1.equals(df2));
107: df2.setSecondSuffix("S");
108: assertTrue(df1.equals(df2));
109:
110: df1.setShowZeroDays(!df1.getShowZeroDays());
111: assertFalse(df1.equals(df2));
112: df2.setShowZeroDays(!df2.getShowZeroDays());
113: assertTrue(df1.equals(df2));
114:
115: df1.setSecondFormatter(new DecimalFormat("0.0"));
116: assertFalse(df1.equals(df2));
117: df2.setSecondFormatter(new DecimalFormat("0.0"));
118: assertTrue(df1.equals(df2));
119: }
120:
121: /**
122: * Two objects that are equal are required to return the same hashCode.
123: */
124: public void testHashCode() {
125: RelativeDateFormat df1 = new RelativeDateFormat(123L);
126: RelativeDateFormat df2 = new RelativeDateFormat(123L);
127: assertTrue(df1.equals(df2));
128: int h1 = df1.hashCode();
129: int h2 = df2.hashCode();
130: assertEquals(h1, h2);
131: }
132:
133: /**
134: * Confirm that cloning works.
135: */
136: public void testCloning() {
137: NumberFormat nf = new DecimalFormat("0");
138: RelativeDateFormat df1 = new RelativeDateFormat();
139: df1.setSecondFormatter(nf);
140: RelativeDateFormat df2 = null;
141: df2 = (RelativeDateFormat) df1.clone();
142: assertTrue(df1 != df2);
143: assertTrue(df1.getClass() == df2.getClass());
144: assertTrue(df1.equals(df2));
145:
146: // is the clone independent
147: nf.setMinimumFractionDigits(2);
148: assertFalse(df1.equals(df2));
149: }
150:
151: }
|