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

File CurrentStringEntityReferenceResolverTest.java

 

Code metrics

0
53
8
1
186
126
8
0.15
6.62
8
1

Classes

Class Line # Actions
CurrentStringEntityReferenceResolverTest 53 53 0% 8 0
1.0100%
 

Contributing tests

This file is covered by 7 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 com.xpn.xwiki.internal.model.reference;
21   
22    import org.apache.commons.lang3.reflect.FieldUtils;
23    import org.junit.jupiter.api.BeforeEach;
24    import org.junit.jupiter.api.Test;
25    import org.xwiki.model.EntityType;
26    import org.xwiki.model.internal.DefaultModelConfiguration;
27    import org.xwiki.model.internal.reference.DefaultSymbolScheme;
28    import org.xwiki.model.internal.reference.ExplicitReferenceEntityReferenceResolver;
29    import org.xwiki.model.internal.reference.ExplicitReferencePageReferenceResolver;
30    import org.xwiki.model.reference.DocumentReference;
31    import org.xwiki.model.reference.EntityReference;
32    import org.xwiki.model.reference.PageReference;
33    import org.xwiki.model.reference.WikiReference;
34    import org.xwiki.test.annotation.ComponentList;
35    import org.xwiki.test.junit5.mockito.InjectMockComponents;
36   
37    import com.xpn.xwiki.doc.XWikiDocument;
38    import com.xpn.xwiki.test.MockitoOldcore;
39    import com.xpn.xwiki.test.junit5.mockito.InjectMockitoOldcore;
40    import com.xpn.xwiki.test.junit5.mockito.OldcoreTest;
41   
42    import static org.junit.jupiter.api.Assertions.assertEquals;
43   
44    /**
45    * Unit tests for {@link CurrentStringEntityReferenceResolver}.
46    *
47    * @version $Id: 96418597d154cf3104c7e7bbf0c0f4eb5ff6c5ac $
48    */
49    @OldcoreTest
50    @ComponentList(value = { CurrentEntityReferenceProvider.class, CurrentStringEntityReferenceResolver.class,
51    DefaultModelConfiguration.class, DefaultSymbolScheme.class, ExplicitReferencePageReferenceResolver.class,
52    ExplicitReferenceEntityReferenceResolver.class })
 
