1 /* 2 * Copyright (c) 2012, 2013, 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. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package javafx.event; 27 28 import java.io.ByteArrayInputStream; 29 import java.io.ByteArrayOutputStream; 30 import java.io.IOException; 31 import java.io.InvalidObjectException; 32 import java.io.ObjectInputStream; 33 import java.io.ObjectOutputStream; 34 import java.util.ArrayList; 35 import java.util.List; 36 import javafx.event.EventType.EventTypeSerialization; 37 import org.junit.Before; 38 import org.junit.Test; 39 40 import static org.junit.Assert.*; 41 import org.junit.Ignore; 42 43 /** 44 * 45 */ 46 public class EventSerializationTest { 47 private ByteArrayOutputStream byteArrayOutputStream; 48 private ObjectOutputStream objectOutputStream; 49 private ObjectInputStream objectInputStream; 50 51 @Before 52 public void setUp() throws IOException { 53 byteArrayOutputStream = new ByteArrayOutputStream(); 54 objectOutputStream = new ObjectOutputStream(byteArrayOutputStream); 55 } 56 57 public void turnToInput() throws IOException { 58 objectInputStream = new ObjectInputStream(new ByteArrayInputStream(byteArrayOutputStream.toByteArray())); 59 } 60 61 @Test 62 public void testPreDefinedEventSerialization() throws IOException, ClassNotFoundException { 63 ActionEvent a = new ActionEvent(); 64 65 objectOutputStream.writeObject(a); 66 turnToInput(); 67 68 ActionEvent ra = (ActionEvent) objectInputStream.readObject(); 69 assertEquals(a.getEventType(), ra.getEventType()); 70 assertEquals(a.isConsumed(), ra.isConsumed()); 71 assertEquals(Event.NULL_SOURCE_TARGET, ra.getSource()); 72 assertEquals(Event.NULL_SOURCE_TARGET, ra.getTarget()); 73 } 74 75 @Test 76 public void testNewEventTypeSerialization() throws IOException, ClassNotFoundException { 77 EventType<Event> eventType = new EventType<Event>(Event.ANY, "MY_TYPE"); 78 Event e = new Event(eventType); 79 80 objectOutputStream.writeObject(e); 81 turnToInput(); 82 Event re = (Event) objectInputStream.readObject(); 83 assertEquals(e.getEventType(), re.getEventType()); 84 assertEquals(e.isConsumed(), re.isConsumed()); 85 assertEquals(Event.NULL_SOURCE_TARGET, re.getSource()); 86 assertEquals(Event.NULL_SOURCE_TARGET, re.getTarget()); 87 88 } 89 90 @Test(expected=InvalidObjectException.class) 91 public void testUnknownEventTypeSerialization() throws IOException, ClassNotFoundException { 92 List<String> l = new ArrayList<String>(); 93 l.add("UNKNOWN"); 94 EventTypeSerialization e = new EventTypeSerialization(l); 95 96 objectOutputStream.writeObject(e); 97 turnToInput(); 98 EventType eType = (EventType) objectInputStream.readObject(); 99 100 } 101 }