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

File CurrentEntityReferenceProviderTest.java

 

Code metrics

0
41
3
1
200
135
3
0.07
13.67
3
1

Classes

Class Line # Actions
CurrentEntityReferenceProviderTest 52 41 0% 3 0
1.0100%
 

Contributing tests

This file is covered by 2 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 java.util.Arrays;
23   
24    import org.apache.commons.lang3.reflect.FieldUtils;
25    import org.junit.jupiter.api.BeforeEach;
26    import org.junit.jupiter.api.Test;
27    import org.xwiki.model.EntityType;
28    import org.xwiki.model.ModelConfiguration;
29    import org.xwiki.model.internal.reference.DefaultEntityReferenceProvider;
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.test.annotation.ComponentList;
34    import org.xwiki.test.junit5.mockito.InjectMockComponents;
35    import org.xwiki.test.junit5.mockito.MockComponent;
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    import static org.mockito.Mockito.when;
44   
45    /**
46    * Unit tests for {@link CurrentEntityReferenceProvider}.
47    *
48    * @version $Id: 8c484eaee77455b343584a130305c49ee05df205 $
49    */
50    @OldcoreTest
51    @ComponentList(DefaultEntityReferenceProvider.class)
 
52    public class CurrentEntityReferenceProviderTest
53    {
54    private static final String CONTEXT_WIKI = "contextwiki";
55   
56    private static final String DEFAULT_WIKI = "defwiki";
57   
58    private static final String DEFAULT_SPACE = "defspace";
59   
60    private static final String DEFAULT_DOCUMENT = "defdocument";
61   
62    private static final String DEFAULT_PAGE = "defpage";
63   
64    private static final String DEFAULT_ATTACHMENT = "deffilename";
65   
66    private static final String DEFAULT_OBJECT = "defobject";
67   
68    private static final String DEFAULT_OBJECT_PROPERTY = "defobjproperty";
69   
70    private static final String DEFAULT_CLASS_PROPERTY = "defclassproperty";
71   
72    private static final String DEFAULT_PAGE_ATTACHMENT = "defpagefilename";
73   
74    private static final String DEFAULT_PAGE_OBJECT = "defpageobject";
75   
76    private static final String DEFAULT_PAGE_OBJECT_PROPERTY = "defpageobjproperty";
77   
78    private static final String DEFAULT_PAGE_CLASS_PROPERTY = "defpageclassproperty";
79   
80    private static final EntityReference DEFAULT_WIKI_REFERENCE = new EntityReference(DEFAULT_WIKI, EntityType.WIKI);
81   
82    private static final EntityReference DEFAULT_SPACE_REFERENCE = new EntityReference(DEFAULT_SPACE, EntityType.SPACE);
83   
84    private static final EntityReference DEFAULT_DOCUMENT_REFERENCE =
85    new EntityReference(DEFAULT_DOCUMENT, EntityType.DOCUMENT);
86   
87    private static final EntityReference DEFAULT_ATTACHMENT_REFERENCE =
88    new EntityReference(DEFAULT_ATTACHMENT, EntityType.ATTACHMENT);
89   
90    private static final EntityReference DEFAULT_OBJECT_REFERENCE =
91    new EntityReference(DEFAULT_OBJECT, EntityType.OBJECT);
92   
93    private static final EntityReference DEFAULT_OBJECT_PROPERTY_REFERENCE =
94    new EntityReference(DEFAULT_OBJECT_PROPERTY, EntityType.OBJECT_PROPERTY);
95   
96    private static final EntityReference DEFAULT_CLASS_PROPERTY_REFERENCE =
97    new EntityReference(DEFAULT_CLASS_PROPERTY, EntityType.CLASS_PROPERTY);
98   
99    private static final EntityReference DEFAULT_PAGE_REFERENCE = new EntityReference(DEFAULT_PAGE, EntityType.PAGE);
100   
101    private static final EntityReference DEFAULT_PAGE_ATTACHMENT_REFERENCE =
102    new EntityReference(DEFAULT_PAGE_ATTACHMENT, EntityType.PAGE_ATTACHMENT);
103   
104    private static final EntityReference DEFAULT_PAGE_OBJECT_REFERENCE =
105    new EntityReference(DEFAULT_PAGE_OBJECT, EntityType.PAGE_OBJECT);
106   
107    private static final EntityReference DEFAULT_PAGE_OBJECT_PROPERTY_REFERENCE =
108    new EntityReference(DEFAULT_PAGE_OBJECT_PROPERTY, EntityType.PAGE_OBJECT_PROPERTY);
109   
110    private static final EntityReference DEFAULT_PAGE_CLASS_PROPERTY_REFERENCE =
111    new EntityReference(DEFAULT_PAGE_CLASS_PROPERTY, EntityType.PAGE_CLASS_PROPERTY);
112   
113    @MockComponent
114    private ModelConfiguration configuration;
115   
116    @InjectMockitoOldcore
117    private MockitoOldcore mockitoOldcore;
118   
119    @InjectMockComponents
120    private CurrentEntityReferenceProvider provider;
121   
 
122  2 toggle @BeforeEach
123    public void beforeEach()
124    {
125  2 this.mockitoOldcore.getXWikiContext().setWikiId(null);
126   
127  2 when(this.configuration.getDefaultReferenceValue(EntityType.WIKI)).thenReturn(DEFAULT_WIKI);
128  2 when(this.configuration.getDefaultReferenceValue(EntityType.SPACE)).thenReturn(DEFAULT_SPACE);
129  2 when(this.configuration.getDefaultReferenceValue(EntityType.DOCUMENT)).thenReturn(DEFAULT_DOCUMENT);
130  2 when(this.configuration.getDefaultReferenceValue(EntityType.OBJECT)).thenReturn(DEFAULT_OBJECT);
131  2 when(this.configuration.getDefaultReferenceValue(EntityType.OBJECT_PROPERTY))
132    .thenReturn(DEFAULT_OBJECT_PROPERTY);
133  2 when(this.configuration.getDefaultReferenceValue(EntityType.CLASS_PROPERTY)).thenReturn(DEFAULT_CLASS_PROPERTY);
134  2 when(this.configuration.getDefaultReferenceValue(EntityType.ATTACHMENT)).thenReturn(DEFAULT_ATTACHMENT);
135  2 when(this.configuration.getDefaultReferenceValue(EntityType.PAGE)).thenReturn(DEFAULT_PAGE);
136  2 when(this.configuration.getDefaultReferenceValue(EntityType.PAGE_OBJECT)).thenReturn(DEFAULT_PAGE_OBJECT);
137  2 when(this.configuration.getDefaultReferenceValue(EntityType.PAGE_OBJECT_PROPERTY))
138    .thenReturn(DEFAULT_PAGE_OBJECT_PROPERTY);
139  2 when(this.configuration.getDefaultReferenceValue(EntityType.PAGE_ATTACHMENT))
140    .thenReturn(DEFAULT_PAGE_ATTACHMENT);
141  2 when(this.configuration.getDefaultReferenceValue(EntityType.PAGE_CLASS_PROPERTY))
142    .thenReturn(DEFAULT_PAGE_CLASS_PROPERTY);
143    }
144   
 
145  1 toggle @Test
146    public void getDefaultReferenceWithoutContextDocument()
147    {
148  1 assertEquals(DEFAULT_WIKI_REFERENCE, this.provider.getDefaultReference(EntityType.WIKI));
149   
150  1 assertEquals(DEFAULT_SPACE_REFERENCE, this.provider.getDefaultReference(EntityType.SPACE));
151  1 assertEquals(DEFAULT_DOCUMENT_REFERENCE, this.provider.getDefaultReference(EntityType.DOCUMENT));
152  1 assertEquals(DEFAULT_ATTACHMENT_REFERENCE, this.provider.getDefaultReference(EntityType.ATTACHMENT));
153  1 assertEquals(DEFAULT_OBJECT_REFERENCE, this.provider.getDefaultReference(EntityType.OBJECT));
154  1 assertEquals(DEFAULT_OBJECT_PROPERTY_REFERENCE, this.provider.getDefaultReference(EntityType.OBJECT_PROPERTY));
155  1 assertEquals(DEFAULT_CLASS_PROPERTY_REFERENCE, this.provider.getDefaultReference(EntityType.CLASS_PROPERTY));
156  1 assertEquals(DEFAULT_PAGE_REFERENCE, this.provider.getDefaultReference(EntityType.PAGE));
157  1 assertEquals(DEFAULT_PAGE_ATTACHMENT_REFERENCE, this.provider.getDefaultReference(EntityType.PAGE_ATTACHMENT));
158  1 assertEquals(DEFAULT_PAGE_OBJECT_REFERENCE, this.provider.getDefaultReference(EntityType.PAGE_OBJECT));
159  1 assertEquals(DEFAULT_PAGE_OBJECT_PROPERTY_REFERENCE,
160    this.provider.getDefaultReference(EntityType.PAGE_OBJECT_PROPERTY));
161  1 assertEquals(DEFAULT_PAGE_CLASS_PROPERTY_REFERENCE,
162    this.provider.getDefaultReference(EntityType.PAGE_CLASS_PROPERTY));
163    }
164   
 
165  1 toggle @Test
166    public void getDefaultReferenceWithContextDocument() throws IllegalAccessException
167    {
168  1 this.mockitoOldcore.getXWikiContext().setWikiId(CONTEXT_WIKI);
169   
170  1 XWikiDocument document =
171    new XWikiDocument(new DocumentReference("docwiki", Arrays.asList("docspace1", "docspace2"), "docname"));
172  1 FieldUtils.writeDeclaredField(document, "pageReferenceCache",
173    new PageReference("docwiki", "docspace1", "docspace2", "docname"), true);
174  1 this.mockitoOldcore.getXWikiContext().setDoc(document);
175   
176  1 assertEquals(new EntityReference(CONTEXT_WIKI, EntityType.WIKI),
177    this.provider.getDefaultReference(EntityType.WIKI));
178   
179  1 assertEquals(
180    new EntityReference("docspace2", EntityType.SPACE, new EntityReference("docspace1", EntityType.SPACE)),
181    this.provider.getDefaultReference(EntityType.SPACE));
182  1 assertEquals(new EntityReference("docname", EntityType.DOCUMENT),
183    this.provider.getDefaultReference(EntityType.DOCUMENT));
184  1 assertEquals(
185    new EntityReference("docname", EntityType.PAGE,
186    new EntityReference("docspace2", EntityType.PAGE, new EntityReference("docspace1", EntityType.PAGE))),
187    this.provider.getDefaultReference(EntityType.PAGE));
188   
189  1 assertEquals(DEFAULT_ATTACHMENT_REFERENCE, this.provider.getDefaultReference(EntityType.ATTACHMENT));
190  1 assertEquals(DEFAULT_OBJECT_REFERENCE, this.provider.getDefaultReference(EntityType.OBJECT));
191  1 assertEquals(DEFAULT_OBJECT_PROPERTY_REFERENCE, this.provider.getDefaultReference(EntityType.OBJECT_PROPERTY));
192  1 assertEquals(DEFAULT_CLASS_PROPERTY_REFERENCE, this.provider.getDefaultReference(EntityType.CLASS_PROPERTY));
193  1 assertEquals(DEFAULT_PAGE_ATTACHMENT_REFERENCE, this.provider.getDefaultReference(EntityType.PAGE_ATTACHMENT));
194  1 assertEquals(DEFAULT_PAGE_OBJECT_REFERENCE, this.provider.getDefaultReference(EntityType.PAGE_OBJECT));
195  1 assertEquals(DEFAULT_PAGE_OBJECT_PROPERTY_REFERENCE,
196    this.provider.getDefaultReference(EntityType.PAGE_OBJECT_PROPERTY));
197  1 assertEquals(DEFAULT_PAGE_CLASS_PROPERTY_REFERENCE,
198    this.provider.getDefaultReference(EntityType.PAGE_CLASS_PROPERTY));
199    }
200    }