53    public class CurrentStringEntityReferenceResolverTest
54    {
55    @InjectMockitoOldcore
56    private MockitoOldcore oldcore;
57   
58    private static final String CURRENT_WIKI = "currentwiki";
59   
60    private static final String CURRENT_SPACE = "currentspace";
61   
62    private static final String CURRENT_DOCUMENT = "currentdocument";
63   
64    private static final String CURRENT_PAGE = "currentpage";
65   
66    private static final String CURRENTDOC_SPACE = "currentdocspace";
67   
68    private static final String CURRENTDOC_DOCUMENT = "currentdocdocument";
69   
70    @InjectMockComponents
71    private CurrentStringEntityReferenceResolver resolver;
72   
 
73  7 toggle @BeforeEach
74    public void beforeEach() throws Exception
75    {
76  7 this.oldcore.getXWikiContext().setWikiId(CURRENT_WIKI);
77    }
78   
 
79  1 toggle @Test
80    public void testResolveDocumentReferenceWhenNoContextWiki() throws Exception
81    {
82  1 this.oldcore.getXWikiContext().setWikiId(null);
83   
84  1 EntityReference reference = resolver.resolve("", EntityType.DOCUMENT);
85   
86  1 assertEquals("xwiki", reference.extractReference(EntityType.WIKI).getName());
87  1 assertEquals("Main", reference.extractReference(EntityType.SPACE).getName());
88  1 assertEquals("WebHome", reference.getName());
89    }
90   
 
91  1 toggle @Test
92    public void testResolveDocumentReferenceWhenNoContextDocument() throws Exception
93    {
94  1 this.oldcore.getXWikiContext().setWikiId(null);
95  1 this.oldcore.getXWikiContext().setDoc(null);
96   
97  1 EntityReference reference = resolver.resolve("", EntityType.DOCUMENT);
98   
99  1 assertEquals("xwiki", reference.extractReference(EntityType.WIKI).getName());
100  1 assertEquals("Main", reference.extractReference(EntityType.SPACE).getName());
101  1 assertEquals("WebHome", reference.getName());
102    }
103   
 
104  1 toggle @Test
105    public void testResolveDocumentReferenceWhenContextDocument() throws Exception
106    {
107  1 this.oldcore.getXWikiContext()
108    .setDoc(new XWikiDocument(new DocumentReference(CURRENT_WIKI, CURRENTDOC_SPACE, CURRENTDOC_DOCUMENT)));
109   
110  1 EntityReference reference = resolver.resolve("", EntityType.DOCUMENT);
111   
112  1 assertEquals(CURRENT_WIKI, reference.extractReference(EntityType.WIKI).getName());
113  1 assertEquals(CURRENTDOC_SPACE, reference.extractReference(EntityType.SPACE).getName());
114  1 assertEquals(CURRENTDOC_DOCUMENT, reference.getName());
115    }
116   
 
117  1 toggle @Test
118    public void testResolveAttachmentReference() throws Exception
119    {
120  1 this.oldcore.getXWikiContext()
121    .setDoc(new XWikiDocument(new DocumentReference(CURRENT_WIKI, CURRENTDOC_SPACE, CURRENTDOC_DOCUMENT)));
122   
123  1 EntityReference reference = resolver.resolve("", EntityType.ATTACHMENT);
124   
125  1 assertEquals(CURRENT_WIKI, reference.extractReference(EntityType.WIKI).getName());
126  1 assertEquals(CURRENTDOC_SPACE, reference.extractReference(EntityType.SPACE).getName());
127  1 assertEquals(CURRENTDOC_DOCUMENT, reference.extractReference(EntityType.DOCUMENT).getName());
128  1 assertEquals("filename", reference.getName());
129    }
130   
 
131  1 toggle @Test
132    public void testResolveAttachmentReferenceWhenMissingParentsAndNoContextDocument()
133    {
134  1 EntityReference reference = resolver.resolve("filename", EntityType.ATTACHMENT);
135   
136  1 assertEquals("WebHome", reference.getParent().getName());
137  1 assertEquals(EntityType.DOCUMENT, reference.getParent().getType());
138  1 assertEquals("Main", reference.getParent().getParent().getName());
139  1 assertEquals(EntityType.SPACE, reference.getParent().getParent().getType());
140  1 assertEquals(CURRENT_WIKI, reference.getParent().getParent().getParent().getName());
141  1 assertEquals(EntityType.WIKI, reference.getParent().getParent().getParent().getType());
142    }
143   
 
144  1 toggle @Test
145    public void testResolveAttachmentReferenceWhenMissingParentsAndContextDocument()
146    {
147  1 this.oldcore.getXWikiContext()
148    .setDoc(new XWikiDocument(new DocumentReference("docwiki", CURRENT_SPACE, CURRENT_DOCUMENT)));
149   
150  1 EntityReference reference = resolver.resolve("filename", EntityType.ATTACHMENT);
151   
152  1 assertEquals(CURRENT_DOCUMENT, reference.getParent().getName());
153  1 assertEquals(EntityType.DOCUMENT, reference.getParent().getType());
154  1 assertEquals(CURRENT_SPACE, reference.getParent().getParent().getName());
155  1 assertEquals(EntityType.SPACE, reference.getParent().getParent().getType());
156  1 assertEquals(CURRENT_WIKI, reference.getParent().getParent().getParent().getName());
157  1 assertEquals(EntityType.WIKI, reference.getParent().getParent().getParent().getType());
158    }
159   
 
160  1 toggle @Test
161    public void testResolvePageReferenceKeywords() throws Exception
162    {
163  1 XWikiDocument document = new XWikiDocument(new DocumentReference("docwiki", CURRENT_SPACE, CURRENT_PAGE));
164  1 FieldUtils.writeDeclaredField(document, "pageReferenceCache",
165    new PageReference("docwiki", CURRENT_SPACE, CURRENT_PAGE), true);
166  1 this.oldcore.getXWikiContext().setDoc(document);
167   
168  1 EntityReference reference = this.resolver.resolve(".", EntityType.PAGE);
169  1 assertEquals(new PageReference(CURRENT_WIKI, CURRENT_SPACE, CURRENT_PAGE), reference);
170   
171  1 reference = this.resolver.resolve("page/.", EntityType.PAGE);
172  1 assertEquals(new PageReference(CURRENT_WIKI, "page"), reference);
173   
174  1 reference = this.resolver.resolve("page/./.", EntityType.PAGE);
175  1 assertEquals(new PageReference(CURRENT_WIKI, "page"), reference);
176   
177  1 reference = this.resolver.resolve("..", EntityType.PAGE);
178  1 assertEquals(new PageReference(CURRENT_WIKI, CURRENT_SPACE), reference);
179   
180  1 reference = this.resolver.resolve("page/..", EntityType.PAGE);
181  1 assertEquals(new WikiReference(CURRENT_WIKI), reference);
182   
183  1 reference = this.resolver.resolve("page1/page2/..", EntityType.PAGE);
184  1 assertEquals(new PageReference(CURRENT_WIKI, "page1"), reference);
185    }
186    }