/*
 * See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.xwiki.resource.internal;

import java.util.Arrays;

import org.junit.Rule;
import org.junit.Test;
import org.xwiki.component.manager.ComponentManager;
import org.xwiki.component.util.DefaultParameterizedType;
import org.xwiki.resource.ResourceReference;
import org.xwiki.resource.ResourceReferenceHandler;
import org.xwiki.resource.ResourceReferenceHandlerChain;
import org.xwiki.resource.ResourceType;
import org.xwiki.test.mockito.MockitoComponentMockingRule;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.same;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.*;

/**
 * Unit tests for {@link MainResourceReferenceHandlerManager}.
 *
 * @version $Id$
 * @since 6.1M2
 */
public class MainResourceReferenceHandlerManagerTest
{
    @Rule
    public MockitoComponentMockingRule<MainResourceReferenceHandlerManager> componentManager =
        new MockitoComponentMockingRule<>(MainResourceReferenceHandlerManager.class);

    @Test
    public void handleWithOrder() throws Exception
    {
        // First Handler component will lower priority
        ResourceReferenceHandler testHandler = mock(ResourceReferenceHandler.class, "handler1");
        when(testHandler.getSupportedResourceReferences()).thenReturn(Arrays.asList(new ResourceType("test")));

        // Second Handler component will higher priority so that it's executed first
        ResourceReferenceHandler beforeTestHandler = mock(ResourceReferenceHandler.class, "handler2");
        when(beforeTestHandler.getSupportedResourceReferences()).thenReturn(Arrays.asList(new ResourceType("test")));
        // We return 1 to mean that the second Handler has a higher priority than the first Handler
        when(beforeTestHandler.compareTo(testHandler)).thenReturn(-1);

        ComponentManager contextComponentManager = this.componentManager.getInstance(ComponentManager.class, "context");
        when(contextComponentManager.<ResourceReferenceHandler>getInstanceList(
            new DefaultParameterizedType(null, ResourceReferenceHandler.class, ResourceType.class)))
                .thenReturn(Arrays.asList(testHandler, beforeTestHandler));

        ResourceReference reference = mock(ResourceReference.class);
        when(reference.getType()).thenReturn(new ResourceType("test"));

        this.componentManager.getComponentUnderTest().handle(reference);

        // Verify that the second Action is called (since it has a higher priority).
        verify(beforeTestHandler).handle(same(reference), any(ResourceReferenceHandlerChain.class));
    }
}

@@ -972,30 +972,19 @@
 nit.
-Rule;%0Aimport org.junit
+jupiter.api
 .Tes
@@ -1287,24 +1287,135 @@
 sourceType;%0A
+import org.xwiki.test.junit5.mockito.ComponentTest;%0Aimport org.xwiki.test.junit5.mockito.InjectMockComponents;%0A
 import org.x
@@ -1744,16 +1744,31 @@
 1M2%0A */%0A
+@ComponentTest%0A
 public c
@@ -1823,31 +1823,18 @@
    @
-Rule%0A    public 
+Inject
 Mock
-ito
 Comp
@@ -1838,28 +1838,29 @@
 omponent
-MockingRule%3C
+s%0A    public 
 MainReso
@@ -1890,93 +1890,10 @@
 ager
-%3E componentManager =%0A        new MockitoComponentMockingRule%3C%3E(MainResourceReferenceH
+ h
 andl
@@ -1901,23 +1901,16 @@
 rManager
-.class)
 ;%0A%0A    @
@@ -1946,16 +1946,49 @@
 thOrder(
+ComponentManager componentManager
 ) throws
@@ -2798,21 +2798,16 @@
 nager = 
-this.
 componen
@@ -3275,53 +3275,22 @@
     
-this.componentManager.getComponentUnderTest()
+handlerManager
 .han

Refactor MainResourceReferenceHandlerManagerTest to junit 5