1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.url.internal.standard

File PathWikiReferenceExtractorTest.java

 

Code metrics

0
32
10
1
141
91
10
0.31
3.2
10
1

Classes

Class Line # Actions
PathWikiReferenceExtractorTest 44 32 0% 10 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 org.xwiki.url.internal.standard;
21   
22    import java.net.URL;
23   
24    import org.junit.Before;
25    import org.junit.Rule;
26    import org.junit.Test;
27    import org.xwiki.context.Execution;
28    import org.xwiki.context.ExecutionContext;
29    import org.xwiki.model.reference.WikiReference;
30    import org.xwiki.test.mockito.MockitoComponentMockingRule;
31    import org.xwiki.url.ExtendedURL;
32    import org.xwiki.wiki.descriptor.WikiDescriptor;
33    import org.xwiki.wiki.descriptor.WikiDescriptorManager;
34   
35    import static org.junit.Assert.assertEquals;
36    import static org.mockito.Mockito.*;
37   
38    /**
39    * Unit tests for {@link PathWikiReferenceExtractor}.
40    *
41    * @version $Id: 1f6d6408ab425a3d80945430b6175723b45c99cb $
42    * @since 6.3M1
43    */
 
44    public class PathWikiReferenceExtractorTest
45    {
46    @Rule
47    public MockitoComponentMockingRule<PathWikiReferenceExtractor> mocker =
48    new MockitoComponentMockingRule<>(PathWikiReferenceExtractor.class);
49   
50    private WikiDescriptorManager wikiDescriptorManager;
51   
 
52  7 toggle @Before
53    public void setUp() throws Exception
54    {
55  7 this.wikiDescriptorManager = mocker.getInstance(WikiDescriptorManager.class);
56  7 when(wikiDescriptorManager.getMainWikiId()).thenReturn("xwiki");
57    }
58   
 
59  1 toggle @Test
60    public void extractWhenWikiDescriptor() throws Exception
61    {
62  1 setUpConfiguration(WikiNotFoundBehavior.REDIRECT_TO_MAIN_WIKI);
63   
64  1 WikiDescriptorManager wikiDescriptorManager = mocker.getInstance(WikiDescriptorManager.class);
65  1 when(wikiDescriptorManager.getByAlias("someWiki")).thenReturn(new WikiDescriptor("wikiid", "someWiki"));
66   
67  1 testAndAssert("http://localhost/xwiki/wiki/someWiki/view/Main/WebHome", "wikiid");
68    }
69   
 
70  1 toggle @Test
71    public void extractWhenNoWikiDescriptor() throws Exception
72    {
73  1 setUpConfiguration(WikiNotFoundBehavior.REDIRECT_TO_MAIN_WIKI);
74  1 testAndAssert("http://localhost/xwiki/wiki/someWiki/view/Main/WebHome", "xwiki");
75    }
76   
 
77  1 toggle @Test
78    public void extractWhenWikiDescriptorButEmptyServerName() throws Exception
79    {
80  1 setUpConfiguration(WikiNotFoundBehavior.REDIRECT_TO_MAIN_WIKI);
81   
82  1 WikiDescriptorManager wikiDescriptorManager = mocker.getInstance(WikiDescriptorManager.class);
83  1 when(wikiDescriptorManager.getByAlias("someWiki")).thenReturn(new WikiDescriptor("", "someWiki"));
84   
85  1 testAndAssert("http://localhost/xwiki/wiki/someWiki/view/Main/WebHome", "xwiki");
86    }
87   
 
88  1 toggle @Test
89    public void extractWhenNoDescriptorMatchingAliasButDescriptorMatchingId() throws Exception
90    {
91  1 setUpConfiguration(WikiNotFoundBehavior.REDIRECT_TO_MAIN_WIKI);
92   
93  1 WikiDescriptorManager wikiDescriptorManager = mocker.getInstance(WikiDescriptorManager.class);
94  1 when(wikiDescriptorManager.getByAlias("someWiki")).thenReturn(null);
95  1 when(wikiDescriptorManager.getById("someWiki")).thenReturn(new WikiDescriptor("dummy", "dummy"));
96   
97  1 testAndAssert("http://localhost/xwiki/wiki/someWiki/view/Main/WebHome", "somewiki");
98    }
99   
 
100  1 toggle @Test
101    public void extractWhenNoWikiDescriptorButWithDomainBasedURL() throws Exception
102    {
103  1 setUpConfiguration(WikiNotFoundBehavior.REDIRECT_TO_MAIN_WIKI);
104  1 testAndAssert("http://wiki.server.com/xwiki/bin/view/Main/WebHome", "xwiki");
105    }
106   
 
107  1 toggle @Test
108    public void extractWhenNoWikiDescriptorAndDisplayErrorWhenWikiNotFound() throws Exception
109    {
110  1 setUpConfiguration(WikiNotFoundBehavior.DISPLAY_ERROR);
111  1 testAndAssert("http://localhost/xwiki/wiki/someWiki/view/Main/WebHome", "somewiki");
112    }
113   
 
114  1 toggle @Test
115    public void extractWhenNoExecutionContext() throws Exception
116    {
117  1 testAndAssert("http://localhost/xwiki/wiki/someWiki/view/Main/WebHome", "somewiki");
118   
119  1 verify(this.wikiDescriptorManager, never()).getByAlias(any());
120  1 verify(this.wikiDescriptorManager, never()).getById(any());
121    }
122   
 
123  7 toggle private void testAndAssert(String urlToTest, String expectedWikiId) throws Exception
124    {
125  7 ExtendedURL url = new ExtendedURL(new URL(urlToTest), "xwiki");
126    // Remove the resource type (i.e. the first segment) since this is what is expected by the extractor
127  7 url.getSegments().remove(0);
128  7 WikiReference wikiReference = this.mocker.getComponentUnderTest().extract(url);
129  7 assertEquals(new WikiReference(expectedWikiId), wikiReference);
130    }
131   
 
132  6 toggle private void setUpConfiguration(WikiNotFoundBehavior wikiNotFoundBehavior) throws Exception
133    {
134    // Simulate a configured Execution Context
135  6 Execution execution = mocker.getInstance(Execution.class);
136  6 when(execution.getContext()).thenReturn(new ExecutionContext());
137   
138  6 StandardURLConfiguration urlConfiguration = mocker.getInstance(StandardURLConfiguration.class);
139  6 when(urlConfiguration.getWikiNotFoundBehavior()).thenReturn(wikiNotFoundBehavior);
140    }
141    }