📄 serialdatetests.java
字号:
/* =================================================== * JCommon : a free general purpose Java class library * =================================================== * * Project Info: http://www.jfree.org/jcommon/index.html * Project Lead: David Gilbert (david.gilbert@object-refinery.com); * * (C) Copyright 2000-2003, by Object Refinery Limited and Contributors. * * 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., 59 Temple Place, Suite 330, * Boston, MA 02111-1307, USA. * * -------------------- * SerialDateTests.java * -------------------- * (C) Copyright 2001-2003, by Object Refinery Limited. * * Original Author: David Gilbert (for Object Refinery Limited); * Contributor(s): -; * * $Id: SerialDateTests.java,v 1.9 2004/04/26 19:15:40 taqua Exp $ * * Changes * ------- * 15-Nov-2001 : Version 1 (DG); * 25-Jun-2002 : Removed unnecessary import (DG); * 24-Oct-2002 : Fixed errors reported by Checkstyle (DG); * 13-Mar-2003 : Added serialization test (DG); * */package org.jfree.date.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 junit.framework.Test;import junit.framework.TestCase;import junit.framework.TestSuite;import org.jfree.date.SerialDate;/** * Some JUnit tests for the {@link SerialDate} class. * */public class SerialDateTests extends TestCase { /** Date representing November 9. */ private SerialDate nov9Y2001; /** * Creates a new test case. * * @param name the name. */ public SerialDateTests(final String name) { super(name); } /** * Returns a test suite for the JUnit test runner. * * @return the test suite. */ public static Test suite() { return new TestSuite(SerialDateTests.class); } /** * Problem set up. */ protected void setUp() { this.nov9Y2001 = SerialDate.createInstance(9, SerialDate.NOVEMBER, 2001); } /** * 9 Nov 2001 plus two months should be 9 Jan 2002. */ public void testAddMonthsTo9Nov2001() { final SerialDate jan9Y2002 = SerialDate.addMonths(2, this.nov9Y2001); final SerialDate answer = SerialDate.createInstance(9, 1, 2002); assertEquals(answer, jan9Y2002); } /** * A test case for a reported bug, now fixed. */ public void testAddMonthsTo5Oct2003() { final SerialDate d1 = SerialDate.createInstance(5, SerialDate.OCTOBER, 2003); final SerialDate d2 = SerialDate.addMonths(2, d1); assertEquals(d2, SerialDate.createInstance(5, SerialDate.DECEMBER, 2003)); } /** * A test case for a reported bug, now fixed. */ public void testAddMonthsTo1Jan2003() { final SerialDate d1 = SerialDate.createInstance(1, SerialDate.JANUARY, 2003); final SerialDate d2 = SerialDate.addMonths(0, d1); assertEquals(d2, d1); } /** * Monday preceding Friday 9 November 2001 should be 5 November. */ public void testMondayPrecedingFriday9Nov2001() { final SerialDate mondayBefore = SerialDate.getPreviousDayOfWeek( SerialDate.MONDAY, this.nov9Y2001 ); assertEquals(5, mondayBefore.getDayOfMonth()); } /** * Monday following Friday 9 November 2001 should be 12 November. */ public void testMondayFollowingFriday9Nov2001() { final SerialDate mondayAfter = SerialDate.getFollowingDayOfWeek( SerialDate.MONDAY, this.nov9Y2001 ); assertEquals(12, mondayAfter.getDayOfMonth()); } /** * Monday nearest Friday 9 November 2001 should be 12 November. */ public void testMondayNearestFriday9Nov2001() { final SerialDate mondayNearest = SerialDate.getNearestDayOfWeek( SerialDate.MONDAY, this.nov9Y2001 ); assertEquals(12, mondayNearest.getDayOfMonth()); } /** * The Monday nearest to 22nd January 1970 falls on the 19th. */ public void testMondayNearest22Jan1970() { final SerialDate jan22Y1970 = SerialDate.createInstance(22, SerialDate.JANUARY, 1970); final SerialDate mondayNearest = SerialDate.getNearestDayOfWeek(SerialDate.MONDAY, jan22Y1970); assertEquals(19, mondayNearest.getDayOfMonth()); } /** * Problem that the conversion of days to strings returns the right result. Actually, this * result depends on the Locale so this test needs to be modified. */ public void testWeekdayCodeToString() { final String test = SerialDate.weekdayCodeToString(SerialDate.SATURDAY); assertEquals("Saturday", test); } /** * Problem the conversion of a string to a weekday. Note that this test will fail if the * default locale doesn't use English weekday names...devise a better test! */ public void testStringToWeekday() { int weekday = SerialDate.stringToWeekdayCode("Wednesday"); assertEquals(SerialDate.WEDNESDAY, weekday); weekday = SerialDate.stringToWeekdayCode(" Wednesday "); assertEquals(SerialDate.WEDNESDAY, weekday); weekday = SerialDate.stringToWeekdayCode("Wed"); assertEquals(SerialDate.WEDNESDAY, weekday); } /** * Problem the conversion of a string to a month. Note that this test will fail if the default * locale doesn't use English month names...devise a better test! */ public void testStringToMonthCode() { int m = SerialDate.stringToMonthCode("January"); assertEquals(SerialDate.JANUARY, m); m = SerialDate.stringToMonthCode(" January "); assertEquals(SerialDate.JANUARY, m); m = SerialDate.stringToMonthCode("Jan"); assertEquals(SerialDate.JANUARY, m); } /** * Tests the conversion of a month code to a string. */ public void testMonthCodeToStringCode() { final String test = SerialDate.monthCodeToString(SerialDate.DECEMBER); assertEquals("December", test); } /** * 1900 is not a leap year. */ public void testIsNotLeapYear1900() { assertTrue(!SerialDate.isLeapYear(1900)); } /** * 2000 is a leap year. */ public void testIsLeapYear2000() { assertTrue(SerialDate.isLeapYear(2000)); } /** * The number of leap years from 1900 up-to-and-including 1899 is 0. */ public void testLeapYearCount1899() { assertEquals(SerialDate.leapYearCount(1899), 0); } /** * The number of leap years from 1900 up-to-and-including 1903 is 0. */ public void testLeapYearCount1903() { assertEquals(SerialDate.leapYearCount(1903), 0); } /** * The number of leap years from 1900 up-to-and-including 1904 is 1. */ public void testLeapYearCount1904() { assertEquals(SerialDate.leapYearCount(1904), 1); } /** * The number of leap years from 1900 up-to-and-including 1999 is 24. */ public void testLeapYearCount1999() { assertEquals(SerialDate.leapYearCount(1999), 24); } /** * The number of leap years from 1900 up-to-and-including 2000 is 25. */ public void testLeapYearCount2000() { assertEquals(SerialDate.leapYearCount(2000), 25); } /** * Serialize an instance, restore it, and check for equality. */ public void testSerialization() { final SerialDate d1 = SerialDate.createInstance(15, 4, 2000); SerialDate d2 = null; try { final ByteArrayOutputStream buffer = new ByteArrayOutputStream(); final ObjectOutput out = new ObjectOutputStream(buffer); out.writeObject(d1); out.close(); final ObjectInput in = new ObjectInputStream(new ByteArrayInputStream(buffer.toByteArray())); d2 = (SerialDate) in.readObject(); in.close(); } catch (Exception e) { System.out.println(e.toString()); } assertEquals(d1, d2); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -