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

File CurrentReferenceEntityReferenceResolverTest.java

 

Code metrics

0
37
6
1
195
126
6
0.16
6.17
6
1

Classes

Class Line # Actions
CurrentReferenceEntityReferenceResolverTest 51 37 0% 6 0
1.0100%
 

Contributing tests

This file is covered by 5 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 javax.inject.Named;
25   
26    import org.junit.jupiter.api.BeforeEach;
27    import org.junit.jupiter.api.Test;
28    import org.xwiki.model.EntityType;
29    import org.xwiki.model.ModelConfiguration;
30    import org.xwiki.model.internal.reference.DefaultEntityReferenceProvider;
31    import org.xwiki.model.reference.DocumentReference;
32    import org.xwiki.model.reference.EntityReference;
33    import org.xwiki.model.reference.EntityReferenceProvider;
34    import org.xwiki.model.reference.PageReference;
35    import org.xwiki.model.reference.SpaceReference;
36    import org.xwiki.test.annotation.ComponentList;
37    import org.xwiki.test.junit5.mockito.ComponentTest;
38    import org.xwiki.test.junit5.mockito.InjectMockComponents;
39    import org.xwiki.test.junit5.mockito.MockComponent;
40   
41    import static org.junit.jupiter.api.Assertions.assertEquals;
42    import static org.mockito.Mockito.when;
43   
44    /**
45    * Unit tests for {@link CurrentReferenceEntityReferenceResolver}.
46    *
47    * @version $Id: d7f936c31c696e2b5261dff665a28aacc64d6765 $
48    */
49    @ComponentTest
50    @ComponentList(DefaultEntityReferenceProvider.class)
 
