1 /* 2 * Copyright (c) 2014, 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 /* 25 @test 26 @bug 4422345 8039083 27 @summary tests serialization of DragSourceListeners 28 @author das@sparc.spb.su area=dnd 29 @library /lib/testlibrary 30 */ 31 32 import java.awt.Button; 33 import java.awt.Component; 34 import java.awt.Cursor; 35 import java.awt.Point; 36 import java.awt.Toolkit; 37 import java.awt.datatransfer.StringSelection; 38 import java.awt.dnd.DnDConstants; 39 import java.awt.dnd.DragGestureEvent; 40 import java.awt.dnd.DragGestureRecognizer; 41 import java.awt.dnd.DragSource; 42 import java.awt.dnd.DragSourceAdapter; 43 import java.awt.dnd.DragSourceContext; 44 import java.awt.dnd.DragSourceListener; 45 import java.awt.dnd.DragSourceMotionListener; 46 import java.awt.event.InputEvent; 47 import java.awt.event.MouseEvent; 48 import java.io.ByteArrayInputStream; 49 import java.io.ByteArrayOutputStream; 50 import java.io.ObjectInputStream; 51 import java.io.ObjectOutputStream; 52 import java.io.Serializable; 53 import java.util.Arrays; 54 import java.util.TooManyListenersException; 55 import java.util.stream.Collectors; 56 import java.util.stream.Stream; 57 58 import static jdk.testlibrary.Asserts.assertEquals; 59 60 public class DragSourceListenerSerializationTest { 61 public static void main(String[] args) throws Exception { 62 DragSource ds = new DragSource(); 63 TestDragSourceAdapter dsa1 = new TestDragSourceAdapter(1); 64 TestDragSourceAdapter dsa2 = new TestDragSourceAdapter(2); 65 Component c = new Button(); 66 DragGestureRecognizer dgr = ds.createDefaultDragGestureRecognizer(c, 67 DnDConstants.ACTION_COPY, 68 e -> e.startDrag(null, null)); 69 MouseEvent me = new MouseEvent(c, MouseEvent.MOUSE_PRESSED, 0, 70 InputEvent.CTRL_MASK, 100, 100, 0, false); 71 DragGestureEvent dge = new DragGestureEvent(dgr, DnDConstants.ACTION_COPY, 72 new Point(100, 100), 73 Arrays.asList(me)); 74 DragSourceContext dsc = new DragSourceContext( 75 Toolkit.getDefaultToolkit().createDragSourceContextPeer(dge), 76 dge, 77 new Cursor(Cursor.HAND_CURSOR), 78 null, null, new StringSelection("TEXT"), null); 79 80 ds.addDragSourceListener(dsa1); 81 ds.addDragSourceListener(dsa2); 82 ds.addDragSourceListener(dsa2); 83 ds.addDragSourceMotionListener(dsa1); 84 ds.addDragSourceMotionListener(dsa1); 85 ds.addDragSourceMotionListener(dsa2); 86 dsc.addDragSourceListener(dsa2); 87 88 byte[] serialized; 89 try (ByteArrayOutputStream bos = new ByteArrayOutputStream(); 90 ObjectOutputStream oos = new ObjectOutputStream(bos)) { 91 oos.writeObject(dsc); 92 serialized = bos.toByteArray(); 93 } 94 95 DragSourceContext dsc_copy; 96 try (ByteArrayInputStream bis = new ByteArrayInputStream(serialized); 97 ObjectInputStream ois = new ObjectInputStream(bis)) { 98 dsc_copy = (DragSourceContext) ois.readObject(); 99 } 100 101 try { 102 dsc_copy.addDragSourceListener(dsa1); 103 throw new RuntimeException("Test failed. Listener addition succeeded"); 104 } catch (TooManyListenersException ignored) { 105 } 106 107 try { 108 dsc_copy.addDragSourceListener(dsa2); 109 throw new RuntimeException("Test failed. Listener addition succeeded"); 110 } catch (TooManyListenersException ignored) { 111 } 112 113 try (ByteArrayOutputStream bos = new ByteArrayOutputStream(); 114 ObjectOutputStream oos = new ObjectOutputStream(bos)) { 115 oos.writeObject(ds); 116 serialized = bos.toByteArray(); 117 } 118 119 DragSource ds_copy; 120 try (ByteArrayInputStream bis = new ByteArrayInputStream(serialized); 121 ObjectInputStream ois = new ObjectInputStream(bis)) { 122 ds_copy = (DragSource) ois.readObject(); 123 } 124 125 DragSourceListener[] dsls = ds_copy.getDragSourceListeners(); 126 assertEquals(3, dsls.length, "DragSourceListeners number"); 127 assertEquals(1, Stream.of(dsls).filter(dsa1::equals).collect(Collectors.counting()).intValue()); 128 assertEquals(2, Stream.of(dsls).filter(dsa2::equals).collect(Collectors.counting()).intValue()); 129 130 DragSourceMotionListener[] dsmls = ds_copy.getDragSourceMotionListeners(); 131 assertEquals(3, dsmls.length, "DragSourceMotionListeners number"); 132 assertEquals(2, Stream.of(dsmls).filter(dsa1::equals).collect(Collectors.counting()).intValue()); 133 assertEquals(1, Stream.of(dsmls).filter(dsa2::equals).collect(Collectors.counting()).intValue()); 134 } 135 } 136 137 class TestDragSourceAdapter extends DragSourceAdapter implements Serializable { 138 final int id; 139 140 TestDragSourceAdapter(int id) { 141 this.id = id; 142 } 143 144 public int getId() { 145 return id; 146 } 147 148 public boolean equals(Object obj) { 149 if (obj instanceof TestDragSourceAdapter) { 150 TestDragSourceAdapter tdsa = (TestDragSourceAdapter) obj; 151 return tdsa.getId() == getId(); 152 } 153 return false; 154 } 155 }