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

File ExportURLFactoryMockitoTest.java

 

Code metrics

0
44
4
1
158
104
4
0.09
11
4
1

Classes

Class Line # Actions
ExportURLFactoryMockitoTest 52 44 0% 4 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.web;
21   
22    import java.net.URL;
23    import java.util.Arrays;
24   
25    import org.junit.Before;
26    import org.junit.Rule;
27    import org.junit.Test;
28    import org.mockito.invocation.InvocationOnMock;
29    import org.mockito.stubbing.Answer;
30    import org.xwiki.model.EntityType;
31    import org.xwiki.model.reference.DocumentReference;
32    import org.xwiki.model.reference.DocumentReferenceResolver;
33    import org.xwiki.model.reference.EntityReference;
34    import org.xwiki.model.reference.EntityReferenceResolver;
35    import org.xwiki.model.reference.EntityReferenceSerializer;
36    import org.xwiki.resource.internal.entity.EntityResourceActionLister;
37    import org.xwiki.test.mockito.MockitoComponentManagerRule;
38    import org.xwiki.url.filesystem.FilesystemExportContext;
39   
40    import com.xpn.xwiki.XWiki;
41    import com.xpn.xwiki.XWikiContext;
42    import com.xpn.xwiki.internal.model.LegacySpaceResolver;
43   
44    import static org.junit.Assert.*;
45    import static org.mockito.Mockito.*;
46   
47    /**
48    * Unit tests for {@link ExportURLFactory}.
49    *
50    * @version $Id: c2314a4315cc6030b4820228daa6127d32e3e61f $
51    */
 
52    public class ExportURLFactoryMockitoTest
53    {
54    @Rule
55    public MockitoComponentManagerRule componentManager = new MockitoComponentManagerRule();
56   
57    private ExportURLFactory factory;
58   
59    private XWikiContext context;
60   
61    private DocumentReferenceResolver<String> currentDocumentReferenceResolver;
62   
63    private EntityReferenceSerializer<String> defaultEntityReferenceSerializer;
64   
65    private EntityReferenceSerializer<String> fsPathEntityReferenceSerializer;
66   
67    private LegacySpaceResolver legacySpaceResolver;
68   
69    private EntityReferenceResolver<String> relativeEntityReferenceResolver;
70   
 
71  2 toggle @Before
72    public void setUp() throws Exception
73    {
74  2 this.context = new XWikiContext();
75  2 this.context.setURL(new URL("http://localhost:8080/xwiki/bin/export/Main/WebHome?format=html"));
76  2 XWikiRequest request = mock(XWikiRequest.class);
77  2 when(request.getHeader("x-forwarded-host")).thenReturn(null);
78  2 this.context.setRequest(request);
79  2 XWikiResponse response = mock(XWikiResponse.class);
80  2 when(response.encodeURL(anyString())).thenAnswer(new Answer()
81    {
 
82  1 toggle @Override
83    public Object answer(InvocationOnMock invocationOnMock) throws Throwable
84    {
85  1 Object[] args = invocationOnMock.getArguments();
86  1 return args[0];
87    }
88    });
89  2 this.context.setResponse(response);
90   
91  2 XWiki xwiki = mock(XWiki.class);
92  2 when(xwiki.getWebAppPath(any(XWikiContext.class))).thenReturn("/xwiki");
93  2 when(xwiki.Param("xwiki.url.protocol", "http")).thenReturn("http");
94  2 when(xwiki.getServletPath("xwiki", this.context)).thenReturn("/bin/");
95  2 when(xwiki.getServerURL("xwiki", this.context)).thenReturn(new URL("http://localhost:8080"));
96  2 this.context.setWiki(xwiki);
97   
98    // Setup component mocks required by XWikiServletURLFactory that ExportURLFactory inherits from
99  2 this.relativeEntityReferenceResolver =
100    this.componentManager.registerMockComponent(EntityReferenceResolver.TYPE_STRING, "relative");
101  2 this.componentManager.registerMockComponent(EntityResourceActionLister.class);
102  2 Utils.setComponentManager(this.componentManager);
103   
104    // Setup component mocks required by ExportURLFactory
105   
106  2 this.currentDocumentReferenceResolver =
107    this.componentManager.registerMockComponent(DocumentReferenceResolver.TYPE_STRING, "current");
108  2 this.defaultEntityReferenceSerializer =
109    this.componentManager.registerMockComponent(EntityReferenceSerializer.TYPE_STRING);
110  2 this.fsPathEntityReferenceSerializer =
111    this.componentManager.registerMockComponent(EntityReferenceSerializer.TYPE_STRING, "fspath");
112  2 this.legacySpaceResolver = this.componentManager.registerMockComponent(LegacySpaceResolver.class);
113   
114  2 this.factory = new ExportURLFactory();
115    }
116   
 
117  1 toggle @Test
118    public void createURLWhenNotInExportedPages() throws Exception
119    {
120  1 DocumentReference pageReference = new DocumentReference("xwiki", "Main", "WebHome");
121  1 when(this.currentDocumentReferenceResolver.resolve("Main.WebHome")).thenReturn(pageReference);
122  1 when(this.defaultEntityReferenceSerializer.serialize(pageReference)).thenReturn("xwiki:Main.WebHome");
123   
124  1 when(this.legacySpaceResolver.resolve("Main")).thenReturn(Arrays.asList("Main"));
125  1 when(this.fsPathEntityReferenceSerializer.serialize(pageReference)).thenReturn("Main/WebHome");
126  1 DocumentReference pageReference2 = new DocumentReference("xwiki", "Main", "SomeOtherPage");
127  1 when(this.fsPathEntityReferenceSerializer.serialize(pageReference2)).thenReturn("Main/SomeOtherPage");
128   
129  1 when(this.relativeEntityReferenceResolver.resolve("Main", EntityType.SPACE)).thenReturn(
130    new EntityReference("Main", EntityType.SPACE));
131   
132  1 this.factory.init(Arrays.asList(pageReference), null, new FilesystemExportContext(), this.context);
133  1 assertEquals(new URL("http://localhost:8080/xwiki/bin/Main/SomeOtherPage"),
134    this.factory.createURL("Main", "SomeOtherPage", "view", null, null, "xwiki", this.context));
135    }
136   
 
137  1 toggle @Test
138    public void createURLWhenInExportedPages() throws Exception
139    {
140  1 DocumentReference pageReference = new DocumentReference("xwiki", "Main", "WebHome");
141  1 when(this.currentDocumentReferenceResolver.resolve("Main.WebHome")).thenReturn(pageReference);
142  1 when(this.defaultEntityReferenceSerializer.serialize(pageReference)).thenReturn("xwiki:Main.WebHome");
143   
144  1 when(this.legacySpaceResolver.resolve("Main")).thenReturn(Arrays.asList("Main"));
145  1 when(this.fsPathEntityReferenceSerializer.serialize(pageReference)).thenReturn("Main/WebHome");
146   
147  1 when(this.relativeEntityReferenceResolver.resolve("Main", EntityType.SPACE)).thenReturn(
148    new EntityReference("Main", EntityType.SPACE));
149   
150  1 FilesystemExportContext exportContext = new FilesystemExportContext();
151    // Simulate locating the doc in pages/Main/WebHome (ie 3 levels deep)
152  1 exportContext.setDocParentLevels(3);
153   
154  1 this.factory.init(Arrays.asList(pageReference), null, exportContext, this.context);
155  1 assertEquals(new URL("file://../../../pages/Main/WebHome.html"),
156    this.factory.createURL("Main", "WebHome", "view", null, null, "xwiki", this.context));
157    }
158    }