1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.display.internal

File DocumentContentDisplayerTest.java

 

Code metrics

0
19
2
1
102
59
2
0.11
9.5
2
1

Classes

Class Line # Actions
DocumentContentDisplayerTest 50 19 0% 2 0
1.0100%
 

Contributing tests

This file is covered by 1 test. .

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.display.internal;
21   
22    import java.util.Collections;
23    import java.util.HashMap;
24   
25    import org.junit.Assert;
26   
27    import org.junit.Rule;
28    import org.junit.Test;
29    import org.mockito.Mockito;
30    import org.mockito.invocation.InvocationOnMock;
31    import org.mockito.stubbing.Answer;
32    import org.xwiki.bridge.DocumentAccessBridge;
33    import org.xwiki.bridge.DocumentModelBridge;
34    import org.xwiki.context.Execution;
35    import org.xwiki.context.ExecutionContext;
36    import org.xwiki.model.reference.DocumentReference;
37    import org.xwiki.model.reference.EntityReferenceSerializer;
38    import org.xwiki.rendering.block.Block;
39    import org.xwiki.rendering.block.XDOM;
40    import org.xwiki.rendering.listener.MetaData;
41    import org.xwiki.rendering.transformation.TransformationContext;
42    import org.xwiki.rendering.transformation.TransformationManager;
43    import org.xwiki.test.mockito.MockitoComponentMockingRule;
44   
45    /**
46    * Unit tests for {@link DocumentContentDisplayer}.
47    *
48    * @version $Id: ffd58843bd97bdb917bcca4e3239c3091574fd8a $
49    */
 
50    public class DocumentContentDisplayerTest
51    {
52    @Rule
53    public final MockitoComponentMockingRule<DocumentDisplayer> mocker =
54    new MockitoComponentMockingRule<DocumentDisplayer>(DocumentContentDisplayer.class);
55   
 
56  1 toggle @Test
57    public void testBaseMetaDataIsSetBeforeExecutingTransformations() throws Exception
58    {
59    // The execution context is expected to have the "xwikicontext" property set.
60  1 Execution mockExecution = mocker.getInstance(Execution.class);
61  1 ExecutionContext executionContext = new ExecutionContext();
62  1 executionContext.setProperty("xwikicontext", new HashMap<String, Object>());
63  1 Mockito.when(mockExecution.getContext()).thenReturn(executionContext);
64   
65    // The document being displayed.
66  1 DocumentModelBridge mockDocument = Mockito.mock(DocumentModelBridge.class);
67  1 XDOM content = new XDOM(Collections.<Block> emptyList());
68  1 Mockito.when(mockDocument.getXDOM()).thenReturn(content);
69   
70    // The reference of the current document musts be set as the value of the BASE meta data.
71  1 DocumentReference currentDocRef = new DocumentReference("wiki", "Space", "Page");
72  1 DocumentAccessBridge mockDocumentAccessBridge = mocker.getInstance(DocumentAccessBridge.class);
73  1 Mockito.when(mockDocumentAccessBridge.getCurrentDocumentReference()).thenReturn(currentDocRef);
74   
75  1 EntityReferenceSerializer<String> serializer = mocker.getInstance(EntityReferenceSerializer.TYPE_STRING);
76  1 Mockito.when(serializer.serialize(currentDocRef)).thenReturn("foo");
77   
78    // We can't verify the meta data after the display method is called because we want to make sure the BASE meta
79    // data is correctly set before XDOM transformations are executed, not after.
80  1 TransformationManager mockTransformationManager = mocker.getInstance(TransformationManager.class);
81  1 Mockito.doAnswer(new Answer<Void>()
82    {
 
83  1 toggle @Override
84    public Void answer(InvocationOnMock invocation) throws Throwable
85    {
86  1 XDOM xdom = (XDOM) invocation.getArguments()[0];
87    // We have to assert the meta data before the transformations are executed not at the end!
88  1 Assert.assertEquals("foo", xdom.getMetaData().getMetaData(MetaData.BASE));
89  1 return null;
90    }
91    }).when(mockTransformationManager)
92    .performTransformations(Mockito.any(XDOM.class), Mockito.any(TransformationContext.class));
93   
94    // Execute the display.
95  1 Assert.assertSame(content,
96    mocker.getComponentUnderTest().display(mockDocument, new DocumentDisplayerParameters()));
97   
98    // Make sure the transformations are executed exactly once, and on the right content.
99  1 Mockito.verify(mockTransformationManager, Mockito.times(1)).performTransformations(Mockito.same(content),
100    Mockito.any(TransformationContext.class));
101    }
102    }