1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.office.viewer.script

File DefaultOfficeViewerScriptServiceTest.java

 

Code metrics

0
32
3
1
130
84
3
0.09
10.67
3
1

Classes

Class Line # Actions
DefaultOfficeViewerScriptServiceTest 58 32 0% 3 0
1.0100%
 

Contributing tests

This file is covered by 3 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 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    * Unit tests for {@link org.xwiki.office.viewer.script.DefaultOfficeViewerScriptService}.
55    *
56    * @version $Id: 9b7dbcf9f109c6174eb7dc6ee8d475fb0615eb66 $
57    */
 
58    public class DefaultOfficeViewerScriptServiceTest
59    {
60    /**
61    * A component manager that automatically mocks all dependencies of {@link org.xwiki.office.viewer.script.DefaultOfficeViewerScriptService}.
62    */
63    @Rule
64    public MockitoComponentMockingRule<OfficeViewerScriptService> mocker =
65    new MockitoComponentMockingRule<OfficeViewerScriptService>(DefaultOfficeViewerScriptService.class,
66    ScriptService.class, "officeviewer");
67   
 
68  1 toggle @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   
 
86  1 toggle @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   
 
114  1 toggle @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    }