1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package com.xpn.xwiki.internal.sheet

File ClassSheetBinderTest.java

 

Code metrics

0
18
2
1
97
50
2
0.11
9
2
1

Classes

Class Line # Actions
ClassSheetBinderTest 45 18 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 com.xpn.xwiki.internal.sheet;
21   
22    import static org.mockito.ArgumentMatchers.*;
23    import static org.mockito.Mockito.*;
24   
25    import java.util.ArrayList;
26    import java.util.Arrays;
27    import java.util.List;
28   
29    import org.junit.Assert;
30    import org.junit.Before;
31    import org.junit.Rule;
32    import org.junit.Test;
33    import org.xwiki.model.reference.DocumentReference;
34    import org.xwiki.model.reference.DocumentReferenceResolver;
35    import org.xwiki.query.Query;
36    import org.xwiki.query.QueryManager;
37    import org.xwiki.sheet.SheetBinder;
38    import org.xwiki.test.mockito.MockitoComponentMockingRule;
39   
40    /**
41    * Unit tests for {@link ClassSheetBinder}.
42    *
43    * @version $Id: 7d4c677b736823078a2c4a8d38049bdc1d66ff64 $
44    */
 
45    public class ClassSheetBinderTest
46    {
47    /**
48    * Mocks the dependencies of the component under test.
49    */
50    @Rule
51    public final MockitoComponentMockingRule<SheetBinder> mocker = new MockitoComponentMockingRule<SheetBinder>(
52    ClassSheetBinder.class);
53   
54    /**
55    * The query used to retrieve the list of all sheet bindings.
56    */
57    private Query sheetBindingsQuery;
58   
 
59  1 toggle @Before
60    public void setUp() throws Exception
61    {
62  1 sheetBindingsQuery = mock(Query.class);
63  1 QueryManager queryManager = mocker.getInstance(QueryManager.class);
64    // This is called when the component is initialized.
65  1 when(queryManager.createQuery(any(), any())).thenReturn(sheetBindingsQuery);
66    }
67   
68    /**
69    * Unit test for {@link ClassSheetBinder#getDocuments(DocumentReference)}.
70    */
 
71  1 toggle @Test
72    public void getDocuments() throws Exception
73    {
74  1 List<Object[]> queryResult = new ArrayList<Object[]>();
75  1 queryResult.add(new Object[] {"Alice", "AnotherSheet"});
76  1 queryResult.add(new Object[] {"Bob", "Sheet"});
77    // If several fields are selected then T=Object[].
78  1 when(sheetBindingsQuery.<Object[]> execute()).thenReturn(queryResult);
79   
80  1 DocumentReference sheetReference = new DocumentReference("wiki", "Space", "Sheet");
81  1 DocumentReferenceResolver<String> documentReferenceResolver =
82    mocker.getInstance(DocumentReferenceResolver.TYPE_STRING);
83   
84  1 DocumentReference aliceReference = new DocumentReference("wiki", "Users", "Alice");
85  1 DocumentReference anotherSheetReference = new DocumentReference("wiki", "Space", "AnotherSheet");
86  1 when(documentReferenceResolver.resolve("Alice", sheetReference)).thenReturn(aliceReference);
87  1 when(documentReferenceResolver.resolve("AnotherSheet", aliceReference)).thenReturn(anotherSheetReference);
88   
89  1 DocumentReference bobReference = new DocumentReference("wiki", "Users", "Bob");
90  1 when(documentReferenceResolver.resolve("Bob", sheetReference)).thenReturn(bobReference);
91  1 when(documentReferenceResolver.resolve("Sheet", bobReference)).thenReturn(sheetReference);
92   
93  1 Assert.assertEquals(Arrays.asList(bobReference), mocker.getComponentUnderTest().getDocuments(sheetReference));
94   
95  1 verify(sheetBindingsQuery).setWiki(sheetReference.getWikiReference().getName());
96    }
97    }