1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package com.xpn.xwiki.web

File ExportActionTest.java

 

Code metrics

0
32
1
1
124
71
1
0.03
32
1
1

Classes

Class Line # Actions
ExportActionTest 52 32 0% 1 0
1.0100%
 

Contributing tests

This file is covered by 1 test. .

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.util.Arrays;
23   
24    import javax.servlet.ServletOutputStream;
25   
26    import org.junit.Rule;
27    import org.junit.Test;
28    import org.mockito.ArgumentCaptor;
29    import org.xwiki.filter.input.InputFilterStream;
30    import org.xwiki.filter.input.InputFilterStreamFactory;
31    import org.xwiki.filter.instance.input.DocumentInstanceInputProperties;
32    import org.xwiki.filter.output.BeanOutputFilterStream;
33    import org.xwiki.filter.output.BeanOutputFilterStreamFactory;
34    import org.xwiki.filter.output.OutputFilterStreamFactory;
35    import org.xwiki.filter.type.FilterStreamType;
36    import org.xwiki.filter.xar.output.XAROutputProperties;
37    import org.xwiki.model.reference.DocumentReference;
38    import org.xwiki.model.reference.DocumentReferenceResolver;
39   
40    import static org.mockito.Mockito.*;
41    import static org.junit.Assert.*;
42   
43    import com.xpn.xwiki.XWikiContext;
44    import com.xpn.xwiki.test.MockitoOldcoreRule;
45   
46    /**
47    * Unit tests for {@link com.xpn.xwiki.web.ExportAction}.
48    *
49    * @version $Id: 78de3a946f4607d596275e7adc35ff88227a16b1 $
50    * @since 6.3M2
51    */
 
52    public class ExportActionTest
53    {
54    @Rule
55    public MockitoOldcoreRule oldcore = new MockitoOldcoreRule();
56   
 
57  1 toggle @Test
58    public void exportFullSpaceUsingWildcardsAsXAR() throws Exception
59    {
60  1 ExportAction action = new ExportAction();
61   
62  1 XWikiContext context = oldcore.getXWikiContext();
63   
64    // Make it a XAR export
65  1 XWikiRequest request = mock(XWikiRequest.class);
66  1 when(request.get("format")).thenReturn("xar");
67  1 context.setRequest(request);
68   
69    // Set other request parameters
70  1 when(request.get("name")).thenReturn("myexport");
71    // Export all pages in the "Space" space
72  1 when(request.getParameterValues("pages")).thenReturn(new String[] {"Space.%"});
73   
74    // Make the current user have programming rights
75  1 when(oldcore.getMockRightService().hasWikiAdminRights(context)).thenReturn(true);
76   
77    // Register some mock resolver to resolve passed page references
78  1 when(oldcore.getMockStore().searchDocumentsNames("where doc.fullName like ?", Arrays.asList("Space.%"),
79    context)).thenReturn(Arrays.asList("Space.Page1", "Space.Page2"));
80  1 when(oldcore.getMockRightService().hasAccessLevel("view", "XWiki.XWikiGuest", "xwiki:Space.Page1", context))
81    .thenReturn(true);
82  1 when(oldcore.getMockRightService().hasAccessLevel("view", "XWiki.XWikiGuest", "xwiki:Space.Page2", context))
83    .thenReturn(true);
84  1 DocumentReferenceResolver<String> resolver =
85    oldcore.getMocker().registerMockComponent(DocumentReferenceResolver.TYPE_STRING, "current");
86  1 when(resolver.resolve("xwiki:Space.Page1")).thenReturn(new DocumentReference("xwiki", "Space", "Page1"));
87  1 when(resolver.resolve("xwiki:Space.Page2")).thenReturn(new DocumentReference("xwiki", "Space", "Page2"));
88   
89    // Register some mock filters so that the export does nothing.
90  1 InputFilterStreamFactory inputFilterStreamFactory = oldcore.getMocker().registerMockComponent(
91    InputFilterStreamFactory.class, FilterStreamType.XWIKI_INSTANCE.serialize());
92  1 when(inputFilterStreamFactory.createInputFilterStream(anyMap())).thenReturn(mock(InputFilterStream.class));
93  1 BeanOutputFilterStreamFactory beanOutputFilterStreamFactory = mock(BeanOutputFilterStreamFactory.class);
94  1 oldcore.getMocker().registerComponent(OutputFilterStreamFactory.class,
95    FilterStreamType.XWIKI_XAR_CURRENT.serialize(), beanOutputFilterStreamFactory);
96  1 when(beanOutputFilterStreamFactory.createOutputFilterStream(any(XAROutputProperties.class))).thenReturn(
97    mock(BeanOutputFilterStream.class));
98   
99    // Set response stream
100  1 XWikiResponse response = mock(XWikiResponse.class);
101  1 ServletOutputStream outputStream = mock(ServletOutputStream.class);
102  1 when(response.getOutputStream()).thenReturn(outputStream);
103  1 context.setResponse(response);
104   
105  1 String result = action.render(oldcore.getXWikiContext());
106   
107    // The tests are below this line!
108   
109    // Verify null is returned (this means the response has been returned)
110  1 assertNull(result);
111   
112    // Verify that the parameters passed to the input stream factory are defining the correct pages
113  1 ArgumentCaptor<DocumentInstanceInputProperties> properties =
114    ArgumentCaptor.forClass(DocumentInstanceInputProperties.class);
115  1 verify(inputFilterStreamFactory).createInputFilterStream(properties.capture());
116  1 assertEquals(false, properties.getValue().isVerbose());
117  1 assertEquals(false, properties.getValue().isWithJRCSRevisions());
118  1 assertEquals(false, properties.getValue().isWithRevisions());
119  1 assertEquals(true, properties.getValue().getEntities().matches(
120    new DocumentReference("xwiki", "Space", "Page1")));
121  1 assertEquals(true, properties.getValue().getEntities().matches(
122    new DocumentReference("xwiki", "Space", "Page2")));
123    }
124    }