/** * Copyright (c) 2012 - 2018 Data In Motion and others. * All rights reserved. * * This program and the accompanying materials are made available under the terms of the * Eclipse Public License v1.0 which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * Data In Motion - initial API and implementation */ package org.gecko.rsa.provider; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; import java.util.LinkedHashMap; import java.util.LinkedList; import java.util.List; import java.util.Map; import org.eclipse.emf.ecore.EObject; import org.gecko.rsa.api.DeSerializationContext; import org.gecko.rsa.api.DeSerializer; import org.gecko.rsa.api.SerializationContext; import org.gecko.rsa.api.Serializer; import org.gecko.rsa.provider.pojo.Address; import org.gecko.rsa.provider.pojo.Person; import org.gecko.rsa.provider.pojo.SpecialPerson; import org.gecko.rsa.provider.ser.EObjectDeSerializer; import org.gecko.rsa.provider.stream.EObjectInputStream; import org.gecko.rsa.provider.stream.EObjectOutputStream; import org.gecko.rsa.rsatest.Contact; import org.gecko.rsa.rsatest.ContactType; import org.gecko.rsa.rsatest.ContextType; import org.gecko.rsa.rsatest.RSATestFactory; import org.junit.Test; /** * * @author mark * @since 31.07.2018 */ public class DataSerializationTest { private final ClassLoader loader = getClass().getClassLoader(); @Test public void testDataInputOutput() { Person p = new Person(); p.setFirstName("Mark"); p.setLastName("Hoffmann"); Address a = new Address(); a.setCity("Gera"); a.setStreet("KKS"); a.setZip("07545"); p.setAddress(a); ByteArrayOutputStream baos = new ByteArrayOutputStream(); try (ObjectOutputStream oos = new ObjectOutputStream(baos)) { oos.writeObject(p); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); try (ObjectInputStream ois = new ObjectInputStream(bais)) { Object o = ois.readObject(); assertTrue(o instanceof Person); Person pr = (Person) o; assertEquals(p.getFirstName(), pr.getFirstName()); assertEquals(p.getLastName(), pr.getLastName()); Address ar = pr.getAddress(); assertNotNull(ar); assertEquals(a.getCity(), ar.getCity()); assertEquals(a.getStreet(), ar.getStreet()); assertEquals(a.getZip(), ar.getZip()); } catch (IOException e) { fail("IOError"); } catch (ClassNotFoundException e) { fail("ClassNotFound"); } } @Test public void testEObjectInputOutput() { Person p = new Person(); p.setFirstName("Mark"); p.setLastName("Hoffmann"); Address a = new Address(); a.setCity("Gera"); a.setStreet("KKS"); a.setZip("07545"); p.setAddress(a); Contact contact01 = RSATestFactory.eINSTANCE.createContact(); contact01.setContext(ContextType.PRIVATE); contact01.setDescription("contact 1 description"); Serializer serializer = new EObjectDeSerializer(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); try (EObjectOutputStream oos = new EObjectOutputStream(baos, serializer)) { oos.writeObject(p); oos.writeObject(contact01); } catch (IOException e) { fail("IOError"); } DeSerializer deserializer = new EObjectDeSerializer(); ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); try (EObjectInputStream ois = new EObjectInputStream(bais, loader, deserializer)) { Object o = ois.readObject(); assertTrue(o instanceof Person); Person pr = (Person) o; assertEquals(p.getFirstName(), pr.getFirstName()); assertEquals(p.getLastName(), pr.getLastName()); Address ar = pr.getAddress(); assertNotNull(ar); assertEquals(a.getCity(), ar.getCity()); assertEquals(a.getStreet(), ar.getStreet()); assertEquals(a.getZip(), ar.getZip()); Object eo = ois.readObject(); assertTrue(eo instanceof Contact); Contact contact = (Contact) eo; assertEquals(contact01.getType(), contact.getType()); assertEquals(contact01.getDescription(), contact.getDescription()); } catch (IOException e) { fail("IOError"); } catch (ClassNotFoundException e) { fail("ClassNotFound"); } } @Test public void testEObjectInputOutputLists() { Person p = new Person(); p.setFirstName("Mark"); p.setLastName("Hoffmann"); Address a = new Address(); a.setCity("Gera"); a.setStreet("KKS"); a.setZip("07545"); p.setAddress(a); Contact contact01 = RSATestFactory.eINSTANCE.createContact(); contact01.setContext(ContextType.PRIVATE); contact01.setDescription("contact 1 description"); Contact contact02 = RSATestFactory.eINSTANCE.createContact(); contact02.setContext(ContextType.WORK); contact02.setDescription("contact 2 description"); List requests = new ArrayList<>(2); requests.add(contact01); requests.add(contact02); Serializer serializer = new EObjectDeSerializer(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); try (EObjectOutputStream oos = new EObjectOutputStream(baos, serializer)) { oos.writeObject(p); oos.writeObject(requests); } catch (IOException e) { fail("IOError"); } DeSerializer deserializer = new EObjectDeSerializer(); ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); try (EObjectInputStream ois = new EObjectInputStream(bais, loader, deserializer)) { Object o = ois.readObject(); assertTrue(o instanceof Person); Person pr = (Person) o; assertEquals(p.getFirstName(), pr.getFirstName()); assertEquals(p.getLastName(), pr.getLastName()); Address ar = pr.getAddress(); assertNotNull(ar); assertEquals(a.getCity(), ar.getCity()); assertEquals(a.getStreet(), ar.getStreet()); assertEquals(a.getZip(), ar.getZip()); Object eo = ois.readObject(); assertTrue(eo instanceof List); List list = (List) eo; assertEquals(2, list.size()); Contact rsaRequest01 = (Contact) list.get(0); assertEquals(contact01.getType(), rsaRequest01.getType()); assertEquals(contact01.getDescription(), rsaRequest01.getDescription()); Contact rsaRequest02 = (Contact) list.get(1); assertEquals(contact02.getType(), rsaRequest02.getType()); assertEquals(contact02.getDescription(), rsaRequest02.getDescription()); } catch (IOException e) { fail("IOError"); } catch (ClassNotFoundException e) { fail("ClassNotFound"); } } @Test public void testEObjectInputOutputListOnly() { Contact contact01 = RSATestFactory.eINSTANCE.createContact(); contact01.setContext(ContextType.PRIVATE); contact01.setDescription("contact 1 description"); Contact contact02 = RSATestFactory.eINSTANCE.createContact(); contact02.setContext(ContextType.WORK); contact02.setDescription("contact 2 description"); List requests = new ArrayList<>(2); requests.add(contact01); requests.add(contact02); Serializer serializer = new EObjectDeSerializer(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); try (EObjectOutputStream oos = new EObjectOutputStream(baos, serializer)) { oos.writeObject(requests); } catch (IOException e) { fail("IOError"); } DeSerializer deserializer = new EObjectDeSerializer(); ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); try (EObjectInputStream ois = new EObjectInputStream(bais, loader, deserializer)) { Object eo = ois.readObject(); assertTrue(eo instanceof List); List list = (List) eo; assertEquals(2, list.size()); Contact rsaRequest01 = (Contact) list.get(0); assertEquals(contact01.getType(), rsaRequest01.getType()); assertEquals(contact01.getDescription(), rsaRequest01.getDescription()); Contact rsaRequest02 = (Contact) list.get(1); assertEquals(contact02.getType(), rsaRequest02.getType()); assertEquals(contact02.getDescription(), rsaRequest02.getDescription()); } catch (IOException e) { fail("IOError"); } catch (ClassNotFoundException e) { fail("ClassNotFound"); } } @Test public void testEObjectInputOutputMaps() { Person p = new Person(); p.setFirstName("Mark"); p.setLastName("Hoffmann"); Address a = new Address(); a.setCity("Gera"); a.setStreet("KKS"); a.setZip("07545"); p.setAddress(a); Contact contact01 = RSATestFactory.eINSTANCE.createContact(); contact01.setContext(ContextType.PRIVATE); contact01.setDescription("contact 1 description"); Contact contact02 = RSATestFactory.eINSTANCE.createContact(); contact02.setContext(ContextType.WORK); contact02.setDescription("contact 2 description"); List requests = new ArrayList<>(2); requests.add(contact01); requests.add(contact02); Map requestMap = new LinkedHashMap(2); Map personMap = new HashMap(); requestMap.put(contact01, p); requestMap.put(contact02, p); personMap.put(p, contact02); Serializer serializer = new EObjectDeSerializer(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); try (EObjectOutputStream oos = new EObjectOutputStream(baos, serializer)) { oos.writeObject(p); oos.writeObject(requestMap); oos.writeObject(personMap); } catch (IOException e) { fail("IOError"); } DeSerializer deserializer = new EObjectDeSerializer(); ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); try (EObjectInputStream ois = new EObjectInputStream(bais, loader, deserializer)) { Object o = ois.readObject(); assertTrue(o instanceof Person); Person pr = (Person) o; assertEquals(p.getFirstName(), pr.getFirstName()); assertEquals(p.getLastName(), pr.getLastName()); Address ar = pr.getAddress(); assertNotNull(ar); assertEquals(a.getCity(), ar.getCity()); assertEquals(a.getStreet(), ar.getStreet()); assertEquals(a.getZip(), ar.getZip()); Object eo = ois.readObject(); assertTrue(eo instanceof Map); Map map = (Map) eo; assertEquals(2, map.size()); Collection key = map.keySet(); List keyList = new LinkedList<>(key); Contact rsaRequest01 = (Contact) keyList.get(0); assertEquals(contact01.getType(), rsaRequest01.getType()); assertEquals(contact01.getDescription(), rsaRequest01.getDescription()); Contact rsaRequest02 = (Contact) keyList.get(1); assertEquals(contact02.getType(), rsaRequest02.getType()); assertEquals(contact02.getDescription(), rsaRequest02.getDescription()); Person pResult01 = (Person) map.get(rsaRequest01); assertNotNull(pResult01); assertEquals(pResult01, pr); Person pResult02 = (Person) map.get(rsaRequest02); assertNotNull(pResult02); assertEquals(pResult02, pr); eo = ois.readObject(); assertTrue(eo instanceof Map); map = (Map) eo; assertEquals(1, map.size()); assertNotNull(map.get(pResult02)); assertEquals(rsaRequest02, map.get(pResult02)); } catch (IOException e) { fail("IOError"); } catch (ClassNotFoundException e) { fail("ClassNotFound"); } } @Test public void testEObjectInputOutputMixed() { SpecialPerson p = new SpecialPerson(); p.setFirstName("Mark"); p.setLastName("Hoffmann"); Address a = new Address(); a.setCity("Gera"); a.setStreet("KKS"); a.setZip("07545"); p.setAddress(a); Contact request01 = RSATestFactory.eINSTANCE.createContact(); request01.setType(ContactType.MOBILE); request01.setDescription("echo"); p.setEMFContact(request01); Serializer serializer = new EObjectDeSerializer(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); try (EObjectOutputStream oos = new EObjectOutputStream(baos, serializer)) { oos.writeObject(p); } catch (IOException e) { fail("IOError"); } DeSerializer deserializer = new EObjectDeSerializer(); ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); try (EObjectInputStream ois = new EObjectInputStream(bais, loader, deserializer)) { Object o = ois.readObject(); assertTrue(o instanceof Person); assertTrue(o instanceof SpecialPerson); SpecialPerson pr = (SpecialPerson) o; assertEquals(p.getFirstName(), pr.getFirstName()); assertEquals(p.getLastName(), pr.getLastName()); Address ar = pr.getAddress(); assertNotNull(ar); assertEquals(a.getCity(), ar.getCity()); assertEquals(a.getStreet(), ar.getStreet()); assertEquals(a.getZip(), ar.getZip()); assertNotNull(p.getEMFContact()); Contact contact01 = p.getEMFContact(); assertEquals(request01.getType(), contact01.getType()); assertEquals(request01.getDescription(), contact01.getDescription()); } catch (IOException e) { fail("IOError"); } catch (ClassNotFoundException e) { fail("ClassNotFound"); } } }