1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
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 |
|
@link |
56 |
|
|
57 |
|
@version |
58 |
|
|
|
|
| 87.9% |
Uncovered Elements: 4 (33) |
Complexity: 7 |
Complexity Density: 0.28 |
|
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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
72 |
8 |
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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (9) |
Complexity: 1 |
Complexity Density: 0.11 |
|
80 |
8 |
@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 |
|
{ |
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
88 |
7 |
@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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (13) |
Complexity: 2 |
Complexity Density: 0.18 |
|
108 |
12 |
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 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
130 |
0 |
protected void resetCache()... |
131 |
|
{ |
132 |
0 |
this.mockCache = mock(Cache.class); |
133 |
|
} |
134 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
135 |
0 |
protected void setCache(String property, Object value)... |
136 |
|
{ |
137 |
0 |
when(this.mockCache.get(property)).thenReturn(value); |
138 |
|
} |
139 |
|
} |