/** * 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.api; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.lang.reflect.InvocationTargetException; import java.util.ArrayList; import java.util.List; import org.eclipse.emf.common.util.EList; import org.eclipse.emf.common.util.URI; import org.eclipse.emf.ecore.resource.Resource; import org.eclipse.emf.ecore.resource.ResourceSet; import org.eclipse.emf.ecore.util.EcoreUtil; import org.gecko.rsa.core.EObjectDeSerializationContext; import org.gecko.rsa.core.EndPointDeSerializer; import org.gecko.rsa.model.rsa.DocumentRoot; import org.gecko.rsa.model.rsa.EndpointDescription; import org.gecko.rsa.model.rsa.EndpointDescriptions; import org.gecko.rsa.model.rsa.Property; import org.gecko.rsa.model.rsa.RSAPackage; import org.junit.Test; import org.osgi.util.promise.Promise; public class EndpointDeSerializerTest { @Test public void testSerializeBinaryNew() throws IOException, InvocationTargetException, InterruptedException { EndPointDeSerializer deserializer = new EndPointDeSerializer(EObjectDeSerializationContext.getBinaryDeSerializationContext(), EObjectDeSerializationContext.getBinarySerializationContext()); ResourceSet rs = deserializer.getResourceSet(); assertNotNull(rs); InputStream is = getClass().getResource("ed1.xml").openStream(); Resource resource = rs.createResource(URI.createURI("ed.xml"), RSAPackage.eCONTENT_TYPE); assertNotNull(resource); resource.load(is, null); assertFalse(resource.getContents().isEmpty()); DocumentRoot root = (DocumentRoot) resource.getContents().get(0); EndpointDescriptions edContainer = root.getEndpointDescriptions(); assertNotNull(edContainer); EList endpointDescriptions = edContainer.getEndpointDescription(); assertEquals(4, endpointDescriptions.size()); EndpointDescription ed1 = endpointDescriptions.get(0); List properties = ed1.getProperty(); assertEquals(5, properties.size()); assertNotNull(ed1.eResource()); Promise osResult = deserializer.serialize(ed1); OutputStream os = osResult.getValue(); assertNull(ed1.eResource()); assertNotNull(os); assertTrue(os instanceof ByteArrayOutputStream); byte[] content = ((ByteArrayOutputStream)os).toByteArray(); ByteArrayInputStream bais = new ByteArrayInputStream(content); Promise edResult = deserializer.deserialize(bais); EndpointDescriptions descriptions = edResult.getValue(); assertNull(descriptions.eResource()); assertNotNull(descriptions); endpointDescriptions = descriptions.getEndpointDescription(); assertEquals(1, endpointDescriptions.size()); EndpointDescription ed1Reload = endpointDescriptions.get(0); properties = ed1.getProperty(); assertEquals(5, properties.size()); assertTrue(EcoreUtil.equals(ed1, ed1Reload)); } @Test public void testSerializeXMLNew() throws IOException, InvocationTargetException, InterruptedException { EndPointDeSerializer deserializer = new EndPointDeSerializer(); ResourceSet rs = deserializer.getResourceSet(); assertNotNull(rs); InputStream is = getClass().getResource("ed1.xml").openStream(); Resource resource = rs.createResource(URI.createURI("ed.xml"), RSAPackage.eCONTENT_TYPE); assertNotNull(resource); resource.load(is, null); assertFalse(resource.getContents().isEmpty()); DocumentRoot root = (DocumentRoot) resource.getContents().get(0); EndpointDescriptions edContainer = root.getEndpointDescriptions(); assertNotNull(edContainer); EList endpointDescriptions = edContainer.getEndpointDescription(); assertEquals(4, endpointDescriptions.size()); EndpointDescription ed1 = endpointDescriptions.get(0); List properties = ed1.getProperty(); assertEquals(5, properties.size()); assertNotNull(ed1.eResource()); Promise osResult = deserializer.serialize(ed1); OutputStream os = osResult.getValue(); assertNull(ed1.eResource()); assertTrue(os instanceof ByteArrayOutputStream); byte[] content = ((ByteArrayOutputStream)os).toByteArray(); assertNotNull(content); String contentString = new String(content); assertTrue(contentString.startsWith("")); ByteArrayInputStream bais = new ByteArrayInputStream(content); Promise edResult = deserializer.deserialize(bais); EndpointDescriptions descriptions = edResult.getValue(); assertNull(descriptions.eResource()); assertNotNull(descriptions); endpointDescriptions = descriptions.getEndpointDescription(); assertEquals(1, endpointDescriptions.size()); EndpointDescription ed1Reload = endpointDescriptions.get(0); properties = ed1.getProperty(); assertEquals(5, properties.size()); assertTrue(EcoreUtil.equals(ed1, ed1Reload)); } @Test public void testCollectionDiff() { List orig = new ArrayList(); orig.add("Mark"); orig.add("Jürgen"); List newList = new ArrayList(); newList.add("Mark"); newList.add("Jürgen Albert"); boolean changed = newList.retainAll(orig); assertTrue(changed); assertEquals(1, newList.size()); assertEquals("Mark", newList.get(0)); } }