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

File MockitoDownloadActionTest.java

 

Code metrics

0
75
3
2
197
123
3
0.04
25
1.5
1

Classes

Class Line # Actions
MockitoDownloadActionTest 58 74 0% 2 0
1.0100%
MockitoDownloadActionTest.StubServletOutputStream 63 1 0% 1 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.io.ByteArrayInputStream;
23    import java.io.ByteArrayOutputStream;
24    import java.io.IOException;
25    import java.util.Arrays;
26    import java.util.Date;
27   
28    import javax.servlet.ServletOutputStream;
29   
30    import org.junit.Rule;
31    import org.junit.Test;
32    import org.xwiki.context.ExecutionContext;
33    import org.xwiki.context.ExecutionContextManager;
34    import org.xwiki.model.reference.AttachmentReference;
35    import org.xwiki.model.reference.DocumentReference;
36    import org.xwiki.resource.ResourceReferenceManager;
37    import org.xwiki.resource.entity.EntityResourceAction;
38    import org.xwiki.resource.entity.EntityResourceReference;
39    import org.xwiki.velocity.VelocityManager;
40   
41    import com.xpn.xwiki.XWiki;
42    import com.xpn.xwiki.XWikiContext;
43    import com.xpn.xwiki.doc.XWikiAttachment;
44    import com.xpn.xwiki.doc.XWikiDocument;
45    import com.xpn.xwiki.plugin.XWikiPluginManager;
46    import com.xpn.xwiki.test.MockitoOldcoreRule;
47   
48    import static org.junit.Assert.*;
49    import static org.mockito.Mockito.*;
50   
51    /**
52    * Unit tests for {@link DownloadAction} using Mockito. This class is supposed to replace the
53    * {@link DownloadActionTest} test, after all the tests have been moved in this one.
54    *
55    * @version $Id: 7953e65d38a1a7a23040e2c46886ee36c72e7bf1 $
56    * @since 7.2M2
57    */
 
