1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package com.xpn.xwiki.internal.model.reference

File DocumentReferenceConverterTest.java

 

Code metrics

0
25
5
1
137
95
5
0.2
5
5
1

Classes

Class Line # Actions
DocumentReferenceConverterTest 49 25 0% 5 0
1.0100%
 

Contributing tests

This file is covered by 4 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 com.xpn.xwiki.internal.model.reference;
21   
22    import org.junit.Assert;
23    import org.junit.Before;
24    import org.junit.Rule;
25    import org.junit.Test;
26    import org.mockito.Mockito;
27    import org.xwiki.component.internal.ContextComponentManagerProvider;
28    import org.xwiki.model.EntityType;
29    import org.xwiki.model.reference.DocumentReference;
30    import org.xwiki.model.reference.DocumentReferenceResolver;
31    import org.xwiki.model.reference.EntityReference;
32    import org.xwiki.model.reference.EntityReferenceSerializer;
33    import org.xwiki.properties.ConverterManager;
34    import org.xwiki.properties.converter.Converter;
35    import org.xwiki.properties.internal.DefaultConverterManager;
36    import org.xwiki.properties.internal.converter.ColorConverter;
37    import org.xwiki.properties.internal.converter.ConvertUtilsConverter;
38    import org.xwiki.properties.internal.converter.EnumConverter;
39    import org.xwiki.test.annotation.ComponentList;
40    import org.xwiki.test.mockito.MockitoComponentMockingRule;
41   
42    /**
43    * Validate {@link ColorConverter} component.
44    *
45    * @version $Id: e4cb4d43d0ca4c04dc4fb6cabeca42e7b621e46d $
46    */
47    @ComponentList(value = { DefaultConverterManager.class, ContextComponentManagerProvider.class, EnumConverter.class,
48    ConvertUtilsConverter.class })
 
49    public class DocumentReferenceConverterTest
50    {
51    @Rule
52    public MockitoComponentMockingRule<Converter<DocumentReference>> mocker =
53    new MockitoComponentMockingRule<Converter<DocumentReference>>(DocumentReferenceConverter.class);
54   
55    private ConverterManager converterManager;
56   
57    private DocumentReferenceResolver<String> mockStringResolver;
58   
59    private DocumentReferenceResolver<EntityReference> mockReferenceResolver;
60   
61    private EntityReferenceSerializer<String> mockSerialier;
62   
 
63  4 toggle @Before
64    public void setUp() throws Exception
65    {
66  4 this.converterManager = mocker.getInstance(ConverterManager.class);
67   
68  4 this.mockStringResolver = this.mocker.getInstance(DocumentReferenceResolver.TYPE_STRING, "current");
69  4 this.mockReferenceResolver = this.mocker.getInstance(DocumentReferenceResolver.TYPE_REFERENCE, "currentgetdocument");
70  4 this.mockSerialier = this.mocker.getInstance(EntityReferenceSerializer.TYPE_STRING, "compact");
71    }
72   
 
73  1 toggle @Test
74    public void testConvertFromString()
75    {
76  1 Mockito.when(this.mockStringResolver.resolve("wiki:space.page")).thenReturn(
77    new DocumentReference("wiki", "space", "page"));
78  1 Assert.assertEquals(new DocumentReference("wiki", "space", "page"),
79    this.converterManager.convert(DocumentReference.class, "wiki:space.page"));
80   
81  1 Mockito.when(this.mockStringResolver.resolve("space.page")).thenReturn(
82    new DocumentReference("currentwiki", "space", "page"));
83  1 Assert.assertEquals(new DocumentReference("currentwiki", "space", "page"),
84    this.converterManager.convert(DocumentReference.class, "space.page"));
85   
86  1 Mockito.when(this.mockStringResolver.resolve("page")).thenReturn(
87    new DocumentReference("currentwiki", "currentspace", "page"));
88  1 Assert.assertEquals(new DocumentReference("currentwiki", "currentspace", "page"),
89    this.converterManager.convert(DocumentReference.class, "page"));
90    }
91   
 
92  1 toggle @Test
93    public void testConvertFromReference()
94    {
95  1 EntityReference reference;
96   
97  1 reference =
98    new EntityReference("page", EntityType.DOCUMENT, new EntityReference("space", EntityType.SPACE,
99    new EntityReference("wiki", EntityType.WIKI)));
100  1 Mockito.when(this.mockReferenceResolver.resolve(reference)).thenReturn(
101    new DocumentReference("wiki", "space", "page"));
102  1 Assert.assertEquals(new DocumentReference("wiki", "space", "page"),
103    this.converterManager.convert(DocumentReference.class, reference));
104   
105  1 reference = new EntityReference("page", EntityType.DOCUMENT, new EntityReference("space", EntityType.SPACE));
106  1 Mockito.when(this.mockReferenceResolver.resolve(reference)).thenReturn(
107    new DocumentReference("currentwiki", "space", "page"));
108  1 Assert.assertEquals(new DocumentReference("currentwiki", "space", "page"),
109    this.converterManager.convert(DocumentReference.class, reference));
110   
111  1 reference = new EntityReference("page", EntityType.DOCUMENT);
112  1 Mockito.when(this.mockReferenceResolver.resolve(reference)).thenReturn(
113    new DocumentReference("currentwiki", "currentspace", "page"));
114  1 Assert.assertEquals(new DocumentReference("currentwiki", "currentspace", "page"),
115    this.converterManager.convert(DocumentReference.class, reference));
116    }
117   
 
118  1 toggle @Test
119    public void testConvertFromNull()
120    {
121  1 Assert.assertNull(this.converterManager.convert(DocumentReference.class, null));
122    }
123   
 
124  1 toggle @Test
125    public void testConvertToString()
126    {
127  1 Mockito.when(this.mockSerialier.serialize(new DocumentReference("wiki", "space", "page"))).thenReturn(
128    "wiki:space.page");
129  1 Assert.assertEquals("wiki:space.page",
130    this.converterManager.convert(String.class, new DocumentReference("wiki", "space", "page")));
131   
132  1 Mockito.when(this.mockSerialier.serialize(new DocumentReference("wiki", "space", "page"))).thenReturn(
133    "space.page");
134  1 Assert.assertEquals("space.page",
135    this.converterManager.convert(String.class, new DocumentReference("wiki", "space", "page")));
136    }
137    }