< prev index next >

test/jdk/java/util/Calendar/bug4316678.java

Print this page
@@ -1,7 +1,7 @@
  /*
-  * Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
+  * Copyright (c) 2000, 2023, Oracle and/or its affiliates. All rights reserved.
   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   *
   * This code is free software; you can redistribute it and/or modify it
   * under the terms of the GNU General Public License version 2 only, as
   * published by the Free Software Foundation.

@@ -19,51 +19,66 @@
   * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
   * or visit www.oracle.com if you need additional information or have any
   * questions.
   */
  
- import java.io.*;
- import java.util.*;
- import java.text.*;
- 
- /**
+ /*
   * @test
   * @bug 4316678
-  * @summary test that Calendar's Serializasion works correctly.
-  * @library /java/text/testlib
+  * @summary test that Calendar's Serialization works correctly.
+  * @run junit bug4316678
   */
- public class bug4316678 extends IntlTest {
  
-     public static void main(String[] args) throws Exception {
-         new bug4316678().run(args);
-     }
+ import java.io.FileInputStream;
+ import java.io.FileOutputStream;
+ import java.io.IOException;
+ import java.io.ObjectInputStream;
+ import java.io.ObjectOutputStream;
+ import java.util.Calendar;
+ import java.util.GregorianCalendar;
+ import java.util.TimeZone;
  
-     public void Test4316678() throws Exception {
-         GregorianCalendar gc1;
-         GregorianCalendar gc2;
-         TimeZone saveZone = TimeZone.getDefault();
+ import org.junit.jupiter.api.AfterAll;
+ import org.junit.jupiter.api.BeforeAll;
+ import org.junit.jupiter.api.Test;
  
-         try {
-             TimeZone.setDefault(TimeZone.getTimeZone("PST"));
+ import static org.junit.jupiter.api.Assertions.assertEquals;
  
-             gc1 = new GregorianCalendar(2000, Calendar.OCTOBER, 10);
-             try (ObjectOutputStream out
-                     = new ObjectOutputStream(new FileOutputStream("bug4316678.ser"))) {
-                 out.writeObject(gc1);
-             }
+ public class bug4316678 {
  
-             try (ObjectInputStream in
-                     = new ObjectInputStream(new FileInputStream("bug4316678.ser"))) {
-                 gc2 = (GregorianCalendar)in.readObject();
-             }
+     private static final String serializedData = "bug4316678.ser";
+ 
+     // Save JVM default TimeZone
+     private static final TimeZone savedTz = TimeZone.getDefault();
+ 
+     // Set custom JVM default TimeZone
+     @BeforeAll
+     static void initAll() {
+         TimeZone.setDefault(TimeZone.getTimeZone("PST"));
+     }
  
-             gc1.set(Calendar.DATE, 16);
-             gc2.set(Calendar.DATE, 16);
-             if (!gc1.getTime().equals(gc2.getTime())) {
-                 errln("Invalid Time :" + gc2.getTime() +
-                     ", expected :" + gc1.getTime());
-             }
-         } finally {
-             TimeZone.setDefault(saveZone);
+     // Restore JVM default Locale and TimeZone
+     @AfterAll
+     static void tearDownAll() {
+         TimeZone.setDefault(savedTz);
+     }
+ 
+     // Test that a serialized GregorianCalendar has the expected values
+     @Test
+     public void serializationTest() throws IOException, ClassNotFoundException {
+         GregorianCalendar gc1 = new GregorianCalendar(2000, Calendar.OCTOBER, 10);
+         GregorianCalendar gc2;
+         try (ObjectOutputStream out
+                 = new ObjectOutputStream(new FileOutputStream(serializedData))) {
+             out.writeObject(gc1);
          }
+ 
+         try (ObjectInputStream in = new ObjectInputStream(new FileInputStream(serializedData))) {
+             gc2 = (GregorianCalendar)in.readObject();
+         }
+ 
+         gc1.set(Calendar.DATE, 16);
+         gc2.set(Calendar.DATE, 16);
+         assertEquals(gc2.getTime(), gc1.getTime(),
+                 "Times should be equal after serialization");
      }
  }
< prev index next >