Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added support for multiple type package URIs in ConfigurableJsonResource #4

Open
wants to merge 19 commits into
base: snapshot
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cnf/central.mvn
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,6 @@ org.geckoprojects.bnd:org.gecko.bnd.jacoco.library:1.1.1

#org.geckoprojects.emf:org.gecko.emf.osgi.api:4.1.1.202202162308
org.geckoprojects.emf:org.gecko.emf.osgi.bnd.library.workspace:4.1.1-SNAPSHOT
org.geckoprojects.emf:org.gecko.emf.osgi.example.model.basic:4.1.1-SNAPSHOT

com.fasterxml.jackson.core:jackson-core:2.13.2
com.fasterxml.jackson.core:jackson-databind:2.13.2
Expand All @@ -168,3 +167,4 @@ org.eclipse.emfcloud:emfjson-jackson:2.1.0-SNAPSHOT
de.undercouch:bson4jackson:2.13.1
org.yaml:snakeyaml:1.30
#org.emfjson:emfjson-jackson:1.3.0
org.geckoprojects.emf:org.gecko.emf.osgi.example.model.basic:4.1.1-SNAPSHOT
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
*/
package org.gecko.emf.bson.tests;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
Expand All @@ -24,9 +25,7 @@
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.resource.impl.ResourceSetImpl;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.gecko.emf.osgi.ResourceFactoryConfigurator;
import org.gecko.emf.osgi.example.model.basic.BasicFactory;
import org.gecko.emf.osgi.example.model.basic.BasicPackage;
import org.gecko.emf.osgi.example.model.basic.Contact;
Expand All @@ -35,6 +34,7 @@
import org.gecko.emf.osgi.example.model.basic.GenderType;
import org.gecko.emf.osgi.example.model.basic.Person;
import org.gecko.emf.osgi.example.model.basic.util.BasicResourceFactoryImpl;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.osgi.service.cm.Configuration;
Expand All @@ -52,6 +52,11 @@
@ExtendWith(BundleContextExtension.class)
@ExtendWith(ServiceExtension.class)
public class BsonConfiguratorTest {

@BeforeEach
public void beforeEach(@InjectService(timeout = 2000) BasicFactory bp) {
System.out.println("Test");
}

@Test
public void testBson(@InjectService ConfigurationAdmin ca) throws IOException {
Expand All @@ -67,14 +72,18 @@ public void testBson(@InjectService ConfigurationAdmin ca) throws IOException {
System.out.println(c.getPid());//foo~bar
System.out.println(c.getFactoryPid());//foo
}

@Test
public void testBson(@InjectService(filter = "(component.name=EMFBsonConfigurator)") ServiceAware<ResourceFactoryConfigurator> sa, @InjectService BasicFactory bf, @InjectService BasicPackage bp) {
public void testBson(@InjectService(timeout = 2000) ServiceAware<ResourceSet> rsAware,
@InjectService BasicFactory bf,
@InjectService BasicPackage bp) {

assertNotNull(rsAware);
assertThat(rsAware.getServices()).hasSize(1);
ResourceSet resourceSet = rsAware.getService();
assertNotNull(resourceSet);

System.out.println(sa.getServiceReference().getPropertyKeys());
ResourceSet resourceSet = createResourceSet(bp);
ResourceFactoryConfigurator configurator = sa.getService();
configurator.configureResourceFactory(resourceSet.getResourceFactoryRegistry());
resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put("test", new BasicResourceFactoryImpl());

Person p = createSamplePerson(bf);
Resource xmiResource = resourceSet.createResource(URI.createURI("person.test"));
Expand Down Expand Up @@ -154,17 +163,4 @@ private Person createSamplePerson(BasicFactory bf) {
p.getContact().add(email);
return p;
}

/**
* @param bp
* @return
*/
private ResourceSet createResourceSet(BasicPackage bp) {
ResourceSet resourceSet = new ResourceSetImpl();
resourceSet.getPackageRegistry().put(BasicPackage.eNS_URI, bp);
resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put("test", new BasicResourceFactoryImpl());
resourceSet.getResourceFactoryRegistry().getContentTypeToFactoryMap().put(BasicPackage.eCONTENT_TYPE, new BasicResourceFactoryImpl());
return resourceSet;
}

}
13 changes: 7 additions & 6 deletions org.gecko.emf.bson.tests/test.bndrun
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
-runrequires: \
bnd.identity;id='org.gecko.emf.bson.tests'
-runrequires: bnd.identity;id='org.gecko.emf.bson.tests'
-runfw: org.apache.felix.framework
-runee: JavaSE-11
-runbundles.junit5: ${test.runbundles}
-runbundles: \
org.apache.felix.configadmin;version='[1.9.22,1.9.23)',\
org.eclipse.emf.ecore.xmi;version='[2.16.0,2.16.1)',\
org.opentest4j;version='[1.2.0,1.2.1)',\
org.osgi.service.component;version='[1.4.0,1.4.1)',\
org.osgi.util.function;version='[1.1.0,1.1.1)',\
org.osgi.util.promise;version='[1.1.1,1.1.2)',\
com.fasterxml.jackson.core.jackson-annotations;version='[2.13.2,2.13.3)',\
com.fasterxml.jackson.core.jackson-core;version='[2.13.2,2.13.3)',\
com.fasterxml.jackson.core.jackson-databind;version='[2.13.2,2.13.3)',\
Expand All @@ -23,12 +20,16 @@
org.gecko.emf.osgi.example.model.basic;version='[4.1.1,4.1.2)',\
com.fasterxml.jackson.dataformat.jackson-dataformat-properties;version='[2.13.2,2.13.3)',\
com.fasterxml.jackson.dataformat.jackson-dataformat-yaml;version='[2.13.2,2.13.3)',\
emfjson-jackson;version='[2.1.0,2.1.1)',\
org.gecko.emf.bson;version=snapshot,\
org.gecko.emf.json;version=snapshot,\
org.yaml.snakeyaml;version='[1.30.0,1.30.1)',\
org.gecko.emf.bson.tests;version=snapshot,\
junit-jupiter-api;version='[5.8.2,5.8.3)',\
junit-jupiter-params;version='[5.8.2,5.8.3)',\
org.osgi.test.common;version='[1.1.0,1.1.1)',\
org.osgi.test.junit5;version='[1.1.0,1.1.1)'
org.osgi.test.junit5;version='[1.1.0,1.1.1)',\
org.apache.felix.converter;version='[1.0.18,1.0.19)',\
org.eclipse.emfcloud.emfjson-jackson;version='[2.1.0,2.1.1)',\
org.osgi.service.cm;version='[1.6.0,1.6.1)',\
org.osgi.util.promise;version='[1.2.0,1.2.1)',\
assertj-core;version='[3.22.0,3.22.1)'
13 changes: 12 additions & 1 deletion org.gecko.emf.json.tests/bnd.bnd
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,15 @@ javac.target: 11

Bundle-Version: 1.0.1.SNAPSHOT
-buildpath: \
org.eclipse.emfcloud.emfjson-jackson;version=latest
org.eclipse.emfcloud.emfjson-jackson;version=latest,\
org.gecko.emf.osgi.component,\
org.eclipse.emf.ecore.xmi,\
org.eclipse.emf.ecore,\
org.gecko.emf.osgi.example.model.basic,\
org.gecko.emf.util.example.model;version=latest

-includeresource.resources: \
data=data/

-runproperties.properties: \
base.path=${.}
31 changes: 31 additions & 0 deletions org.gecko.emf.json.tests/data/exampleTypeByClass.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"type": "Building",
"location": {
"type": "org.gecko.emf.util.example.model.examplemodel.Polygon",
"coordinates": [
[
[
100,
0
],
[
101,
0
],
[
101,
1
],
[
100,
1
],
[
100,
0
]
]
]
}
}

31 changes: 31 additions & 0 deletions org.gecko.emf.json.tests/data/exampleTypeByName.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"type": "Building",
"location": {
"type": "Polygon",
"coordinates": [
[
[
100,
0
],
[
101,
0
],
[
101,
1
],
[
100,
1
],
[
100,
0
]
]
]
}
}

30 changes: 30 additions & 0 deletions org.gecko.emf.json.tests/data/exampleTypeByURI.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"location": {
"type": "http://datainmotion.com/emf/util/examplemodel/1.0#//Polygon",
"coordinates": [
[
[
100,
0
],
[
101,
0
],
[
101,
1
],
[
100,
1
],
[
100,
0
]
]
]
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
/**
* Copyright (c) 2012 - 2022 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 v2.0 which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v20.html
*
* Contributors:
* Data In Motion - initial API and implementation
*/
package org.gecko.emf.json.tests;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.gecko.emf.json.configuration.ConfigurableJsonResource;
import org.gecko.emf.json.constants.EMFJs;
import org.gecko.emf.osgi.example.model.basic.BasicFactory;
import org.gecko.emf.osgi.example.model.basic.BasicPackage;
import org.gecko.emf.osgi.example.model.basic.Person;
import org.junit.jupiter.api.Test;
import org.osgi.test.common.annotation.InjectService;
import org.osgi.test.common.service.ServiceAware;

/**
*
* @author mark
* @since 15.07.2022
*/
public class RootElementTest {

@Test
public void testSaveJson(@InjectService(timeout = 2000) ServiceAware<ResourceSet> rsAware,
@InjectService(timeout = 2000) ServiceAware<BasicFactory> bfAware) {

assertNotNull(rsAware);
assertThat(rsAware.getServices()).hasSize(1);
ResourceSet resourceSet = rsAware.getService();
assertNotNull(resourceSet);

assertNotNull(bfAware);
assertThat(bfAware.getServices()).hasSize(1);
BasicFactory factoryImpl = bfAware.getService();
assertNotNull(factoryImpl);

Resource resource = resourceSet.createResource(URI.createURI("test.json"));
assertNotNull(resource);
assertTrue(resource instanceof ConfigurableJsonResource);

Person p = factoryImpl.createPerson();
p.setFirstName("Emil");
p.setLastName("Tester");
resource.getContents().add(p);

ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
resource.save(baos, null);
} catch (IOException e) {
fail("Error saving Person");
}
String result = new String(baos.toByteArray());
System.out.println(result);
}



@Test
public void testLoadJsonError(@InjectService(timeout = 2000) ServiceAware<ResourceSet> rsAware) {

assertNotNull(rsAware);
assertThat(rsAware.getServices()).hasSize(1);
ResourceSet resourceSet = rsAware.getService();
assertNotNull(resourceSet);

String json = "{\n"
+ " \"firstName\" : \"Emil\",\n"
+ " \"lastName\" : \"Tester\"\n"
+ "}";
Resource loadResource = resourceSet.createResource(URI.createURI("test-load-error.json"));
assertNotNull(loadResource);
assertTrue(loadResource instanceof ConfigurableJsonResource);

ByteArrayInputStream bais = new ByteArrayInputStream(json.getBytes());
try {
loadResource.load(bais, null);
assertEquals(0, loadResource.getContents().size());
} catch (IOException e) {
fail("Error loading Person");
}
}


@Test
public void testLoadJson(@InjectService(timeout = 2000) ServiceAware<ResourceSet> rsAware,
@InjectService(timeout = 2000) ServiceAware<BasicPackage> basicPackageAware) {

assertNotNull(rsAware);
assertThat(rsAware.getServices()).hasSize(1);
ResourceSet resourceSet = rsAware.getService();
assertNotNull(resourceSet);

assertNotNull(basicPackageAware);
assertThat(basicPackageAware.getServices()).hasSize(1);
BasicPackage packageImpl = basicPackageAware.getService();
assertNotNull(packageImpl);

String json = "{\n"
+ " \"firstName\" : \"Emil\",\n"
+ " \"lastName\" : \"Tester\"\n"
+ "}";
Resource loadResource = resourceSet.createResource(URI.createURI("test-load.json"));
assertNotNull(loadResource);
assertTrue(loadResource instanceof ConfigurableJsonResource);

ByteArrayInputStream bais = new ByteArrayInputStream(json.getBytes());
try {
Map<String, Object> loadOptions = new HashMap<String, Object>();
loadOptions.put(EMFJs.OPTION_ROOT_ELEMENT, packageImpl.getPerson());
loadResource.load(bais, loadOptions);
assertEquals(1, loadResource.getContents().size());
Person p = (Person) loadResource.getContents().get(0);
assertEquals("Emil", p.getFirstName());
assertEquals("Tester", p.getLastName());
} catch (IOException e) {
fail("Error loading Person");
}
}

}
Loading