1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
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 |
|
@link |
53 |
|
@link |
54 |
|
|
55 |
|
@version |
56 |
|
@since |
57 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (76) |
Complexity: 2 |
Complexity Density: 0.03 |
|
58 |
|
public class MockitoDownloadActionTest |
59 |
|
{ |
60 |
|
@Rule |
61 |
|
public MockitoOldcoreRule oldcore = new MockitoOldcoreRule(); |
62 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 1 |
|
63 |
|
private class StubServletOutputStream extends ServletOutputStream |
64 |
|
{ |
65 |
|
public ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
66 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
67 |
8 |
public void write(int i) throws IOException... |
68 |
|
{ |
69 |
8 |
baos.write(i); |
70 |
|
} |
71 |
|
} |
72 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (31) |
Complexity: 1 |
Complexity Density: 0.03 |
1PASS
|
|
73 |
1 |
@Test... |
74 |
|
public void renderWhenAttachmentIsInANestedSpace() throws Exception |
75 |
|
{ |
76 |
1 |
DownloadAction action = new DownloadAction(); |
77 |
1 |
XWikiContext xcontext = this.oldcore.getXWikiContext(); |
78 |
|
|
79 |
|
|
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 |
|
|
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 |
|
|
94 |
1 |
XWikiDocument document = mock(XWikiDocument.class); |
95 |
1 |
when(document.getAttachment("file.ext")).thenReturn(attachment); |
96 |
1 |
xcontext.setDoc(document); |
97 |
|
|
98 |
|
|
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 |
|
|
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 |
|
|
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 |
|
|
115 |
|
|
116 |
1 |
assertNull(action.render(xcontext)); |
117 |
|
|
118 |
|
|
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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (43) |
Complexity: 1 |
Complexity Density: 0.02 |
1PASS
|
|
127 |
1 |
@Test... |
128 |
|
public void renderWhenZipExplorerPluginURL() throws Exception |
129 |
|
{ |
130 |
1 |
DownloadAction action = new DownloadAction(); |
131 |
1 |
XWikiContext xcontext = this.oldcore.getXWikiContext(); |
132 |
|
|
133 |
|
|
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 |
|
|
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 |
|
|
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 |
|
|
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 |
|
|
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 |
|
|
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 |
|
|
174 |
1 |
doReturn(true).when(xwiki).checkAccess(eq("download"), any(XWikiDocument.class), any(XWikiContext.class)); |
175 |
|
|
176 |
|
|
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 |
|
|
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 |
|
|
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 |
|
} |