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

File StandardExtendedURLResourceReferenceSerializerTest.java

 

Code metrics

0
15
3
2
96
57
4
0.27
5
1.5
1.33

Classes

Class Line # Actions
StandardExtendedURLResourceReferenceSerializerTest 44 14 0% 3 1
0.937593.8%
StandardExtendedURLResourceReferenceSerializerTest.TestResourceReference 50 1 0% 1 0
1.0100%
 

Contributing tests

This file is covered by 2 tests. .

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.url.internal.standard;
21   
22    import org.junit.Rule;
23    import org.junit.Test;
24    import org.xwiki.component.manager.ComponentLookupException;
25    import org.xwiki.component.manager.ComponentManager;
26    import org.xwiki.component.util.DefaultParameterizedType;
27    import org.xwiki.resource.AbstractResourceReference;
28    import org.xwiki.resource.ResourceReferenceSerializer;
29    import org.xwiki.resource.ResourceType;
30    import org.xwiki.resource.UnsupportedResourceReferenceException;
31    import org.xwiki.test.mockito.MockitoComponentMockingRule;
32    import org.xwiki.url.ExtendedURL;
33   
34    import static org.junit.Assert.assertEquals;
35    import static org.junit.Assert.fail;
36    import static org.mockito.Mockito.*;
37   
38    /**
39    * Unit tests for {@link StandardExtendedURLResourceReferenceSerializer}.
40    *
41    * @version $Id: 45bc91da00b1a7d9e78afce9067773bc487dab91 $
42    * @since 6.1M2
43    */
 
44    public class StandardExtendedURLResourceReferenceSerializerTest
45    {
46    @Rule
47    public MockitoComponentMockingRule<StandardExtendedURLResourceReferenceSerializer> mocker =
48    new MockitoComponentMockingRule<>(StandardExtendedURLResourceReferenceSerializer.class);
49   
 
50    public class TestResourceReference extends AbstractResourceReference
51    {
 
52  2 toggle public TestResourceReference()
53    {
54  2 setType(new ResourceType("test"));
55    }
56    }
57   
 
58  1 toggle @Test
59    public void serialize() throws Exception
60    {
61  1 TestResourceReference resource = new TestResourceReference();
62   
63  1 ResourceReferenceSerializer serializer = mock(ResourceReferenceSerializer.class);
64   
65  1 ComponentManager componentManager = this.mocker.getInstance(ComponentManager.class, "context");
66  1 when(componentManager.getInstance(new DefaultParameterizedType(null, ResourceReferenceSerializer.class,
67    TestResourceReference.class, ExtendedURL.class), "standard")).thenReturn(serializer);
68   
69  1 this.mocker.getComponentUnderTest().serialize(resource);
70   
71    // Verify the serializer is called and with the proper parameters
72  1 verify(serializer).serialize(same(resource));
73    }
74   
 
75  1 toggle @Test
76    public void serializeWhenNoMatchingSerializer() throws Exception
77    {
78  1 TestResourceReference resource = new TestResourceReference();
79   
80  1 ComponentManager componentManager = this.mocker.getInstance(ComponentManager.class, "context");
81  1 when(componentManager.getInstance(new DefaultParameterizedType(null, ResourceReferenceSerializer.class,
82    TestResourceReference.class, ExtendedURL.class), "standard")).thenThrow(
83    new ComponentLookupException("error"));
84  1 when(componentManager.getInstance(new DefaultParameterizedType(null, ResourceReferenceSerializer.class,
85    TestResourceReference.class, ExtendedURL.class))).thenThrow(
86    new ComponentLookupException("error"));
87   
88  1 try {
89  1 this.mocker.getComponentUnderTest().serialize(resource);
90  0 fail("Should have thrown an exception here");
91    } catch (UnsupportedResourceReferenceException expected) {
92  1 assertEquals("Failed to find serializer for Resource Reference [type = [test], parameters = []] and "
93    + "URL format [standard]", expected.getMessage());
94    }
95    }
96    }