Skip to content

Commit

Permalink
[Misc] Convert a JUnit4-based rendering tests to JUnit5
Browse files Browse the repository at this point in the history
  • Loading branch information
vmassol committed Nov 20, 2024
1 parent 8c2e8c6 commit 95e312a
Showing 1 changed file with 61 additions and 128 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,20 @@
*/
package org.xwiki.rendering.macro.useravatar;

import java.lang.reflect.Type;

import org.jmock.Expectations;
import org.jmock.Mockery;
import org.jmock.integration.junit4.JUnit4Mockery;
import org.junit.runner.RunWith;
import org.xwiki.bridge.DocumentAccessBridge;
import org.xwiki.bridge.SkinAccessBridge;
import org.xwiki.component.descriptor.ComponentDescriptor;
import org.xwiki.component.descriptor.DefaultComponentDescriptor;
import org.xwiki.component.manager.ComponentManager;
import org.xwiki.component.util.ReflectionUtils;
import org.xwiki.model.EntityType;
import org.xwiki.model.reference.AttachmentReference;
import org.xwiki.model.reference.DocumentReference;
import org.xwiki.model.reference.DocumentReferenceResolver;
import org.xwiki.model.reference.EntityReference;
import org.xwiki.model.reference.EntityReferenceSerializer;
import org.xwiki.model.reference.EntityReferenceValueProvider;
import org.xwiki.rendering.test.integration.RenderingTestSuite;
import org.xwiki.rendering.test.integration.junit5.RenderingTests;
import org.xwiki.test.annotation.AllComponents;
import org.xwiki.test.mockito.MockitoComponentManager;

import static org.mockito.Mockito.when;

