1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package com.xpn.xwiki.internal.store

File PropertyConverterTest.java

 

Code metrics

0
53
8
1
168
109
8
0.15
6.62
8
1

Classes

Class Line # Actions
PropertyConverterTest 45 53 0% 8 0
1.0100%
 

Contributing tests

This file is covered by 8 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.store;
21   
22    import java.util.Arrays;
23   
24    import org.junit.Rule;
25    import org.junit.Test;
26    import org.xwiki.test.mockito.MockitoComponentMockingRule;
27   
28    import com.xpn.xwiki.objects.DoubleProperty;
29    import com.xpn.xwiki.objects.IntegerProperty;
30    import com.xpn.xwiki.objects.LongProperty;
31    import com.xpn.xwiki.objects.StringListProperty;
32    import com.xpn.xwiki.objects.StringProperty;
33    import com.xpn.xwiki.objects.classes.DBListClass;
34    import com.xpn.xwiki.objects.classes.NumberClass;
35    import com.xpn.xwiki.objects.classes.StringClass;
36   
37    import static org.junit.Assert.*;
38    import static org.mockito.Mockito.*;
39   
40    /**
41    * Unit tests for {@link PropertyConverter}.
42    *
43    * @version $Id: 3cab2b97e05a70733fd9316864692711847df95e $
44    */
 
45    public class PropertyConverterTest
46    {
47    @Rule
48    public MockitoComponentMockingRule<PropertyConverter> mocker = new MockitoComponentMockingRule<PropertyConverter>(
49    PropertyConverter.class);
50   
51    /**
52    * @see XWIKI-8649: Error when changing the number type of a field from an application
53    */
 
54  1 toggle @Test
55    public void doubleToInteger() throws Exception
56    {
57    // The number property whose type has changed from Double to Integer.
58  1 NumberClass numberClass = mock(NumberClass.class);
59  1 IntegerProperty integerProperty = mock(IntegerProperty.class);
60  1 when(numberClass.newProperty()).thenReturn(integerProperty);
61  1 when(numberClass.getNumberType()).thenReturn("integer");
62   
63  1 DoubleProperty doubleProperty = mock(DoubleProperty.class);
64  1 when(doubleProperty.getValue()).thenReturn(3.5);
65   
66  1 assertEquals(integerProperty, this.mocker.getComponentUnderTest().convertProperty(doubleProperty, numberClass));
67  1 verify(integerProperty).setValue(3);
68    }
69   
 
70  1 toggle @Test
71    public void unsetDoubleToInteger() throws Exception
72    {
73  1 NumberClass numberClass = mock(NumberClass.class);
74  1 DoubleProperty unsetDoubleProperty = mock(DoubleProperty.class, "unset");
75  1 assertNull(this.mocker.getComponentUnderTest().convertProperty(unsetDoubleProperty, numberClass));
76    }
77   
 
78  1 toggle @Test
79    public void multipleToSingleSelectOnDBList() throws Exception
80    {
81    // The Database List property that was switched from multiple select to single select.
82  1 DBListClass dbListClass = mock(DBListClass.class);
83  1 when(dbListClass.isMultiSelect()).thenReturn(false);
84  1 StringProperty stringProperty = mock(StringProperty.class);
85  1 when(dbListClass.newProperty()).thenReturn(stringProperty);
86   
87  1 StringListProperty stringListProperty = mock(StringListProperty.class);
88  1 when(stringListProperty.getValue()).thenReturn(Arrays.asList("one", "two"));
89   
90  1 assertEquals(stringProperty,
91    this.mocker.getComponentUnderTest().convertProperty(stringListProperty, dbListClass));
92  1 verify(stringProperty).setValue("one");
93    }
94   
 
95  1 toggle @Test
96    public void multipleToSingleSelectOnEmptyDBList() throws Exception
97    {
98    // The Database List property that was switched from multiple select to single select.
99  1 DBListClass dbListClass = mock(DBListClass.class);
100  1 when(dbListClass.isMultiSelect()).thenReturn(false);
101   
102  1 StringListProperty emptyListProperty = mock(StringListProperty.class);
103  1 when(emptyListProperty.getValue()).thenReturn(Arrays.asList());
104   
105  1 assertNull(this.mocker.getComponentUnderTest().convertProperty(emptyListProperty, dbListClass));
106    }
107   
 
108  1 toggle @Test
109    public void singleToMultipleSelectOnDBList() throws Exception
110    {
111    // The Database List property that was switched from single select to multiple select.
112  1 DBListClass dbListClass = mock(DBListClass.class);
113  1 when(dbListClass.isMultiSelect()).thenReturn(true);
114  1 StringListProperty stringListProperty = mock(StringListProperty.class);
115  1 when(dbListClass.newProperty()).thenReturn(stringListProperty);
116   
117  1 StringProperty stringProperty = mock(StringProperty.class);
118  1 when(stringProperty.getValue()).thenReturn("one");
119   
120  1 assertEquals(stringListProperty,
121    this.mocker.getComponentUnderTest().convertProperty(stringProperty, dbListClass));
122  1 verify(stringListProperty).setValue(Arrays.asList("one"));
123    }
124   
 
125  1 toggle @Test
126    public void singleToMultipleSelectOnUnsetDBList() throws Exception
127    {
128    // The Database List property that was switched from single select to multiple select.
129  1 DBListClass dbListClass = mock(DBListClass.class);
130  1 when(dbListClass.isMultiSelect()).thenReturn(true);
131   
132  1 StringProperty stringProperty = mock(StringProperty.class);
133  1 when(stringProperty.getValue()).thenReturn(null);
134   
135  1 assertNull(this.mocker.getComponentUnderTest().convertProperty(stringProperty, dbListClass));
136    }
137   
 
138  1 toggle @Test
139    public void longToString() throws Exception
140    {
141  1 LongProperty longProperty = new LongProperty();
142  1 longProperty.setValue(Long.MAX_VALUE);
143   
144  1 StringClass stringClass = mock(StringClass.class);
145  1 when(stringClass.newProperty()).thenReturn(new StringProperty());
146  1 StringProperty stringProperty = new StringProperty();
147  1 when(stringClass.fromString(longProperty.toText())).thenReturn(stringProperty);
148   
149  1 assertEquals(stringProperty, this.mocker.getComponentUnderTest().convertProperty(longProperty, stringClass));
150    }
151   
 
152  1 toggle @Test
153    public void stringToNumber() throws Exception
154    {
155  1 StringProperty stringProperty = new StringProperty();
156  1 stringProperty.setValue("one");
157   
158  1 NumberClass numberClass = mock(NumberClass.class);
159  1 when(numberClass.newProperty()).thenReturn(new IntegerProperty());
160  1 when(numberClass.fromString(stringProperty.toText())).thenReturn(null);
161  1 when(numberClass.getName()).thenReturn("age");
162  1 when(numberClass.getClassName()).thenReturn("Some.Class");
163   
164  1 assertNull(this.mocker.getComponentUnderTest().convertProperty(stringProperty, numberClass));
165  1 verify(this.mocker.getMockedLogger()).warn(
166    "Incompatible data migration when changing field [{}] of class [{}]", "age", "Some.Class");
167    }
168    }