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 under the terms
  10. * of the GNU Lesser General Public License as published by the Free Software Foundation;
  11. * either version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
  14. * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  15. * See the GNU Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public License along with this
  18. * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  19. * Boston, MA 02111-1307, USA.
  20. *
  21. * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
  22. * in the United States and other countries.]
  23. *
  24. * ---------------------
  25. * MillisecondTests.java
  26. * ---------------------
  27. * (C) Copyright 2002-2005 by Object Refinery Limited.
  28. *
  29. * Original Author: David Gilbert (for Object Refinery Limited);
  30. * Contributor(s): -;
  31. *
  32. * $Id: FixedMillisecondTests.java,v 1.2 2005/01/11 16:00:35 mungady Exp $
  33. *
  34. * Changes
  35. * -------
  36. * 29-Jan-2002 : Version 1 (DG);
  37. * 17-Oct-2002 : Fixed errors reported by Checkstyle (DG);
  38. * 21-Oct-2003 : Added hashCode test (DG);
  39. *
  40. */
  41. package org.jfree.data.time.junit;
  42. import java.io.ByteArrayInputStream;
  43. import java.io.ByteArrayOutputStream;
  44. import java.io.ObjectInput;
  45. import java.io.ObjectInputStream;
  46. import java.io.ObjectOutput;
  47. import java.io.ObjectOutputStream;
  48. import junit.framework.Test;
  49. import junit.framework.TestCase;
  50. import junit.framework.TestSuite;
  51. import org.jfree.data.time.FixedMillisecond;
  52. /**
  53. * Tests for the {@link FixedMillisecond} class.
  54. */
  55. public class FixedMillisecondTests extends TestCase {
  56. /**
  57. * Returns the tests as a test suite.
  58. *
  59. * @return The test suite.
  60. */
  61. public static Test suite() {
  62. return new TestSuite(FixedMillisecondTests.class);
  63. }
  64. /**
  65. * Constructs a new set of tests.
  66. *
  67. * @param name the name of the tests.
  68. */
  69. public FixedMillisecondTests(String name) {
  70. super(name);
  71. }
  72. /**
  73. * Serialize an instance, restore it, and check for equality.
  74. */
  75. public void testSerialization() {
  76. FixedMillisecond m1 = new FixedMillisecond();
  77. FixedMillisecond m2 = null;
  78. try {
  79. ByteArrayOutputStream buffer = new ByteArrayOutputStream();
  80. ObjectOutput out = new ObjectOutputStream(buffer);
  81. out.writeObject(m1);
  82. out.close();
  83. ObjectInput in = new ObjectInputStream(
  84. new ByteArrayInputStream(buffer.toByteArray())
  85. );
  86. m2 = (FixedMillisecond) in.readObject();
  87. in.close();
  88. }
  89. catch (Exception e) {
  90. System.out.println(e.toString());
  91. }
  92. assertEquals(m1, m2);
  93. }
  94. /**
  95. * Two objects that are equal are required to return the same hashCode.
  96. */
  97. public void testHashcode() {
  98. FixedMillisecond m1 = new FixedMillisecond(500000L);
  99. FixedMillisecond m2 = new FixedMillisecond(500000L);
  100. assertTrue(m1.equals(m2));
  101. int h1 = m1.hashCode();
  102. int h2 = m2.hashCode();
  103. assertEquals(h1, h2);
  104. }
  105. /**
  106. * The {@link FixedMillisecond} class is immutable, so should not be {@link Cloneable}.
  107. */
  108. public void testNotCloneable() {
  109. FixedMillisecond m = new FixedMillisecond(500000L);
  110. assertFalse(m instanceof Cloneable);
  111. }
  112. }