16
16
17
17
import static org .assertj .core .api .Assertions .assertThat ;
18
18
import static org .junit .Assert .assertEquals ;
19
+ import static org .junit .Assert .assertFalse ;
19
20
import static org .junit .Assert .assertNotNull ;
21
+ import static org .junit .Assert .assertSame ;
20
22
import static org .junit .Assert .assertThrows ;
21
23
import static org .junit .Assert .assertTrue ;
22
24
@@ -47,6 +49,7 @@ public class ExampleQueriesTestCase {
47
49
private ResourceSet resourceSet ;
48
50
private XPathContext xpathContext ;
49
51
private Resource resource ;
52
+ private XPathContextFactory <EObject > xpathContextFactory ;
50
53
51
54
@ Before
52
55
public void setUp () {
@@ -58,13 +61,14 @@ public void setUp() {
58
61
resourceSet .getPackageRegistry ().put (XpathtestPackage .eNS_URI , XpathtestPackage .eINSTANCE );
59
62
URI uri = URI .createPlatformPluginURI ("/org.eclipse.e4.emf.xpath.test/model/Test.xmi" , true );
60
63
resource = resourceSet .getResource (uri , true );
61
- XPathContextFactory < EObject > f = EcoreXPathContextFactory .newInstance ();
62
- xpathContext = f .newContext (resource .getContents ().get (0 ));
64
+ xpathContextFactory = EcoreXPathContextFactory .newInstance ();
65
+ xpathContext = xpathContextFactory .newContext (resource .getContents ().get (0 ));
63
66
}
64
67
65
68
@ After
66
69
public void tearDown () {
67
70
xpathContext = null ;
71
+ xpathContextFactory = null ;
68
72
resource .unload ();
69
73
resourceSet .getResources ().remove (resource );
70
74
}
@@ -82,6 +86,7 @@ public void testSimpleQuery() {
82
86
assertThat (application ).isInstanceOf (Root .class );
83
87
84
88
assertThrows (JXPathNotFoundException .class , () -> xpathContext .getValue (".[@id='nixda']" ));
89
+ assertFalse (xpathContext .iterate (".[@id='nixda']" ).hasNext ());
85
90
86
91
application = xpathContext .getValue (".[@id='root']" );
87
92
assertThat (application ).isInstanceOf (Root .class );
@@ -95,6 +100,8 @@ public void testSimpleQuery() {
95
100
assertThat (xpathContext .getValue ("//.[ecore:eClassName(.)='ExtendedNode']" )).isInstanceOf (ExtendedNode .class );
96
101
97
102
assertNotNull (xpathContext .getValue ("//.[ecore:eClassName(.)='ExtendedNode']" , ExtendedNode .class ));
103
+
104
+ assertEquals (rootApplication , xpathContext .getValue ("." ));
98
105
}
99
106
100
107
@ Test
@@ -119,4 +126,15 @@ public void testMenuQuery() {
119
126
assertEquals (26 , list .size ());
120
127
}
121
128
129
+ @ Test
130
+ public void testRelative () {
131
+ EObject context = resource .getContents ().get (0 );
132
+ List <EObject > eContents = context .eContents ();
133
+ EObject firstElement = eContents .get (0 );
134
+ XPathContext nestedXpathContext = xpathContextFactory .newContext (xpathContext , firstElement );
135
+
136
+ List <Node > dotList = nestedXpathContext .stream ("." , Node .class ).toList ();
137
+ assertEquals (1 , dotList .size ());
138
+ assertSame (firstElement , dotList .get (0 ));
139
+ }
122
140
}
0 commit comments