-
Notifications
You must be signed in to change notification settings - Fork 188
/
TestPath.java
275 lines (221 loc) · 11.7 KB
/
TestPath.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/*
* Copyright (c) 2010-2013 Evolveum and contributors
*
* This work is dual-licensed under the Apache License 2.0
* and European Union Public License. See LICENSE file for details.
*/
package com.evolveum.midpoint.prism;
import static org.testng.AssertJUnit.*;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import javax.xml.namespace.QName;
import org.testng.annotations.Test;
import com.evolveum.midpoint.prism.path.*;
/**
* @author semancik
*/
public class TestPath extends AbstractPrismTest {
private static final String NS = "http://example.com/";
private static Map<Character, Function<Object[], ItemPath>> creators = new HashMap<>();
static {
creators.put('C', seq -> getPrismContext().path(seq));
creators.put('S', ItemPath::create);
creators.put('N', seq -> seq.length == 1 ? ItemName.fromQName((QName) seq[0]) : ItemPath.create(seq));
creators.put('R', seq -> creators.get("CSN".charAt((int) (Math.random() * 3))).apply(seq));
}
/**
* See TestEditSchema.testEmptyPath()
*/
@Test
public void testEmptyPath() {
System.out.println("UniformItemPath.empty() = " + UniformItemPath.empty());
assertNotNull(UniformItemPath.empty());
}
@Test
public void testPathNormalize() {
PrismContext prismContext = getPrismContext();
// GIVEN
UniformItemPath path1 = prismContext.path(new QName(NS, "foo"), new QName(NS, "bar"));
UniformItemPath path2 = prismContext.path(new QName(NS, "foo"), 123L, new QName(NS, "bar"));
UniformItemPath path22 = prismContext.path(new QName(NS, "foo"), 123L, new QName(NS, "bar"), null);
UniformItemPath path3 = prismContext.path(new QName(NS, "foo"), 123L, new QName(NS, "bar"), 333L);
UniformItemPath path4 = prismContext.path(new QName(NS, "x"));
UniformItemPath path5 = prismContext.path(new QName(NS, "foo"), 123L);
UniformItemPath pathE = prismContext.emptyPath();
// WHEN
UniformItemPath normalized1 = path1.normalize();
UniformItemPath normalized2 = path2.normalize();
UniformItemPath normalized22 = path22.normalize();
UniformItemPath normalized3 = path3.normalize();
UniformItemPath normalized4 = path4.normalize();
UniformItemPath normalized5 = path5.normalize();
UniformItemPath normalizedE = pathE.normalize();
// THEN
System.out.println("Normalized path 1:" + normalized1);
System.out.println("Normalized path 2:" + normalized2);
System.out.println("Normalized path 22:" + normalized22);
System.out.println("Normalized path 3:" + normalized3);
System.out.println("Normalized path 4:" + normalized4);
System.out.println("Normalized path 5:" + normalized5);
System.out.println("Normalized path E:" + normalizedE);
assertNormalizedPath(normalized1, "foo", null, "bar");
assertNormalizedPath(normalized2, "foo", 123L, "bar");
assertNormalizedPath(normalized22, "foo", 123L, "bar", null);
assertNormalizedPath(normalized3, "foo", 123L, "bar", 333L);
assertNormalizedPath(normalized4, "x");
assertNormalizedPath(normalized5, "foo", 123L);
assert normalizedE.isEmpty() : "normalizedE is not empty";
}
@SuppressWarnings("SpellCheckingInspection")
@Test
public void testPathCompareComplex() {
testPathCompare("CCCCCCCCCCCC");
}
@SuppressWarnings("SpellCheckingInspection")
@Test
public void testPathCompareSimple() {
testPathCompare("SSSSSSSSSSSS");
}
@SuppressWarnings("SpellCheckingInspection")
@Test
public void testPathCompareSingleNames() {
testPathCompare("NNNNNNNNNNNN");
}
@SuppressWarnings("SpellCheckingInspection")
@Test
public void testPathCompareRandom() {
testPathCompare("RRRRRRRRRRRR");
}
@SuppressWarnings("SpellCheckingInspection")
@Test
public void testPathCompareHalf1() {
testPathCompare("CCCCCCSSSSSS");
}
@SuppressWarnings("SpellCheckingInspection")
@Test
public void testPathCompareHalf2() {
testPathCompare("SSSSSSNNNNNN");
}
private void testPathCompare(String source) {
// GIVEN
// /foo
ItemPath pathFoo = create(source, 0, new QName(NS, "foo"));
// /foo/
ItemPath pathFooNull = create(source, 1, new QName(NS, "foo"), null);
// /foo/[123]
ItemPath pathFoo123 = create(source, 2, new QName(NS, "foo"), 123L);
// /foo/bar
ItemPath pathFooBar = create(source, 3, new QName(NS, "foo"), new QName(NS, "bar"));
// /foo/[123]/bar
ItemPath pathFoo123Bar = create(source, 4, new QName(NS, "foo"), 123L, new QName(NS, "bar"));
// /foo/bar/baz
ItemPath pathFooBarBaz = create(source, 5, new QName(NS, "foo"), new QName(NS, "bar"), new QName(NS, "baz"));
// /foo/[123]/bar/baz
ItemPath pathFoo123BarBaz = create(source, 6, new QName(NS, "foo"), 123L, new QName(NS, "bar"), new QName(NS, "baz"));
// /foo/baz/baz
ItemPath pathFooBazBaz = create(source, 7, new QName(NS, "foo"), new QName(NS, "baz"), new QName(NS, "baz"));
// /foo/[123]/baz/baz
ItemPath pathFoo123BazBaz = create(source, 8, new QName(NS, "foo"), 123L, new QName(NS, "baz"), new QName(NS, "baz"));
// /foo//bar
ItemPath pathFooNullBar = create(source, 9, new QName(NS, "foo"), null, new QName(NS, "bar"));
// /zoo
ItemPath pathZoo = create(source, 10, new QName(NS, "zoo"));
ItemPath empty = create(source, 11);
List<ItemPath> onlyEmpty = Collections.singletonList(empty);
List<ItemPath> onlyFoo = Collections.singletonList(pathFoo);
List<ItemPath> onlyFooBar = Collections.singletonList(pathFooBar);
// WHEN, THEN
assertTrue(pathFoo.isSubPath(pathFooNull));
assertFalse(pathFoo.equivalent(pathFoo123));
assertFalse(pathFooNull.equivalent(pathFoo123));
assertTrue(pathFoo.isSubPath(pathFooBar));
assertTrue(pathFooBar.isSuperPath(pathFoo));
assertTrue(pathFooBar.equivalent(pathFooNullBar));
assertTrue(empty.isSubPath(pathFoo));
assertFalse(pathFoo.isSubPath(empty));
assertTrue(pathFoo123Bar.isSubPathOrEquivalent(pathFoo123BarBaz));
assertFalse(pathFoo123Bar.isSubPathOrEquivalent(pathZoo));
assertFalse(pathFoo123Bar.isSubPathOrEquivalent(pathFoo123BazBaz));
assertFalse(pathFooBar.isSubPathOrEquivalent(pathFoo123BarBaz));
assertFalse(pathFooBar.isSubPathOrEquivalent(pathZoo));
assertFalse(pathFooBar.isSubPathOrEquivalent(pathFoo123BazBaz));
assertTrue(pathFooBar.isSubPathOrEquivalent(pathFoo123BarBaz.namedSegmentsOnly()));
assertTrue(pathFooBar.isSubPathOrEquivalent(pathFooBar));
assertFalse(pathFooBar.isSubPathOrEquivalent(pathZoo));
assertFalse(pathFooBar.isSubPathOrEquivalent(pathFooBazBaz));
assertTrue(ItemPathCollectionsUtil.containsSubpathOrEquivalent(onlyEmpty, pathFoo));
assertTrue(ItemPathCollectionsUtil.containsSubpath(onlyEmpty, pathFoo));
assertFalse(ItemPathCollectionsUtil.containsSuperpathOrEquivalent(onlyEmpty, pathFoo));
assertFalse(ItemPathCollectionsUtil.containsSuperpath(onlyEmpty, pathFoo));
assertTrue(ItemPathCollectionsUtil.containsSubpathOrEquivalent(onlyEmpty, empty));
assertFalse(ItemPathCollectionsUtil.containsSubpath(onlyEmpty, empty));
assertTrue(ItemPathCollectionsUtil.containsSuperpathOrEquivalent(onlyEmpty, empty));
assertFalse(ItemPathCollectionsUtil.containsSuperpath(onlyEmpty, empty));
assertTrue(ItemPathCollectionsUtil.containsSubpathOrEquivalent(onlyFoo, pathFoo));
assertFalse(ItemPathCollectionsUtil.containsSubpath(onlyFoo, pathFoo));
assertTrue(ItemPathCollectionsUtil.containsSuperpathOrEquivalent(onlyFoo, pathFoo));
assertFalse(ItemPathCollectionsUtil.containsSuperpath(onlyFoo, pathFoo));
assertFalse(ItemPathCollectionsUtil.containsSubpathOrEquivalent(onlyFoo, empty));
assertFalse(ItemPathCollectionsUtil.containsSubpath(onlyFoo, empty));
assertTrue(ItemPathCollectionsUtil.containsSuperpathOrEquivalent(onlyFoo, empty));
assertTrue(ItemPathCollectionsUtil.containsSuperpath(onlyFoo, empty));
assertFalse(ItemPathCollectionsUtil.containsSubpathOrEquivalent(onlyFoo, pathZoo));
assertFalse(ItemPathCollectionsUtil.containsSubpath(onlyFoo, pathZoo));
assertFalse(ItemPathCollectionsUtil.containsSuperpathOrEquivalent(onlyFoo, pathZoo));
assertFalse(ItemPathCollectionsUtil.containsSuperpath(onlyFoo, pathZoo));
assertFalse(ItemPathCollectionsUtil.containsSubpathOrEquivalent(onlyFooBar, pathFoo));
assertFalse(ItemPathCollectionsUtil.containsSubpath(onlyFooBar, pathFoo));
assertTrue(ItemPathCollectionsUtil.containsSuperpathOrEquivalent(onlyFooBar, pathFoo));
assertTrue(ItemPathCollectionsUtil.containsSuperpath(onlyFooBar, pathFoo));
assertTrue(ItemPathCollectionsUtil.containsSubpathOrEquivalent(onlyFooBar, pathFooBarBaz));
assertTrue(ItemPathCollectionsUtil.containsSubpath(onlyFooBar, pathFooBarBaz));
assertFalse(ItemPathCollectionsUtil.containsSuperpathOrEquivalent(onlyFooBar, pathFooBarBaz));
assertFalse(ItemPathCollectionsUtil.containsSuperpath(onlyFooBar, pathFooBarBaz));
}
private ItemPath create(String source, int index, Object... components) {
return creators.get(source.charAt(index)).apply(components);
}
@Test
public void testPathRemainder() {
PrismContext prismContext = getPrismContext();
// GIVEN
UniformItemPath pathFoo = prismContext.path(new QName(NS, "foo"));
UniformItemPath pathBar = prismContext.path(new QName(NS, "bar"));
UniformItemPath pathFooNull = prismContext.path(new QName(NS, "foo"), null);
UniformItemPath pathFoo123 = prismContext.path(new QName(NS, "foo"), 123L);
UniformItemPath pathFooBar = prismContext.path(new QName(NS, "foo"), new QName(NS, "bar"));
UniformItemPath pathFooNullBar = prismContext.path(new QName(NS, "foo"), null,
new QName(NS, "bar"));
// WHEN
UniformItemPath remainder1 = pathFooBar.remainder(pathFooNull);
// THEN
assertEquals("Remainder fooBar, fooNull", pathBar, remainder1);
}
private void assertNormalizedPath(UniformItemPath normalized, Object... expected) {
assertEquals("wrong path length", normalized.size(), expected.length);
for (int i = 0; i < normalized.size(); i += 2) {
ItemPathSegment nameSegment = normalized.getSegment(i);
assert ItemPath.isName(nameSegment) : "Expected name segment but it was " + nameSegment.getClass();
QName name = ItemPath.toName(nameSegment);
assert name != null : "name is null";
assert name.getNamespaceURI().equals(NS) : "wrong namespace: " + name.getNamespaceURI();
assert name.getLocalPart().equals(expected[i]) : "wrong local name, expected " + expected[i] + ", was " + name.getLocalPart();
if (i + 1 < expected.length) {
Object idSegment = normalized.getSegment(i + 1);
assert ItemPath.isId(idSegment) : "Expected is segment but it was " + idSegment.getClass();
Long id = ItemPath.toId(idSegment);
assertId(id, (Long) expected[i + 1]);
}
}
}
private void assertId(Long actual, Long expected) {
if (expected == null && actual == null) {
return;
}
assert expected != null : "Expected null id but it was " + actual;
assertEquals("wrong id", expected, actual);
}
}