1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package org.xwiki.url.internal.standard

File DomainWikiReferenceExtractorTest.java

 

Code metrics

0
35
12
1
154
102
12
0.34
2.92
12
1

Classes

Class Line # Actions
DomainWikiReferenceExtractorTest 44 35 0% 12 0
1.0100%
 

Contributing tests

This file is covered by 9 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: 65be0ea825e4828f20c3176b2c38ba79affe4999 $
42    * @since 6.3M1
43    */
 
44    public class DomainWikiReferenceExtractorTest
45    {
46    @Rule
47    public MockitoComponentMockingRule<DomainWikiReferenceExtractor> mocker =
48    new MockitoComponentMockingRule<>(DomainWikiReferenceExtractor.class);
49   
50    private WikiDescriptorManager wikiDescriptorManager;
51   
 
52  9 toggle @Before
53    public void setUp() throws Exception
54    {
55  9 this.wikiDescriptorManager = mocker.getInstance(WikiDescriptorManager.class);
56  9 when(wikiDescriptorManager.getMainWikiId()).thenReturn("xwiki");
57    }
58   
 
59  1 toggle @Test
60    public void extractWhenNoWikiDescriptorForFullDomain() throws Exception
61    {
62  1 setUpConfiguration(WikiNotFoundBehavior.REDIRECT_TO_MAIN_WIKI);
63  1 testAndAssert("http://wiki.server.com/xwiki/bin/view/Main/WebHome", "xwiki");
64    }
65   
 
66  1 toggle @Test
67    public void extractWhenNoWikiDescriptorForFullDomainButDescriptorForSubdomain() throws Exception
68    {
69  1 setUpConfiguration(WikiNotFoundBehavior.REDIRECT_TO_MAIN_WIKI);
70   
71  1 WikiDescriptorManager wikiDescriptorManager = mocker.getInstance(WikiDescriptorManager.class);
72  1 when(wikiDescriptorManager.getByAlias("wiki.server.com")).thenReturn(null);
73  1 when(wikiDescriptorManager.getById("wiki")).thenReturn(new WikiDescriptor("dummy", "dummy"));
74   
75  1 testAndAssert("http://wiki.server.com/xwiki/bin/view/Main/WebHome", "wiki");
76    }
77   
 
78  1 toggle @Test
79    public void extractWhenNoWikiDescriptorButStartsWithWWW() throws Exception
80    {
81  1 setUpConfiguration(WikiNotFoundBehavior.REDIRECT_TO_MAIN_WIKI);
82  1 testAndAssert("http://www.wiki.com/xwiki/bin/view/Main/WebHome", "xwiki");
83    }
84   
 
85  1 toggle @Test
86    public void extractWhenNoWikiDescriptorButStartsWithLocalhost() throws Exception
87    {
88  1 setUpConfiguration(WikiNotFoundBehavior.REDIRECT_TO_MAIN_WIKI);
89  1 testAndAssert("http://localhost/xwiki/bin/view/Main/WebHome", "xwiki");
90    }
91   
 
92  1 toggle @Test
93    public void extractWhenNoWikiDescriptorButStartsWithIP() throws Exception
94    {
95  1 setUpConfiguration(WikiNotFoundBehavior.REDIRECT_TO_MAIN_WIKI);
96  1 testAndAssert("http://192.168.0.10/xwiki/bin/view/Main/WebHome", "xwiki");
97    }
98   
 
99  1 toggle @Test
100    public void extractWhenWikiDescriptor() throws Exception
101    {
102  1 setUpConfiguration(WikiNotFoundBehavior.REDIRECT_TO_MAIN_WIKI);
103   
104  1 WikiDescriptorManager wikiDescriptorManager = mocker.getInstance(WikiDescriptorManager.class);
105  1 when(wikiDescriptorManager.getByAlias("wiki.server.com")).thenReturn(new WikiDescriptor("wikiid", "wiki"));
106   
107  1 testAndAssert("http://wiki.server.com/xwiki/bin/view/Main/WebHome", "wikiid");
108    }
109   
 
110  1 toggle @Test
111    public void extractWhenNoWikiDescriptorAndDisplayErrorWhenWikiNotFound() throws Exception
112    {
113  1 setUpConfiguration(WikiNotFoundBehavior.DISPLAY_ERROR);
114  1 testAndAssert("http://wiki.server.com/xwiki/bin/view/Main/WebHome", "wiki");
115    }
116   
 
117  1 toggle @Test
118    public void extractWhenNoAliasAndUnderscoreInDomainName() throws Exception
119    {
120    // Simulate a configured Execution Context
121  1 Execution execution = mocker.getInstance(Execution.class);
122  1 when(execution.getContext()).thenReturn(new ExecutionContext());
123   
124  1 testAndAssert("http://some_domain.server.com/xwiki/bin/view/Main/WebHome", "some_domain");
125    }
126   
 
127  1 toggle @Test
128    public void extractWhenNoExecutionContext() throws Exception
129    {
130  1 testAndAssert("http://domain.server.com/xwiki/bin/view/Main/WebHome", "domain");
131   
132  1 verify(this.wikiDescriptorManager, never()).getByAlias(any());
133  1 verify(this.wikiDescriptorManager, never()).getById(any());
134    }
135   
 
136  9 toggle private void testAndAssert(String urlToTest, String expectedWikiId) throws Exception
137    {
138  9 ExtendedURL url = new ExtendedURL(new URL(urlToTest), "xwiki");
139    // Remove the resource type (i.e. the first segment) since this is what is expected by the extractor
140  9 url.getSegments().remove(0);
141  9 WikiReference wikiReference = this.mocker.getComponentUnderTest().extract(url);
142  9 assertEquals(new WikiReference(expectedWikiId), wikiReference);
143    }
144   
 
145  7 toggle private void setUpConfiguration(WikiNotFoundBehavior wikiNotFoundBehavior) throws Exception
146    {
147    // Simulate a configured Execution Context
148  7 Execution execution = mocker.getInstance(Execution.class);
149  7 when(execution.getContext()).thenReturn(new ExecutionContext());
150   
151  7 StandardURLConfiguration urlConfiguration = mocker.getInstance(StandardURLConfiguration.class);
152  7 when(urlConfiguration.getWikiNotFoundBehavior()).thenReturn(wikiNotFoundBehavior);
153    }
154    }