1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package com.xpn.xwiki.internal.sheet.scripting

File SheetScriptServiceTest.java

 

Code metrics

0
26
5
1
158
88
5
0.19
5.2
5
1

Classes

Class Line # Actions
SheetScriptServiceTest 57 26 0% 5 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 com.xpn.xwiki.internal.sheet.scripting;
21   
22    import java.util.Arrays;
23   
24    import org.junit.Assert;
25    import org.junit.Before;
26    import org.junit.Rule;
27    import org.junit.Test;
28    import org.mockito.ArgumentMatcher;
29    import org.xwiki.bridge.DocumentAccessBridge;
30    import org.xwiki.bridge.DocumentModelBridge;
31    import org.xwiki.configuration.ConfigurationSource;
32    import org.xwiki.model.reference.DocumentReference;
33    import org.xwiki.rendering.syntax.Syntax;
34    import org.xwiki.script.service.ScriptService;
35    import org.xwiki.sheet.SheetBinder;
36    import org.xwiki.sheet.SheetManager;
37    import org.xwiki.test.annotation.AfterComponent;
38    import org.xwiki.test.annotation.AllComponents;
39    import org.xwiki.test.mockito.MockitoComponentMockingRule;
40   
41    import com.xpn.xwiki.api.Document;
42    import com.xpn.xwiki.doc.XWikiDocument;
43    import com.xpn.xwiki.script.sheet.SheetScriptService;
44    import com.xpn.xwiki.web.Utils;
45   
46    import static org.mockito.ArgumentMatchers.argThat;
47    import static org.mockito.ArgumentMatchers.same;
48    import static org.mockito.Mockito.when;
49   
50    /**
51    * Unit tests for {@link com.xpn.xwiki.script.sheet.SheetScriptService}.
52    *
53    * @version $Id: 0f31d9f9c9d5f84b18a973cf262da653223b7d1c $
54    */
55    // We need to register all components because we use XWikiDocument.
56    @AllComponents
 
57    public class SheetScriptServiceTest
58    {
59    /**
60    * Mocks the dependencies of the component under test.
61    */
62    @Rule
63    public final MockitoComponentMockingRule<ScriptService> mocker =
64    new MockitoComponentMockingRule<ScriptService>(SheetScriptService.class);
65   
66    /**
67    * The script service being tested.
68    */
69    private SheetScriptService sheetScriptService;
70   
71    private SheetBinder mockClassSheetBinder;
72   
73    private SheetBinder mockDocumentSheetBinder;
74   
75    private DocumentAccessBridge mockDocumentAccessBridge;
76   
77    /**
78    * Test setup.
79    *
80    * @throws Exception if test setup fails
81    */
 
82  2 toggle @Before
83    public void setUp() throws Exception
84    {
85    // Required in order to create a new instance of XWikiDocument.
86  2 Utils.setComponentManager(mocker);
87  2 this.sheetScriptService = (SheetScriptService) mocker.getComponentUnderTest();
88    }
89   
 
90  2 toggle @AfterComponent
91    public void afterComponent() throws Exception
92    {
93    // Because of @AllComponents all component are injected (for XWikiDocument) while in this case we would like
94    // SheetScriptService to be isolated
95   
96  2 this.mockClassSheetBinder = this.mocker.registerMockComponent(SheetBinder.class, "class");
97  2 this.mockDocumentSheetBinder = this.mocker.registerMockComponent(SheetBinder.class, "document");
98  2 this.mockDocumentAccessBridge = this.mocker.registerMockComponent(DocumentAccessBridge.class);
99   
100  2 this.mocker.registerMockComponent(SheetManager.class);
101  2 this.mocker.registerMockComponent(ConfigurationSource.class, "all");
102  2 this.mocker.registerMockComponent(ConfigurationSource.class, "xwikiproperties");
103    }
104   
105    /**
106    * Tests that {@link SheetScriptService#bindClassSheet(Document, DocumentReference)} clones the document first to
107    * follow the practice from {@link Document}. This is required in order to not modify the cached document.
108    *
109    * @throws Exception if the test fails to lookup the class sheet binder
110    */
 
111  1 toggle @Test
112    public void bindClassSheetClonesDocument() throws Exception
113    {
114  1 DocumentReference classReference = new DocumentReference("wiki", "Space", "MyClass");
115  1 final XWikiDocument classDocument = new XWikiDocument(classReference);
116  1 classDocument.setSyntax(Syntax.PLAIN_1_0);
117  1 Document classDocumentApi = new Document(classDocument, null);
118   
119  1 final DocumentReference sheetReference =
120    new DocumentReference("MySheet", classReference.getLastSpaceReference());
121   
122  1 when(this.mockClassSheetBinder.bind(argThat(new ArgumentMatcher<DocumentModelBridge>()
123    {
 
124  1 toggle @Override
125    public boolean matches(DocumentModelBridge argument)
126    {
127  1 return classDocument.equals(argument) && classDocument != argument;
128    }
129    }), same(sheetReference))).thenReturn(true);
130   
131  1 Assert.assertTrue(this.sheetScriptService.bindClassSheet(classDocumentApi, sheetReference));
132    }
133   
134    /**
135    * Unit test for {@link SheetScriptService#getDocuments(DocumentReference)}.
136    */
 
137  1 toggle @Test
138    public void getDocuments() throws Exception
139    {
140  1 DocumentReference sheetReference = new DocumentReference("wiki", "Space", "Sheet");
141  1 DocumentReference publicDocumentReference = new DocumentReference("wiki", "Space", "PublicPage");
142  1 DocumentReference privateDocumentReference = new DocumentReference("wiki", "Space", "PrivatePage");
143  1 DocumentReference publicClassReference = new DocumentReference("wiki", "Space", "PublicClass");
144  1 DocumentReference privateClassReference = new DocumentReference("wiki", "Space", "PrivateClass");
145   
146  1 when(this.mockDocumentSheetBinder.getDocuments(sheetReference))
147    .thenReturn(Arrays.asList(publicDocumentReference, privateDocumentReference));
148   
149  1 when(this.mockClassSheetBinder.getDocuments(sheetReference))
150    .thenReturn(Arrays.asList(privateClassReference, publicClassReference));
151   
152  1 when(this.mockDocumentAccessBridge.isDocumentViewable(publicClassReference)).thenReturn(true);
153  1 when(this.mockDocumentAccessBridge.isDocumentViewable(publicDocumentReference)).thenReturn(true);
154   
155  1 Assert.assertEquals(Arrays.asList(publicDocumentReference, publicClassReference),
156    this.sheetScriptService.getDocuments(sheetReference));
157    }
158    }