From c12ce64f63deb8cbb97ac9b80068fa60622e1041 Mon Sep 17 00:00:00 2001 From: ghillairet Date: Mon, 21 Mar 2016 09:03:26 +0100 Subject: [PATCH] [#81] attribute with null as value is not checked --- .../databind/deser/EObjectDeserializer.java | 81 ++++++++++--------- .../org/emfjson/jackson/junit/TestSuite.java | 6 +- .../junit/tests/NullValueInBetweenTest.java | 44 ---------- .../jackson/junit/tests/ValueTest.java | 38 +++++++++ .../test-load-null-value-in-between.json | 8 -- 5 files changed, 85 insertions(+), 92 deletions(-) delete mode 100644 src/test/java/org/emfjson/jackson/junit/tests/NullValueInBetweenTest.java delete mode 100644 src/test/resources/tests/test-load-null-value-in-between.json diff --git a/src/main/java/org/emfjson/jackson/databind/deser/EObjectDeserializer.java b/src/main/java/org/emfjson/jackson/databind/deser/EObjectDeserializer.java index 9edb2e36..35e64dd3 100644 --- a/src/main/java/org/emfjson/jackson/databind/deser/EObjectDeserializer.java +++ b/src/main/java/org/emfjson/jackson/databind/deser/EObjectDeserializer.java @@ -143,10 +143,11 @@ protected EObject doDeserialize(JsonParser jp, EClass defaultType, Deserializati return postDeserialize(buffer, current, defaultType, ctxt); } - protected EObject postDeserialize(TokenBuffer buffer, - EObject object, - EClass defaultType, - DeserializationContext ctxt) throws IOException { + protected EObject postDeserialize( + TokenBuffer buffer, + EObject object, + EClass defaultType, + DeserializationContext ctxt) throws IOException { if (object == null && defaultType == null) { return null; @@ -194,12 +195,13 @@ private EClass findRoot(DeserializationContext ctxt) { return options.rootElement; } - private void readFeature(JsonParser jp, - EObject current, - String fieldName, - DeserializationContext ctxt, - Resource resource, - ReferenceEntries entries) throws IOException { + private void readFeature( + JsonParser jp, + EObject current, + String fieldName, + DeserializationContext ctxt, + Resource resource, + ReferenceEntries entries) throws IOException { final EClass eClass = current.eClass(); final EStructuralFeature feature = cache.getEStructuralFeature(eClass, fieldName); @@ -209,7 +211,7 @@ private void readFeature(JsonParser jp, if (jp.getCurrentToken() == JsonToken.FIELD_NAME) { jp.nextToken(); } - + if (jp.getCurrentToken() == JsonToken.VALUE_NULL) { return; } @@ -240,12 +242,13 @@ private void readFeature(JsonParser jp, } @SuppressWarnings("unchecked") - private void readContainment(JsonParser jp, - EObject owner, - DeserializationContext ctxt, - EReference reference, - Resource resource, - ReferenceEntries entries) throws IOException { + private void readContainment( + JsonParser jp, + EObject owner, + DeserializationContext ctxt, + EReference reference, + Resource resource, + ReferenceEntries entries) throws IOException { final EClass eType = (EClass) owner.eClass().getFeatureType(reference).getEClassifier(); final Class type = eType.getInstanceClass(); @@ -303,10 +306,11 @@ private void readContainment(JsonParser jp, Read the key-values pair of a JSON object and put it in a Map. */ @SuppressWarnings("unchecked") - private void readMap(JsonParser jp, - EObject owner, - DeserializationContext ctxt, - EReference reference) throws IOException { + private void readMap( + JsonParser jp, + EObject owner, + DeserializationContext ctxt, + EReference reference) throws IOException { final Collection map = (Collection) owner.eGet(reference); @@ -334,11 +338,12 @@ private void readMap(JsonParser jp, } } - private void readReference(JsonParser jp, - EObject owner, - EReference reference, - ReferenceEntries entries, - DeserializationContext context) throws IOException { + private void readReference( + JsonParser jp, + EObject owner, + EReference reference, + ReferenceEntries entries, + DeserializationContext context) throws IOException { if (jp.getCurrentToken() == JsonToken.START_ARRAY) { while (jp.nextToken() != JsonToken.END_ARRAY) { @@ -349,11 +354,12 @@ private void readReference(JsonParser jp, } } - private void readAttribute(JsonParser jp, - EObject owner, - EAttribute attribute, - Resource resource, - DeserializationContext ctxt) throws IOException { + private void readAttribute( + JsonParser jp, + EObject owner, + EAttribute attribute, + Resource resource, + DeserializationContext ctxt) throws IOException { final EDataType dataType = (EDataType) owner.eClass().getFeatureType(attribute).getEClassifier(); if (dataType == null) { @@ -371,12 +377,13 @@ private void readAttribute(JsonParser jp, } } - private void readSingleAttribute(JsonParser jp, - EObject owner, - EAttribute attribute, - Resource resource, - EDataType dataType, - DeserializationContext ctxt) throws IOException { + private void readSingleAttribute( + JsonParser jp, + EObject owner, + EAttribute attribute, + Resource resource, + EDataType dataType, + DeserializationContext ctxt) throws IOException { final Class type = dataType.getInstanceClass(); diff --git a/src/test/java/org/emfjson/jackson/junit/TestSuite.java b/src/test/java/org/emfjson/jackson/junit/TestSuite.java index 548403c5..f63f8a16 100644 --- a/src/test/java/org/emfjson/jackson/junit/TestSuite.java +++ b/src/test/java/org/emfjson/jackson/junit/TestSuite.java @@ -11,12 +11,11 @@ */ package org.emfjson.jackson.junit; +import org.emfjson.jackson.junit.tests.*; import org.junit.runner.RunWith; import org.junit.runners.Suite; import org.junit.runners.Suite.SuiteClasses; -import org.emfjson.jackson.junit.tests.*; - @RunWith(Suite.class) @SuiteClasses({ AnnotationTest.class, @@ -44,4 +43,5 @@ ValueTest.class, NullValueInBetweenTest.class }) -public class TestSuite {} +public class TestSuite { +} diff --git a/src/test/java/org/emfjson/jackson/junit/tests/NullValueInBetweenTest.java b/src/test/java/org/emfjson/jackson/junit/tests/NullValueInBetweenTest.java deleted file mode 100644 index 69bd1a0c..00000000 --- a/src/test/java/org/emfjson/jackson/junit/tests/NullValueInBetweenTest.java +++ /dev/null @@ -1,44 +0,0 @@ -/* - * Copyright (c) 2016 Data In Motion UG. - * 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: - * Juergen Albert - initial API and implementation - * - */ -package org.emfjson.jackson.junit.tests; - -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; - -import java.io.IOException; - -import org.eclipse.emf.common.util.URI; -import org.eclipse.emf.ecore.resource.Resource; -import org.emfjson.jackson.junit.model.PrimaryObject; -import org.emfjson.jackson.junit.support.TestSupport; -import org.junit.Test; - -public class NullValueInBetweenTest extends TestSupport { - - @Test - public void testSaveOneRootObjectWithAttributes() throws IOException { - - Resource resource = resourceSet.createResource(URI.createURI("src/test/resources/tests/test-load-null-value-in-between.json")); - assertNotNull(resource); - - resource.load(null); - - assertTrue(!resource.getContents().isEmpty()); - PrimaryObject type = (PrimaryObject) resource.getContents().get(0); - - assertNull(type.getName()); - assertNotNull(type.getFeatureMapAttributeType1()); - assertNotNull(type.getFeatureMapAttributeType2()); - } - -} diff --git a/src/test/java/org/emfjson/jackson/junit/tests/ValueTest.java b/src/test/java/org/emfjson/jackson/junit/tests/ValueTest.java index abfe2274..edd890d6 100644 --- a/src/test/java/org/emfjson/jackson/junit/tests/ValueTest.java +++ b/src/test/java/org/emfjson/jackson/junit/tests/ValueTest.java @@ -34,6 +34,7 @@ import java.util.Date; import java.util.List; +import static org.assertj.core.api.Assertions.assertThat; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; @@ -115,6 +116,43 @@ public void testStringValues() throws IOException { assertEquals(expected, mapper.valueToTree(resource)); } + @Test + public void testLoadStringValues() throws IOException { + JsonNode data = mapper.createObjectNode() + .put("eClass", "http://www.emfjson.org/jackson/model#//ETypes") + .put("eString", "Hello") + .set("eStrings", mapper.createArrayNode() + .add("Hello") + .add("The") + .add("World")); + + Resource resource = resourceSet.createResource(URI.createURI("tests/test.json")); + resource.load(new ByteArrayInputStream(mapper.writeValueAsBytes(data)), null); + + EObject root = resource.getContents().get(0); + assertEquals(ModelPackage.Literals.ETYPES, root.eClass()); + + assertEquals("Hello", ((ETypes) root).getEString()); + + assertThat(((ETypes) root).getEStrings()) + .containsExactly("Hello", "The", "World"); + } + + @Test + public void testLoadNullValue() throws IOException { + JsonNode data = mapper.createObjectNode() + .put("eClass", "http://www.emfjson.org/jackson/model#//ETypes") + .putNull("eString"); + + Resource resource = resourceSet.createResource(URI.createURI("tests/test.json")); + resource.load(new ByteArrayInputStream(mapper.writeValueAsBytes(data)), null); + + EObject root = resource.getContents().get(0); + assertEquals(ModelPackage.Literals.ETYPES, root.eClass()); + + assertThat(((ETypes) root).getEString()).isNull(); + } + @Test public void testIntValues() throws IOException { JsonNode expected = mapper.createObjectNode() diff --git a/src/test/resources/tests/test-load-null-value-in-between.json b/src/test/resources/tests/test-load-null-value-in-between.json deleted file mode 100644 index 242b5283..00000000 --- a/src/test/resources/tests/test-load-null-value-in-between.json +++ /dev/null @@ -1,8 +0,0 @@ -{ - "eClass": "http://www.emfjson.org/jackson/model#//PrimaryObject", - "name": null, - "featureMapAttributeType1": "Hello", - "featureMapAttributeType2": "World" -} - -