/* * Copyright (c) 2015, 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. * * This code 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 General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * 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.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.time.ZoneId; import java.util.Base64; import java.util.Locale; import java.util.TimeZone; import java.util.logging.Level; import java.util.logging.LogRecord; import java.util.logging.SimpleFormatter; import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.stream.Stream; /** * @test * @bug 8072645 * @summary tests the compatibility of LogRecord serial form between * JDK 8 and JDK 9. Ideally this test should be run on both platforms. * (It is designed to run on both). * @run main/othervm SerializeLogRecord * @author danielfuchs */ public class SerializeLogRecord { /** * Serializes a log record, encode the serialized bytes in base 64, and * prints pseudo java code that can be cut and pasted into this test. * @param record the log record to serialize, encode in base 64, and for * which test data will be generated. * @return A string containing the generated pseudo java code. * @throws IOException Unexpected. * @throws ClassNotFoundException Unexpected. */ public static String generate(LogRecord record) throws IOException, ClassNotFoundException { // Format the given logRecord using the SimpleFormatter SimpleFormatter formatter = new SimpleFormatter(); String str = formatter.format(record); // Serialize the given LogRecord final ByteArrayOutputStream baos = new ByteArrayOutputStream(); final ObjectOutputStream oos = new ObjectOutputStream(baos); oos.writeObject(record); oos.flush(); oos.close(); // Now we're going to perform a number of smoke tests before // generating the Java pseudo code. // // First checks that the log record can be deserialized final ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); final ObjectInputStream ois = new ObjectInputStream(bais); final LogRecord record2 = (LogRecord)ois.readObject(); // Format the deserialized LogRecord using the SimpleFormatter, and // check that the string representation obtained matches the string // representation of the original LogRecord String str2 = formatter.format(record2); if (!str.equals(str2)) throw new RuntimeException("Unexpected values in deserialized object:" + "\n\tExpected: " + str + "\n\tRetrieved: "+str); // Now get a Base64 string representation of the serialized bytes. final String base64 = Base64.getEncoder().encodeToString(baos.toByteArray()); // Check that we can deserialize a log record from the Base64 string // representation we just computed. final ByteArrayInputStream bais2 = new ByteArrayInputStream(Base64.getDecoder().decode(base64)); final ObjectInputStream ois2 = new ObjectInputStream(bais2); final LogRecord record3 = (LogRecord)ois2.readObject(); // Format the new deserialized LogRecord using the SimpleFormatter, and // check that the string representation obtained matches the string // representation of the original LogRecord String str3 = formatter.format(record3); if (!str.equals(str3)) throw new RuntimeException("Unexpected values in deserialized object:" + "\n\tExpected: " + str + "\n\tRetrieved: "+str); //System.out.println(base64); //System.out.println(); // Generates the Java Pseudo code that can be cut & pasted into // this test (see Jdk8SerializedLog and Jdk9SerializedLog below) final StringBuilder sb = new StringBuilder(); sb.append(" /**").append('\n'); sb.append(" * Base64 encoded string for LogRecord object.").append('\n'); sb.append(" * Java version: ").append(System.getProperty("java.version")).append('\n'); sb.append(" **/").append('\n'); sb.append(" final String base64 = ").append("\n "); final int last = base64.length() - 1; for (int i=0; i TestCase.valueOf(x)).forEach((x) -> { switch(x) { case GENERATE: generate(); break; case TESTJDK8: Jdk8SerializedLog.test(); break; case TESTJDK9: Jdk9SerializedLog.test(); break; } }); } }