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

File WikiPreferencesConfigurationSourceTest.java

 

Code metrics

0
41
9
1
165
105
9
0.22
4.56
9
1

Classes

Class Line # Actions
WikiPreferencesConfigurationSourceTest 44 41 0% 9 0
1.0100%
 

Contributing tests

This file is covered by 6 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.configuration.internal;
21   
22    import static org.mockito.Mockito.when;
23   
24    import java.util.Arrays;
25    import java.util.HashSet;
26    import java.util.List;
27    import java.util.Locale;
28   
29    import org.junit.Assert;
30    import org.junit.Test;
31    import org.xwiki.component.manager.ComponentLookupException;
32    import org.xwiki.configuration.internal.test.AbstractTestDocumentConfigurationSource;
33    import org.xwiki.model.reference.DocumentReference;
34    import org.xwiki.model.reference.LocalDocumentReference;
35    import org.xwiki.properties.converter.ConversionException;
36   
37    import com.xpn.xwiki.XWikiException;
38   
39    /**
40    * Unit tests for {@link WikiPreferencesConfigurationSource}.
41    *
42    * @version $Id: 31e2e0d488d6f5dbc1fcec1211d30dc30000b5eb
43    */
 
44    public class WikiPreferencesConfigurationSourceTest extends AbstractTestDocumentConfigurationSource
45    {
 
46  6 toggle public WikiPreferencesConfigurationSourceTest()
47    {
48  6 super(WikiPreferencesConfigurationSource.class);
49    }
50   
 
51  22 toggle @Override
52    protected LocalDocumentReference getClassReference()
53    {
54  22 return WikiPreferencesConfigurationSource.CLASS_REFERENCE;
55    }
56   
 
57  6 toggle @Override
58    public void before() throws Exception
59    {
60  6 super.before();
61   
62  6 this.oldcore.getXWikiContext().setLocale(Locale.ENGLISH);
63    }
64   
65    // Tests
66   
 
67  1 toggle @Test
68    public void testGetProperty() throws Exception
69    {
70  1 Assert.assertEquals(null, this.componentManager.getComponentUnderTest().getProperty("key", String.class));
71  1 Assert.assertEquals("default", this.componentManager.getComponentUnderTest().getProperty("key", "default"));
72  1 Assert.assertEquals(null, this.componentManager.getComponentUnderTest().getProperty("key"));
73  1 Assert.assertEquals(null, this.componentManager.getComponentUnderTest().getProperty("key", Integer.class));
74   
75    // Validate result for simple String key
76   
77  1 setStringProperty(new DocumentReference(CURRENT_WIKI, WikiPreferencesConfigurationSource.CLASS_SPACE_NAME,
78    WikiPreferencesConfigurationSource.CLASS_PAGE_NAME), "key", "value");
79   
80  1 Assert.assertEquals("value", this.componentManager.getComponentUnderTest().getProperty("key", String.class));
81  1 Assert.assertEquals("value", this.componentManager.getComponentUnderTest().getProperty("key", "default"));
82  1 Assert.assertEquals("value", this.componentManager.getComponentUnderTest().getProperty("key"));
83   
84    // Validate result for non existing key
85   
86  1 Assert.assertEquals(null, this.componentManager.getComponentUnderTest().getProperty("wrongkey", String.class));
87  1 Assert
88    .assertEquals("default", this.componentManager.getComponentUnderTest().getProperty("wrongkey", "default"));
89  1 Assert.assertEquals(null, this.componentManager.getComponentUnderTest().getProperty("wrongkey"));
90  1 Assert.assertEquals(null, this.componentManager.getComponentUnderTest().getProperty("wrongkey", Integer.class));
91   
92    // Check that the --- is empty hack "works"
93   
94  1 setStringProperty(new DocumentReference(CURRENT_WIKI, WikiPreferencesConfigurationSource.CLASS_SPACE_NAME,
95    WikiPreferencesConfigurationSource.CLASS_PAGE_NAME), "key", WikiPreferencesConfigurationSource.NO_VALUE);
96   
97  1 Assert.assertEquals(null, this.componentManager.getComponentUnderTest().getProperty("key", String.class));
98  1 Assert.assertEquals("default", this.componentManager.getComponentUnderTest().getProperty("key", "default"));
99  1 Assert.assertEquals(null, this.componentManager.getComponentUnderTest().getProperty("key"));
100  1 Assert.assertEquals(null, this.componentManager.getComponentUnderTest().getProperty("key", Integer.class));
101    }
102   
 
103  1 toggle @Test
104    public void testGetPropertyTwiceWithDifferentType() throws Exception
105    {
106  1 when(this.mockConverter.convert(String.class, "10")).thenReturn("10");
107  1 when(this.mockConverter.convert(Integer.class, "10")).thenReturn(10);
108   
109  1 setStringProperty(new DocumentReference(CURRENT_WIKI, WikiPreferencesConfigurationSource.CLASS_SPACE_NAME,
110    WikiPreferencesConfigurationSource.CLASS_PAGE_NAME), "key", "10");
111   
112  1 Assert.assertEquals("10", this.componentManager.getComponentUnderTest().getProperty("key", String.class));
113  1 Assert.assertEquals((Integer) 10,
114    this.componentManager.getComponentUnderTest().<Integer>getProperty("key", Integer.class));
115    }
116   
 
117  1 toggle @Test(expected = ConversionException.class)
118    public void testGetPropertyWithWrongType() throws Exception
119    {
120  1 when(this.mockConverter.convert(Integer.class, "value")).thenThrow(ConversionException.class);
121   
122  1 setStringProperty(new DocumentReference(CURRENT_WIKI, WikiPreferencesConfigurationSource.CLASS_SPACE_NAME,
123    WikiPreferencesConfigurationSource.CLASS_PAGE_NAME), "key", "value");
124   
125  1 Assert.assertEquals("values", this.componentManager.getComponentUnderTest().getProperty("key", Integer.class));
126    }
127   
 
128  1 toggle @Test
129    public void testGetKeys() throws ComponentLookupException, XWikiException
130    {
131  1 setStringProperty(new DocumentReference(CURRENT_WIKI, WikiPreferencesConfigurationSource.CLASS_SPACE_NAME,
132    WikiPreferencesConfigurationSource.CLASS_PAGE_NAME), "key1", "value");
133  1 setStringProperty(new DocumentReference(CURRENT_WIKI, WikiPreferencesConfigurationSource.CLASS_SPACE_NAME,
134    WikiPreferencesConfigurationSource.CLASS_PAGE_NAME), "key2", "value");
135  1 setStringProperty(new DocumentReference(CURRENT_WIKI, WikiPreferencesConfigurationSource.CLASS_SPACE_NAME,
136    WikiPreferencesConfigurationSource.CLASS_PAGE_NAME), "emptykey", "");
137  1 setStringProperty(new DocumentReference(CURRENT_WIKI, WikiPreferencesConfigurationSource.CLASS_SPACE_NAME,
138    WikiPreferencesConfigurationSource.CLASS_PAGE_NAME), "emptykey2", "---");
139   
140  1 List<String> result = this.componentManager.getComponentUnderTest().getKeys();
141   
142  1 Assert.assertEquals(new HashSet<String>(Arrays.asList("key1", "key2")), new HashSet<String>(result));
143    }
144   
 
145  1 toggle @Test
146    public void testContainsKey() throws XWikiException, ComponentLookupException
147    {
148  1 setStringProperty(new DocumentReference(CURRENT_WIKI, WikiPreferencesConfigurationSource.CLASS_SPACE_NAME,
149    WikiPreferencesConfigurationSource.CLASS_PAGE_NAME), "key", "value");
150   
151  1 Assert.assertTrue(this.componentManager.getComponentUnderTest().containsKey("key"));
152  1 Assert.assertFalse(this.componentManager.getComponentUnderTest().containsKey("wrongkey"));
153    }
154   
 
155  1 toggle @Test
156    public void testIsEmpty() throws ComponentLookupException, XWikiException
157    {
158  1 Assert.assertTrue(this.componentManager.getComponentUnderTest().isEmpty());
159   
160  1 setStringProperty(new DocumentReference(CURRENT_WIKI, WikiPreferencesConfigurationSource.CLASS_SPACE_NAME,
161    WikiPreferencesConfigurationSource.CLASS_PAGE_NAME), "key", "value");
162   
163  1 Assert.assertFalse(this.componentManager.getComponentUnderTest().isEmpty());
164    }
165    }