1414package org .eclipse .e4 .emf .xpath .test ;
1515
1616import static org .assertj .core .api .Assertions .assertThat ;
17+ import static org .junit .Assert .assertEquals ;
1718import static org .junit .Assert .assertNotNull ;
1819
20+ import java .util .List ;
21+
1922import org .eclipse .e4 .emf .xpath .EcoreXPathContextFactory ;
2023import org .eclipse .e4 .emf .xpath .XPathContext ;
2124import org .eclipse .e4 .emf .xpath .XPathContextFactory ;
2225import org .eclipse .e4 .ui .internal .workbench .E4XMIResourceFactory ;
26+ import org .eclipse .e4 .ui .model .application .MAddon ;
2327import org .eclipse .e4 .ui .model .application .MApplication ;
2428import org .eclipse .e4 .ui .model .application .commands .impl .CommandsPackageImpl ;
2529import org .eclipse .e4 .ui .model .application .impl .ApplicationPackageImpl ;
2832import org .eclipse .e4 .ui .model .application .ui .impl .UiPackageImpl ;
2933import org .eclipse .e4 .ui .model .application .ui .menu .MMenu ;
3034import org .eclipse .e4 .ui .model .application .ui .menu .impl .MenuPackageImpl ;
35+ import org .eclipse .e4 .ui .model .fragment .MModelFragments ;
36+ import org .eclipse .e4 .ui .model .fragment .MStringModelFragment ;
3137import org .eclipse .emf .common .util .URI ;
3238import org .eclipse .emf .ecore .EObject ;
3339import org .eclipse .emf .ecore .resource .Resource ;
@@ -41,11 +47,14 @@ public class ExampleQueriesApplicationTest {
4147
4248 private ResourceSet resourceSet ;
4349 private XPathContext xpathContext ;
50+ private XPathContext xpathChildContext ;
4451 private Resource resource ;
52+ private Resource childResource ;
4553
4654 @ SuppressWarnings ("restriction" )
4755 @ Before
4856 public void setUp () {
57+
4958 resourceSet = new ResourceSetImpl ();
5059 resourceSet .getResourceFactoryRegistry ().getExtensionToFactoryMap ()
5160 .put (Resource .Factory .Registry .DEFAULT_EXTENSION , new E4XMIResourceFactory ());
@@ -63,13 +72,19 @@ public void setUp() {
6372 resource = resourceSet .getResource (uri , true );
6473 XPathContextFactory <EObject > f = EcoreXPathContextFactory .newInstance ();
6574 xpathContext = f .newContext (resource .getContents ().get (0 ));
75+ URI childUri = URI .createPlatformPluginURI ("/org.eclipse.e4.emf.xpath.test/model/fragment.e4xmi" , true );
76+ childResource = resourceSet .getResource (childUri , true );
77+ xpathChildContext = f .newContext (xpathContext , childResource .getContents ().get (0 ));
6678 }
6779
6880 @ After
6981 public void tearDown () {
7082 xpathContext = null ;
7183 resource .unload ();
7284 resourceSet .getResources ().remove (resource );
85+ xpathChildContext = null ;
86+ childResource .unload ();
87+ resourceSet .getResources ().remove (childResource );
7388 }
7489
7590 @ Test
@@ -80,17 +95,48 @@ public void testAccessingTheApplication() {
8095
8196 @ Test
8297 public void testAccessingTheMainMenu () {
83- Object menu = xpathContext .getValue ("//mainMenu" );
84- assertThat ( menu ). isInstanceOf ( MMenu .class );
98+ assertThat ( xpathContext .getValue ("//mainMenu" )). isInstanceOf ( MMenu . class );
99+ assertNotNull ( xpathContext . getValue ( "//mainMenu" , MMenu .class ) );
85100
86- MMenu mMenu = xpathContext .getValue ("//mainMenu" , MMenu .class );
87- assertNotNull ( mMenu );
101+ assertNotNull ( xpathContext .getValue ("/children /mainMenu" , MMenu .class ) );
102+ assertThat ( xpathContext . getValue ( "/children/mainMenu" )). isInstanceOf ( MMenu . class );
88103 }
89104
90105 @ Test
91106 public void testAccessingAllMenus () {
92107 Object menuEntries = xpathContext .getValue ("//mainMenu/children" );
93- assertNotNull (menuEntries );
108+ assertThat (menuEntries ).isInstanceOf (List .class );
109+ List <?> list = (List <?>) menuEntries ;
110+ assertEquals (2 , list .size ());
111+ assertThat (list ).allMatch (MMenu .class ::isInstance , "Is instanceof of MMenu" ) //
112+ .anyMatch (e -> "File" .equals (((MMenu ) e ).getLabel ()))
113+ .anyMatch (e -> "Help" .equals (((MMenu ) e ).getLabel ()));
94114 }
95115
96- }
116+ @ Test
117+ public void testAccessingTheModelFragments () {
118+ Object modelFragments = xpathChildContext .getValue ("/" );
119+ assertThat (modelFragments ).isInstanceOf (MModelFragments .class );
120+ }
121+
122+ @ Test
123+ public void testAccessingTheStringModelFragment () {
124+ Object modelFragment = xpathChildContext .getValue ("//fragments[1]" );
125+ assertThat (modelFragment ).isInstanceOf (MStringModelFragment .class );
126+
127+ MStringModelFragment mModelFragment = xpathChildContext .getValue ("//fragments[1]" , MStringModelFragment .class );
128+ assertNotNull (mModelFragment );
129+
130+ Object modelFragment2 = xpathChildContext .getValue ("/fragments[1]" );
131+ assertThat (modelFragment2 ).isInstanceOf (MStringModelFragment .class );
132+ }
133+
134+ @ Test
135+ public void testAccessingTheAddons () {
136+ Object addon = xpathChildContext .getValue ("//elements[1]" );
137+ assertThat (addon ).isInstanceOf (MAddon .class );
138+
139+ MAddon mAddon = xpathChildContext .getValue ("//elements[1]" , MAddon .class );
140+ assertNotNull (mAddon );
141+ }
142+ }
0 commit comments