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

File DefaultSheetManagerTest.java

 

Code metrics

0
76
8
1
257
145
8
0.11
9.5
8
1

Classes

Class Line # Actions
DefaultSheetManagerTest 51 76 0% 8 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.sheet.internal;
21   
22    import java.util.Arrays;
23    import java.util.Collections;
24   
25    import org.junit.Assert;
26   
27    import org.jmock.Expectations;
28    import org.jmock.Sequence;
29    import org.junit.Before;
30    import org.junit.Test;
31    import org.xwiki.bridge.DocumentAccessBridge;
32    import org.xwiki.bridge.DocumentModelBridge;
33    import org.xwiki.context.Execution;
34    import org.xwiki.context.ExecutionContext;
35    import org.xwiki.model.reference.DocumentReference;
36    import org.xwiki.model.reference.DocumentReferenceResolver;
37    import org.xwiki.sheet.SheetBinder;
38    import org.xwiki.sheet.SheetManager;
39    import org.xwiki.test.jmock.AbstractMockingComponentTestCase;
40    import org.xwiki.test.annotation.AllComponents;
41    import org.xwiki.test.jmock.annotation.MockingRequirement;
42   
43    /**
44    * Unit tests for {@link DefaultSheetManager}.
45    *
46    * @version $Id: ef1d0db960f2b6f529f30c511221e7af72373bcc $
47    * @since 4.2M1
48    */
49    @AllComponents
50    @MockingRequirement(value = DefaultSheetManager.class, exceptions = { DocumentReferenceResolver.class })
 
