1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.rendering.macro.useravatar; |
21 |
|
|
22 |
|
import java.lang.reflect.Type; |
23 |
|
|
24 |
|
import org.jmock.Expectations; |
25 |
|
import org.jmock.Mockery; |
26 |
|
import org.jmock.integration.junit4.JUnit4Mockery; |
27 |
|
import org.junit.runner.RunWith; |
28 |
|
import org.xwiki.bridge.DocumentAccessBridge; |
29 |
|
import org.xwiki.bridge.SkinAccessBridge; |
30 |
|
import org.xwiki.component.descriptor.ComponentDescriptor; |
31 |
|
import org.xwiki.component.descriptor.DefaultComponentDescriptor; |
32 |
|
import org.xwiki.component.manager.ComponentManager; |
33 |
|
import org.xwiki.component.util.ReflectionUtils; |
34 |
|
import org.xwiki.model.EntityType; |
35 |
|
import org.xwiki.model.reference.AttachmentReference; |
36 |
|
import org.xwiki.model.reference.DocumentReference; |
37 |
|
import org.xwiki.model.reference.DocumentReferenceResolver; |
38 |
|
import org.xwiki.model.reference.EntityReference; |
39 |
|
import org.xwiki.model.reference.EntityReferenceSerializer; |
40 |
|
import org.xwiki.model.reference.EntityReferenceValueProvider; |
41 |
|
import org.xwiki.rendering.test.integration.RenderingTestSuite; |
42 |
|
|
43 |
|
|
44 |
|
|
45 |
|
@link |
46 |
|
|
47 |
|
@version |
48 |
|
@since |
49 |
|
|
50 |
|
@RunWith(RenderingTestSuite.class) |
|
|
| 100% |
Uncovered Elements: 0 (87) |
Complexity: 10 |
Complexity Density: 0.13 |
|
51 |
|
public class IntegrationTests |
52 |
|
{ |
|
|
| 100% |
Uncovered Elements: 0 (17) |
Complexity: 1 |
Complexity Density: 0.06 |
|
53 |
5 |
@RenderingTestSuite.Initialized... |
54 |
|
public void initialize(ComponentManager componentManager) throws Exception |
55 |
|
{ |
56 |
5 |
Mockery mockery = new JUnit4Mockery(); |
57 |
|
|
58 |
|
|
59 |
5 |
final SkinAccessBridge mockSkinAccessBridge = |
60 |
|
registerMockComponent(componentManager, mockery, SkinAccessBridge.class); |
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
61 |
5 |
mockery.checking(new Expectations() {{... |
62 |
5 |
allowing(mockSkinAccessBridge).getSkinFile("icons/xwiki/noavatar.png"); |
63 |
5 |
will(returnValue("/xwiki/resources/icons/xwiki/noavatar.png")); |
64 |
|
}}); |
65 |
|
|
66 |
|
|
67 |
5 |
final DocumentReference adminUserReference = new DocumentReference("wiki", "XWiki", "Admin"); |
68 |
5 |
final DocumentReference userWithoutAvatarReference = |
69 |
|
new DocumentReference("wiki", "XWiki", "ExistingUserWithoutAvatar"); |
70 |
5 |
final DocumentReference userNotExistingReference = new DocumentReference("wiki", "XWiki", "UserNotExisting"); |
71 |
5 |
final DocumentReference userWithNonExistingAvatarFileReference = |
72 |
|
new DocumentReference("wiki", "XWiki", "UserWithNonExistingAvatarFile"); |
73 |
5 |
final DocumentReference userWithExceptionRetrievingAvatarFileReference = |
74 |
|
new DocumentReference("wiki", "XWiki", "UserWithExceptionRetrievingAvatarFile"); |
75 |
5 |
final DocumentReference userClassReference = new DocumentReference("wiki", "XWiki", "XWikiUsers"); |
76 |
5 |
final DocumentAccessBridge mockDocumentAccessBridge = |
77 |
|
registerMockComponent(componentManager, mockery, DocumentAccessBridge.class); |
|
|
| 100% |
Uncovered Elements: 0 (26) |
Complexity: 1 |
Complexity Density: 0.04 |
|
78 |
5 |
mockery.checking(new Expectations() {{... |
79 |
5 |
allowing(mockDocumentAccessBridge).exists(adminUserReference); will(returnValue(true)); |
80 |
5 |
allowing(mockDocumentAccessBridge).exists(userWithoutAvatarReference); will(returnValue(true)); |
81 |
5 |
allowing(mockDocumentAccessBridge).exists(with(any(String.class))); will(returnValue(false)); |
82 |
5 |
allowing(mockDocumentAccessBridge).exists(userNotExistingReference); will(returnValue(false)); |
83 |
5 |
allowing(mockDocumentAccessBridge).exists(userWithNonExistingAvatarFileReference); will(returnValue(true)); |
84 |
5 |
allowing(mockDocumentAccessBridge).exists(userWithExceptionRetrievingAvatarFileReference); will(returnValue(true)); |
85 |
|
|
86 |
5 |
allowing(mockDocumentAccessBridge).getProperty(adminUserReference, userClassReference, "avatar"); |
87 |
5 |
will(returnValue("mockAvatar.png")); |
88 |
5 |
allowing(mockDocumentAccessBridge).getProperty(userWithoutAvatarReference, userClassReference, |
89 |
5 |
"avatar"); will(returnValue(null)); |
90 |
5 |
allowing(mockDocumentAccessBridge).getProperty(userWithNonExistingAvatarFileReference, |
91 |
5 |
userClassReference, "avatar"); will(returnValue("mockAvatar.png")); |
92 |
5 |
allowing(mockDocumentAccessBridge).getProperty(userWithExceptionRetrievingAvatarFileReference, |
93 |
5 |
userClassReference, "avatar"); will(returnValue("mockAvatar.png")); |
94 |
|
|
95 |
5 |
allowing(mockDocumentAccessBridge).getAttachmentVersion(new AttachmentReference("mockAvatar.png", |
96 |
5 |
adminUserReference)); will(returnValue("1.1")); |
97 |
5 |
allowing(mockDocumentAccessBridge).getAttachmentVersion(new AttachmentReference("mockAvatar.png", |
98 |
5 |
userWithNonExistingAvatarFileReference)); will(returnValue(null)); |
99 |
5 |
allowing(mockDocumentAccessBridge).getAttachmentVersion(new AttachmentReference("mockAvatar.png", |
100 |
5 |
userWithExceptionRetrievingAvatarFileReference)); will(throwException(new Exception("Sum Ting Wong"))); |
101 |
|
}}); |
102 |
|
|
103 |
|
|
104 |
5 |
final DocumentReferenceResolver<String> mockDocumentReferenceResolver = |
105 |
|
registerMockComponent(componentManager, mockery, DocumentReferenceResolver.TYPE_STRING, "current"); |
|
|
| 100% |
Uncovered Elements: 0 (10) |
Complexity: 1 |
Complexity Density: 0.1 |
|
106 |
5 |
mockery.checking(new Expectations() {{... |
107 |
5 |
allowing(mockDocumentReferenceResolver).resolve("XWiki.Admin", |
108 |
|
new EntityReference("XWiki", EntityType.SPACE)); |
109 |
5 |
will(returnValue(adminUserReference)); |
110 |
5 |
allowing(mockDocumentReferenceResolver).resolve("XWiki.ExistingUserWithoutAvatar", |
111 |
|
new EntityReference("XWiki", EntityType.SPACE)); |
112 |
5 |
will(returnValue(userWithoutAvatarReference)); |
113 |
5 |
allowing(mockDocumentReferenceResolver).resolve("XWiki.UserNotExisting", |
114 |
|
new EntityReference("XWiki", EntityType.SPACE)); |
115 |
5 |
will(returnValue(userNotExistingReference)); |
116 |
5 |
allowing(mockDocumentReferenceResolver).resolve("XWiki.UserWithNonExistingAvatarFile", |
117 |
|
new EntityReference("XWiki", EntityType.SPACE)); |
118 |
5 |
will(returnValue(userWithNonExistingAvatarFileReference)); |
119 |
5 |
allowing(mockDocumentReferenceResolver).resolve("XWiki.UserWithExceptionRetrievingAvatarFile", |
120 |
|
new EntityReference("XWiki", EntityType.SPACE)); |
121 |
5 |
will(returnValue(userWithExceptionRetrievingAvatarFileReference)); |
122 |
|
}}); |
123 |
|
|
124 |
|
|
125 |
5 |
final EntityReferenceSerializer<String> mockEntityReferenceSerializer = |
126 |
|
registerMockComponent(componentManager, mockery, EntityReferenceSerializer.TYPE_STRING, "compactwiki"); |
|
|
| 100% |
Uncovered Elements: 0 (10) |
Complexity: 1 |
Complexity Density: 0.1 |
|
127 |
5 |
mockery.checking(new Expectations() {{... |
128 |
5 |
allowing(mockEntityReferenceSerializer).serialize( |
129 |
|
new AttachmentReference("mockAvatar.png", adminUserReference)); |
130 |
5 |
will(returnValue("XWiki.Admin@mockAvatar.png")); |
131 |
5 |
allowing(mockEntityReferenceSerializer).serialize(userNotExistingReference); |
132 |
5 |
will(returnValue("XWiki.UserNotExisting")); |
133 |
5 |
allowing(mockEntityReferenceSerializer).serialize( |
134 |
|
new AttachmentReference("mockAvatar.png", userWithNonExistingAvatarFileReference)); |
135 |
5 |
will(returnValue("XWiki.UserWithNonExistingAvatarFile@mockAvatar.png")); |
136 |
5 |
allowing(mockEntityReferenceSerializer).serialize( |
137 |
|
new AttachmentReference("mockAvatar.png", userWithExceptionRetrievingAvatarFileReference)); |
138 |
5 |
will(returnValue("XWiki.UserWithExceptionRetrievingAvatarFile@mockAvatar.png")); |
139 |
5 |
allowing(mockEntityReferenceSerializer).serialize(userWithExceptionRetrievingAvatarFileReference); |
140 |
5 |
will(returnValue("XWiki.UserWithExceptionRetrievingAvatarFile")); |
141 |
|
}}); |
142 |
|
|
143 |
|
|
144 |
5 |
final EntityReferenceValueProvider mockEntityReferenceValueProvider = |
145 |
|
registerMockComponent(componentManager, mockery, EntityReferenceValueProvider.class, "current"); |
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
146 |
5 |
mockery.checking(new Expectations() {{... |
147 |
5 |
allowing(mockEntityReferenceValueProvider).getDefaultValue(EntityType.WIKI); will(returnValue("wiki")); |
148 |
|
}}); |
149 |
|
} |
150 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
151 |
15 |
private static <T> T registerMockComponent(ComponentManager componentManager, Mockery mockery,... |
152 |
|
Type role, String hint) throws Exception |
153 |
|
{ |
154 |
15 |
DefaultComponentDescriptor<T> descriptor = createComponentDescriptor(role); |
155 |
15 |
descriptor.setRoleHint(hint); |
156 |
|
|
157 |
15 |
return registerMockComponent(componentManager, mockery, descriptor); |
158 |
|
} |
159 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
160 |
10 |
private static <T> T registerMockComponent(ComponentManager componentManager, Mockery mockery,... |
161 |
|
Type role) throws Exception |
162 |
|
{ |
163 |
10 |
return registerMockComponent(componentManager, mockery, IntegrationTests.<T> createComponentDescriptor(role)); |
164 |
|
} |
165 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
166 |
25 |
private static <T> T registerMockComponent(ComponentManager componentManager, Mockery mockery,... |
167 |
|
ComponentDescriptor<T> descriptor) throws Exception |
168 |
|
{ |
169 |
25 |
T mock = mockery.mock((Class<T>)ReflectionUtils.getTypeClass(descriptor.getRoleType())); |
170 |
25 |
componentManager.registerComponent(descriptor, mock); |
171 |
|
|
172 |
25 |
return mock; |
173 |
|
} |
174 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
175 |
25 |
private static <T> DefaultComponentDescriptor<T> createComponentDescriptor(Type role)... |
176 |
|
{ |
177 |
25 |
DefaultComponentDescriptor<T> descriptor = new DefaultComponentDescriptor<T>(); |
178 |
25 |
descriptor.setRoleType(role); |
179 |
|
|
180 |
25 |
return descriptor; |
181 |
|
} |
182 |
|
} |