1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.resource.internal

File MainResourceReferenceHandlerManagerTest.java

 

Code metrics

0
11
1
1
77
38
1
0.09
11
1
1

Classes

Class Line # Actions
MainResourceReferenceHandlerManagerTest 45 11 0% 1 0
1.0100%
 

Contributing tests

This file is covered by 1 test. .

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.resource.internal;
21   
22    import java.util.Arrays;
23   
24    import org.junit.Rule;
25    import org.junit.Test;
26    import org.xwiki.component.manager.ComponentManager;
27    import org.xwiki.component.util.DefaultParameterizedType;
28    import org.xwiki.resource.ResourceReference;
29    import org.xwiki.resource.ResourceReferenceHandler;
30    import org.xwiki.resource.ResourceReferenceHandlerChain;
31    import org.xwiki.resource.ResourceType;
32    import org.xwiki.test.mockito.MockitoComponentMockingRule;
33   
34    import static org.mockito.ArgumentMatchers.any;
35    import static org.mockito.ArgumentMatchers.same;
36    import static org.mockito.Mockito.mock;
37    import static org.mockito.Mockito.*;
38   
39    /**
40    * Unit tests for {@link MainResourceReferenceHandlerManager}.
41    *
42    * @version $Id: 86a06e7b380797c6379dad045169c869e4d7e57b $
43    * @since 6.1M2
44    */
 
45    public class MainResourceReferenceHandlerManagerTest
46    {
47    @Rule
48    public MockitoComponentMockingRule<MainResourceReferenceHandlerManager> componentManager =
49    new MockitoComponentMockingRule<>(MainResourceReferenceHandlerManager.class);
50   
 
51  1 toggle @Test
52    public void handleWithOrder() throws Exception
53    {
54    // First Handler component will lower priority
55  1 ResourceReferenceHandler testHandler = mock(ResourceReferenceHandler.class, "handler1");
56  1 when(testHandler.getSupportedResourceReferences()).thenReturn(Arrays.asList(new ResourceType("test")));
57   
58    // Second Handler component will higher priority so that it's executed first
59  1 ResourceReferenceHandler beforeTestHandler = mock(ResourceReferenceHandler.class, "handler2");
60  1 when(beforeTestHandler.getSupportedResourceReferences()).thenReturn(Arrays.asList(new ResourceType("test")));
61    // We return 1 to mean that the second Handler has a higher priority than the first Handler
62  1 when(beforeTestHandler.compareTo(testHandler)).thenReturn(1);
63   
64  1 ComponentManager contextComponentManager = this.componentManager.getInstance(ComponentManager.class, "context");
65  1 when(contextComponentManager.<ResourceReferenceHandler>getInstanceList(
66    new DefaultParameterizedType(null, ResourceReferenceHandler.class, ResourceType.class)))
67    .thenReturn(Arrays.asList(testHandler, beforeTestHandler));
68   
69  1 ResourceReference reference = mock(ResourceReference.class);
70  1 when(reference.getType()).thenReturn(new ResourceType("test"));
71   
72  1 this.componentManager.getComponentUnderTest().handle(reference);
73   
74    // Verify that the second Action is called (since it has a higher priority).
75  1 verify(beforeTestHandler).handle(same(reference), any(ResourceReferenceHandlerChain.class));
76    }
77    }