/**
* Run all tests found in {@code *.test} files located in the classpath. These {@code *.test} files must follow the
Expand All @@ -47,136 +41,75 @@
* @version $Id$
* @since 3.0RC1
*/
@RunWith(RenderingTestSuite.class)
public class IntegrationTests
@AllComponents
@RenderingTests.Scope(pattern = "macrouseravatar.*")
public class IntegrationTests implements RenderingTests
{
@RenderingTestSuite.Initialized
public void initialize(ComponentManager componentManager) throws Exception
@RenderingTests.Initialized
public void initialize(MockitoComponentManager componentManager) throws Exception
{
Mockery mockery = new JUnit4Mockery();

// Skin Access Bridge Mock
final SkinAccessBridge mockSkinAccessBridge =
registerMockComponent(componentManager, mockery, SkinAccessBridge.class);
mockery.checking(new Expectations() {{
allowing(mockSkinAccessBridge).getSkinFile("icons/xwiki/noavatar.png");
will(returnValue("/xwiki/resources/icons/xwiki/noavatar.png"));
}});
SkinAccessBridge sab = componentManager.registerMockComponent(SkinAccessBridge.class);
when(sab.getSkinFile("icons/xwiki/noavatar.png")).thenReturn("/xwiki/resources/icons/xwiki/noavatar.png");

// Document Access Bridge Mock
final DocumentReference adminUserReference = new DocumentReference("wiki", "XWiki", "Admin");
final DocumentReference userWithoutAvatarReference =
DocumentReference adminUserReference = new DocumentReference("wiki", "XWiki", "Admin");
DocumentReference userWithoutAvatarReference =
new DocumentReference("wiki", "XWiki", "ExistingUserWithoutAvatar");
final DocumentReference userNotExistingReference = new DocumentReference("wiki", "XWiki", "UserNotExisting");
final DocumentReference userWithNonExistingAvatarFileReference =
DocumentReference userNotExistingReference = new DocumentReference("wiki", "XWiki", "UserNotExisting");
DocumentReference userWithNonExistingAvatarFileReference =
new DocumentReference("wiki", "XWiki", "UserWithNonExistingAvatarFile");
final DocumentReference userWithExceptionRetrievingAvatarFileReference =
DocumentReference userWithExceptionRetrievingAvatarFileReference =
new DocumentReference("wiki", "XWiki", "UserWithExceptionRetrievingAvatarFile");
final DocumentReference userClassReference = new DocumentReference("wiki", "XWiki", "XWikiUsers");
final DocumentAccessBridge mockDocumentAccessBridge =
registerMockComponent(componentManager, mockery, DocumentAccessBridge.class);
mockery.checking(new Expectations() {{
allowing(mockDocumentAccessBridge).exists(adminUserReference); will(returnValue(true));
allowing(mockDocumentAccessBridge).exists(userWithoutAvatarReference); will(returnValue(true));
allowing(mockDocumentAccessBridge).exists(with(any(String.class))); will(returnValue(false));
allowing(mockDocumentAccessBridge).exists(userNotExistingReference); will(returnValue(false));
allowing(mockDocumentAccessBridge).exists(userWithNonExistingAvatarFileReference); will(returnValue(true));
allowing(mockDocumentAccessBridge).exists(userWithExceptionRetrievingAvatarFileReference); will(returnValue(true));

allowing(mockDocumentAccessBridge).getProperty(adminUserReference, userClassReference, "avatar");
will(returnValue("mockAvatar.png"));
allowing(mockDocumentAccessBridge).getProperty(userWithoutAvatarReference, userClassReference,
"avatar"); will(returnValue(null));
allowing(mockDocumentAccessBridge).getProperty(userWithNonExistingAvatarFileReference,
userClassReference, "avatar"); will(returnValue("mockAvatar.png"));
allowing(mockDocumentAccessBridge).getProperty(userWithExceptionRetrievingAvatarFileReference,
userClassReference, "avatar"); will(returnValue("mockAvatar.png"));

allowing(mockDocumentAccessBridge).getAttachmentVersion(new AttachmentReference("mockAvatar.png",
adminUserReference)); will(returnValue("1.1"));
allowing(mockDocumentAccessBridge).getAttachmentVersion(new AttachmentReference("mockAvatar.png",
userWithNonExistingAvatarFileReference)); will(returnValue(null));
allowing(mockDocumentAccessBridge).getAttachmentVersion(new AttachmentReference("mockAvatar.png",
userWithExceptionRetrievingAvatarFileReference)); will(throwException(new Exception("Sum Ting Wong")));
}});
DocumentReference userClassReference = new DocumentReference("wiki", "XWiki", "XWikiUsers");
DocumentAccessBridge dab = componentManager.registerMockComponent(DocumentAccessBridge.class);
when(dab.exists(adminUserReference)).thenReturn(true);
when(dab.exists(userWithoutAvatarReference)).thenReturn(true);
when(dab.exists(userWithNonExistingAvatarFileReference)).thenReturn(true);
when(dab.exists(userWithExceptionRetrievingAvatarFileReference)).thenReturn(true);
when(dab.getProperty(adminUserReference, userClassReference, "avatar"))
.thenReturn("mockAvatar.png");
when(dab.getProperty(userWithoutAvatarReference, userClassReference, "avatar"))
.thenReturn(null);
when(dab.getProperty(userWithNonExistingAvatarFileReference, userClassReference, "avatar"))
.thenReturn("mockAvatar.png");
when(dab.getProperty(userWithExceptionRetrievingAvatarFileReference, userClassReference, "avatar"))
.thenReturn("mockAvatar.png");
when(dab.getAttachmentVersion(new AttachmentReference("mockAvatar.png", adminUserReference))).thenReturn("1.1");
when(dab.getAttachmentVersion(new AttachmentReference("mockAvatar.png",
userWithNonExistingAvatarFileReference))).thenReturn(null);
when(dab.getAttachmentVersion(new AttachmentReference("mockAvatar.png",
userWithExceptionRetrievingAvatarFileReference))).thenThrow(new Exception("Sum Ting Wong"));

// Document Resolver Mock
final DocumentReferenceResolver<String> mockDocumentReferenceResolver =
registerMockComponent(componentManager, mockery, DocumentReferenceResolver.TYPE_STRING, "current");
mockery.checking(new Expectations() {{
allowing(mockDocumentReferenceResolver).resolve("XWiki.Admin",
new EntityReference("XWiki", EntityType.SPACE));
will(returnValue(adminUserReference));
allowing(mockDocumentReferenceResolver).resolve("XWiki.ExistingUserWithoutAvatar",
new EntityReference("XWiki", EntityType.SPACE));
will(returnValue(userWithoutAvatarReference));
allowing(mockDocumentReferenceResolver).resolve("XWiki.UserNotExisting",
new EntityReference("XWiki", EntityType.SPACE));
will(returnValue(userNotExistingReference));
allowing(mockDocumentReferenceResolver).resolve("XWiki.UserWithNonExistingAvatarFile",
new EntityReference("XWiki", EntityType.SPACE));
will(returnValue(userWithNonExistingAvatarFileReference));
allowing(mockDocumentReferenceResolver).resolve("XWiki.UserWithExceptionRetrievingAvatarFile",
new EntityReference("XWiki", EntityType.SPACE));
will(returnValue(userWithExceptionRetrievingAvatarFileReference));
}});
DocumentReferenceResolver<String> drr =
componentManager.registerMockComponent(DocumentReferenceResolver.TYPE_STRING, "current");
when(drr.resolve("XWiki.Admin", new EntityReference("XWiki", EntityType.SPACE))).thenReturn(adminUserReference);
when(drr.resolve("XWiki.ExistingUserWithoutAvatar", new EntityReference("XWiki", EntityType.SPACE)))
.thenReturn(userWithoutAvatarReference);
when(drr.resolve("XWiki.UserNotExisting", new EntityReference("XWiki", EntityType.SPACE)))
.thenReturn(userNotExistingReference);
when(drr.resolve("XWiki.UserWithNonExistingAvatarFile", new EntityReference("XWiki", EntityType.SPACE)))
.thenReturn(userWithNonExistingAvatarFileReference);
when(drr.resolve("XWiki.UserWithExceptionRetrievingAvatarFile", new EntityReference("XWiki", EntityType.SPACE)))
.thenReturn(userWithExceptionRetrievingAvatarFileReference);

// Entity Reference Serializer Mock
final EntityReferenceSerializer<String> mockEntityReferenceSerializer =
registerMockComponent(componentManager, mockery, EntityReferenceSerializer.TYPE_STRING, "compactwiki");
mockery.checking(new Expectations() {{
allowing(mockEntityReferenceSerializer).serialize(
new AttachmentReference("mockAvatar.png", adminUserReference));
will(returnValue("[email protected]"));
allowing(mockEntityReferenceSerializer).serialize(userNotExistingReference);
will(returnValue("XWiki.UserNotExisting"));
allowing(mockEntityReferenceSerializer).serialize(
new AttachmentReference("mockAvatar.png", userWithNonExistingAvatarFileReference));
will(returnValue("[email protected]"));
allowing(mockEntityReferenceSerializer).serialize(
new AttachmentReference("mockAvatar.png", userWithExceptionRetrievingAvatarFileReference));
will(returnValue("[email protected]"));
allowing(mockEntityReferenceSerializer).serialize(userWithExceptionRetrievingAvatarFileReference);
will(returnValue("XWiki.UserWithExceptionRetrievingAvatarFile"));
}});
EntityReferenceSerializer<String> ers =
componentManager.registerMockComponent(EntityReferenceSerializer.TYPE_STRING, "compactwiki");
when(ers.serialize(new AttachmentReference("mockAvatar.png", adminUserReference)))
.thenReturn("[email protected]");
when(ers.serialize(userNotExistingReference)).thenReturn("XWiki.UserNotExisting");
when(ers.serialize(new AttachmentReference("mockAvatar.png", userWithNonExistingAvatarFileReference)))
.thenReturn("[email protected]");
when(ers.serialize(new AttachmentReference("mockAvatar.png", userWithExceptionRetrievingAvatarFileReference)))
.thenReturn("[email protected]");
when(ers.serialize(userWithExceptionRetrievingAvatarFileReference))
.thenReturn("XWiki.UserWithExceptionRetrievingAvatarFile");

// Entity Reference Serializer Mock
final EntityReferenceValueProvider mockEntityReferenceValueProvider =
registerMockComponent(componentManager, mockery, EntityReferenceValueProvider.class, "current");
mockery.checking(new Expectations() {{
allowing(mockEntityReferenceValueProvider).getDefaultValue(EntityType.WIKI); will(returnValue("wiki"));
}});
}

private static <T> T registerMockComponent(ComponentManager componentManager, Mockery mockery,
Type role, String hint) throws Exception
{
DefaultComponentDescriptor<T> descriptor = createComponentDescriptor(role);
descriptor.setRoleHint(hint);

return registerMockComponent(componentManager, mockery, descriptor);
}

private static <T> T registerMockComponent(ComponentManager componentManager, Mockery mockery,
Type role) throws Exception
{
return registerMockComponent(componentManager, mockery, IntegrationTests.<T> createComponentDescriptor(role));
}

private static <T> T registerMockComponent(ComponentManager componentManager, Mockery mockery,
ComponentDescriptor<T> descriptor) throws Exception
{
T mock = mockery.mock((Class<T>)ReflectionUtils.getTypeClass(descriptor.getRoleType()));
componentManager.registerComponent(descriptor, mock);

return mock;
}

private static <T> DefaultComponentDescriptor<T> createComponentDescriptor(Type role)
{
DefaultComponentDescriptor<T> descriptor = new DefaultComponentDescriptor<T>();
descriptor.setRoleType(role);

return descriptor;
EntityReferenceValueProvider ervp =
componentManager.registerMockComponent(EntityReferenceValueProvider.class, "current");
when(ervp.getDefaultValue(EntityType.WIKI)).thenReturn("wiki");
}
}

0 comments on commit 95e312a

Please sign in to comment.