1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.rendering.macro.useravatar

File IntegrationTests.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart10.png
0% of files have more coverage

Code metrics

0
77
10
1
182
134
10
0.13
7.7
10
1

Classes

Class Line # Actions
IntegrationTests 51 77 0% 10 0
1.0100%
 

Contributing tests

No tests hitting this source file were found.

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.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    * Run all tests found in {@code *.test} files located in the classpath. These {@code *.test} files must follow the
45    * conventions described in {@link org.xwiki.rendering.test.integration.TestDataParser}.
46    *
47    * @version $Id: 103401d2b6ac70b402dbb528addaf98b3f190b06 $
48    * @since 3.0RC1
49    */
50    @RunWith(RenderingTestSuite.class)
 
51    public class IntegrationTests
52    {
 
53  5 toggle @RenderingTestSuite.Initialized
54    public void initialize(ComponentManager componentManager) throws Exception
55    {
56  5 Mockery mockery = new JUnit4Mockery();
57   
58    // Skin Access Bridge Mock
59  5 final SkinAccessBridge mockSkinAccessBridge =
60    registerMockComponent(componentManager, mockery, SkinAccessBridge.class);
 
61  5 toggle 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    // Document Access Bridge Mock
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);
 
78  5 toggle 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    // Document Resolver Mock
104  5 final DocumentReferenceResolver<String> mockDocumentReferenceResolver =
105    registerMockComponent(componentManager, mockery, DocumentReferenceResolver.TYPE_STRING, "current");
 
106  5 toggle 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    // Entity Reference Serializer Mock
125  5 final EntityReferenceSerializer<String> mockEntityReferenceSerializer =
126    registerMockComponent(componentManager, mockery, EntityReferenceSerializer.TYPE_STRING, "compactwiki");
 
127  5 toggle 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    // Entity Reference Serializer Mock
144  5 final EntityReferenceValueProvider mockEntityReferenceValueProvider =
145    registerMockComponent(componentManager, mockery, EntityReferenceValueProvider.class, "current");
 
146  5 toggle mockery.checking(new Expectations() {{
147  5 allowing(mockEntityReferenceValueProvider).getDefaultValue(EntityType.WIKI); will(returnValue("wiki"));
148    }});
149    }
150   
 
151  15 toggle 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   
 
160  10 toggle 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   
 
166  25 toggle 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   
 
175  25 toggle 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    }