Uploaded image for project: 'XWiki Platform'
  1. XWiki Platform
  2. XWIKI-15232

Introduce JUnit5 extension for writing old core tests

    XMLWordPrintable

Details

    • New Feature
    • Resolution: Fixed
    • Major
    • 10.4-rc-1
    • 10.3
    • Test Framework
    • None
    • Unit
    • Unknown
    • N/A

    Description

      Example usage:

      @OldcoreTest
      public class WikiUIExtensionComponentBuilderTest implements WikiUIExtensionConstants
      {
          @InjectMockComponents
          private WikiUIExtensionComponentBuilder builder;
      
          private static final DocumentReference DOC_REF =
              new DocumentReference("xwiki", "XWiki", "MyUIExtension", Locale.ROOT);
      
          private static final DocumentReference AUTHOR_REFERENCE = new DocumentReference("xwiki", "XWiki", "Admin");
      
          private XWikiDocument componentDoc;
      
          @BeforeComponent
          public void configure(MockitoComponentManager componentManager, MockitoOldcore oldcore) throws Exception
          {
              // Required by BaseObjectReference
              DocumentReferenceResolver<String> resolver =
                  componentManager.registerMockComponent(DocumentReferenceResolver.TYPE_STRING);
              when(resolver.resolve("XWiki.UIExtension"))
                  .thenReturn(new DocumentReference("xwiki", "XWiki", "UIExtensionClass"));
      
              DelegateComponentManager wikiComponentManager = new DelegateComponentManager();
              wikiComponentManager.setComponentManager(componentManager);
              componentManager.registerComponent(ComponentManager.class, "wiki", wikiComponentManager);
      
              // Components accessed through dynamic lookup.
              VelocityManager velocityManager = componentManager.registerMockComponent(VelocityManager.class);
              when(velocityManager.getVelocityEngine()).thenReturn(mock(VelocityEngine.class));
              when(velocityManager.getVelocityContext()).thenReturn(mock(VelocityContext.class));
      
              ModelContext modelContext = componentManager.registerMockComponent(ModelContext.class);
              when(modelContext.getCurrentEntityReference()).thenReturn(DOC_REF);
      
              componentManager.registerMockComponent(RenderingContext.class);
              componentManager.registerMockComponent(Transformation.class, "macro");
              componentManager.registerMockComponent(ContentParser.class);
      
              // The document holding the UI extension object.
              this.componentDoc = mock(XWikiDocument.class, "xwiki:XWiki.MyUIExtension");
              when(this.componentDoc.getDocumentReference()).thenReturn(DOC_REF);
              when(this.componentDoc.getAuthorReference()).thenReturn(AUTHOR_REFERENCE);
              when(this.componentDoc.getContentAuthorReference()).thenReturn(AUTHOR_REFERENCE);
              when(this.componentDoc.getSyntax()).thenReturn(Syntax.XWIKI_2_1);
              oldcore.getDocuments().put(DOC_REF, componentDoc);
          }
      
          @Test
          public void buildGlobalComponentsWithoutPR()
          {
              BaseObject extensionObject = createExtensionObject("id", "extensionPointId", "content", "parameters", "global");
              Throwable exception = assertThrows(WikiComponentException.class, () -> {
                  this.builder.buildComponents(extensionObject);
              });
              assertEquals("Registering global UI extensions requires programming rights", exception.getMessage());
          }
      
          @Test
          public void buildWikiLevelComponentsWithoutAdminRights()
          {
              BaseObject extensionObject = createExtensionObject("id", "extensionPointId", "content", "parameters", "wiki");
              Throwable exception = assertThrows(WikiComponentException.class, () -> {
                  this.builder.buildComponents(extensionObject);
              });
              assertEquals("Registering UI extensions at wiki level requires wiki administration rights",
                  exception.getMessage());
          }
      
          @Test
          public void buildComponents(ComponentManager componentManager) throws Exception
          {
              BaseObject extensionObject = createExtensionObject("name", "extensionPointId", "content",
                  "key=value=foo\nkey2=value2\ninvalid=\n\n=invalid", "user");
      
              ContentParser contentParser = componentManager.getInstance(ContentParser.class);
              VelocityManager velocityManager = componentManager.getInstance(VelocityManager.class);
      
              List<WikiComponent> components = this.builder.buildComponents(extensionObject);
      
              assertEquals(1, components.size());
              verify(contentParser).parse("content", Syntax.XWIKI_2_1, DOC_REF);
      
              UIExtension uiExtension = (UIExtension) components.get(0);
              Map<String, String> parameters = uiExtension.getParameters();
      
              assertEquals(2, parameters.size());
              verify(velocityManager.getVelocityEngine()).evaluate(any(VelocityContext.class), any(StringWriter.class),
                  eq("name:key"), eq("value=foo"));
              verify(velocityManager.getVelocityEngine()).evaluate(any(VelocityContext.class), any(StringWriter.class),
                  eq("name:key2"), eq("value2"));
          }
      
          private BaseObject createExtensionObject(String id, String extensionPointId, String content, String parameters,
              String scope)
          {
              BaseObject extensionObject = mock(BaseObject.class, id);
              when(extensionObject.getStringValue(ID_PROPERTY)).thenReturn(id);
              when(extensionObject.getStringValue(EXTENSION_POINT_ID_PROPERTY)).thenReturn(extensionPointId);
              when(extensionObject.getStringValue(CONTENT_PROPERTY)).thenReturn(content);
              when(extensionObject.getStringValue(PARAMETERS_PROPERTY)).thenReturn(parameters);
              when(extensionObject.getStringValue(SCOPE_PROPERTY)).thenReturn(scope);
              BaseObjectReference objectReference =
                  new BaseObjectReference(new ObjectReference("XWiki.UIExtensionClass[0]", DOC_REF));
              when(extensionObject.getReference()).thenReturn(objectReference);
      
              when(extensionObject.getOwnerDocument()).thenReturn(this.componentDoc);
      
              return extensionObject;
          }
      }
      

      Attachments

        Issue Links

          Activity

            People

              vmassol Vincent Massol
              vmassol Vincent Massol
              Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: