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

File DocumentReferenceTest.java

 

Code metrics

0
36
11
1
147
100
11
0.31
3.27
11
1

Classes

Class Line # Actions
DocumentReferenceTest 39 36 0% 11 0
1.0100%
 

Contributing tests

This file is covered by 11 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.util.Arrays;
23    import java.util.List;
24    import java.util.Locale;
25   
26    import org.junit.jupiter.api.Test;
27    import org.xwiki.model.EntityType;
28   
29    import static org.junit.jupiter.api.Assertions.assertEquals;
30    import static org.junit.jupiter.api.Assertions.assertSame;
31    import static org.junit.jupiter.api.Assertions.assertThrows;
32   
33    /**
34    * Unit tests for {@link org.xwiki.model.reference.DocumentReference}.
35    *
36    * @version $Id: b2564906a53371726ecb2414cea49168162d8f35 $
37    * @since 2.2M1
38    */
 
39    public class DocumentReferenceTest
40    {
 
41  1 toggle @Test
42    public void testConstructors()
43    {
44  1 DocumentReference reference = new DocumentReference("wiki", "space", "page");
45  1 assertEquals(reference, new DocumentReference(new EntityReference("page", EntityType.DOCUMENT,
46    new EntityReference("space", EntityType.SPACE, new EntityReference("wiki", EntityType.WIKI)))));
47  1 assertEquals(reference, new DocumentReference("wiki", Arrays.asList("space"), "page"));
48  1 assertEquals(reference, new DocumentReference("page", new SpaceReference("space", new WikiReference("wiki"))));
49    }
50   
 
51  1 toggle @Test
52    public void testInvalidType()
53    {
54  1 IllegalArgumentException e = assertThrows(IllegalArgumentException.class,
55    () -> new DocumentReference(new EntityReference("page", EntityType.SPACE)));
56   
57  1 assertEquals("Invalid type [SPACE] for a document reference", e.getMessage());
58    }
59   
 
60  1 toggle @Test
61    public void testInvalidNullParent()
62    {
63  1 IllegalArgumentException e =
64    assertThrows(IllegalArgumentException.class, () -> new DocumentReference("page", null));
65   
66  1 assertEquals("Invalid parent reference [null] in a document reference", e.getMessage());
67    }
68   
 
69  1 toggle @Test
70    public void testInvalidParentType()
71    {
72  1 IllegalArgumentException e = assertThrows(IllegalArgumentException.class,
73    () -> new DocumentReference(new EntityReference("page", EntityType.DOCUMENT, new WikiReference("wiki"))));
74   
75  1 assertEquals("Invalid parent reference [Wiki wiki] in a document reference", e.getMessage());
76    }
77   
 
78  1 toggle @Test
79    public void testGetWikiReference()
80    {
81  1 DocumentReference reference = new DocumentReference("wiki", "space", "page");
82  1 assertEquals(new WikiReference("wiki"), reference.getWikiReference());
83    }
84   
 
85  1 toggle @Test
86    public void testGetLastSpaceReferenceWhenOneSpace()
87    {
88  1 DocumentReference documentReference = new DocumentReference("wiki", "space", "page");
89  1 SpaceReference spaceReference = documentReference.getLastSpaceReference();
90  1 assertEquals(new SpaceReference("space", new WikiReference("wiki")), spaceReference);
91    }
92   
 
93  1 toggle @Test
94    public void testGetLastSpaceReferenceWhenMultipleSpaces()
95    {
96  1 DocumentReference reference = new DocumentReference("wiki", Arrays.asList("space1", "space2"), "page");
97  1 assertEquals(new SpaceReference("space2", new SpaceReference("space1", new WikiReference("wiki"))),
98    reference.getLastSpaceReference());
99    }
100   
 
101  1 toggle @Test
102    public void testSpaceReferences()
103    {
104  1 DocumentReference reference1 = new DocumentReference("wiki", "space", "page");
105  1 List<SpaceReference> spaceRefs = reference1.getSpaceReferences();
106  1 assertEquals(1, spaceRefs.size());
107  1 assertEquals(new SpaceReference("space", new WikiReference("wiki")), spaceRefs.get(0));
108   
109  1 DocumentReference reference2 = new DocumentReference("wiki", Arrays.asList("space1", "space2"), "page");
110  1 List<SpaceReference> spaceRefs2 = reference2.getSpaceReferences();
111  1 assertEquals(2, spaceRefs2.size());
112  1 assertEquals(new SpaceReference("space1", new WikiReference("wiki")), spaceRefs2.get(0));
113  1 assertEquals(new SpaceReference("space2", new SpaceReference("space1", new WikiReference("wiki"))),
114    spaceRefs2.get(1));
115    }
116   
 
117  1 toggle @Test
118    public void testToString()
119    {
120  1 DocumentReference reference1 = new DocumentReference("wiki", "space", "page");
121  1 assertEquals("wiki:space.page", reference1.toString());
122   
123  1 DocumentReference reference2 = new DocumentReference("wiki", "space", "page", Locale.FRANCE);
124  1 assertEquals("wiki:space.page(fr_FR)", reference2.toString());
125   
126  1 DocumentReference reference3 = new DocumentReference("wiki", "space", "page", "en");
127  1 assertEquals("wiki:space.page(en)", reference3.toString());
128    }
129   
 
130  1 toggle @Test
131    public void testCreateDocumentReferenceFromLocalDocumentReference()
132    {
133  1 assertEquals("wiki:space.page",
134    new DocumentReference(new LocalDocumentReference("space", "page"), new WikiReference("wiki")).toString());
135    }
136   
 
137  1 toggle @Test
138    public void testReplaceParent()
139    {
140  1 DocumentReference reference = new DocumentReference("wiki", "space", "page").replaceParent(
141    new EntityReference("space2", EntityType.SPACE, new EntityReference("wiki2", EntityType.WIKI)));
142   
143  1 assertEquals(new DocumentReference("wiki2", "space2", "page"), reference);
144   
145  1 assertSame(reference, reference.replaceParent(reference.getParent()));
146    }
147    }