/* * Copyright (c) 2015 Guillaume Hillairet. * 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: * Guillaume Hillairet - initial API and implementation * */ package org.emfjson.jackson.tests; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import org.eclipse.emf.common.util.EList; import org.eclipse.emf.common.util.URI; import org.eclipse.emf.ecore.*; import org.eclipse.emf.ecore.resource.Resource; import org.eclipse.emf.ecore.resource.ResourceSet; import org.emfjson.jackson.support.StandardFixture; import org.junit.ClassRule; import org.junit.Test; import java.io.ByteArrayInputStream; import java.io.IOException; import static org.junit.Assert.assertEquals; public class AnnotationTest { @ClassRule public static StandardFixture fixture = new StandardFixture(); private ObjectMapper mapper = fixture.mapper(); private ResourceSet resourceSet = fixture.getResourceSet(); @Test public void testSaveAnnotation() throws IOException { JsonNode expected = mapper.createObjectNode() .put("eClass", "http://www.eclipse.org/emf/2002/Ecore#//EClass") .put("name", "Foo") .set("eAnnotations", mapper.createArrayNode() .add(mapper.createObjectNode() .put("source", "source") .set("details", mapper.createObjectNode() .put("displayName", "value")))); EClass eClass = EcoreFactory.eINSTANCE.createEClass(); eClass.setName("Foo"); EAnnotation eAnnotation = EcoreFactory.eINSTANCE.createEAnnotation(); eAnnotation.setEModelElement(eClass); eAnnotation.setSource("source"); eAnnotation.getDetails().put("displayName", "value"); eClass.getEAnnotations().add(eAnnotation); Resource resource = resourceSet.createResource(URI.createURI("test.json")); resource.getContents().add(eClass); JsonNode result = mapper.valueToTree(resource); assertEquals(expected, result); } @Test public void testLoadAnnotation() throws IOException { JsonNode data = mapper.createObjectNode() .put("eClass", "http://www.eclipse.org/emf/2002/Ecore#//EClass") .put("name", "Foo") .set("eAnnotations", mapper.createArrayNode() .add(mapper.createObjectNode() .put("eClass", "http://www.eclipse.org/emf/2002/Ecore#//EAnnotation") .put("source", "source") .set("details", mapper.createObjectNode() .put("displayName", "value")))); Resource resource = resourceSet.createResource(URI.createURI("test.json")); resource.load(new ByteArrayInputStream(mapper.writeValueAsBytes(data)), null); assertEquals(1, resource.getContents().size()); EObject root = resource.getContents().get(0); assertEquals(EcorePackage.Literals.ECLASS, root.eClass()); EList<EAnnotation> annotations = ((EClass) root).getEAnnotations(); assertEquals(1, annotations.size()); EAnnotation annotation = annotations.get(0); assertEquals(1, annotation.getDetails().size()); assertEquals("displayName", annotation.getDetails().get(0).getKey()); assertEquals("value", annotation.getDetails().get(0).getValue()); } }