51    public class CurrentReferenceEntityReferenceResolverTest
52    {
53    private static final String CURRENT_WIKI = "currentwiki";
54   
55    private static final String CURRENT_SPACE = "currentspace";
56   
57    private static final String CURRENT_DOCUMENT = "currentdocument";
58   
59    private static final String CURRENT_PAGE = "currentpage";
60   
61    private static final String DEFAULT_WIKI = "defwiki";
62   
63    private static final String DEFAULT_SPACE = "defspace";
64   
65    private static final String DEFAULT_DOCUMENT = "defdocument";
66   
67    private static final String DEFAULT_PAGE = "defpage";
68   
69    private static final String DEFAULT_ATTACHMENT = "deffilename";
70   
71    private static final String DEFAULT_OBJECT = "defobject";
72   
73    private static final String DEFAULT_OBJECT_PROPERTY = "defobjproperty";
74   
75    private static final String DEFAULT_CLASS_PROPERTY = "defclassproperty";
76   
77    private static final String DEFAULT_PAGE_ATTACHMENT = "defpagefilename";
78   
79    private static final String DEFAULT_PAGE_OBJECT = "defpageobject";
80   
81    private static final String DEFAULT_PAGE_OBJECT_PROPERTY = "defpageobjproperty";
82   
83    private static final String DEFAULT_PAGE_CLASS_PROPERTY = "defpageclassproperty";
84   
85    private static final EntityReference CURRENT_WIKI_REFERENCE = new EntityReference(CURRENT_WIKI, EntityType.WIKI);
86   
87    private static final EntityReference CURRENT_SPACE_REFERENCE = new EntityReference(CURRENT_SPACE, EntityType.SPACE);
88   
89    private static final EntityReference CURRENT_DOCUMENT_REFERENCE =
90    new EntityReference(CURRENT_DOCUMENT, EntityType.DOCUMENT);
91   
92    private static final EntityReference CURRENT_PAGE_REFERENCE = new EntityReference(CURRENT_PAGE, EntityType.PAGE);
93   
94    @MockComponent
95    @Named("current")
96    private EntityReferenceProvider currentProvider;
97   
98    @MockComponent
99    private ModelConfiguration configuration;
100   
101    @InjectMockComponents
102    private CurrentReferenceEntityReferenceResolver resolver;
103   
 
104  5 toggle @BeforeEach
105    public void beforeEach() throws Exception
106    {
107  5 when(this.currentProvider.getDefaultReference(EntityType.WIKI)).thenReturn(CURRENT_WIKI_REFERENCE);
108  5 when(this.currentProvider.getDefaultReference(EntityType.SPACE)).thenReturn(CURRENT_SPACE_REFERENCE);
109  5 when(this.currentProvider.getDefaultReference(EntityType.DOCUMENT)).thenReturn(CURRENT_DOCUMENT_REFERENCE);
110  5 when(this.currentProvider.getDefaultReference(EntityType.PAGE)).thenReturn(CURRENT_PAGE_REFERENCE);
111   
112  5 when(this.configuration.getDefaultReferenceValue(EntityType.WIKI)).thenReturn(DEFAULT_WIKI);
113  5 when(this.configuration.getDefaultReferenceValue(EntityType.SPACE)).thenReturn(DEFAULT_SPACE);
114  5 when(this.configuration.getDefaultReferenceValue(EntityType.DOCUMENT)).thenReturn(DEFAULT_DOCUMENT);
115  5 when(this.configuration.getDefaultReferenceValue(EntityType.OBJECT)).thenReturn(DEFAULT_OBJECT);
116  5 when(this.configuration.getDefaultReferenceValue(EntityType.OBJECT_PROPERTY))
117    .thenReturn(DEFAULT_OBJECT_PROPERTY);
118  5 when(this.configuration.getDefaultReferenceValue(EntityType.CLASS_PROPERTY)).thenReturn(DEFAULT_CLASS_PROPERTY);
119  5 when(this.configuration.getDefaultReferenceValue(EntityType.ATTACHMENT)).thenReturn(DEFAULT_ATTACHMENT);
120  5 when(this.configuration.getDefaultReferenceValue(EntityType.PAGE)).thenReturn(DEFAULT_PAGE);
121  5 when(this.configuration.getDefaultReferenceValue(EntityType.PAGE_OBJECT)).thenReturn(DEFAULT_PAGE_OBJECT);
122  5 when(this.configuration.getDefaultReferenceValue(EntityType.PAGE_OBJECT_PROPERTY))
123    .thenReturn(DEFAULT_PAGE_OBJECT_PROPERTY);
124  5 when(this.configuration.getDefaultReferenceValue(EntityType.PAGE_ATTACHMENT))
125    .thenReturn(DEFAULT_PAGE_ATTACHMENT);
126  5 when(this.configuration.getDefaultReferenceValue(EntityType.PAGE_CLASS_PROPERTY))
127    .thenReturn(DEFAULT_PAGE_CLASS_PROPERTY);
128    }
129   
 
130  1 toggle @Test
131    public void testResolveAttachmentReferenceWhenMissingParents()
132    {
133  1 EntityReference reference =
134    resolver.resolve(new EntityReference("filename", EntityType.ATTACHMENT), EntityType.ATTACHMENT);
135   
136  1 assertEquals(CURRENT_DOCUMENT, reference.getParent().getName());
137  1 assertEquals(EntityType.DOCUMENT, reference.getParent().getType());
138  1 assertEquals(CURRENT_SPACE, 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 resolvePageReferenceWhenTypeIsDocument()
146    {
147  1 EntityReference reference =
148    this.resolver.resolve(new EntityReference("document", EntityType.DOCUMENT), EntityType.PAGE);
149   
150  1 assertEquals(new PageReference(CURRENT_WIKI, CURRENT_SPACE, "document"), reference);
151   
152  1 reference = this.resolver.resolve(new EntityReference(DEFAULT_DOCUMENT, EntityType.DOCUMENT), EntityType.PAGE);
153   
154  1 assertEquals(new PageReference(CURRENT_WIKI, CURRENT_SPACE), reference);
155    }
156   
 
157  1 toggle @Test
158    public void resolvePageReferenceWhenTypeIsSpace()
159    {
160  1 EntityReference reference =
161    this.resolver.resolve(new EntityReference("space", EntityType.SPACE), EntityType.PAGE);
162   
163  1 assertEquals(new PageReference(CURRENT_WIKI, "space"), reference);
164    }
165   
 
166  1 toggle @Test
167    public void resolveDocumentReferenceWhenTypeIsPage()
168    {
169  1 EntityReference reference =
170    this.resolver.resolve(new EntityReference("page", EntityType.PAGE), EntityType.DOCUMENT);
171   
172  1 assertEquals(new DocumentReference(CURRENT_WIKI, "page", DEFAULT_DOCUMENT), reference);
173   
174  1 reference = this.resolver.resolve(
175    new EntityReference("page1", EntityType.PAGE, new EntityReference("page2", EntityType.PAGE)),
176    EntityType.DOCUMENT);
177   
178  1 assertEquals(new DocumentReference(CURRENT_WIKI, Arrays.asList("page2", "page1"), DEFAULT_DOCUMENT), reference);
179    }
180   
 
181  1 toggle @Test
182    public void resolveSpaceReferenceWhenTypeIsPage()
183    {
184  1 EntityReference reference =
185    this.resolver.resolve(new EntityReference("page", EntityType.PAGE), EntityType.SPACE);
186   
187  1 assertEquals(new SpaceReference(CURRENT_WIKI, Arrays.asList("page")), reference);
188   
189  1 reference = this.resolver.resolve(
190    new EntityReference("page1", EntityType.PAGE, new EntityReference("page2", EntityType.PAGE)),
191    EntityType.SPACE);
192   
193  1 assertEquals(new SpaceReference(CURRENT_WIKI, Arrays.asList("page2", "page1")), reference);
194    }
195    }