1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package org.xwiki.model.reference

File EntityReferenceTest.java

 

Code metrics

4
272
25
1
593
452
30
0.11
10.88
25
1.2

Classes

Class Line # Actions
EntityReferenceTest 52 272 0% 30 5
0.983388798.3%
 

Contributing tests

This file is covered by 23 tests. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package org.xwiki.model.reference;
21   
22    import java.io.ByteArrayInputStream;
23    import java.io.ByteArrayOutputStream;
24    import java.io.ObjectInputStream;
25    import java.io.ObjectOutputStream;
26    import java.io.Serializable;
27    import java.util.ArrayList;
28    import java.util.Collections;
29    import java.util.HashMap;
30    import java.util.List;
31    import java.util.Map;
32    import java.util.UUID;
33   
34    import org.junit.jupiter.api.Test;
35    import org.xwiki.model.EntityType;
36   
37    import static org.junit.jupiter.api.Assertions.assertEquals;
38    import static org.junit.jupiter.api.Assertions.assertFalse;
39    import static org.junit.jupiter.api.Assertions.assertNotSame;
40    import static org.junit.jupiter.api.Assertions.assertNull;
41    import static org.junit.jupiter.api.Assertions.assertSame;
42    import static org.junit.jupiter.api.Assertions.assertThrows;
43    import static org.junit.jupiter.api.Assertions.assertTrue;
44    import static org.junit.jupiter.api.Assertions.fail;
45   
46    /**
47    * Unit tests for {@link EntityReference}.
48    *
49    * @version $Id: 0e6b22a5d5c9570565f09738df8888df5619b486 $
50    * @since 2.2M1
51    */
 
