1 /* 2 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 import java.awt.AWTKeyStroke; 25 import java.awt.event.InputEvent; 26 import java.io.ByteArrayInputStream; 27 import java.io.ByteArrayOutputStream; 28 import java.io.ObjectInput; 29 import java.io.ObjectInputStream; 30 import java.io.ObjectOutput; 31 import java.io.ObjectOutputStream; 32 import javax.swing.KeyStroke; 33 34 /* 35 * @test 36 * @bug 8133453 37 * @summary Remove AWTKeyStroke.registerSubclass(Class) method 38 * @author Alexander Scherbatiy 39 * @run main/othervm TestAWTKeyStroke 40 * @run main/othervm/policy=policy -Djava.security.manager TestAWTKeyStroke 41 */ 42 public class TestAWTKeyStroke { 43 44 public static void main(String[] args) throws Exception { 45 46 int modifiers = InputEvent.CTRL_MASK | InputEvent.CTRL_DOWN_MASK; 47 checkAWTKeyStroke('A', modifiers, true); 48 checkKeyStroke('B', modifiers, false); 49 checkAWTKeyStroke('C', modifiers, false); 50 checkKeyStroke('D', modifiers, true); 51 checkSerializedKeyStrokes('E', modifiers, true); 52 } 53 54 private static void checkAWTKeyStroke(int keyCode, int modifiers, 55 boolean onKeyRelease) throws Exception { 56 57 AWTKeyStroke awtKeyStroke1 = AWTKeyStroke.getAWTKeyStroke( 58 keyCode, modifiers, onKeyRelease); 59 60 checkAWTKeyStroke(awtKeyStroke1, keyCode, modifiers, onKeyRelease); 61 62 AWTKeyStroke awtKeyStroke2 = AWTKeyStroke.getAWTKeyStroke( 63 keyCode, modifiers, onKeyRelease); 64 65 if (awtKeyStroke1 != awtKeyStroke2) { 66 throw new RuntimeException("AWTKeyStroke is not cached!"); 67 } 68 69 checkSerializedKeyStroke(awtKeyStroke1); 70 } 71 72 private static void checkKeyStroke(int keyCode, int modifiers, 73 boolean onKeyRelease) throws Exception { 74 75 KeyStroke keyStroke1 = KeyStroke.getKeyStroke( 76 keyCode, modifiers, onKeyRelease); 77 checkAWTKeyStroke(keyStroke1, keyCode, modifiers, onKeyRelease); 78 79 KeyStroke keyStroke2 = KeyStroke.getKeyStroke( 80 keyCode, modifiers, onKeyRelease); 81 82 if (keyStroke1 != keyStroke2) { 83 throw new RuntimeException("KeyStroke is not cached!"); 84 } 85 86 checkSerializedKeyStroke(keyStroke1); 87 } 88 89 private static void checkSerializedKeyStrokes(int keyCode, int modifiers, 90 boolean onKeyRelease) throws Exception { 91 92 AWTKeyStroke awtKeyStroke = AWTKeyStroke.getAWTKeyStroke( 93 keyCode, modifiers, onKeyRelease); 94 95 KeyStroke keyStroke = KeyStroke.getKeyStroke( 96 keyCode, modifiers, onKeyRelease); 97 98 if (awtKeyStroke != getSerializedAWTKeyStroke(awtKeyStroke)) { 99 throw new RuntimeException("Serialized AWTKeyStroke is not cached!"); 100 } 101 102 awtKeyStroke = AWTKeyStroke.getAWTKeyStroke( 103 keyCode, modifiers, !onKeyRelease); 104 105 if (!keyStroke.equals(getSerializedAWTKeyStroke(keyStroke))) { 106 throw new RuntimeException("Serialized KeyStroke is not cached!"); 107 } 108 } 109 110 private static void checkAWTKeyStroke(AWTKeyStroke awtKeyStroke, 111 int keyCode, int modifiers, boolean onKeyRelease) { 112 113 if (awtKeyStroke.getKeyCode() != keyCode) { 114 throw new RuntimeException("Wrong key code!"); 115 } 116 117 if (awtKeyStroke.getModifiers() != modifiers) { 118 throw new RuntimeException("Wrong modifiers!"); 119 } 120 121 if (awtKeyStroke.isOnKeyRelease() != onKeyRelease) { 122 throw new RuntimeException("Wrong on key release!"); 123 } 124 } 125 126 private static void checkSerializedKeyStroke(AWTKeyStroke keyStroke) 127 throws Exception { 128 if (keyStroke != getSerializedAWTKeyStroke(keyStroke)) { 129 throw new RuntimeException("New instance is returned during" 130 + " serialization!"); 131 } 132 } 133 134 private static AWTKeyStroke getSerializedAWTKeyStroke(AWTKeyStroke keyStroke) 135 throws Exception { 136 137 try (ByteArrayOutputStream bos = new ByteArrayOutputStream(); 138 ObjectOutput out = new ObjectOutputStream(bos)) { 139 out.writeObject(keyStroke); 140 byte[] bytes = bos.toByteArray(); 141 142 try (ByteArrayInputStream bis = new ByteArrayInputStream(bytes); 143 ObjectInput in = new ObjectInputStream(bis)) { 144 return (AWTKeyStroke) in.readObject(); 145 } 146 } 147 } 148 }