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

File AbstractTestDocumentConfigurationSource.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart9.png
38% of files have more coverage

Code metrics

2
25
6
1
139
90
7
0.28
4.17
6
1.17

Classes

Class Line # Actions
AbstractTestDocumentConfigurationSource 59 25 0% 7 4
0.878787987.9%
 

Contributing tests

This file is covered by 8 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.configuration.internal.test;
21   
22    import static org.mockito.ArgumentMatchers.any;
23    import static org.mockito.ArgumentMatchers.anyObject;
24    import static org.mockito.ArgumentMatchers.eq;
25    import static org.mockito.Mockito.mock;
26    import static org.mockito.Mockito.when;
27   
28    import java.lang.reflect.Type;
29   
30    import org.junit.Before;
31    import org.junit.Rule;
32    import org.mockito.invocation.InvocationOnMock;
33    import org.mockito.stubbing.Answer;
34    import org.xwiki.cache.Cache;
35    import org.xwiki.cache.CacheManager;
36    import org.xwiki.cache.config.CacheConfiguration;
37    import org.xwiki.configuration.ConfigurationSource;
38    import org.xwiki.configuration.internal.AbstractDocumentConfigurationSource;
39    import org.xwiki.model.reference.DocumentReference;
40    import org.xwiki.model.reference.DocumentReferenceResolver;
41    import org.xwiki.model.reference.EntityReference;
42    import org.xwiki.model.reference.LocalDocumentReference;
43    import org.xwiki.model.reference.WikiReference;
44    import org.xwiki.properties.ConverterManager;
45    import org.xwiki.test.mockito.MockitoComponentMockingRule;
46    import org.xwiki.wiki.descriptor.WikiDescriptorManager;
47   
48    import com.xpn.xwiki.XWikiContext;
49    import com.xpn.xwiki.XWikiException;
50    import com.xpn.xwiki.doc.XWikiDocument;
51    import com.xpn.xwiki.objects.BaseObject;
52    import com.xpn.xwiki.test.MockitoOldcoreRule;
53   
54    /**
55    * Utility to test all extensions of {@link AbstractDocumentConfigurationSource}.
56    *
57    * @version $Id: d6c1332e1f4bc5efdcd53c70edbeaacfe965e424 $
58    */
 
59    public abstract class AbstractTestDocumentConfigurationSource
60    {
61    @Rule
62    public MockitoOldcoreRule oldcore;
63   
64    public MockitoComponentMockingRule<ConfigurationSource> componentManager;
65   
66    protected static final String CURRENT_WIKI = "currentwiki";
67   
68    protected Cache<Object> mockCache;
69   
70    protected ConverterManager mockConverter;
71   
 
72  8 toggle public AbstractTestDocumentConfigurationSource(Class<? extends ConfigurationSource> clazz)
73    {
74  8 this.componentManager = new MockitoComponentMockingRule<ConfigurationSource>(clazz);
75  8 this.oldcore = new MockitoOldcoreRule(this.componentManager);
76    }
77   
78    protected abstract LocalDocumentReference getClassReference();
79   
 
80  8 toggle @Before
81    public void before() throws Exception
82    {
83  8 this.mockCache = mock(Cache.class);
84  8 this.mockConverter = this.componentManager.getInstance(ConverterManager.class);
85   
86  8 when(this.mockConverter.convert(any(Type.class), anyObject())).then(new Answer<Object>()
87    {
 
88  7 toggle @Override
89    public Object answer(InvocationOnMock invocation) throws Throwable
90    {
91  7 return invocation.getArguments()[1];
92    }
93    });
94   
95  8 CacheManager cacheManager = this.componentManager.getInstance(CacheManager.class);
96   
97  8 when(cacheManager.createNewCache(any(CacheConfiguration.class))).thenReturn(this.mockCache);
98   
99  8 WikiDescriptorManager wikiManager = this.componentManager.getInstance(WikiDescriptorManager.class);
100  8 when(wikiManager.getCurrentWikiId()).thenReturn(CURRENT_WIKI);
101   
102  8 DocumentReferenceResolver<EntityReference> mockCurrentEntityResolver =
103    this.componentManager.registerMockComponent(DocumentReferenceResolver.TYPE_REFERENCE, "current");
104  8 when(mockCurrentEntityResolver.resolve(eq(getClassReference()), any(DocumentReference.class))).thenReturn(
105    new DocumentReference(getClassReference(), new WikiReference(CURRENT_WIKI)));
106    }
107   
 
108  12 toggle protected void setStringProperty(DocumentReference documentReference, String propertyName, String propertyValue)
109    throws XWikiException
110    {
111  12 XWikiContext xcontext = this.oldcore.getXWikiContext();
112   
113  12 XWikiDocument document = this.oldcore.getXWikiContext().getWiki().getDocument(documentReference, xcontext);
114   
115  12 LocalDocumentReference classReference = getClassReference();
116   
117  12 BaseObject baseOject = document.getXObject(classReference);
118  12 if (baseOject == null) {
119  8 baseOject = new BaseObject();
120  8 baseOject.setDocumentReference(documentReference);
121  8 baseOject.setXClassReference(classReference);
122  8 document.addXObject(baseOject);
123    }
124   
125  12 baseOject.setStringValue(propertyName, propertyValue);
126   
127  12 xcontext.getWiki().saveDocument(document, xcontext);
128    }
129   
 
130  0 toggle protected void resetCache()
131    {
132  0 this.mockCache = mock(Cache.class);
133    }
134   
 
135  0 toggle protected void setCache(String property, Object value)
136    {
137  0 when(this.mockCache.get(property)).thenReturn(value);
138    }
139    }