52    public class EntityReferenceTest
53    {
 
54  17 toggle private Map<String, Serializable> getParamMap(int nb)
55    {
56  17 Map<String, Serializable> map = new HashMap<>(nb);
57  55 for (int i = 0; i < nb; i++) {
58  38 map.put(UUID.randomUUID().toString(), UUID.randomUUID());
59    }
60  17 return map;
61    }
62   
 
63  9 toggle private boolean checkParamMap(EntityReference ref, Map<String, Serializable> map)
64    {
65  9 for (Map.Entry<String, Serializable> entry : map.entrySet()) {
66  23 if (entry.getValue() != ref.getParameter(entry.getKey())) {
67  0 return false;
68    }
69    }
70  9 return true;
71    }
72   
 
73  1 toggle @Test
74    public void extractReference()
75    {
76  1 EntityReference wiki = new EntityReference("wiki", EntityType.WIKI);
77  1 EntityReference space1 = new EntityReference("space1", EntityType.SPACE, wiki);
78  1 EntityReference space2 = new EntityReference("space2", EntityType.SPACE, space1);
79  1 EntityReference reference = new EntityReference("page", EntityType.DOCUMENT, space2);
80   
81  1 assertSame(wiki, reference.extractReference(EntityType.WIKI));
82  1 assertSame(space2, reference.extractReference(EntityType.SPACE));
83  1 assertSame(reference, reference.extractReference(EntityType.DOCUMENT));
84  1 assertNull(reference.extractReference(EntityType.ATTACHMENT));
85    }
86   
 
87  1 toggle @Test
88    public void extractFirstReference()
89    {
90  1 EntityReference wiki = new EntityReference("wiki", EntityType.WIKI);
91  1 EntityReference space1 = new EntityReference("space1", EntityType.SPACE, wiki);
92  1 EntityReference space2 = new EntityReference("space2", EntityType.SPACE, space1);
93  1 EntityReference reference = new EntityReference("page", EntityType.DOCUMENT, space2);
94   
95  1 assertSame(wiki, reference.extractFirstReference(EntityType.WIKI));
96  1 assertSame(space1, reference.extractFirstReference(EntityType.SPACE));
97  1 assertSame(reference, reference.extractFirstReference(EntityType.DOCUMENT));
98  1 assertNull(reference.extractFirstReference(EntityType.ATTACHMENT));
99    }
100   
 
101  1 toggle @Test
102    public void getRoot()
103    {
104  1 EntityReference wiki = new EntityReference("wiki", EntityType.WIKI);
105  1 EntityReference reference =
106    new EntityReference("page", EntityType.DOCUMENT, new EntityReference("space", EntityType.SPACE, wiki));
107  1 assertSame(wiki, reference.getRoot());
108    }
109   
 
110  1 toggle @Test
111    public void getReversedReferenceChain()
112    {
113  1 EntityReference wiki = new EntityReference("wiki", EntityType.WIKI);
114  1 EntityReference space = new EntityReference("space", EntityType.SPACE, wiki);
115  1 EntityReference reference = new EntityReference("page", EntityType.DOCUMENT, space);
116   
117  1 List<EntityReference> list = reference.getReversedReferenceChain();
118   
119  1 assertEquals(3, list.size());
120  1 assertSame(wiki, list.get(0));
121  1 assertSame(space, list.get(1));
122  1 assertSame(reference, list.get(2));
123    }
124   
 
125  1 toggle @Test
126    public void copyConstructor()
127    {
128  1 Map<String, Serializable> map1 = getParamMap(3);
129  1 Map<String, Serializable> map2 = getParamMap(1);
130   
131  1 EntityReference parent =
132    new EntityReference("space", EntityType.SPACE, new EntityReference("wiki", EntityType.WIKI, null, map2));
133   
134  1 EntityReference reference = new EntityReference("page", EntityType.DOCUMENT, parent, map1);
135   
136  1 assertEquals("page", reference.getName());
137  1 assertSame(EntityType.DOCUMENT, reference.getType());
138  1 assertSame(parent, reference.getParent());
139  1 assertTrue(checkParamMap(reference, map1));
140  1 assertTrue(checkParamMap(reference.getParent().getParent(), map2));
141    }
142   
 
143  1 toggle @Test
144    public void validateEquals()
145    {
146  1 EntityReference reference1 = new EntityReference("page", EntityType.DOCUMENT,
147    new EntityReference("space", EntityType.SPACE, new EntityReference("wiki", EntityType.WIKI)));
148   
149  1 EntityReference reference2 = new EntityReference("page", EntityType.DOCUMENT,
150    new EntityReference("space", EntityType.SPACE, new EntityReference("wiki", EntityType.WIKI)));
151   
152  1 EntityReference reference3 = new EntityReference("page", EntityType.DOCUMENT,
153    new EntityReference("space", EntityType.SPACE, new EntityReference("wiki2", EntityType.WIKI)));
154   
155  1 EntityReference reference4 = new EntityReference("page", EntityType.DOCUMENT,
156    new EntityReference("space2", EntityType.SPACE, new EntityReference("wiki", EntityType.WIKI)));
157   
158  1 EntityReference reference5 = new EntityReference("page2", EntityType.DOCUMENT,
159    new EntityReference("space", EntityType.SPACE, new EntityReference("wiki", EntityType.WIKI)));
160   
161  1 EntityReference reference6 = new EntityReference("page", EntityType.DOCUMENT);
162   
163  1 Map<String, Serializable> map = getParamMap(3);
164  1 EntityReference reference7 = new EntityReference("page", EntityType.DOCUMENT,
165    new EntityReference("space", EntityType.SPACE, new EntityReference("wiki", EntityType.WIKI)), map);
166   
167  1 EntityReference reference8 = new EntityReference("page", EntityType.DOCUMENT,
168    new EntityReference("space", EntityType.SPACE, new EntityReference("wiki", EntityType.WIKI)), map);
169   
170  1 EntityReference reference9 = new EntityReference("page", EntityType.DOCUMENT,
171    new EntityReference("space", EntityType.SPACE, new EntityReference("wiki", EntityType.WIKI), map));
172   
173  1 EntityReference reference10 = new EntityReference("page", EntityType.DOCUMENT,
174    new EntityReference("space", EntityType.SPACE, new EntityReference("wiki", EntityType.WIKI, null, map)));
175   
176  1 assertTrue(reference1.equals(reference1));
177  1 assertTrue(reference1.equals(reference2));
178  1 assertFalse(reference1.equals(reference3));
179  1 assertFalse(reference1.equals(reference4));
180  1 assertFalse(reference1.equals(reference5));
181  1 assertFalse(reference1.equals(reference6));
182  1 assertEquals(reference6, new EntityReference("page", EntityType.DOCUMENT));
183  1 assertFalse(reference1.equals(reference7));
184  1 assertTrue(reference7.equals(reference8));
185  1 assertFalse(reference1.equals(reference9));
186  1 assertFalse(reference7.equals(reference9));
187  1 assertFalse(reference1.equals(reference10));
188  1 assertFalse(reference7.equals(reference10));
189    }
190   
 
191  1 toggle @Test
192    public void equalsTo()
193    {
194  1 EntityReference documentReference = new EntityReference("page", EntityType.DOCUMENT,
195    new EntityReference("space", EntityType.SPACE, new EntityReference("wiki", EntityType.WIKI)));
196   
197  1 EntityReference localdocumentReference =
198    new EntityReference("page", EntityType.DOCUMENT, new EntityReference("space", EntityType.SPACE));
199   
200  1 assertTrue(localdocumentReference.equals(documentReference, EntityType.SPACE));
201  1 assertTrue(documentReference.equals(localdocumentReference, EntityType.SPACE));
202   
203  1 EntityReference localdocumentReference2 =
204    new EntityReference("page", EntityType.DOCUMENT, new EntityReference("space2", EntityType.SPACE));
205   
206  1 assertFalse(localdocumentReference.equals(localdocumentReference2, EntityType.SPACE));
207    }
208   
 
209  1 toggle @Test
210    public void equalsFromTo()
211    {
212  1 EntityReference documentReference = new EntityReference("page1", EntityType.DOCUMENT,
213    new EntityReference("space", EntityType.SPACE, new EntityReference("wiki", EntityType.WIKI)));
214   
215  1 EntityReference spaceReference =
216    new EntityReference("space", EntityType.SPACE, new EntityReference("wiki2", EntityType.WIKI));
217   
218  1 assertTrue(spaceReference.equals(documentReference, EntityType.SPACE, EntityType.SPACE));
219  1 assertTrue(documentReference.equals(spaceReference, EntityType.SPACE, EntityType.SPACE));
220   
221  1 assertFalse(spaceReference.equals(documentReference, EntityType.SPACE, EntityType.WIKI));
222  1 assertFalse(documentReference.equals(spaceReference, EntityType.SPACE, EntityType.WIKI));
223    }
224   
 
225  1 toggle @Test
226    public void equalsNonRecursive()
227    {
228  1 EntityReference documentReference1 = new EntityReference("page", EntityType.DOCUMENT,
229    new EntityReference("space", EntityType.SPACE, new EntityReference("wiki", EntityType.WIKI)));
230  1 EntityReference documentReference2 = new EntityReference("page", EntityType.DOCUMENT,
231    new EntityReference("space2", EntityType.SPACE, new EntityReference("wiki2", EntityType.WIKI)));
232   
233  1 assertTrue(documentReference1.equalsNonRecursive(documentReference2));
234   
235  1 assertFalse(documentReference1.getParent().equalsNonRecursive(documentReference2.getParent()));
236    }
237   
 
238  1 toggle @Test
239    public void validateHashCode()
240    {
241  1 EntityReference reference1 = new EntityReference("page", EntityType.DOCUMENT,
242    new EntityReference("space", EntityType.SPACE, new EntityReference("wiki", EntityType.WIKI)));
243   
244  1 EntityReference reference2 = new EntityReference("page", EntityType.DOCUMENT,
245    new EntityReference("space", EntityType.SPACE, new EntityReference("wiki", EntityType.WIKI)));
246   
247  1 EntityReference reference3 = new EntityReference("page", EntityType.DOCUMENT,
248    new EntityReference("space", EntityType.SPACE, new EntityReference("wiki2", EntityType.WIKI)));
249   
250  1 EntityReference reference4 = new EntityReference("page", EntityType.DOCUMENT,
251    new EntityReference("space2", EntityType.SPACE, new EntityReference("wiki", EntityType.WIKI)));
252   
253  1 EntityReference reference5 = new EntityReference("page2", EntityType.DOCUMENT,
254    new EntityReference("space", EntityType.SPACE, new EntityReference("wiki", EntityType.WIKI)));
255   
256  1 EntityReference reference6 = new EntityReference("page", EntityType.DOCUMENT);
257   
258  1 Map<String, Serializable> map = getParamMap(3);
259  1 EntityReference reference7 = new EntityReference("page", EntityType.DOCUMENT,
260    new EntityReference("space", EntityType.SPACE, new EntityReference("wiki", EntityType.WIKI)), map);
261   
262  1 EntityReference reference8 = new EntityReference("page", EntityType.DOCUMENT,
263    new EntityReference("space", EntityType.SPACE, new EntityReference("wiki", EntityType.WIKI)), map);
264   
265  1 EntityReference reference9 = new EntityReference("page", EntityType.DOCUMENT,
266    new EntityReference("space", EntityType.SPACE, new EntityReference("wiki", EntityType.WIKI), map));
267   
268  1 EntityReference reference10 = new EntityReference("page", EntityType.DOCUMENT,
269    new EntityReference("space", EntityType.SPACE, new EntityReference("wiki", EntityType.WIKI, null, map)));
270   
271  1 assertEquals(reference1.hashCode(), reference1.hashCode());
272  1 assertEquals(reference1.hashCode(), reference2.hashCode());
273  1 assertFalse(reference1.hashCode() == reference3.hashCode());
274  1 assertFalse(reference1.hashCode() == reference4.hashCode());
275  1 assertFalse(reference1.hashCode() == reference5.hashCode());
276  1 assertFalse(reference1.hashCode() == reference6.hashCode());
277  1 assertEquals(reference6.hashCode(), new EntityReference("page", EntityType.DOCUMENT).hashCode());
278  1 assertFalse(reference1.hashCode() == reference7.hashCode());
279  1 assertEquals(reference7.hashCode(), reference8.hashCode());
280  1 assertFalse(reference1.hashCode() == reference9.hashCode());
281  1 assertFalse(reference7.hashCode() == reference9.hashCode());
282  1 assertFalse(reference1.hashCode() == reference10.hashCode());
283  1 assertFalse(reference7.hashCode() == reference10.hashCode());
284    }
285   
 
286  1 toggle @Test
287    public void compareTo()
288    {
289  1 EntityReference reference = new EntityReference("f", EntityType.DOCUMENT,
290    new EntityReference("e", EntityType.SPACE, new EntityReference("d", EntityType.WIKI)));
291   
292  1 EntityReference reference2 = new EntityReference("c", EntityType.DOCUMENT,
293    new EntityReference("b", EntityType.SPACE, new EntityReference("a", EntityType.WIKI)));
294   
295  1 EntityReference reference3 = new EntityReference("c", EntityType.DOCUMENT,
296    new EntityReference("a", EntityType.SPACE, new EntityReference("a", EntityType.WIKI)));
297   
298  1 EntityReference reference31 = new EntityReference("c", EntityType.DOCUMENT, new EntityReference("x",
299    EntityType.SPACE, new EntityReference("a", EntityType.SPACE, new EntityReference("a", EntityType.WIKI))));
300   
301  1 assertEquals(0, reference.compareTo(reference));
302  1 assertTrue(reference.compareTo(reference2) > 0);
303  1 assertTrue(reference.compareTo(reference3) > 0);
304   
305  1 Map<String, Serializable> map1 = new HashMap<>(3);
306  1 map1.put("param1", "a");
307  1 map1.put("param2", "b");
308  1 map1.put("param3", "c");
309  1 Map<String, Serializable> map2 = new HashMap<>(2);
310  1 map2.put("param1", "a");
311  1 map2.put("param3", "c");
312  1 Map<String, Serializable> map3 = new HashMap<>(2);
313  1 map3.put("param1", "b");
314  1 map3.put("param3", "c");
315   
316  1 EntityReference reference4 = new EntityReference("c", EntityType.DOCUMENT,
317    new EntityReference("a", EntityType.SPACE, new EntityReference("a", EntityType.WIKI)), map1);
318   
319  1 EntityReference reference5 = new EntityReference("c", EntityType.DOCUMENT,
320    new EntityReference("a", EntityType.SPACE, new EntityReference("a", EntityType.WIKI)), map2);
321   
322  1 EntityReference reference6 = new EntityReference("c", EntityType.DOCUMENT,
323    new EntityReference("a", EntityType.SPACE, new EntityReference("a", EntityType.WIKI)), map3);
324   
325  1 EntityReference reference61 =
326    new EntityReference("c", EntityType.DOCUMENT, new EntityReference("x", EntityType.SPACE,
327    new EntityReference("a", EntityType.SPACE, new EntityReference("a", EntityType.WIKI)), map3));
328   
329  1 assertEquals(0, reference4.compareTo(reference4));
330  1 assertTrue(reference4.compareTo(reference5) > 0);
331  1 assertTrue(reference4.compareTo(reference6) < 0);
332  1 assertTrue(reference4.compareTo(reference61) < 0);
333   
334  1 List<EntityReference> list = new ArrayList<>();
335  1 list.add(reference);
336  1 list.add(reference2);
337  1 list.add(reference3);
338  1 list.add(reference31);
339  1 list.add(reference4);
340  1 list.add(reference5);
341  1 list.add(reference6);
342  1 list.add(reference61);
343  1 Collections.sort(list);
344   
345    // Reference3 is first since it serializes as a:a.c which comes before a:a.x.c, a:b.c and d:e.f
346  1 assertSame(reference3, list.get(0));
347  1 assertSame(reference5, list.get(1));
348  1 assertSame(reference4, list.get(2));
349  1 assertSame(reference6, list.get(3));
350  1 assertSame(reference31, list.get(4));
351  1 assertSame(reference61, list.get(5));
352  1 assertSame(reference2, list.get(6));
353  1 assertSame(reference, list.get(7));
354    }
355   
 
356  1 toggle @Test
357    public void nullTypeInConstructor()
358    {
359  1 try {
360  1 new EntityReference("name", null);
361  0 fail("Should have thrown an exception here");
362    } catch (IllegalArgumentException expected) {
363  1 assertEquals("An Entity Reference type cannot be null", expected.getMessage());
364    }
365    }
366   
 
367  1 toggle @Test
368    public void nullNameInConstructor()
369    {
370  1 try {
371  1 new EntityReference(null, EntityType.WIKI);
372  0 fail("Should have thrown an exception here");
373    } catch (IllegalArgumentException expected) {
374  1 assertEquals("An Entity Reference name cannot be null or empty", expected.getMessage());
375    }
376    }
377   
 
378  1 toggle @Test
379    public void emptyNameInConstructor()
380    {
381  1 try {
382  1 new EntityReference("", EntityType.WIKI);
383  0 fail("Should have thrown an exception here");
384    } catch (IllegalArgumentException expected) {
385  1 assertEquals("An Entity Reference name cannot be null or empty", expected.getMessage());
386    }
387    }
388   
 
389  1 toggle @Test
390    public void testReplaceParent1()
391    {
392  1 Map<String, Serializable> map1 = getParamMap(3);
393  1 Map<String, Serializable> map2 = getParamMap(2);
394  1 Map<String, Serializable> map3 = getParamMap(1);
395   
396  1 EntityReference wiki = new EntityReference("wiki", EntityType.WIKI, null, map3);
397   
398  1 EntityReference space = new EntityReference("space", EntityType.SPACE, wiki, map2);
399  1 EntityReference space2 =
400    new EntityReference("space2", EntityType.SPACE, new EntityReference("wiki2", EntityType.WIKI));
401   
402  1 EntityReference reference = new EntityReference("page", EntityType.DOCUMENT, space, map1);
403  1 EntityReference referenceSpace2 = reference.replaceParent(space2);
404   
405  1 assertNotSame(reference, referenceSpace2);
406  1 assertTrue(checkParamMap(referenceSpace2, map1));
407  1 assertSame(space2, referenceSpace2.getParent());
408   
409  1 assertSame(reference.replaceParent(space), reference);
410    }
411   
 
412  1 toggle @Test
413    public void replaceParent2()
414    {
415  1 Map<String, Serializable> map1 = getParamMap(3);
416  1 Map<String, Serializable> map2 = getParamMap(2);
417  1 Map<String, Serializable> map3 = getParamMap(1);
418   
419  1 EntityReference wiki = new EntityReference("wiki", EntityType.WIKI, null, map3);
420  1 EntityReference wiki2 = new EntityReference("wiki2", EntityType.WIKI);
421   
422  1 EntityReference space = new EntityReference("space", EntityType.SPACE, wiki, map2);
423  1 EntityReference space2 = new EntityReference("space2", EntityType.SPACE, wiki);
424   
425  1 EntityReference reference = new EntityReference("page", EntityType.DOCUMENT, space, map1);
426  1 EntityReference referenceSpace2 = reference.replaceParent(space, space2);
427  1 EntityReference referenceWiki2 = reference.replaceParent(wiki, wiki2);
428   
429  1 assertNotSame(reference, referenceSpace2);
430  1 assertTrue(checkParamMap(referenceSpace2, map1));
431  1 assertSame(space2, referenceSpace2.getParent());
432   
433  1 assertNotSame(reference, referenceWiki2);
434  1 assertTrue(checkParamMap(referenceWiki2, map1));
435  1 assertNotSame(space, referenceWiki2.getParent());
436  1 assertTrue(checkParamMap(referenceWiki2.getParent(), map2));
437  1 assertSame(wiki2, referenceWiki2.getParent().getParent());
438   
439  1 assertSame(reference.replaceParent(wiki, wiki), reference);
440  1 assertSame(reference.replaceParent(space, space), reference);
441    }
442   
 
443  1 toggle @Test
444    public void replaceUnknownParent2()
445    {
446  1 EntityReference wiki = new EntityReference("wiki", EntityType.WIKI);
447  1 EntityReference wiki2 = new EntityReference("wiki2", EntityType.WIKI);
448   
449  1 EntityReference space = new EntityReference("space", EntityType.SPACE, wiki);
450   
451  1 IllegalArgumentException e = assertThrows(IllegalArgumentException.class,
452    () -> space.replaceParent(new EntityReference("nowiki", EntityType.WIKI), wiki2));
453   
454  1 assertEquals(
455    "The old reference [Wiki nowiki] does not belong to the parents chain of the reference [Wiki wiki]",
456    e.getMessage());
457    }
458   
 
459  1 toggle @Test
460    public void constructorCloneNullReference()
461    {
462  1 IllegalArgumentException e = assertThrows(IllegalArgumentException.class, () -> new EntityReference(null,
463    new EntityReference("wiki", EntityType.WIKI), new EntityReference("wiki2", EntityType.WIKI)));
464   
465  1 assertEquals("Cloned reference must not be null", e.getMessage());
466    }
467   
 
468  1 toggle @Test
469    public void appendParent()
470    {
471  1 Map<String, Serializable> map1 = getParamMap(3);
472  1 Map<String, Serializable> map2 = getParamMap(2);
473   
474  1 EntityReference wiki = new EntityReference("wiki", EntityType.WIKI, null, map2);
475   
476  1 EntityReference space = new EntityReference("space", EntityType.SPACE, wiki, map1);
477  1 EntityReference spaceAlone = new EntityReference("space", EntityType.SPACE, null, map1);
478   
479  1 EntityReference reference = new EntityReference("page", EntityType.DOCUMENT, space);
480  1 EntityReference referenceSpace = new EntityReference("page", EntityType.DOCUMENT).appendParent(space);
481  1 EntityReference referenceWiki = new EntityReference("page", EntityType.DOCUMENT, spaceAlone).appendParent(wiki);
482   
483  1 assertNotSame(reference, referenceSpace);
484  1 assertEquals(reference, referenceSpace);
485  1 assertSame(reference.getParent(), referenceSpace.getParent());
486   
487  1 assertNotSame(reference, referenceWiki);
488  1 assertEquals(reference, referenceWiki);
489  1 assertSame(reference.getParent().getParent(), referenceSpace.getParent().getParent());
490    }
491   
 
492  1 toggle @Test
493    public void removeParent()
494    {
495  1 Map<String, Serializable> map1 = getParamMap(3);
496  1 Map<String, Serializable> map2 = getParamMap(2);
497   
498  1 EntityReference wiki = new EntityReference("wiki", EntityType.WIKI);
499  1 EntityReference space = new EntityReference("space", EntityType.SPACE, wiki, map2);
500  1 EntityReference reference = new EntityReference("page", EntityType.DOCUMENT, space, map1);
501  1 EntityReference referenceSpace = reference.removeParent(space);
502  1 EntityReference referenceWiki = reference.removeParent(wiki);
503   
504  1 assertEquals(reference.getName(), referenceSpace.getName());
505  1 assertTrue(checkParamMap(referenceSpace, map1));
506  1 assertNull(referenceSpace.getParent());
507  1 assertEquals(reference.getName(), referenceWiki.getName());
508   
509  1 assertTrue(checkParamMap(referenceWiki, map1));
510  1 assertEquals(reference.getParent().getName(), referenceWiki.getParent().getName());
511  1 assertTrue(checkParamMap(referenceWiki.getParent(), map2));
512  1 assertNull(referenceWiki.getParent().getParent());
513    }
514   
 
515  1 toggle @Test
516    public void hasParent()
517    {
518  1 EntityReference wiki = new EntityReference("wiki", EntityType.WIKI);
519  1 EntityReference alice = new EntityReference("alice", EntityType.SPACE, wiki);
520  1 EntityReference bob = new EntityReference("bob", EntityType.SPACE, alice);
521  1 EntityReference carol = new EntityReference("carol", EntityType.DOCUMENT, bob);
522   
523  1 assertTrue(wiki.hasParent(null));
524  1 assertFalse(wiki.hasParent(wiki));
525  1 assertFalse(wiki.hasParent(alice));
526   
527  1 assertFalse(alice.hasParent(null));
528  1 assertTrue(alice.hasParent(wiki));
529  1 assertFalse(alice.hasParent(alice));
530  1 assertFalse(alice.hasParent(bob));
531  1 assertFalse(alice.hasParent(carol));
532   
533  1 assertTrue(bob.hasParent(wiki));
534  1 assertTrue(bob.hasParent(alice));
535   
536  1 assertFalse(carol.hasParent(null));
537  1 assertTrue(carol.hasParent(wiki));
538  1 assertTrue(carol.hasParent(alice));
539  1 assertTrue(carol.hasParent(bob));
540  1 assertFalse(carol.hasParent(carol));
541   
542  1 assertFalse(carol.hasParent(bob.removeParent(alice)));
543  1 assertFalse(carol.hasParent(bob.removeParent(wiki)));
544    }
545   
 
546  1 toggle @Test
547    public void entityReferenceSerialization() throws Exception
548    {
549  1 ByteArrayOutputStream baos = new ByteArrayOutputStream();
550  1 ObjectOutputStream oos = new ObjectOutputStream(baos);
551   
552  1 EntityReference reference =
553    new EntityReference("page", EntityType.DOCUMENT,
554    new EntityReference("space", EntityType.SPACE,
555    new EntityReference("wiki", EntityType.WIKI, null, getParamMap(1)), getParamMap(2)),
556    getParamMap(3));
557   
558  1 oos.writeObject(reference);
559   
560  1 ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
561  1 ObjectInputStream ois = new ObjectInputStream(bais);
562   
563  1 EntityReference outRefs = (EntityReference) ois.readObject();
564   
565  1 assertEquals(reference, outRefs);
566    }
567   
 
568  1 toggle @Test
569    public void validateToString()
570    {
571  1 EntityReference reference1 = new EntityReference("page", EntityType.DOCUMENT,
572    new EntityReference("space", EntityType.SPACE, new EntityReference("wiki", EntityType.WIKI)));
573  1 assertEquals("Document wiki:space.page", reference1.toString());
574   
575  1 EntityReference reference2 = new EntityReference("page", EntityType.DOCUMENT);
576  1 assertEquals("Document page", reference2.toString());
577   
578  1 EntityReference reference3 =
579    new EntityReference("page", EntityType.DOCUMENT, new EntityReference("space", EntityType.SPACE));
580  1 assertEquals("Document space.page", reference3.toString());
581   
582    // We don't handle parameters in Entity Reference because they're internal and not exposed and at the moment
583    // they can only be used in a restricted manner (only LOCALE) by a DocumentReference.
584  1 Map<String, Serializable> map = new HashMap<>();
585  1 map.put("key1", "value1");
586  1 EntityReference reference4 =
587    new EntityReference("page", EntityType.DOCUMENT, new EntityReference("space", EntityType.SPACE), map);
588  1 assertEquals("Document space.page", reference4.toString());
589   
590  1 EntityReference reference5 = new EntityReference("attachment", EntityType.ATTACHMENT, reference1);
591  1 assertEquals("Attachment wiki:space.page@attachment", reference5.toString());
592    }
593    }