1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package org.xwiki.office.viewer.script

File DefaultOfficeViewerScriptServiceTest.java

 

Code metrics

0
32
3
1
131
85
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.jodconverter.document.DefaultDocumentFormatRegistry;
29    import org.junit.Assert;
30   
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: 9f8a03aeaed48cc7cb4aeef49d837a3af24f38f1 $
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(DefaultDocumentFormatRegistry.getInstance());
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.getTranslatedDocumentInstance(attachmentReference.getDocumentReference()))
98    .thenReturn(document);
99  1 when(document.getSyntax()).thenReturn(Syntax.TEX_1_0);
100   
101  1 XDOM xdom = new XDOM(Collections.<Block> emptyList());
102  1 OfficeViewer viewer = mocker.getInstance(OfficeViewer.class);
103  1 when(viewer.createView(attachmentReference, Collections.<String, String> emptyMap())).thenReturn(xdom);
104   
105  1 BlockRenderer xhtmlRenderer = mocker.registerMockComponent(BlockRenderer.class, "xhtml/1.0");
106   
107  1 Assert.assertEquals("", mocker.getComponentUnderTest().view(attachmentReference));
108   
109  1 TransformationManager transformationManager = mocker.getInstance(TransformationManager.class);
110  1 verify(transformationManager).performTransformations(eq(xdom), notNull(TransformationContext.class));
111   
112  1 verify(xhtmlRenderer).render(eq(xdom), notNull(WikiPrinter.class));
113    }
114   
 
115  1 toggle @Test
116    public void viewThrowingException() throws Exception
117    {
118  1 ExecutionContext executionContext = new ExecutionContext();
119  1 executionContext.setProperty("officeView.caughtException", "before");
120   
121  1 Execution execution = mocker.getInstance(Execution.class);
122  1 when(execution.getContext()).thenReturn(executionContext);
123   
124  1 Assert.assertNull(mocker.getComponentUnderTest().view(null));
125   
126  1 Exception e = mocker.getComponentUnderTest().getCaughtException();
127  1 Assert.assertTrue(e instanceof NullPointerException);
128   
129  1 verify(mocker.getMockedLogger()).error("Failed to view office document: null", e);
130    }
131    }