1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
|
/*
* Copyright (c) 2002, 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
@bug 4715486
@summary Tests that FocusEvent.opposite is not serialized
@key headful
@run main OppositeSerialization
*/
import java.awt.Button;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.event.FocusEvent;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.lang.reflect.InvocationTargetException;
import static java.lang.Integer.valueOf;
/**
* "This is an AUTOMATIC test",
* "however, that's what it does:",
* "1. It tests that FocusEvent.opposite field is written",
* "to serialized stream as null regardless of whether it",
* "is actually null or not. For this purpose, we serialize",
* "a FocusEvent with really huge opposite, and then check",
* "if serialized object is huge or not.",
* "2. It tests that FocusEvent.opposite deserializes as",
* "null, even if it was serialized in the previous version",
* "of JDK. For this purpose, file old.ser is included into",
* "test. It is FocusEvent serialized with 1.4, with non-null",
* "opposite. We check that after deserialization opposite",
* "field is null"
*/
public class OppositeSerialization {
static Button b1;
static Frame b2;
public static void main(String[] args) throws InterruptedException,
InvocationTargetException {
EventQueue.invokeAndWait(() -> {
b1 = new Button("OppositeSerialization - Source");
b2 = new Frame("OppositeSerialization - Opposite");
b2.setLayout(new FlowLayout());
for (int i = 0; i < 10000; i++) {
String s = (valueOf(i)).toString();
b2.add(new Button("Button" + s));
}
});
FocusEvent evt1 = new FocusEvent(b1, FocusEvent.FOCUS_GAINED, false, b2);
/*
* Here we test that opposite component isn't serialized.
* We created a really huge opposite component for a focus
* event evt1 and now we'll see if the size of serialized data
* is big.
*/
try {
FileOutputStream fos = new FileOutputStream("new.ser");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(evt1);
oos.flush();
} catch (Exception e1) {
System.out.println("Sorry! Couldn't write the stream");
System.out.println("The test failed, but the reason is " +
"unrelated to the subject");
throw new RuntimeException("The test couldn't write serialized data");
}
File file = new File("new.ser");
if (file.length() > 50000) {
System.out.println("The test failed: serialized " +
"FocusEvent too huge");
System.err.println("Serialized FocusEvent is too huge.");
System.err.println("Probably opposite field is " +
"serialized incorrectly.");
throw new RuntimeException("Serialized FocusEvent is too huge");
}
/*
* Here we test that opposite is not deserialized even if it is present
* in the stream. old.ser is created with JDK1.4 using the following
* source code:
*
* import java.awt.event.*;
* import java.io.*;
* import java.awt.*;
*
* public class OldFocusSerializer {
*
* public static void main(String[] args) {
*
* Button b1 = new Button("Source");
* Button b2 = new Button("Opposite");
*
* FocusEvent evt1 = new FocusEvent(b1,
* FocusEvent.FOCUS_GAINED,
* false,
* b2);
*
* try {
* FileOutputStream fos = new FileOutputStream("old.ser");
* ObjectOutputStream oos = new ObjectOutputStream(fos);
* oos.writeObject(evt1);
* oos.flush();
* } catch (IOException e) {
* System.out.println("Sorry! Couldn't write the stream");
* }
* }
* }
*/
FocusEvent evt2;
String testPath = System.getProperty("test.src", ".");
try {
FileInputStream fis = new FileInputStream(testPath +
File.separator + "old.ser");
ObjectInputStream ois = new ObjectInputStream(fis);
evt2 = (FocusEvent)ois.readObject();
} catch (Exception e2) {
System.out.println("The test failed as it couldn't read the stream");
throw new RuntimeException("The test couldn't read serialized data");
}
if (evt2.getOppositeComponent() != null) {
System.out.println("The test failed: opposite component " +
"deserialized to non-null value");
System.err.println("FocusEvent stored in old.ser should have " +
"null opposite field.");
throw new RuntimeException("Non-null opposite component " +
"after deserialization");
}
if (b2 != null) {
EventQueue.invokeAndWait(() -> b2.dispose());
}
System.out.println("The test passed");
}
}
|