Debian Codegraph @

tests/org.eclipse.emf.test.xml/src/org/eclipse/emf/test/xml/xmi/XMIResourceURIFragmentsTest.java

raw
1 /**
2 * Copyright (c) 2018 Eclipse contributors and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v2.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v20.html
7 */
8 package org.eclipse.emf.test.xml.xmi;
9
10
11 import java.util.List;
12
13 import org.eclipse.emf.common.util.URI;
14 import org.eclipse.emf.ecore.EClass;
15 import org.eclipse.emf.ecore.EObject;
16 import org.eclipse.emf.ecore.EPackage;
17 import org.eclipse.emf.ecore.EReference;
18 import org.eclipse.emf.ecore.EcoreFactory;
19 import org.eclipse.emf.ecore.resource.Resource;
20 import org.eclipse.emf.ecore.util.EcoreUtil;
21 import org.eclipse.emf.ecore.xmi.XMLResource;
22 import org.eclipse.emf.ecore.xmi.impl.XMIResourceImpl;
23 import org.junit.Assert;
24 import org.junit.Before;
25 import org.junit.Test;
26
27
28 public class XMIResourceURIFragmentsTest
29 {
30 private static final EClass NODE_CLASS;
31
32 private static final EReference NODES_REFERENCE;
33
34 static
35 {
36 EPackage ePackage = EcoreFactory.eINSTANCE.createEPackage();
37 ePackage.setName("nodes");
38 ePackage.setNsPrefix("nodes");
39 ePackage.setNsURI("nodes");
40
41 EClass eClass = EcoreFactory.eINSTANCE.createEClass();
42 eClass.setName("Node");
43 ePackage.getEClassifiers().add(eClass);
44 NODE_CLASS = eClass;
45
46 EReference eReference = EcoreFactory.eINSTANCE.createEReference();
47 eReference.setName("nodes");
48 eReference.setEType(eClass);
49 eReference.setContainment(true);
50 eReference.setUpperBound(-1);
51 eClass.getEStructuralFeatures().add(eReference);
52 NODES_REFERENCE = eReference;
53 }
54
55 private Resource resource;
56
57 private Resource resourceWithRelativeIDFragments;
58
59 @Before
60 public void setUp()
61 {
62 resource = new XMIResourceImpl();
63 resource.setURI(URI.createURI("resource.xmi"));
64
65 resourceWithRelativeIDFragments = new XMIResourceImpl()
66 {
67 @Override
68 protected boolean supportIDRelativeURIFragmentPaths()
69 {
70 return true;
71 }
72 };
73 resourceWithRelativeIDFragments.setURI(URI.createURI("resourceWithRelativeIDFragments.xmi"));
74 }
75
76 protected EObject createNode()
77 {
78 return EcoreUtil.create(NODE_CLASS);
79 }
80
81 protected void addChild(EObject node, EObject child)
82 {
83 @SuppressWarnings("unchecked")
84 List<EObject> nodes = (List<EObject>)node.eGet(NODES_REFERENCE);
85 nodes.add(child);
86 }
87
88 protected void setID(EObject node, String id)
89 {
90 ((XMLResource)node.eResource()).setID(node, id);
91 }
92
93 @Test
94 public void testSingleRoot()
95 {
96 EObject node = createNode();
97 resource.getContents().add(node);
98 Assert.assertEquals("/", resource.getURIFragment(node));
99 Assert.assertSame(node, resource.getEObject("/"));
100 }
101
102 @Test
103 public void testSingleRootWithID()
104 {
105 EObject node = createNode();
106 resource.getContents().add(node);
107 setID(node, "root");
108 Assert.assertEquals("root", resource.getURIFragment(node));
109 Assert.assertSame(node, resource.getEObject("root"));
110 }
111
112 @Test
113 public void testMultipleRoot()
114 {
115 EObject node0 = createNode();
116 resource.getContents().add(node0);
117 EObject node1 = createNode();
118 resource.getContents().add(node1);
119 Assert.assertEquals("/1", resource.getURIFragment(node1));
120 Assert.assertSame(node1, resource.getEObject("/1"));
121 }
122
123 @Test
124 public void testMultipleRootWithID()
125 {
126 EObject node0 = createNode();
127 resource.getContents().add(node0);
128 EObject node1 = createNode();
129 resource.getContents().add(node1);
130 setID(node1, "id");
131 Assert.assertEquals("id", resource.getURIFragment(node1));
132 Assert.assertSame(node1, resource.getEObject("id"));
133 }
134
135 @Test
136 public void testNestedNode()
137 {
138 EObject node0 = createNode();
139 resourceWithRelativeIDFragments.getContents().add(node0);
140 EObject node1 = createNode();
141 addChild(node0, node1);
142 Assert.assertEquals("//@nodes.0", resourceWithRelativeIDFragments.getURIFragment(node1));
143 Assert.assertSame(node1, resourceWithRelativeIDFragments.getEObject("//@nodes.0"));
144 }
145
146 @Test
147 public void testNestedNodeWithID()
148 {
149 EObject node0 = createNode();
150 resourceWithRelativeIDFragments.getContents().add(node0);
151 EObject node1 = createNode();
152 addChild(node0, node1);
153 setID(node1, "id");
154 Assert.assertEquals("id", resourceWithRelativeIDFragments.getURIFragment(node1));
155 Assert.assertSame(node1, resourceWithRelativeIDFragments.getEObject("id"));
156 }
157
158 @Test
159 public void testNestedNodeWithIDAndRootID()
160 {
161 EObject node0 = createNode();
162 resourceWithRelativeIDFragments.getContents().add(node0);
163 setID(node0, "id0");
164 EObject node1 = createNode();
165 addChild(node0, node1);
166 setID(node1, "id1");
167 Assert.assertEquals("id1", resourceWithRelativeIDFragments.getURIFragment(node1));
168 Assert.assertSame(node1, resourceWithRelativeIDFragments.getEObject("id1"));
169 }
170
171 @Test
172 public void testNestedNodeWithRootID()
173 {
174 EObject node0 = createNode();
175 resourceWithRelativeIDFragments.getContents().add(node0);
176 setID(node0, "id");
177 EObject node1 = createNode();
178 addChild(node0, node1);
179 Assert.assertEquals("/?id/@nodes.0", resourceWithRelativeIDFragments.getURIFragment(node1));
180 Assert.assertSame(node1, resourceWithRelativeIDFragments.getEObject("/?id/@nodes.0"));
181 }
182
183 @Test
184 public void testNestedDeepNodeWithIntermediateID()
185 {
186 EObject node0 = createNode();
187 resourceWithRelativeIDFragments.getContents().add(node0);
188 EObject node1 = createNode();
189 addChild(node0, node1);
190 EObject node2 = createNode();
191 addChild(node1, node2);
192 setID(node2, "id");
193 EObject node3 = createNode();
194 addChild(node2, node3);
195 Assert.assertEquals("/?id/@nodes.0", resourceWithRelativeIDFragments.getURIFragment(node3));
196 Assert.assertSame(node3, resourceWithRelativeIDFragments.getEObject("/?id/@nodes.0"));
197 }
198 }