| 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.script; |
| 21 |
|
|
| 22 |
|
import static org.mockito.ArgumentMatchers.*; |
| 23 |
|
import static org.mockito.Mockito.*; |
| 24 |
|
|
| 25 |
|
import java.util.Arrays; |
| 26 |
|
import java.util.Collections; |
| 27 |
|
|
| 28 |
|
import org.junit.Assert; |
| 29 |
|
|
| 30 |
|
import org.artofsolving.jodconverter.document.DefaultDocumentFormatRegistry; |
| 31 |
|
import org.junit.Rule; |
| 32 |
|
import org.junit.Test; |
| 33 |
|
import org.xwiki.bridge.DocumentAccessBridge; |
| 34 |
|
import org.xwiki.bridge.DocumentModelBridge; |
| 35 |
|
import org.xwiki.context.Execution; |
| 36 |
|
import org.xwiki.context.ExecutionContext; |
| 37 |
|
import org.xwiki.model.reference.AttachmentReference; |
| 38 |
|
import org.xwiki.model.reference.DocumentReference; |
| 39 |
|
import org.xwiki.office.viewer.OfficeViewer; |
| 40 |
|
import org.xwiki.office.viewer.OfficeViewerScriptService; |
| 41 |
|
import org.xwiki.officeimporter.converter.OfficeConverter; |
| 42 |
|
import org.xwiki.officeimporter.server.OfficeServer; |
| 43 |
|
import org.xwiki.rendering.block.Block; |
| 44 |
|
import org.xwiki.rendering.block.XDOM; |
| 45 |
|
import org.xwiki.rendering.renderer.BlockRenderer; |
| 46 |
|
import org.xwiki.rendering.renderer.printer.WikiPrinter; |
| 47 |
|
import org.xwiki.rendering.syntax.Syntax; |
| 48 |
|
import org.xwiki.rendering.transformation.TransformationContext; |
| 49 |
|
import org.xwiki.rendering.transformation.TransformationManager; |
| 50 |
|
import org.xwiki.script.service.ScriptService; |
| 51 |
|
import org.xwiki.test.mockito.MockitoComponentMockingRule; |
| 52 |
|
|
| 53 |
|
|
| 54 |
|
@link |
| 55 |
|
|
| 56 |
|
@version |
| 57 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (35) |
Complexity: 3 |
Complexity Density: 0.09 |
|
| 58 |
|
public class DefaultOfficeViewerScriptServiceTest |
| 59 |
|
{ |
| 60 |
|
|
| 61 |
|
@link |
| 62 |
|
|
| 63 |
|
@Rule |
| 64 |
|
public MockitoComponentMockingRule<OfficeViewerScriptService> mocker = |
| 65 |
|
new MockitoComponentMockingRule<OfficeViewerScriptService>(DefaultOfficeViewerScriptService.class, |
| 66 |
|
ScriptService.class, "officeviewer"); |
| 67 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (8) |
Complexity: 1 |
Complexity Density: 0.12 |
1PASS
|
|
| 68 |
1 |
@Test... |
| 69 |
|
public void isMimeTypeSupported() throws Exception |
| 70 |
|
{ |
| 71 |
1 |
OfficeServer officeServer = mocker.getInstance(OfficeServer.class); |
| 72 |
1 |
OfficeConverter officeConverter = mock(OfficeConverter.class); |
| 73 |
1 |
when(officeServer.getConverter()).thenReturn(officeConverter); |
| 74 |
1 |
when(officeConverter.getFormatRegistry()).thenReturn(new DefaultDocumentFormatRegistry()); |
| 75 |
|
|
| 76 |
1 |
for (String mediaType : Arrays.asList("application/vnd.oasis.opendocument.text", "application/msword", |
| 77 |
|
"application/vnd.oasis.opendocument.presentation", "application/vnd.ms-powerpoint", |
| 78 |
|
"application/vnd.oasis.opendocument.spreadsheet", "application/vnd.ms-excel")) { |
| 79 |
6 |
Assert.assertTrue(mocker.getComponentUnderTest().isMimeTypeSupported(mediaType)); |
| 80 |
|
} |
| 81 |
1 |
for (String mediaType : Arrays.asList("foo/bar", "application/pdf")) { |
| 82 |
2 |
Assert.assertFalse(mocker.getComponentUnderTest().isMimeTypeSupported(mediaType)); |
| 83 |
|
} |
| 84 |
|
} |
| 85 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (16) |
Complexity: 1 |
Complexity Density: 0.06 |
1PASS
|
|
| 86 |
1 |
@Test... |
| 87 |
|
public void view() throws Exception |
| 88 |
|
{ |
| 89 |
1 |
Execution execution = mocker.getInstance(Execution.class); |
| 90 |
1 |
when(execution.getContext()).thenReturn(new ExecutionContext()); |
| 91 |
|
|
| 92 |
1 |
AttachmentReference attachmentReference = |
| 93 |
|
new AttachmentReference("file.odt", new DocumentReference("wiki", "Space", "Page")); |
| 94 |
1 |
DocumentModelBridge document = mock(DocumentModelBridge.class); |
| 95 |
1 |
DocumentAccessBridge documentAccessBridge = mocker.getInstance(DocumentAccessBridge.class); |
| 96 |
1 |
when(documentAccessBridge.isDocumentViewable(attachmentReference.getDocumentReference())).thenReturn(true); |
| 97 |
1 |
when(documentAccessBridge.getDocument(attachmentReference.getDocumentReference())).thenReturn(document); |
| 98 |
1 |
when(document.getSyntax()).thenReturn(Syntax.TEX_1_0); |
| 99 |
|
|
| 100 |
1 |
XDOM xdom = new XDOM(Collections.<Block> emptyList()); |
| 101 |
1 |
OfficeViewer viewer = mocker.getInstance(OfficeViewer.class); |
| 102 |
1 |
when(viewer.createView(attachmentReference, Collections.<String, String> emptyMap())).thenReturn(xdom); |
| 103 |
|
|
| 104 |
1 |
BlockRenderer xhtmlRenderer = mocker.registerMockComponent(BlockRenderer.class, "xhtml/1.0"); |
| 105 |
|
|
| 106 |
1 |
Assert.assertEquals("", mocker.getComponentUnderTest().view(attachmentReference)); |
| 107 |
|
|
| 108 |
1 |
TransformationManager transformationManager = mocker.getInstance(TransformationManager.class); |
| 109 |
1 |
verify(transformationManager).performTransformations(eq(xdom), notNull(TransformationContext.class)); |
| 110 |
|
|
| 111 |
1 |
verify(xhtmlRenderer).render(eq(xdom), notNull(WikiPrinter.class)); |
| 112 |
|
} |
| 113 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (8) |
Complexity: 1 |
Complexity Density: 0.12 |
1PASS
|
|
| 114 |
1 |
@Test... |
| 115 |
|
public void viewThrowingException() throws Exception |
| 116 |
|
{ |
| 117 |
1 |
ExecutionContext executionContext = new ExecutionContext(); |
| 118 |
1 |
executionContext.setProperty("officeView.caughtException", "before"); |
| 119 |
|
|
| 120 |
1 |
Execution execution = mocker.getInstance(Execution.class); |
| 121 |
1 |
when(execution.getContext()).thenReturn(executionContext); |
| 122 |
|
|
| 123 |
1 |
Assert.assertNull(mocker.getComponentUnderTest().view(null)); |
| 124 |
|
|
| 125 |
1 |
Exception e = mocker.getComponentUnderTest().getCaughtException(); |
| 126 |
1 |
Assert.assertTrue(e instanceof NullPointerException); |
| 127 |
|
|
| 128 |
1 |
verify(mocker.getMockedLogger()).error("Failed to view office document: null", e); |
| 129 |
|
} |
| 130 |
|
} |