58    public class MockitoDownloadActionTest
59    {
60    @Rule
61    public MockitoOldcoreRule oldcore = new MockitoOldcoreRule();
62   
 
63    private class StubServletOutputStream extends ServletOutputStream
64    {
65    public ByteArrayOutputStream baos = new ByteArrayOutputStream();
66   
 
67  8 toggle public void write(int i) throws IOException
68    {
69  8 baos.write(i);
70    }
71    }
72   
 
73  1 toggle @Test
74    public void renderWhenAttachmentIsInANestedSpace() throws Exception
75    {
76  1 DownloadAction action = new DownloadAction();
77  1 XWikiContext xcontext = this.oldcore.getXWikiContext();
78   
79    // Set the Request URL
80  1 XWikiServletRequestStub request = new XWikiServletRequestStub();
81  1 request.setRequestURI("http://localhost:8080/xwiki/bin/download/space1/space2/page/file.ext");
82  1 xcontext.setRequest(request);
83   
84    // Setup the returned attachment
85  1 XWikiAttachment attachment = mock(XWikiAttachment.class);
86  1 when(attachment.getContentSize(xcontext)).thenReturn(100);
87  1 Date now = new Date();
88  1 when(attachment.getDate()).thenReturn(now);
89  1 when(attachment.getFilename()).thenReturn("file.ext");
90  1 when(attachment.getContentInputStream(xcontext)).thenReturn(new ByteArrayInputStream("test".getBytes()));
91  1 when(attachment.getMimeType(xcontext)).thenReturn("mimetype");
92   
93    // Set the current doc
94  1 XWikiDocument document = mock(XWikiDocument.class);
95  1 when(document.getAttachment("file.ext")).thenReturn(attachment);
96  1 xcontext.setDoc(document);
97   
98    // Set the Plugin Manager
99  1 XWikiPluginManager pluginManager = mock(XWikiPluginManager.class);
100  1 when(pluginManager.downloadAttachment(attachment, xcontext)).thenReturn(attachment);
101  1 doReturn(pluginManager).when(this.oldcore.getSpyXWiki()).getPluginManager();
102   
103    // Set the Response
104  1 XWikiResponse response = mock(XWikiResponse.class);
105  1 StubServletOutputStream ssos = new StubServletOutputStream();
106  1 when(response.getOutputStream()).thenReturn(ssos);
107  1 xcontext.setResponse(response);
108   
109    // Set the Resource Reference Manager used to parse the URL and extract the attachment name
110  1 ResourceReferenceManager rrm = this.oldcore.getMocker().registerMockComponent(ResourceReferenceManager.class);
111  1 when(rrm.getResourceReference()).thenReturn(new EntityResourceReference(new AttachmentReference("file.ext",
112    new DocumentReference("wiki", Arrays.asList("space1", "space2"), "page")), EntityResourceAction.VIEW));
113   
114    // Note: we don't give PR and the attachment is not an authorized mime type.
115   
116  1 assertNull(action.render(xcontext));
117   
118    // This is the test, we verify what is set in the response
119  1 verify(response).setContentType("mimetype");
120  1 verify(response).setHeader("Accept-Ranges", "bytes");
121  1 verify(response).addHeader("Content-disposition", "attachment; filename*=utf-8''file.ext");
122  1 verify(response).setDateHeader("Last-Modified", now.getTime());
123  1 verify(response).setContentLength(100);
124  1 assertEquals("test", ssos.baos.toString());
125    }
126   
 
127  1 toggle @Test
128    public void renderWhenZipExplorerPluginURL() throws Exception
129    {
130  1 DownloadAction action = new DownloadAction();
131  1 XWikiContext xcontext = this.oldcore.getXWikiContext();
132   
133    // Set the Request URL
134  1 XWikiServletRequestStub request = new XWikiServletRequestStub();
135  1 request.setRequestURI("http://localhost:8080/xwiki/bin/download/space/page/file.ext/some/path");
136  1 xcontext.setRequest(request);
137   
138    // Set the current doc and current wiki
139  1 XWikiDocument document = mock(XWikiDocument.class);
140  1 when(document.getAttachment("path")).thenReturn(null);
141  1 xcontext.setDoc(document);
142  1 xcontext.setWikiId("wiki");
143  1 xcontext.setAction("download");
144   
145    // Set the Response
146  1 XWikiResponse response = mock(XWikiResponse.class);
147  1 StubServletOutputStream ssos = new StubServletOutputStream();
148  1 when(response.getOutputStream()).thenReturn(ssos);
149  1 xcontext.setResponse(response);
150   
151    // Set the Resource Reference Manager used to parse the URL and extract the attachment name
152  1 ResourceReferenceManager rrm = this.oldcore.getMocker().registerMockComponent(ResourceReferenceManager.class);
153  1 when(rrm.getResourceReference()).thenReturn(new EntityResourceReference(new AttachmentReference("path",
154    new DocumentReference("wiki", Arrays.asList("space", "page", "file.ext"), "some")),
155    EntityResourceAction.VIEW));
156   
157    // Setup the returned attachment
158  1 XWikiAttachment attachment = mock(XWikiAttachment.class);
159  1 when(attachment.getContentSize(xcontext)).thenReturn(100);
160  1 Date now = new Date();
161  1 when(attachment.getDate()).thenReturn(now);
162  1 when(attachment.getFilename()).thenReturn("file.ext");
163  1 when(attachment.getContentInputStream(xcontext)).thenReturn(new ByteArrayInputStream("test".getBytes()));
164  1 when(attachment.getMimeType(xcontext)).thenReturn("mimetype");
165  1 when(attachment.clone()).thenReturn(attachment);
166   
167    // Configure an existing doc in the store
168  1 XWiki xwiki = this.oldcore.getSpyXWiki();
169  1 XWikiDocument backwardCompatDocument = new XWikiDocument(new DocumentReference("wiki", "space", "page"));
170  1 backwardCompatDocument.addAttachment(attachment);
171  1 xwiki.saveDocument(backwardCompatDocument, xcontext);
172   
173    // Make sure the user has permission to access the doc
174  1 doReturn(true).when(xwiki).checkAccess(eq("download"), any(XWikiDocument.class), any(XWikiContext.class));
175   
176    // Setup ExecutionContextManager & VelocityManager using in the context backup
177  1 ExecutionContextManager ecm = this.oldcore.getMocker().registerMockComponent(ExecutionContextManager.class);
178  1 ExecutionContext ec = this.oldcore.getExecutionContext();
179  1 when(ecm.clone(ec)).thenReturn(ec);
180  1 VelocityManager vm = this.oldcore.getMocker().registerMockComponent(VelocityManager.class);
181   
182    // Set the Plugin Manager
183  1 XWikiPluginManager pluginManager = mock(XWikiPluginManager.class);
184  1 when(pluginManager.downloadAttachment(attachment, xcontext)).thenReturn(attachment);
185  1 doReturn(pluginManager).when(this.oldcore.getSpyXWiki()).getPluginManager();
186   
187  1 assertNull(action.render(xcontext));
188   
189    // This is the test, we verify what is set in the response
190  1 verify(response).setContentType("mimetype");
191  1 verify(response).setHeader("Accept-Ranges", "bytes");
192  1 verify(response).addHeader("Content-disposition", "attachment; filename*=utf-8''file.ext");
193  1 verify(response).setDateHeader("Last-Modified", now.getTime());
194  1 verify(response).setContentLength(100);
195  1 assertEquals("test", ssos.baos.toString());
196    }
197    }