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

File DocumentTitleDisplayerTest.java

 

Code metrics

0
40
3
1
144
94
3
0.08
13.33
3
1

Classes

Class Line # Actions
DocumentTitleDisplayerTest 63 40 0% 3 0
1.0100%
 

Contributing tests

This file is covered by 2 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.display.internal;
21   
22    import java.io.Reader;
23    import java.io.StringReader;
24    import java.util.Arrays;
25    import java.util.HashMap;
26   
27    import org.apache.commons.io.IOUtils;
28    import org.junit.Before;
29    import org.junit.Rule;
30    import org.junit.Test;
31    import org.mockito.ArgumentCaptor;
32    import org.mockito.Mockito;
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.EntityType;
38    import org.xwiki.model.ModelContext;
39    import org.xwiki.model.reference.DocumentReference;
40    import org.xwiki.model.reference.EntityReference;
41    import org.xwiki.model.reference.EntityReferenceProvider;
42    import org.xwiki.model.reference.WikiReference;
43    import org.xwiki.rendering.block.WordBlock;
44    import org.xwiki.rendering.block.XDOM;
45    import org.xwiki.rendering.parser.Parser;
46    import org.xwiki.security.authorization.AuthorizationManager;
47    import org.xwiki.security.authorization.Right;
48    import org.xwiki.test.mockito.MockitoComponentMockingRule;
49   
50    import static org.junit.Assert.assertEquals;
51    import static org.junit.Assert.assertSame;
52    import static org.mockito.ArgumentMatchers.any;
53    import static org.mockito.ArgumentMatchers.eq;
54    import static org.mockito.Mockito.mock;
55    import static org.mockito.Mockito.verify;
56    import static org.mockito.Mockito.when;
57   
58    /**
59    * Unit tests for {@link DocumentTitleDisplayer}.
60    *
61    * @version $Id: 46a55546ea4f5dba389e1d2e872ddaf07c8d3cdb $
62    */
 
63    public class DocumentTitleDisplayerTest
64    {
65    @Rule
66    public final MockitoComponentMockingRule<DocumentDisplayer> mocker =
67    new MockitoComponentMockingRule<DocumentDisplayer>(DocumentTitleDisplayer.class);
68   
 
69  2 toggle @Before
70    public void configure() throws Exception
71    {
72    // The execution context is expected to have the "xwikicontext" property set.
73  2 Execution mockExecution = this.mocker.getInstance(Execution.class);
74  2 ExecutionContext executionContext = new ExecutionContext();
75  2 executionContext.setProperty("xwikicontext", new HashMap<String, Object>());
76  2 Mockito.when(mockExecution.getContext()).thenReturn(executionContext);
77    }
78   
 
79  1 toggle @Test
80    public void fallbackOnSpaceNameWhenSpaceHomePageTitleIsEmpty() throws Exception
81    {
82  1 EntityReferenceProvider defaultEntityReferenceProvider = this.mocker.getInstance(EntityReferenceProvider.class);
83  1 when(defaultEntityReferenceProvider.getDefaultReference(EntityType.DOCUMENT)).thenReturn(
84    new EntityReference("Page", EntityType.DOCUMENT));
85   
86  1 DocumentModelBridge document = mock(DocumentModelBridge.class);
87  1 when(document.getDocumentReference()).thenReturn(new DocumentReference("wiki", Arrays.asList("Space"), "Page"));
88   
89  1 XDOM titleXDOM = new XDOM(Arrays.asList(new WordBlock("Space")));
90   
91  1 Parser plainTextParser = this.mocker.getInstance(Parser.class, "plain/1.0");
92  1 when(plainTextParser.parse(any(StringReader.class))).thenReturn(titleXDOM);
93   
94  1 DocumentDisplayerParameters params = new DocumentDisplayerParameters();
95  1 params.setTitleDisplayed(true);
96   
97  1 assertSame(titleXDOM, this.mocker.getComponentUnderTest().display(document, params));
98   
99  1 ArgumentCaptor<Reader> argument = ArgumentCaptor.forClass(Reader.class);
100  1 verify(plainTextParser).parse(argument.capture());
101  1 assertEquals("Space", IOUtils.toString(argument.getValue()));
102    }
103   
 
104  1 toggle @Test
105    public void whenSettingTheContextDocumentTheContextWikiIsAlsoSet() throws Exception
106    {
107  1 EntityReferenceProvider defaultEntityReferenceProvider = this.mocker.getInstance(EntityReferenceProvider.class);
108  1 when(defaultEntityReferenceProvider.getDefaultReference(EntityType.DOCUMENT)).thenReturn(
109    new EntityReference("Page", EntityType.DOCUMENT));
110   
111  1 DocumentModelBridge document = mock(DocumentModelBridge.class);
112  1 DocumentReference documentReference = new DocumentReference("wiki", Arrays.asList("Space"), "Page");
113  1 when(document.getDocumentReference()).thenReturn(documentReference);
114  1 when(document.getTitle()).thenReturn("title");
115   
116  1 XDOM titleXDOM = new XDOM(Arrays.asList(new WordBlock("title")));
117   
118  1 Parser plainTextParser = this.mocker.getInstance(Parser.class, "plain/1.0");
119  1 when(plainTextParser.parse(any(StringReader.class))).thenReturn(titleXDOM);
120   
121  1 ModelContext modelContext = this.mocker.getInstance(ModelContext.class);
122  1 WikiReference currentWikiReference = new WikiReference("currentWiki");
123  1 when(modelContext.getCurrentEntityReference()).thenReturn(currentWikiReference);
124   
125  1 AuthorizationManager authorizationManager = this.mocker.getInstance(AuthorizationManager.class);
126  1 when(authorizationManager.hasAccess(eq(Right.SCRIPT), any(), any())).thenReturn(true);
127   
128  1 DocumentAccessBridge dab = this.mocker.getInstance(DocumentAccessBridge.class);
129   
130  1 DocumentDisplayerParameters params = new DocumentDisplayerParameters();
131  1 params.setTitleDisplayed(true);
132  1 params.setExecutionContextIsolated(true);
133   
134  1 this.mocker.getComponentUnderTest().display(document, params);
135   
136    // Check that the context is set.
137  1 verify(dab).pushDocumentInContext(any(), eq(documentReference));
138  1 verify(modelContext).setCurrentEntityReference(documentReference.getWikiReference());
139   
140    // Check that the context is restored.
141  1 verify(dab).popDocumentFromContext(any());
142  1 verify(modelContext).setCurrentEntityReference(currentWikiReference);
143    }
144    }