From c527460ee78e1d5ccece2349a56c9edafc4fb828 Mon Sep 17 00:00:00 2001 From: Patrick Ziegler Date: Sun, 9 Feb 2025 15:14:57 +0100 Subject: [PATCH 1/2] Extend o.e.e4.emf.xpath.test to cover test child context Contributes to - https://github.com/eclipse-platform/eclipse.platform.ui/issues/423 --- .../model/fragment.e4xmi | 6 ++ .../test/ExampleQueriesApplicationTest.java | 55 +++++++++++++++++-- 2 files changed, 56 insertions(+), 5 deletions(-) create mode 100644 tests/org.eclipse.e4.emf.xpath.test/model/fragment.e4xmi diff --git a/tests/org.eclipse.e4.emf.xpath.test/model/fragment.e4xmi b/tests/org.eclipse.e4.emf.xpath.test/model/fragment.e4xmi new file mode 100644 index 00000000000..92a364e7750 --- /dev/null +++ b/tests/org.eclipse.e4.emf.xpath.test/model/fragment.e4xmi @@ -0,0 +1,6 @@ + + + + + + diff --git a/tests/org.eclipse.e4.emf.xpath.test/src/org/eclipse/e4/emf/xpath/test/ExampleQueriesApplicationTest.java b/tests/org.eclipse.e4.emf.xpath.test/src/org/eclipse/e4/emf/xpath/test/ExampleQueriesApplicationTest.java index 465892dd3cf..bf5c3f1b93a 100644 --- a/tests/org.eclipse.e4.emf.xpath.test/src/org/eclipse/e4/emf/xpath/test/ExampleQueriesApplicationTest.java +++ b/tests/org.eclipse.e4.emf.xpath.test/src/org/eclipse/e4/emf/xpath/test/ExampleQueriesApplicationTest.java @@ -14,12 +14,16 @@ package org.eclipse.e4.emf.xpath.test; import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; +import java.util.List; + import org.eclipse.e4.emf.xpath.EcoreXPathContextFactory; import org.eclipse.e4.emf.xpath.XPathContext; import org.eclipse.e4.emf.xpath.XPathContextFactory; import org.eclipse.e4.ui.internal.workbench.E4XMIResourceFactory; +import org.eclipse.e4.ui.model.application.MAddon; import org.eclipse.e4.ui.model.application.MApplication; import org.eclipse.e4.ui.model.application.commands.impl.CommandsPackageImpl; import org.eclipse.e4.ui.model.application.impl.ApplicationPackageImpl; @@ -28,6 +32,8 @@ import org.eclipse.e4.ui.model.application.ui.impl.UiPackageImpl; import org.eclipse.e4.ui.model.application.ui.menu.MMenu; import org.eclipse.e4.ui.model.application.ui.menu.impl.MenuPackageImpl; +import org.eclipse.e4.ui.model.fragment.MModelFragments; +import org.eclipse.e4.ui.model.fragment.MStringModelFragment; import org.eclipse.emf.common.util.URI; import org.eclipse.emf.ecore.EObject; import org.eclipse.emf.ecore.resource.Resource; @@ -41,7 +47,9 @@ public class ExampleQueriesApplicationTest { private ResourceSet resourceSet; private XPathContext xpathContext; + private XPathContext xpathChildContext; private Resource resource; + private Resource childResource; @SuppressWarnings("restriction") @Before @@ -63,6 +71,9 @@ public void setUp() { resource = resourceSet.getResource(uri, true); XPathContextFactory f = EcoreXPathContextFactory.newInstance(); xpathContext = f.newContext(resource.getContents().get(0)); + URI childUri = URI.createPlatformPluginURI("/org.eclipse.e4.emf.xpath.test/model/fragment.e4xmi", true); + childResource = resourceSet.getResource(childUri, true); + xpathChildContext = f.newContext(xpathContext, childResource.getContents().get(0)); } @After @@ -70,6 +81,9 @@ public void tearDown() { xpathContext = null; resource.unload(); resourceSet.getResources().remove(resource); + xpathChildContext = null; + childResource.unload(); + resourceSet.getResources().remove(childResource); } @Test @@ -80,17 +94,48 @@ public void testAccessingTheApplication() { @Test public void testAccessingTheMainMenu() { - Object menu = xpathContext.getValue("//mainMenu"); - assertThat(menu).isInstanceOf(MMenu.class); + assertThat(xpathContext.getValue("//mainMenu")).isInstanceOf(MMenu.class); + assertNotNull(xpathContext.getValue("//mainMenu", MMenu.class)); - MMenu mMenu = xpathContext.getValue("//mainMenu", MMenu.class); - assertNotNull(mMenu); + assertNotNull(xpathContext.getValue("/children/mainMenu", MMenu.class)); + assertThat(xpathContext.getValue("/children/mainMenu")).isInstanceOf(MMenu.class); } @Test public void testAccessingAllMenus() { Object menuEntries = xpathContext.getValue("//mainMenu/children"); - assertNotNull(menuEntries); + assertThat(menuEntries).isInstanceOf(List.class); + List list = (List) menuEntries; + assertEquals(2, list.size()); + assertThat(list).allMatch(MMenu.class::isInstance, "Is instanceof of MMenu") // + .anyMatch(e -> "File".equals(((MMenu) e).getLabel())) + .anyMatch(e -> "Help".equals(((MMenu) e).getLabel())); + } + + @Test + public void testAccessingTheModelFragments() { + Object modelFragments = xpathChildContext.getValue("/"); + assertThat(modelFragments).isInstanceOf(MModelFragments.class); + } + + @Test + public void testAccessingTheStringModelFragment() { + Object modelFragment = xpathChildContext.getValue("//fragments[1]"); + assertThat(modelFragment).isInstanceOf(MStringModelFragment.class); + + MStringModelFragment mModelFragment = xpathChildContext.getValue("//fragments[1]", MStringModelFragment.class); + assertNotNull(mModelFragment); + + Object modelFragment2 = xpathChildContext.getValue("/fragments[1]"); + assertThat(modelFragment2).isInstanceOf(MStringModelFragment.class); } + @Test + public void testAccessingTheAddons() { + Object addon = xpathChildContext.getValue("//elements[1]"); + assertThat(addon).isInstanceOf(MAddon.class); + + MAddon mAddon = xpathChildContext.getValue("//elements[1]", MAddon.class); + assertNotNull(mAddon); + } } From 79adb21a7eae6f5bd970d62e3acd0081d0be2d7d Mon Sep 17 00:00:00 2001 From: Ed Merks Date: Wed, 5 Feb 2025 15:19:52 +0100 Subject: [PATCH 2/2] Extend o.e.e4.emf.xpath.test - Add ExampleQueriesTestCase.testRelative() to test relative paths via a nested context. Contributes to - https://github.com/eclipse-platform/eclipse.platform.ui/issues/423 --- .../xpath/test/ExampleQueriesTestCase.java | 22 +++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/tests/org.eclipse.e4.emf.xpath.test/src/org/eclipse/e4/emf/xpath/test/ExampleQueriesTestCase.java b/tests/org.eclipse.e4.emf.xpath.test/src/org/eclipse/e4/emf/xpath/test/ExampleQueriesTestCase.java index 3ed6a4a0f5e..5f6a80cfc2f 100644 --- a/tests/org.eclipse.e4.emf.xpath.test/src/org/eclipse/e4/emf/xpath/test/ExampleQueriesTestCase.java +++ b/tests/org.eclipse.e4.emf.xpath.test/src/org/eclipse/e4/emf/xpath/test/ExampleQueriesTestCase.java @@ -16,7 +16,9 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertSame; import static org.junit.Assert.assertThrows; import static org.junit.Assert.assertTrue; @@ -47,6 +49,7 @@ public class ExampleQueriesTestCase { private ResourceSet resourceSet; private XPathContext xpathContext; private Resource resource; + private XPathContextFactory xpathContextFactory; @Before public void setUp() { @@ -58,13 +61,14 @@ public void setUp() { resourceSet.getPackageRegistry().put(XpathtestPackage.eNS_URI, XpathtestPackage.eINSTANCE); URI uri = URI.createPlatformPluginURI("/org.eclipse.e4.emf.xpath.test/model/Test.xmi", true); resource = resourceSet.getResource(uri, true); - XPathContextFactory f = EcoreXPathContextFactory.newInstance(); - xpathContext = f.newContext(resource.getContents().get(0)); + xpathContextFactory = EcoreXPathContextFactory.newInstance(); + xpathContext = xpathContextFactory.newContext(resource.getContents().get(0)); } @After public void tearDown() { xpathContext = null; + xpathContextFactory = null; resource.unload(); resourceSet.getResources().remove(resource); } @@ -82,6 +86,7 @@ public void testSimpleQuery() { assertThat(application).isInstanceOf(Root.class); assertThrows(JXPathNotFoundException.class, () -> xpathContext.getValue(".[@id='nixda']")); + assertFalse(xpathContext.iterate(".[@id='nixda']").hasNext()); application = xpathContext.getValue(".[@id='root']"); assertThat(application).isInstanceOf(Root.class); @@ -95,6 +100,8 @@ public void testSimpleQuery() { assertThat(xpathContext.getValue("//.[ecore:eClassName(.)='ExtendedNode']")).isInstanceOf(ExtendedNode.class); assertNotNull(xpathContext.getValue("//.[ecore:eClassName(.)='ExtendedNode']", ExtendedNode.class)); + + assertEquals(rootApplication, xpathContext.getValue(".")); } @Test @@ -119,4 +126,15 @@ public void testMenuQuery() { assertEquals(26, list.size()); } + @Test + public void testRelative() { + EObject context = resource.getContents().get(0); + List eContents = context.eContents(); + EObject firstElement = eContents.get(0); + XPathContext nestedXpathContext = xpathContextFactory.newContext(xpathContext, firstElement); + + List dotList = nestedXpathContext.stream(".", Node.class).toList(); + assertEquals(1, dotList.size()); + assertSame(firstElement, dotList.get(0)); + } }