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

File DefaultModelBridgeTest.java

 

Code metrics

0
28
5
1
125
78
5
0.18
5.6
5
1

Classes

Class Line # Actions
DefaultModelBridgeTest 56 28 0% 5 0
1.0100%
 

Contributing tests

This file is covered by 3 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.eventstream.internal;
21   
22    import java.util.Arrays;
23    import java.util.Collections;
24    import java.util.HashMap;
25    import java.util.List;
26    import java.util.Map;
27   
28    import javax.inject.Provider;
29   
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.model.reference.EntityReference;
36    import org.xwiki.model.reference.LocalDocumentReference;
37    import org.xwiki.test.mockito.MockitoComponentMockingRule;
38   
39    import com.xpn.xwiki.XWiki;
40    import com.xpn.xwiki.XWikiContext;
41    import com.xpn.xwiki.doc.XWikiDocument;
42    import com.xpn.xwiki.objects.BaseObject;
43   
44    import static org.junit.Assert.assertEquals;
45    import static org.junit.Assert.assertTrue;
46    import static org.mockito.ArgumentMatchers.eq;
47    import static org.mockito.Mockito.mock;
48    import static org.mockito.Mockito.when;
49   
50    /**
51    * Unit tests for {@link DefaultModelBridge}.
52    *
53    * @version $Id: 3a825f1fefb565defc59673e3c0d05c381b94d12 $
54    * @since 9.6RC1
55    */
 
56    public class DefaultModelBridgeTest
57    {
58    @Rule
59    public final MockitoComponentMockingRule<ModelBridge> mocker =
60    new MockitoComponentMockingRule<>(DefaultModelBridge.class);
61   
62    private Provider<XWikiContext> contextProvider;
63   
64    private DocumentReferenceResolver<String> documentReferenceResolver;
65   
 
66  3 toggle @Before
67    public void setUp() throws Exception
68    {
69  3 this.contextProvider = this.mocker.getInstance(XWikiContext.TYPE_PROVIDER);
70   
71  3 this.documentReferenceResolver = this.mocker.getInstance(DocumentReferenceResolver.TYPE_STRING);
72    }
73   
 
74  1 toggle @Test
75    public void testAuthorReference() throws Exception
76    {
77  1 XWikiContext context = mock(XWikiContext.class);
78  1 XWiki xwiki = mock(XWiki.class);
79  1 XWikiDocument document = mock(XWikiDocument.class);
80  1 DocumentReference authorReference = mock(DocumentReference.class);
81  1 EntityReference entityReference = mock(EntityReference.class);
82   
83  1 when(this.contextProvider.get()).thenReturn(context);
84  1 when(context.getWiki()).thenReturn(xwiki);
85  1 when(xwiki.getDocument(entityReference, context)).thenReturn(document);
86  1 when(document.getAuthorReference()).thenReturn(authorReference);
87   
88  1 DocumentReference result = this.mocker.getComponentUnderTest().getAuthorReference(entityReference);
89   
90  1 assertEquals(authorReference, result);
91    }
92   
 
93  1 toggle @Test
94    public void testCheckXObjectPresenceWithEmptyList() throws Exception
95    {
96  1 Object object = new Object();
97  1 List<String> xObjectTypes = Collections.EMPTY_LIST;
98   
99  1 assertTrue(this.mocker.getComponentUnderTest().checkXObjectPresence(xObjectTypes, object));
100    }
101   
 
102  1 toggle @Test
103    public void testCheckXObjectPresenceWithPresentXObject() throws Exception
104    {
105  1 XWikiDocument document = mock(XWikiDocument.class);
106  1 List<String> xObjectTypes = Arrays.asList("type1");
107   
108  1 DocumentReference documentReferenceFromDocumentXObjects = mock(DocumentReference.class);
109  1 Map<DocumentReference, List<BaseObject>> documentXObjects =
 
110  1 toggle new HashMap<DocumentReference, List<BaseObject>>() {{
111  1 put(documentReferenceFromDocumentXObjects, Collections.EMPTY_LIST);
112    }};
113   
114  1 DocumentReference resolvedType1 = mock(DocumentReference.class);
115  1 LocalDocumentReference localDocumentReferenceType1 = mock(LocalDocumentReference.class);
116   
117  1 when(resolvedType1.getLocalDocumentReference()).thenReturn(localDocumentReferenceType1);
118  1 when(this.documentReferenceResolver.resolve(eq("type1"))).thenReturn(resolvedType1);
119  1 when(document.getXObjects()).thenReturn(documentXObjects);
120   
121  1 when(documentReferenceFromDocumentXObjects.getLocalDocumentReference()).thenReturn(localDocumentReferenceType1);
122   
123  1 assertTrue(this.mocker.getComponentUnderTest().checkXObjectPresence(xObjectTypes, document));
124    }
125    }