1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.office.viewer.internal; |
21 |
|
|
22 |
|
import java.io.ByteArrayInputStream; |
23 |
|
import java.io.File; |
24 |
|
import java.io.InputStream; |
25 |
|
import java.lang.reflect.Type; |
26 |
|
import java.util.ArrayList; |
27 |
|
import java.util.Arrays; |
28 |
|
import java.util.Collections; |
29 |
|
import java.util.HashMap; |
30 |
|
import java.util.HashSet; |
31 |
|
import java.util.Map; |
32 |
|
|
33 |
|
import org.artofsolving.jodconverter.document.DefaultDocumentFormatRegistry; |
34 |
|
import org.junit.Assert; |
35 |
|
import org.junit.Before; |
36 |
|
import org.junit.Rule; |
37 |
|
import org.junit.Test; |
38 |
|
import org.xwiki.bridge.DocumentAccessBridge; |
39 |
|
import org.xwiki.cache.Cache; |
40 |
|
import org.xwiki.cache.CacheManager; |
41 |
|
import org.xwiki.cache.config.CacheConfiguration; |
42 |
|
import org.xwiki.component.util.DefaultParameterizedType; |
43 |
|
import org.xwiki.model.reference.AttachmentReference; |
44 |
|
import org.xwiki.model.reference.AttachmentReferenceResolver; |
45 |
|
import org.xwiki.model.reference.DocumentReference; |
46 |
|
import org.xwiki.model.reference.EntityReferenceSerializer; |
47 |
|
import org.xwiki.office.viewer.OfficeResourceViewer; |
48 |
|
import org.xwiki.officeimporter.builder.PresentationBuilder; |
49 |
|
import org.xwiki.officeimporter.builder.XDOMOfficeDocumentBuilder; |
50 |
|
import org.xwiki.officeimporter.converter.OfficeConverter; |
51 |
|
import org.xwiki.officeimporter.document.XDOMOfficeDocument; |
52 |
|
import org.xwiki.officeimporter.server.OfficeServer; |
53 |
|
import org.xwiki.properties.ConverterManager; |
54 |
|
import org.xwiki.rendering.block.Block; |
55 |
|
import org.xwiki.rendering.block.ExpandedMacroBlock; |
56 |
|
import org.xwiki.rendering.block.ImageBlock; |
57 |
|
import org.xwiki.rendering.block.MetaDataBlock; |
58 |
|
import org.xwiki.rendering.block.XDOM; |
59 |
|
import org.xwiki.rendering.block.match.ClassBlockMatcher; |
60 |
|
import org.xwiki.rendering.listener.MetaData; |
61 |
|
import org.xwiki.rendering.listener.reference.AttachmentResourceReference; |
62 |
|
import org.xwiki.rendering.listener.reference.ResourceReference; |
63 |
|
import org.xwiki.rendering.listener.reference.ResourceType; |
64 |
|
import org.xwiki.rendering.renderer.reference.ResourceReferenceTypeSerializer; |
65 |
|
import org.xwiki.rendering.syntax.Syntax; |
66 |
|
import org.xwiki.resource.ResourceReferenceSerializer; |
67 |
|
import org.xwiki.resource.temporary.TemporaryResourceReference; |
68 |
|
import org.xwiki.resource.temporary.TemporaryResourceStore; |
69 |
|
import org.xwiki.test.mockito.MockitoComponentMockingRule; |
70 |
|
import org.xwiki.url.ExtendedURL; |
71 |
|
|
72 |
|
import static org.junit.Assert.*; |
73 |
|
import static org.mockito.ArgumentMatchers.*; |
74 |
|
import static org.mockito.Mockito.*; |
75 |
|
|
76 |
|
|
77 |
|
@link |
78 |
|
|
79 |
|
@version |
80 |
|
|
|
|
| 98.9% |
Uncovered Elements: 1 (94) |
Complexity: 8 |
Complexity Density: 0.09 |
|
81 |
|
public class DefaultOfficeResourceViewerTest |
82 |
|
{ |
83 |
|
private static final String ATTACHEMENT_NAME = "Test file.doc"; |
84 |
|
|
85 |
|
|
86 |
|
|
87 |
|
|
88 |
|
private static final AttachmentReference ATTACHMENT_REFERENCE = new AttachmentReference(ATTACHEMENT_NAME, |
89 |
|
new DocumentReference("xwiki", "Main", "Test")); |
90 |
|
|
91 |
|
|
92 |
|
|
93 |
|
|
94 |
|
private static final String STRING_DOCUMENT_REFERENCE = "xwiki:Main.Test"; |
95 |
|
|
96 |
|
|
97 |
|
|
98 |
|
|
99 |
|
private static final String STRING_ATTACHMENT_REFERENCE = STRING_DOCUMENT_REFERENCE + '@' + ATTACHEMENT_NAME; |
100 |
|
|
101 |
|
private static final String STRING_ATTACHMENT_RESOURCE_REFERENCE = "attach:" + STRING_ATTACHMENT_REFERENCE; |
102 |
|
|
103 |
|
|
104 |
|
@link |
105 |
|
|
106 |
|
private static final AttachmentResourceReference ATTACHMENT_RESOURCE_REFERENCE = new AttachmentResourceReference( |
107 |
|
STRING_ATTACHMENT_REFERENCE); |
108 |
|
|
109 |
|
|
110 |
|
|
111 |
|
|
112 |
|
private static final Map<String, ?> DEFAULT_VIEW_PARAMETERS = Collections.singletonMap("ownerDocument", |
113 |
|
ATTACHMENT_REFERENCE.getDocumentReference()); |
114 |
|
|
115 |
|
|
116 |
|
@link@link |
117 |
|
|
118 |
|
private static final String CACHE_KEY = STRING_DOCUMENT_REFERENCE + '/' + ATTACHEMENT_NAME + '/' |
119 |
|
+ DEFAULT_VIEW_PARAMETERS.hashCode(); |
120 |
|
|
121 |
|
|
122 |
|
|
123 |
|
|
124 |
|
private static final String ATTACHMENT_VERSION = "1.1"; |
125 |
|
|
126 |
|
|
127 |
|
@link |
128 |
|
|
129 |
|
@Rule |
130 |
|
public MockitoComponentMockingRule<OfficeResourceViewer> mocker = |
131 |
|
new MockitoComponentMockingRule<OfficeResourceViewer>(DefaultOfficeResourceViewer.class); |
132 |
|
|
133 |
|
|
134 |
|
@link |
135 |
|
|
136 |
|
private DocumentAccessBridge documentAccessBridge; |
137 |
|
|
138 |
|
|
139 |
|
@link |
140 |
|
|
141 |
|
private XDOMOfficeDocumentBuilder officeDocumentBuilder; |
142 |
|
|
143 |
|
private ResourceReferenceTypeSerializer resourceReferenceSerializer; |
144 |
|
|
145 |
|
|
146 |
|
@link |
147 |
|
|
148 |
|
private Cache<OfficeDocumentView> attachmentCache; |
149 |
|
|
150 |
|
|
151 |
|
@link |
152 |
|
|
153 |
|
private Cache<OfficeDocumentView> externalCache; |
154 |
|
|
155 |
|
|
156 |
|
|
157 |
|
|
158 |
|
@throws |
159 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (20) |
Complexity: 1 |
Complexity Density: 0.05 |
|
160 |
6 |
@Before... |
161 |
|
public void configure() throws Exception |
162 |
|
{ |
163 |
6 |
final CacheManager cacheManager = mocker.getInstance(CacheManager.class); |
164 |
6 |
attachmentCache = mock(Cache.class, "attachment"); |
165 |
6 |
externalCache = mock(Cache.class, "external"); |
166 |
6 |
when(cacheManager.<OfficeDocumentView>createNewCache(notNull(CacheConfiguration.class))).thenReturn( |
167 |
|
attachmentCache, externalCache); |
168 |
|
|
169 |
6 |
EntityReferenceSerializer<String> entityReferenceSerializer = |
170 |
|
mocker.getInstance(EntityReferenceSerializer.TYPE_STRING); |
171 |
6 |
when(entityReferenceSerializer.serialize(ATTACHMENT_REFERENCE)).thenReturn(STRING_ATTACHMENT_REFERENCE); |
172 |
6 |
when(entityReferenceSerializer.serialize(ATTACHMENT_REFERENCE.getDocumentReference())).thenReturn( |
173 |
|
STRING_DOCUMENT_REFERENCE); |
174 |
|
|
175 |
6 |
AttachmentReferenceResolver<String> attachmentReferenceResolver = |
176 |
|
mocker.getInstance(AttachmentReferenceResolver.TYPE_STRING, "current"); |
177 |
6 |
when(attachmentReferenceResolver.resolve(STRING_ATTACHMENT_REFERENCE)).thenReturn(ATTACHMENT_REFERENCE); |
178 |
|
|
179 |
6 |
this.resourceReferenceSerializer = mocker.getInstance(ResourceReferenceTypeSerializer.class); |
180 |
6 |
when(this.resourceReferenceSerializer.serialize(ATTACHMENT_RESOURCE_REFERENCE)).thenReturn( |
181 |
|
STRING_ATTACHMENT_RESOURCE_REFERENCE); |
182 |
|
|
183 |
6 |
ConverterManager converterManager = mocker.getInstance(ConverterManager.class); |
184 |
6 |
when(converterManager.convert(boolean.class, null)).thenReturn(false); |
185 |
6 |
when(converterManager.convert(DocumentReference.class, ATTACHMENT_REFERENCE.getDocumentReference())) |
186 |
|
.thenReturn(ATTACHMENT_REFERENCE.getDocumentReference()); |
187 |
|
|
188 |
6 |
documentAccessBridge = mocker.getInstance(DocumentAccessBridge.class); |
189 |
6 |
officeDocumentBuilder = mocker.getInstance(XDOMOfficeDocumentBuilder.class); |
190 |
|
|
191 |
6 |
OfficeServer officeServer = mocker.getInstance(OfficeServer.class); |
192 |
6 |
OfficeConverter officeConverter = mock(OfficeConverter.class); |
193 |
6 |
when(officeServer.getConverter()).thenReturn(officeConverter); |
194 |
6 |
when(officeConverter.getFormatRegistry()).thenReturn(new DefaultDocumentFormatRegistry()); |
195 |
|
} |
196 |
|
|
197 |
|
|
198 |
|
|
199 |
|
|
200 |
|
@throws |
201 |
|
|
|
|
| 83.3% |
Uncovered Elements: 1 (6) |
Complexity: 2 |
Complexity Density: 0.33 |
1PASS
|
|
202 |
1 |
@Test... |
203 |
|
public void testViewNonExistingOfficeAttachment() throws Exception |
204 |
|
{ |
205 |
1 |
when(attachmentCache.get(CACHE_KEY)).thenReturn(null); |
206 |
1 |
when(documentAccessBridge.getAttachmentReferences(ATTACHMENT_REFERENCE.getDocumentReference())).thenReturn( |
207 |
|
Collections.<AttachmentReference>emptyList()); |
208 |
|
|
209 |
1 |
try { |
210 |
1 |
mocker.getComponentUnderTest().createView(ATTACHMENT_RESOURCE_REFERENCE, DEFAULT_VIEW_PARAMETERS); |
211 |
0 |
Assert.fail("Expected exception."); |
212 |
|
} catch (Exception e) { |
213 |
1 |
Assert.assertEquals(String.format("Attachment [%s] does not exist.", ATTACHMENT_REFERENCE), e.getMessage()); |
214 |
|
} |
215 |
|
} |
216 |
|
|
217 |
|
|
218 |
|
|
219 |
|
|
220 |
|
@throws |
221 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (9) |
Complexity: 1 |
Complexity Density: 0.11 |
1PASS
|
|
222 |
1 |
@Test... |
223 |
|
public void testViewExistingOfficeAttachmentWithCacheMiss() throws Exception |
224 |
|
{ |
225 |
1 |
when(attachmentCache.get(CACHE_KEY)).thenReturn(null); |
226 |
1 |
when(documentAccessBridge.getAttachmentReferences(ATTACHMENT_REFERENCE.getDocumentReference())).thenReturn( |
227 |
|
Arrays.asList(ATTACHMENT_REFERENCE)); |
228 |
1 |
when(documentAccessBridge.getAttachmentVersion(ATTACHMENT_REFERENCE)).thenReturn(ATTACHMENT_VERSION); |
229 |
|
|
230 |
1 |
ByteArrayInputStream attachmentContent = new ByteArrayInputStream(new byte[256]); |
231 |
1 |
when(documentAccessBridge.getAttachmentContent(ATTACHMENT_REFERENCE)).thenReturn(attachmentContent); |
232 |
|
|
233 |
1 |
XDOMOfficeDocument xdomOfficeDocument = |
234 |
|
new XDOMOfficeDocument(new XDOM(new ArrayList<Block>()), new HashMap<String, byte[]>(), mocker); |
235 |
1 |
when( |
236 |
|
officeDocumentBuilder.build(attachmentContent, ATTACHMENT_REFERENCE.getName(), |
237 |
|
ATTACHMENT_REFERENCE.getDocumentReference(), false)).thenReturn(xdomOfficeDocument); |
238 |
|
|
239 |
1 |
mocker.getComponentUnderTest().createView(ATTACHMENT_RESOURCE_REFERENCE, DEFAULT_VIEW_PARAMETERS); |
240 |
|
|
241 |
1 |
verify(attachmentCache).set(eq(CACHE_KEY), notNull(AttachmentOfficeDocumentView.class)); |
242 |
|
} |
243 |
|
|
244 |
|
|
245 |
|
|
246 |
|
|
247 |
|
@throws |
248 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
1PASS
|
|
249 |
1 |
@Test... |
250 |
|
public void testViewExistingOfficeAttachmentWithCacheHit() throws Exception |
251 |
|
{ |
252 |
1 |
AttachmentOfficeDocumentView officeDocumentView = |
253 |
|
new AttachmentOfficeDocumentView(ATTACHMENT_RESOURCE_REFERENCE, ATTACHMENT_REFERENCE, ATTACHMENT_VERSION, |
254 |
|
new XDOM(new ArrayList<Block>()), new HashSet<File>()); |
255 |
1 |
when(attachmentCache.get(CACHE_KEY)).thenReturn(officeDocumentView); |
256 |
|
|
257 |
1 |
when(documentAccessBridge.getAttachmentReferences(ATTACHMENT_REFERENCE.getDocumentReference())).thenReturn( |
258 |
|
Arrays.asList(ATTACHMENT_REFERENCE)); |
259 |
1 |
when(documentAccessBridge.getAttachmentVersion(ATTACHMENT_REFERENCE)).thenReturn(ATTACHMENT_VERSION); |
260 |
|
|
261 |
1 |
Assert.assertNotNull(mocker.getComponentUnderTest().createView(ATTACHMENT_RESOURCE_REFERENCE, |
262 |
|
DEFAULT_VIEW_PARAMETERS)); |
263 |
|
} |
264 |
|
|
265 |
|
|
266 |
|
|
267 |
|
|
268 |
|
@throws |
269 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
1PASS
|
|
270 |
1 |
@Test... |
271 |
|
public void testViewExistingOfficeFileWithCacheHit() throws Exception |
272 |
|
{ |
273 |
1 |
ResourceReference resourceReference = new ResourceReference("http://resource", ResourceType.URL); |
274 |
|
|
275 |
1 |
when(this.resourceReferenceSerializer.serialize(resourceReference)).thenReturn( |
276 |
|
"url:" + resourceReference.getReference()); |
277 |
|
|
278 |
1 |
OfficeDocumentView officeDocumentView = |
279 |
|
new OfficeDocumentView(resourceReference, new XDOM(new ArrayList<Block>()), new HashSet<File>()); |
280 |
1 |
when( |
281 |
|
externalCache.get(STRING_DOCUMENT_REFERENCE + "/url:http://resource/" + DEFAULT_VIEW_PARAMETERS.hashCode())) |
282 |
|
.thenReturn(officeDocumentView); |
283 |
|
|
284 |
1 |
Assert.assertNotNull(mocker.getComponentUnderTest().createView(resourceReference, DEFAULT_VIEW_PARAMETERS)); |
285 |
|
} |
286 |
|
|
287 |
|
|
288 |
|
|
289 |
|
|
290 |
|
|
291 |
|
@throws |
292 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (11) |
Complexity: 1 |
Complexity Density: 0.09 |
1PASS
|
|
293 |
1 |
@Test... |
294 |
|
public void testViewANewVersionOfAnExistingOfficeAttachment() throws Exception |
295 |
|
{ |
296 |
1 |
AttachmentOfficeDocumentView officeDocumentView = |
297 |
|
new AttachmentOfficeDocumentView(ATTACHMENT_RESOURCE_REFERENCE, ATTACHMENT_REFERENCE, ATTACHMENT_VERSION, |
298 |
|
new XDOM(new ArrayList<Block>()), new HashSet<File>()); |
299 |
1 |
when(attachmentCache.get(CACHE_KEY)).thenReturn(officeDocumentView); |
300 |
|
|
301 |
1 |
when(documentAccessBridge.getAttachmentReferences(ATTACHMENT_REFERENCE.getDocumentReference())).thenReturn( |
302 |
|
Arrays.asList(ATTACHMENT_REFERENCE)); |
303 |
1 |
when(documentAccessBridge.getAttachmentVersion(ATTACHMENT_REFERENCE)).thenReturn("2.1"); |
304 |
|
|
305 |
1 |
ByteArrayInputStream attachmentContent = new ByteArrayInputStream(new byte[256]); |
306 |
1 |
when(documentAccessBridge.getAttachmentContent(ATTACHMENT_REFERENCE)).thenReturn(attachmentContent); |
307 |
|
|
308 |
1 |
XDOMOfficeDocument xdomOfficeDocument = |
309 |
|
new XDOMOfficeDocument(new XDOM(new ArrayList<Block>()), new HashMap<String, byte[]>(), mocker); |
310 |
1 |
when( |
311 |
|
officeDocumentBuilder.build(attachmentContent, ATTACHMENT_REFERENCE.getName(), |
312 |
|
ATTACHMENT_REFERENCE.getDocumentReference(), false)).thenReturn(xdomOfficeDocument); |
313 |
|
|
314 |
1 |
Assert.assertNotNull(mocker.getComponentUnderTest().createView(ATTACHMENT_RESOURCE_REFERENCE, |
315 |
|
DEFAULT_VIEW_PARAMETERS)); |
316 |
|
|
317 |
1 |
verify(attachmentCache).remove(CACHE_KEY); |
318 |
1 |
verify(attachmentCache).set(eq(CACHE_KEY), notNull(AttachmentOfficeDocumentView.class)); |
319 |
|
} |
320 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (31) |
Complexity: 1 |
Complexity Density: 0.03 |
1PASS
|
|
321 |
1 |
@Test... |
322 |
|
public void testViewPresentation() throws Exception |
323 |
|
{ |
324 |
1 |
AttachmentResourceReference attachResourceRef = |
325 |
|
new AttachmentResourceReference("xwiki:Some.Page@presentation.odp"); |
326 |
1 |
DocumentReference documentReference = new DocumentReference("wiki", "Some", "Page"); |
327 |
1 |
AttachmentReference attachmentReference = new AttachmentReference("presentation.odp", documentReference); |
328 |
|
|
329 |
1 |
AttachmentReferenceResolver<String> attachmentReferenceResolver = |
330 |
|
mocker.getInstance(AttachmentReferenceResolver.TYPE_STRING, "current"); |
331 |
1 |
when(attachmentReferenceResolver.resolve(attachResourceRef.getReference())).thenReturn(attachmentReference); |
332 |
|
|
333 |
1 |
when(documentAccessBridge.getAttachmentReferences(attachmentReference.getDocumentReference())).thenReturn( |
334 |
|
Arrays.asList(attachmentReference)); |
335 |
1 |
when(documentAccessBridge.getAttachmentVersion(attachmentReference)).thenReturn("3.2"); |
336 |
|
|
337 |
1 |
ByteArrayInputStream attachmentContent = new ByteArrayInputStream(new byte[256]); |
338 |
1 |
when(documentAccessBridge.getAttachmentContent(attachmentReference)).thenReturn(attachmentContent); |
339 |
|
|
340 |
1 |
ResourceReference imageReference = new ResourceReference("slide0.png", ResourceType.URL); |
341 |
1 |
ExpandedMacroBlock galleryMacro = |
342 |
|
new ExpandedMacroBlock("gallery", Collections.singletonMap("width", "300px"), null, false); |
343 |
1 |
galleryMacro.addChild(new ImageBlock(imageReference, true)); |
344 |
1 |
XDOM xdom = new XDOM(Collections.<Block>singletonList(galleryMacro)); |
345 |
|
|
346 |
1 |
Map<String, byte[]> artifacts = Collections.singletonMap("slide0.png", new byte[8]); |
347 |
1 |
XDOMOfficeDocument xdomOfficeDocument = new XDOMOfficeDocument(xdom, artifacts, mocker); |
348 |
|
|
349 |
1 |
PresentationBuilder presentationBuilder = mocker.getInstance(PresentationBuilder.class); |
350 |
1 |
when(presentationBuilder.build(attachmentContent, attachmentReference.getName(), documentReference)) |
351 |
|
.thenReturn(xdomOfficeDocument); |
352 |
|
|
353 |
1 |
Map<String, ?> viewParameters = Collections.singletonMap("ownerDocument", documentReference); |
354 |
1 |
TemporaryResourceReference temporaryResourceReference = new TemporaryResourceReference("officeviewer", |
355 |
|
Arrays.asList(String.valueOf(viewParameters.hashCode()), "slide0.png"), documentReference); |
356 |
|
|
357 |
1 |
Type type = new DefaultParameterizedType(null, ResourceReferenceSerializer.class, |
358 |
|
TemporaryResourceReference.class, ExtendedURL.class); |
359 |
1 |
ResourceReferenceSerializer<TemporaryResourceReference, ExtendedURL> urlTemporaryResourceReferenceSerializer = |
360 |
|
mocker.getInstance(type, "standard/tmp"); |
361 |
1 |
ExtendedURL extendedURL = new ExtendedURL(Arrays.asList("url", "to", "slide0.png")); |
362 |
1 |
when(urlTemporaryResourceReferenceSerializer.serialize(temporaryResourceReference)).thenReturn(extendedURL); |
363 |
|
|
364 |
1 |
XDOM output = this.mocker.getComponentUnderTest().createView(attachResourceRef, viewParameters); |
365 |
|
|
366 |
1 |
ImageBlock imageBlock = |
367 |
|
(ImageBlock) output.getBlocks(new ClassBlockMatcher(ImageBlock.class), Block.Axes.DESCENDANT).get(0); |
368 |
1 |
assertEquals("/url/to/slide0.png", imageBlock.getReference().getReference()); |
369 |
|
|
370 |
1 |
galleryMacro = (ExpandedMacroBlock) output |
371 |
|
.getBlocks(new ClassBlockMatcher(ExpandedMacroBlock.class), Block.Axes.DESCENDANT).get(0); |
372 |
1 |
assertFalse(galleryMacro.getParent() instanceof XDOM); |
373 |
1 |
assertEquals(Syntax.XWIKI_2_1, |
374 |
|
((MetaDataBlock) galleryMacro.getParent()).getMetaData().getMetaData(MetaData.SYNTAX)); |
375 |
|
|
376 |
1 |
TemporaryResourceStore store = mocker.getInstance(TemporaryResourceStore.class); |
377 |
1 |
verify(store).createTemporaryFile(eq(temporaryResourceReference), any(InputStream.class)); |
378 |
|
} |
379 |
|
} |