Skip to content

Commit

Permalink
8077371: Binary files in JAXP test should be removed
Browse files Browse the repository at this point in the history
Reviewed-by: joehw
  • Loading branch information
Mahendra Chhipa committed Oct 20, 2023
1 parent fe52917 commit 4010642
Show file tree
Hide file tree
Showing 16 changed files with 860 additions and 120 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* Copyright (c) 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.
*
* 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.
*/

/**
* This class provide the template for JDK version specific GregorianCalendarAndDurationSerData.java src file.
*/
public class GregorianCalAndDurSerDataTemplate {
public static final String ORACLE_COPY_RIGHT = """
/*
* Copyright (c) %s, 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.
*/
\s
/**
* Mechanically generated %s specific serialization bytes for XMLGregorianCalendar and Duration data type.
* Do not edit this file.
*/""";
public static final String GREGO_CAL_DUR_SER_CLASS = """
public class %sGregorianCalendarAndDurationSerData extends GregorianCalendarAndDurationSerData {
%s
%s
@Override
public byte[] getGregorianCalendarByteArray() {
return gregorianCalendarBytes;
}
\s
@Override
public byte[] getDurationBytes() {
return durationBytes;
}
};""";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
/*
* Copyright (c) 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.
*
* 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.
*/

/**
* @test
* @summary utility to generate Gregorian Calendar and Duration serialized data java classes.
* @run junit/manual GregorianCalAndDurSerDataUtil
*/

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.time.LocalDate;
import java.util.Formatter;

import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.Duration;
import javax.xml.datatype.XMLGregorianCalendar;

import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;

/**
* Utility to generate the java source file for Gregorian Calendar and Duration serialized data
* for specific version of JDK to be added in SerializationTest. Execute this test with desired version
* of JDK to generate the java source file.
*/
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
public class GregorianCalAndDurSerDataUtil {
static String JDK = "JDK" + System.getProperty("java.version");
static String testsrc = System.getProperty("test.src");
final static String EXPECTED_CAL = "0001-01-01T00:00:00.0000000-05:00";
final static String EXPECTED_DURATION = "P1Y1M1DT1H1M1S";
String srcFilePrefix = JDK.toUpperCase().replace("-", "_");


/**
* Create the serialized Bytes array and serialized bytes base64 string for GregorianCalender and Duration
* with jdk under test and generate the java source file.
* @throws DatatypeConfigurationException Unexpected.
* @throws IOException Unexpected.
*/
@BeforeAll
public void setup() throws DatatypeConfigurationException, IOException {
DatatypeFactory dtf = DatatypeFactory.newInstance();
XMLGregorianCalendar xmlGregorianCalendar = dtf.newXMLGregorianCalendar(EXPECTED_CAL);
Duration duration = dtf.newDuration(EXPECTED_DURATION);
String copyRightStr = GregorianCalAndDurSerDataTemplate.ORACLE_COPY_RIGHT;
String classStr = GregorianCalAndDurSerDataTemplate.GREGO_CAL_DUR_SER_CLASS;
try(ByteArrayOutputStream baos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(baos);
ByteArrayOutputStream baos2 = new ByteArrayOutputStream(); ObjectOutputStream oos2 = new ObjectOutputStream(baos2)) {
//Serialize the given xmlGregorianCalendar
oos.writeObject(xmlGregorianCalendar);
//Serialize the given xml Duration
oos2.writeObject(duration);
Files.deleteIfExists(Path.of(testsrc,srcFilePrefix+"GregorianCalendarAndDurationSerData.java"));

copyRightStr = String.format(copyRightStr, LocalDate.now().getYear(), JDK);
classStr = String.format(classStr, srcFilePrefix, generatePseudoCodeForGregCalSerBytes(baos),
generatePseudoCodeForDurationSerBytes(baos2));
String srcStr = copyRightStr + "\n" + classStr;
Files.writeString(Path.of(testsrc,srcFilePrefix+"GregorianCalendarAndDurationSerData.java"), srcStr);
}
}

/**
* Verify that Java source file is created.
*/
@Test
void testFileCreated() {
assertTrue(Files.exists(Path.of(testsrc,srcFilePrefix+"GregorianCalendarAndDurationSerData.java")));
}

/**
* Generates the Java Pseudo code for serialized Gregorian Calendar byte array.
* @param baos Serialized GregorianCalendar ByteArrayOutputStream.
* @return pseudocode String for serialized Gregorian Calendar byte array.
*/
public static String generatePseudoCodeForGregCalSerBytes(ByteArrayOutputStream baos) {
byte [] bytes = baos.toByteArray();
StringBuilder sb = new StringBuilder(bytes.length * 5);
sb.append("private final byte[] gregorianCalendarBytes = {");
return generatePseudoCode(sb, bytes);
}

/**
* Generates the Java Pseudo code for serialized Duration byte array.
* @param baos Serialized Duration ByteArrayOutputStream.
* @return pseudocode String for serialized Duration byte array.
*/
public static String generatePseudoCodeForDurationSerBytes(ByteArrayOutputStream baos) {
byte [] bytesdur = baos.toByteArray();
StringBuilder sb = new StringBuilder(bytesdur.length * 5);
sb.append("private final byte[] durationBytes = {");
return generatePseudoCode(sb, bytesdur);
}

private static String generatePseudoCode(StringBuilder sb, byte [] bytes) {
final int linelen = 8;
// HexFormat hex = HexFormat.of().withPrefix(" (byte) 0x").withSuffix(",");
// for (int i = 0; i < bytes.length; i += linelen) {
// sb.append("\n");
// sb.append(hex.formatHex(bytes, i, Math.min(i + linelen, bytes.length)));
// }
// sb.append("};");
Formatter fmt = new Formatter(sb);
for (int i = 0; i <bytes.length; i++) {
if (i % linelen == 0) {
fmt.format("%n ");
}
fmt.format(" (byte) 0x%x,", bytes[i] & 0xff);
}
fmt.format("%n };%n");
return sb.toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright (c) 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.
*
* 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.
*/

/**
* Abstract class for serialized bytes of XMLGregorianCalendar and Duration data type for different JDK versions.
*/
public abstract class GregorianCalendarAndDurationSerData {
//Returns byte array of serialized GregorianCalendar.
public abstract byte[] getGregorianCalendarByteArray();

//Returns byte array of serialized Duration.
public abstract byte[] getDurationBytes();
}

3 comments on commit 4010642

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@GoeLin
Copy link
Member

@GoeLin GoeLin commented on 4010642 Jan 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/backport jdk21u-dev

@openjdk
Copy link

@openjdk openjdk bot commented on 4010642 Jan 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@GoeLin the backport was successfully created on the branch backport-GoeLin-40106422 in my personal fork of openjdk/jdk21u-dev. To create a pull request with this backport targeting openjdk/jdk21u-dev:master, just click the following link:

➡️ Create pull request

The title of the pull request is automatically filled in correctly and below you find a suggestion for the pull request body:

Hi all,

This pull request contains a backport of commit 40106422 from the openjdk/jdk repository.

The commit being backported was authored by Mahendra Chhipa on 20 Oct 2023 and was reviewed by Joe Wang.

Thanks!

If you need to update the source branch of the pull then run the following commands in a local clone of your personal fork of openjdk/jdk21u-dev:

$ git fetch https://github.com/openjdk-bots/jdk21u-dev.git backport-GoeLin-40106422:backport-GoeLin-40106422
$ git checkout backport-GoeLin-40106422
# make changes
$ git add paths/to/changed/files
$ git commit --message 'Describe additional changes made'
$ git push https://github.com/openjdk-bots/jdk21u-dev.git backport-GoeLin-40106422

Please sign in to comment.