51    public class DefaultSheetManagerTest extends AbstractMockingComponentTestCase<SheetManager>
52    {
53    /**
54    * The name of the execution context sheet property.
55    */
56    private static final String SHEET_PROPERTY = "sheet";
57   
58    /**
59    * The action property of the sheet descriptor class. See {@link #SHEET_CLASS_REFERENCE}.
60    */
61    private static final String ACTION_PROPERTY = "action";
62   
63    /**
64    * The name of a wiki.
65    */
66    private static final String WIKI_NAME = "wiki";
67   
68    /**
69    * The sheet descriptor class reference.
70    */
71    private static final DocumentReference SHEET_CLASS_REFERENCE = new DocumentReference(WIKI_NAME, "XWiki",
72    "SheetDescriptorClass");
73   
74    /**
75    * The execution context.
76    */
77    private ExecutionContext context = new ExecutionContext();
78   
79    /**
80    * The component used to access the documents.
81    */
82    private DocumentAccessBridge documentAccessBridge;
83   
84    /**
85    * The component used to retrieve the custom document sheets.
86    */
87    private SheetBinder documentSheetBinder;
88   
89    /**
90    * The component used to retrieve the class sheets.
91    */
92    private SheetBinder classSheetBinder;
93   
94    /**
95    * The component used to access the old model.
96    */
97    private ModelBridge modelBridge;
98   
99    /**
100    * The document whose sheets are retrieved.
101    */
102    private DocumentModelBridge document;
103   
 
104  2 toggle @Before
105    public void configure() throws Exception
106    {
107  2 documentAccessBridge = getComponentManager().getInstance(DocumentAccessBridge.class);
108  2 documentSheetBinder = getComponentManager().getInstance(SheetBinder.class, "document");
109  2 classSheetBinder = getComponentManager().getInstance(SheetBinder.class, "class");
110  2 modelBridge = getComponentManager().getInstance(ModelBridge.class);
111  2 document = getMockery().mock(DocumentModelBridge.class);
112  2 final Execution execution = getComponentManager().getInstance(Execution.class);
113  2 getMockery().checking(new Expectations()
114    {
 
115  2 toggle {
116  2 allowing(execution).getContext();
117  2 will(returnValue(context));
118   
119  2 allowing(document).getDocumentReference();
120  2 will(returnValue(new DocumentReference(WIKI_NAME, "Space", "Page")));
121    }
122    });
123    }
124   
125    /**
126    * Tests that the sheet specified on the execution context overwrites the document and class sheets.
127    *
128    * @throws Exception if the test fails to lookup components
129    */
 
130  1 toggle @Test
131    public void testExecutionContextSheet() throws Exception
132    {
133    // (1) The sheet is specified on the execution context and the target document is the current document.
134  1 context.setProperty(SHEET_PROPERTY, "Code.Sheet");
135  1 final DocumentReference sheetReference = new DocumentReference(WIKI_NAME, "Code", "Sheet");
136   
137  1 getMockery().checking(new Expectations()
138    {
 
139  1 toggle {
140  1 oneOf(documentAccessBridge).getCurrentDocumentReference();
141  1 will(returnValue(document.getDocumentReference()));
142   
143  1 oneOf(documentAccessBridge).exists(sheetReference);
144  1 will(returnValue(true));
145   
146    // The specified sheet matches the current action.
147  1 oneOf(documentAccessBridge).getProperty(sheetReference, SHEET_CLASS_REFERENCE, ACTION_PROPERTY);
148  1 will(returnValue(""));
149    }
150    });
151   
152  1 Assert.assertEquals(Arrays.asList(sheetReference), getMockedComponent().getSheets(document, "view"));
153   
154    // (2) The sheet is specified on the execution context but the target document is not the current document.
155  1 getMockery().checking(new Expectations()
156    {
 
157  1 toggle {
158  1 oneOf(documentAccessBridge).getCurrentDocumentReference();
159  1 will(returnValue(null));
160   
161  1 oneOf(documentSheetBinder).getSheets(document);
162  1 will(returnValue(Collections.emptyList()));
163   
164  1 oneOf(modelBridge).getXObjectClassReferences(document);
165  1 will(returnValue(Collections.emptySet()));
166    }
167    });
168  1 Assert.assertTrue(getMockedComponent().getSheets(document, "edit").isEmpty());
169   
170    // (3) The sheet is not specified on the execution context.
171  1 context.removeProperty(SHEET_PROPERTY);
172   
173  1 getMockery().checking(new Expectations()
174    {
 
175  1 toggle {
176  1 oneOf(documentSheetBinder).getSheets(document);
177  1 will(returnValue(Collections.emptyList()));
178   
179  1 oneOf(modelBridge).getXObjectClassReferences(document);
180  1 will(returnValue(Collections.emptySet()));
181    }
182    });
183   
184  1 Assert.assertTrue(getMockedComponent().getSheets(document, "get").isEmpty());
185    }
186   
187    /**
188    * Tests the order in which sheets are determined: execution context, document sheets and finally class sheets.
189    *
190    * @throws Exception shouldn't happen, but some methods include "throws" in their signature
191    */
 
192  1 toggle @Test
193    public void testSheetResolutionSequence() throws Exception
194    {
195  1 final String contextSheetName = "ContextSheet";
196  1 context.setProperty(SHEET_PROPERTY, contextSheetName);
197  1 final DocumentReference documentSheetReference = new DocumentReference(WIKI_NAME, "ABC", "DocumentSheet");
198  1 final DocumentReference classSheetReference = new DocumentReference(WIKI_NAME, "BlogCode", "BlogPostSheet");
199  1 final DocumentReference classReference = new DocumentReference(WIKI_NAME, "Blog", "BlogPostClass");
200  1 final DocumentModelBridge classDocument = getMockery().mock(DocumentModelBridge.class, "xclass");
201  1 final String currentAction = "foo";
202  1 final Sequence sheetResolutionSequence = getMockery().sequence("sheetResolutionSequence");
203   
204  1 getMockery().checking(new Expectations()
205    {
 
206  1 toggle {
207    // (1) Look for the sheet specified in the execution context.
208  1 oneOf(documentAccessBridge).getCurrentDocumentReference();
209  1 inSequence(sheetResolutionSequence);
210  1 will(returnValue(document.getDocumentReference()));
211   
212    // The sheet is resolved relative to the target document.
213  1 oneOf(documentAccessBridge).exists(
214    new DocumentReference(document.getDocumentReference().getWikiReference().getName(), document
215    .getDocumentReference().getLastSpaceReference().getName(), contextSheetName));
216  1 inSequence(sheetResolutionSequence);
217  1 will(returnValue(false));
218   
219    // (2) Look for the custom document sheets.
220  1 oneOf(documentSheetBinder).getSheets(document);
221  1 inSequence(sheetResolutionSequence);
222  1 will(returnValue(Collections.singletonList(documentSheetReference)));
223   
224  1 oneOf(documentAccessBridge).exists(documentSheetReference);
225  1 inSequence(sheetResolutionSequence);
226  1 will(returnValue(true));
227   
228  1 oneOf(documentAccessBridge).getProperty(documentSheetReference, SHEET_CLASS_REFERENCE, ACTION_PROPERTY);
229  1 inSequence(sheetResolutionSequence);
230  1 will(returnValue("bar"));
231   
232    // (3) Look for the class sheets.
233  1 oneOf(modelBridge).getXObjectClassReferences(document);
234  1 inSequence(sheetResolutionSequence);
235  1 will(returnValue(Collections.singleton(classReference)));
236   
237  1 oneOf(documentAccessBridge).getTranslatedDocumentInstance(classReference);
238  1 inSequence(sheetResolutionSequence);
239  1 will(returnValue(classDocument));
240   
241  1 oneOf(classSheetBinder).getSheets(classDocument);
242  1 inSequence(sheetResolutionSequence);
243  1 will(returnValue(Collections.singletonList(classSheetReference)));
244   
245  1 oneOf(documentAccessBridge).exists(classSheetReference);
246  1 inSequence(sheetResolutionSequence);
247  1 will(returnValue(true));
248   
249  1 oneOf(documentAccessBridge).getProperty(classSheetReference, SHEET_CLASS_REFERENCE, ACTION_PROPERTY);
250  1 inSequence(sheetResolutionSequence);
251  1 will(returnValue(currentAction));
252    }
253    });
254   
255  1 getMockedComponent().getSheets(document, currentAction);
256    }
257    }