diff --git a/org.gecko.emf.osgi/src/org/gecko/emf/osgi/provider/DefaultResourceSetFactory.java b/org.gecko.emf.osgi/src/org/gecko/emf/osgi/provider/DefaultResourceSetFactory.java index 1fb93ccd..86bc4a4a 100644 --- a/org.gecko.emf.osgi/src/org/gecko/emf/osgi/provider/DefaultResourceSetFactory.java +++ b/org.gecko.emf.osgi/src/org/gecko/emf/osgi/provider/DefaultResourceSetFactory.java @@ -31,7 +31,6 @@ import java.util.concurrent.atomic.AtomicReference; import org.eclipse.emf.ecore.EPackage; -import org.eclipse.emf.ecore.impl.EPackageRegistryImpl; import org.eclipse.emf.ecore.resource.Resource; import org.eclipse.emf.ecore.resource.Resource.Factory; import org.eclipse.emf.ecore.resource.Resource.Factory.Registry; @@ -324,7 +323,7 @@ public ResourceSet createResourceSet() { throw new IllegalStateException("There is no Resource Factory Registry available. This should not happen"); } ResourceSet resourceSet = internalCreateResourceSet(); - resourceSet.setPackageRegistry(new EPackageRegistryImpl(packageRegistry)); + resourceSet.setPackageRegistry(new DelegatingEPackageRegistry(packageRegistry)); resourceSet.setResourceFactoryRegistry(new DelegatingResourceFactoryRegistry(rfr)); resourceSetConfigurators.forEach(c->c.configureResourceSet(resourceSet)); return resourceSet; diff --git a/org.gecko.emf.osgi/src/org/gecko/emf/osgi/provider/DelegatingEPackageRegistry.java b/org.gecko.emf.osgi/src/org/gecko/emf/osgi/provider/DelegatingEPackageRegistry.java new file mode 100644 index 00000000..31f96c04 --- /dev/null +++ b/org.gecko.emf.osgi/src/org/gecko/emf/osgi/provider/DelegatingEPackageRegistry.java @@ -0,0 +1,142 @@ +/** + * Copyright (c) 2012 - 2024 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 2.0 + * which is available at https://www.eclipse.org/legal/epl-2.0/ + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * Data In Motion - initial API and implementation + */ +package org.gecko.emf.osgi.provider; + +import java.util.Collection; +import java.util.Collections; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; +import java.util.function.BiConsumer; + +import org.eclipse.emf.ecore.EPackage; +import org.eclipse.emf.ecore.impl.EPackageRegistryImpl; + +/** + * Fully {@link Map} implementing {@link EPackageRegistryImpl} + * @author Mark Hoffmann + * @since 09.04.2024 + */ +public class DelegatingEPackageRegistry extends EPackageRegistryImpl { + + /** serialVersionUID */ + private static final long serialVersionUID = 1L; + + /** + * Creates a new instance. + */ + public DelegatingEPackageRegistry() { + super(new EPackageRegistryImpl()); + } + + /** + * Creates a new instance. + */ + public DelegatingEPackageRegistry(EPackage.Registry delegate) { + super(delegate); + } + + /* + * (non-Javadoc) + * @see java.util.HashMap#values() + */ + @Override + public Collection values() { + Set objects = new HashSet<>(delegateRegistry.values()); + objects.addAll(super.values()); + return Collections.unmodifiableCollection(objects); + } + + /* + * (non-Javadoc) + * @see java.util.HashMap#keySet() + */ + @Override + public Set keySet() { + Set objects = new HashSet<>(delegateRegistry.keySet()); + objects.addAll(super.keySet()); + return Collections.unmodifiableSet(objects); + } + + /* + * (non-Javadoc) + * @see java.util.HashMap#size() + */ + @Override + public int size() { + return super.size() + delegateRegistry.size(); + } + + /* + * (non-Javadoc) + * @see java.util.HashMap#get(java.lang.Object) + */ + @Override + public Object get(Object key) { + Object object = super.get(key); + if (object != null) { + return object; + } + return delegateRegistry.get(key); + } + + /* + * (non-Javadoc) + * @see java.util.HashMap#isEmpty() + */ + @Override + public boolean isEmpty() { + return super.isEmpty() && delegateRegistry.isEmpty(); + } + + /* + * (non-Javadoc) + * @see java.util.HashMap#containsValue(java.lang.Object) + */ + @Override + public boolean containsValue(Object value) { + return super.containsValue(value) || delegateRegistry.containsValue(value); + } + + /* + * (non-Javadoc) + * @see org.eclipse.emf.ecore.impl.EPackageRegistryImpl#containsKey(java.lang.Object) + */ + @Override + public boolean containsKey(Object key) { + return super.containsKey(key) || delegateRegistry.containsKey(key); + } + + /* + * (non-Javadoc) + * @see java.util.HashMap#entrySet() + */ + @Override + public Set> entrySet() { + Set> entrySet = new HashSet<>(delegateRegistry.entrySet()); + entrySet.addAll(super.entrySet()); + return Collections.unmodifiableSet(entrySet); + } + + /* + * (non-Javadoc) + * @see java.util.HashMap#forEach(java.util.function.BiConsumer) + */ + @Override + public void forEach(BiConsumer action) { + super.forEach(action); + delegateRegistry.forEach(action); + } + +} diff --git a/org.gecko.emf.osgi/src/org/gecko/emf/osgi/provider/package-info.java b/org.gecko.emf.osgi/src/org/gecko/emf/osgi/provider/package-info.java index 8858d588..dd490cd8 100644 --- a/org.gecko.emf.osgi/src/org/gecko/emf/osgi/provider/package-info.java +++ b/org.gecko.emf.osgi/src/org/gecko/emf/osgi/provider/package-info.java @@ -11,5 +11,5 @@ * Contributors: * Data In Motion - initial API and implementation */ -@org.osgi.annotation.versioning.Version("3.0.0") +@org.osgi.annotation.versioning.Version("3.1.0") package org.gecko.emf.osgi.provider; diff --git a/org.gecko.emf.osgi/test/org/gecko/emf/osgi/provider/DelegatingEPackageRegistryTest.java b/org.gecko.emf.osgi/test/org/gecko/emf/osgi/provider/DelegatingEPackageRegistryTest.java new file mode 100644 index 00000000..ed46eef7 --- /dev/null +++ b/org.gecko.emf.osgi/test/org/gecko/emf/osgi/provider/DelegatingEPackageRegistryTest.java @@ -0,0 +1,306 @@ +/** + * 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 2.0 + * which is available at https://www.eclipse.org/legal/epl-2.0/ + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * Data In Motion - initial API and implementation + */ +package org.gecko.emf.osgi.provider; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Set; + +import org.eclipse.emf.ecore.EcorePackage; +import org.eclipse.emf.ecore.impl.EPackageRegistryImpl; +import org.junit.jupiter.api.Test; + +/** + * Tests {@link DelegatingHashMap} + * @author Juergen Albert + * @since 2 Dec 2022 + */ +class DelegatingEPackageRegistryTest { + + /** + * Test method for {@link org.gecko.emf.osgi.provider.DelegatingHashMap#size()}. + */ + @Test + void testSize() { + EPackageRegistryImpl d = new EPackageRegistryImpl(); + DelegatingEPackageRegistry r = new DelegatingEPackageRegistry(d); + assertEquals(0, r.size()); + + d.put("test", EcorePackage.eINSTANCE); + assertEquals(1, d.size()); + assertEquals(1, r.size()); + + r.put("test2", EcorePackage.eINSTANCE); + assertEquals(2, r.size()); + d.clear(); + assertEquals(1, r.size()); + assertEquals(0, d.size()); + } + + /** + * Test method for {@link org.gecko.emf.osgi.provider.DelegatingHashMap#isEmpty()}. + */ + @Test + void testIsEmpty() { + EPackageRegistryImpl d = new EPackageRegistryImpl(); + DelegatingEPackageRegistry r = new DelegatingEPackageRegistry(d); + + assertTrue(r.isEmpty()); + + d.put("test", EcorePackage.eINSTANCE); + assertFalse(r.isEmpty()); + + r.put("test2", EcorePackage.eINSTANCE); + assertFalse(d.isEmpty()); + + d.clear(); + assertFalse(r.isEmpty()); + + r.clear(); + assertTrue(r.isEmpty()); + + } + + /** + * Test method for {@link org.gecko.emf.osgi.provider.DelegatingHashMap#containsKey(java.lang.Object)}. + */ + @Test + void testContainsKey() { + EPackageRegistryImpl d = new EPackageRegistryImpl(); + DelegatingEPackageRegistry r = new DelegatingEPackageRegistry(d); + + d.put("1", "test"); + + assertEquals("test", r.get("1")); + + r.put("2", "test2"); + r.put("3", "test2"); + + assertTrue(r.containsKey("1")); + assertTrue(r.containsKey("2")); + assertTrue(r.containsKey("3")); + + assertTrue(d.containsKey("1")); + assertFalse(d.containsKey("2")); + assertFalse(d.containsKey("3")); + } + + /** + * Test method for {@link org.gecko.emf.osgi.provider.DelegatingHashMap#containsValue(java.lang.Object)}. + */ + @Test + void testContainsValue() { + EPackageRegistryImpl d = new EPackageRegistryImpl(); + DelegatingEPackageRegistry r = new DelegatingEPackageRegistry(d); + + d.put("1", "test"); + + assertEquals("test", r.get("1")); + + r.put("2", "test2"); + r.put("3", "test3"); + + assertTrue(r.containsValue("test")); + assertTrue(r.containsValue("test2")); + assertTrue(r.containsValue("test3")); + + assertTrue(d.containsValue("test")); + assertFalse(d.containsValue("test2")); + assertFalse(d.containsValue("test3")); + } + + /** + * Test method for {@link org.gecko.emf.osgi.provider.DelegatingHashMap#entrySet()}. + */ + @Test + void testEntrySet() { + EPackageRegistryImpl d = new EPackageRegistryImpl(); + DelegatingEPackageRegistry r = new DelegatingEPackageRegistry(d); + + d.put("1", "test"); + + assertEquals("test", r.get("1")); + + r.put("1", "test2"); + r.put("2", "test3"); + + assertEquals("test2", r.get("1")); + assertEquals("test3", r.get("2")); + assertEquals("test", d.get("1")); + + Set> entrySet = r.entrySet(); + + assertEquals(3, entrySet.size()); + } + + /** + * Test method for {@link org.gecko.emf.osgi.provider.DelegatingHashMap#get(java.lang.Object)}. + */ + @Test + void testGet() { + EPackageRegistryImpl d = new EPackageRegistryImpl(); + DelegatingEPackageRegistry r = new DelegatingEPackageRegistry(d); + + d.put("1", "test"); + + assertEquals("test", r.get("1")); + + r.put("1", "test2"); + + assertEquals("test2", r.get("1")); + assertEquals("test", d.get("1")); } + + /** + * Test method for {@link org.gecko.emf.osgi.provider.DelegatingHashMap#values()}. + */ + @Test + void testValues() { + EPackageRegistryImpl d = new EPackageRegistryImpl(); + DelegatingEPackageRegistry r = new DelegatingEPackageRegistry(d); + + d.put("1", "test"); + + assertEquals("test", r.get("1")); + + r.put("1", "test2"); + r.put("2", "test3"); + + assertEquals("test2", r.get("1")); + assertEquals("test3", r.get("2")); + assertEquals("test", d.get("1")); + + Collection values = r.values(); + + assertEquals(3, values.size()); + } + + /** + * Test method for {@link org.gecko.emf.osgi.provider.DelegatingHashMap#keySet()}. + */ + @Test + void testKeySet() { + EPackageRegistryImpl d = new EPackageRegistryImpl(); + DelegatingEPackageRegistry r = new DelegatingEPackageRegistry(d); + + d.put("1", "test"); + + assertEquals("test", r.get("1")); + + r.put("1", "test2"); + r.put("2", "test3"); + + assertEquals("test2", r.get("1")); + assertEquals("test3", r.get("2")); + assertEquals("test", d.get("1")); + + Set keySet = r.keySet(); + + assertEquals(2, keySet.size()); + } + + /** + * Test method for {@link org.gecko.emf.osgi.provider.DelegatingHashMap#put(java.lang.Object, java.lang.Object)}. + */ + @Test + void testPut() { + EPackageRegistryImpl d = new EPackageRegistryImpl(); + DelegatingEPackageRegistry r = new DelegatingEPackageRegistry(d); + + d.put("1", "test"); + + assertEquals("test", r.get("1")); + + r.put("1", "test2"); + + assertEquals("test2", r.get("1")); + assertEquals("test", d.get("1")); + } + + /** + * Test method for {@link org.gecko.emf.osgi.provider.DelegatingHashMap#remove(java.lang.Object)}. + */ + @Test + void testRemove() { + EPackageRegistryImpl d = new EPackageRegistryImpl(); + DelegatingEPackageRegistry r = new DelegatingEPackageRegistry(d); + + d.put("1", "test"); + + assertEquals("test", r.get("1")); + + r.put("1", "test2"); + + assertEquals("test2", r.get("1")); + assertEquals("test", d.get("1")); + + d.remove("1"); + + assertEquals("test2", r.get("1")); + assertNull(d.get("1")); + } + + /** + * Test method for {@link org.gecko.emf.osgi.provider.DelegatingHashMap#putAll(java.util.Map)}. + */ + @Test + void testPutAll() { + EPackageRegistryImpl d = new EPackageRegistryImpl(); + DelegatingEPackageRegistry r = new DelegatingEPackageRegistry(d); + + d.put("1", "test"); + + assertEquals(1, d.size()); + assertEquals(1, r.size()); + + Map toAdd = new HashMap(); + toAdd.put("2", "test2"); + toAdd.put("3", "test2"); + + r.putAll(toAdd); + assertEquals(1, d.size()); + assertEquals(3, r.size()); + } + + /** + * Test method for {@link org.gecko.emf.osgi.provider.DelegatingHashMap#clear()}. + */ + @Test + void testClear() { + EPackageRegistryImpl d = new EPackageRegistryImpl(); + DelegatingEPackageRegistry r = new DelegatingEPackageRegistry(d); + + d.put("1", "test"); + + assertEquals("test", r.get("1")); + + r.put("2", "test2"); + r.put("3", "test2"); + + assertEquals(3, r.size()); + assertEquals(1, d.size()); + + d.clear(); + + assertEquals(2, r.size()); + assertEquals(0, d.size()); + } + +}