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

File StaticListClassTest.java

 

Code metrics

4
91
9
1
256
169
11
0.12
10.11
9
1.22

Classes

Class Line # Actions
StaticListClassTest 45 91 0% 11 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.objects.classes;
21   
22    import java.util.Arrays;
23    import java.util.List;
24   
25    import org.codehaus.plexus.util.StringUtils;
26    import org.junit.Rule;
27    import org.junit.Test;
28    import org.xwiki.model.reference.DocumentReference;
29   
30    import com.xpn.xwiki.objects.BaseObject;
31    import com.xpn.xwiki.objects.ListProperty;
32    import com.xpn.xwiki.test.MockitoOldcoreRule;
33    import com.xpn.xwiki.test.reference.ReferenceComponentList;
34   
35    import static org.junit.Assert.assertEquals;
36    import static org.junit.Assert.assertTrue;
37    import static org.mockito.Mockito.doReturn;
38   
39    /**
40    * Unit tests for {@link StaticListClass}.
41    *
42    * @version $Id: 0f73c0636b8a0f79fab5abbaec6bd853acb03796 $
43    */
44    @ReferenceComponentList
 
45    public class StaticListClassTest
46    {
47    /**
48    * Static list values that contain HTML special characters that need to be escaped in the HTML display.
49    */
50    private static final List<String> VALUES_WITH_HTML_SPECIAL_CHARS = Arrays.asList("a<b>c", "1\"2'3", "x{y&z");
51   
52    @Rule
53    public MockitoOldcoreRule oldcore = new MockitoOldcoreRule();
54   
55    /** Tests that {@link StaticListClass#getList} returns values sorted according to the property's sort option. */
 
56  1 toggle @Test
57    public void testGetListIsSorted()
58    {
59  1 StaticListClass listClass = new StaticListClass();
60  1 listClass.setValues("a=A|c=D|d=C|b");
61   
62  1 assertEquals("Default order was not preserved.", "[a, c, d, b]",
63    listClass.getList(this.oldcore.getXWikiContext()).toString());
64  1 listClass.setSort("none");
65  1 assertEquals("Default order was not preserved.", "[a, c, d, b]",
66    listClass.getList(this.oldcore.getXWikiContext()).toString());
67  1 listClass.setSort("id");
68  1 assertEquals("Items were not ordered by ID.", "[a, b, c, d]",
69    listClass.getList(this.oldcore.getXWikiContext()).toString());
70  1 listClass.setSort("value");
71  1 assertEquals("Items were not ordered by value.", "[a, b, d, c]",
72    listClass.getList(this.oldcore.getXWikiContext()).toString());
73    }
74   
75    /**
76    * Tests that the list values are joined using the specified separator without being XML-encoded.
77    *
78    * @see "XWIKI-9680: Apostrophes in static list value are encoded on .display()"
79    */
 
80  1 toggle @Test
81    public void testDisplayView()
82    {
83  1 ListProperty listProperty = new ListProperty();
84  1 listProperty.setValue(VALUES_WITH_HTML_SPECIAL_CHARS);
85   
86  1 String propertyName = "foo";
87  1 BaseObject object = new BaseObject();
88  1 object.addField(propertyName, listProperty);
89   
90  1 StaticListClass staticListClass = new StaticListClass();
91  1 staticListClass.setSeparator(" * ");
92  1 staticListClass.setValues(VALUES_WITH_HTML_SPECIAL_CHARS.get(0) + '|' + VALUES_WITH_HTML_SPECIAL_CHARS.get(1)
93    + '=' + StringUtils.reverse(VALUES_WITH_HTML_SPECIAL_CHARS.get(1)) + '|'
94    + VALUES_WITH_HTML_SPECIAL_CHARS.get(2));
95  1 assertEquals("a<b>c * 3'2\"1 * x{y&z", staticListClass.displayView(propertyName, "", object, null));
96    }
97   
98    /**
99    * Tests the HTML output produced in edit mode.
100    *
101    * @param displayType the display type (input, radio, select, etc.)
102    * @param selectedValues the selected values
103    * @param expectedHTML the expected HTML output
104    */
 
105  4 toggle private void testDisplayEdit(String displayType, List<String> selectedValues, String expectedHTML)
106    {
107  4 ListProperty listProperty = new ListProperty();
108  4 listProperty.setValue(selectedValues);
109   
110    // Use special XML characters, even if they are not valid inside an XML name, just to test the XML escaping.
111  4 String propertyName = "b&a<r";
112  4 String prefix = "w>v";
113  4 BaseObject object = new BaseObject();
114  4 object.addField(propertyName, listProperty);
115   
116  4 StaticListClass staticListClass = new StaticListClass();
117  4 staticListClass.setSize(7);
118  4 StringBuilder values = new StringBuilder();
119  4 for (String value : VALUES_WITH_HTML_SPECIAL_CHARS) {
120  12 if (values.length() > 0) {
121  8 values.append('|');
122    }
123  12 values.append(value).append('=').append(StringUtils.reverse(value));
124    }
125  4 staticListClass.setValues(values.toString());
126   
127  4 staticListClass.setDisplayType(displayType);
128  4 assertEquals(expectedHTML, staticListClass.displayEdit(propertyName, prefix, object, null));
129    }
130   
131    /**
132    * Tests the 'input' display type.
133    */
 
134  1 toggle @Test
135    public void testDisplayEditInput()
136    {
137  1 testDisplayEdit("input", VALUES_WITH_HTML_SPECIAL_CHARS, "<input size='7' id='w&#62;vb&#38;a&#60;r' "
138    + "value='a&#60;b&#62;c|1&#34;2&#39;3|x&#123;y&#38;z' name='w&#62;vb&#38;a&#60;r' type='text'/>");
139    }
140   
141    /**
142    * Tests the 'select' display type.
143    */
 
144  1 toggle @Test
145    public void testDisplayEditSelect()
146    {
147  1 String expectedHTML = "<select id='w&#62;vb&#38;a&#60;r' name='w&#62;vb&#38;a&#60;r' size='7'>"
148    + "<option value='a&#60;b&#62;c' label='c&#62;b&#60;a'>c&#62;b&#60;a</option>"
149    + "<option selected='selected' value='1&#34;2&#39;3' label='3&#39;2&#34;1'>3&#39;2&#34;1</option>"
150    + "<option value='x&#123;y&#38;z' label='z&#38;y&#123;x'>z&#38;y&#123;x</option>"
151    + "</select><input name='w&#62;vb&#38;a&#60;r' type='hidden' value=''/>";
152  1 testDisplayEdit("select", Arrays.asList(VALUES_WITH_HTML_SPECIAL_CHARS.get(1)), expectedHTML);
153    }
154   
155    /**
156    * Tests the 'radio' display type.
157    */
 
158  1 toggle @Test
159    public void testDisplayEditRadio()
160    {
161  1 StringBuilder expectedHTML = new StringBuilder();
162  1 expectedHTML.append("<label class=\"xwiki-form-listclass\" for=\"xwiki-form-b&#38;a&#60;r-0-0\">");
163  1 expectedHTML.append("<input id='xwiki-form-b&#38;a&#60;r-0-0' value='a&#60;b&#62;c'");
164  1 expectedHTML.append(" name='w&#62;vb&#38;a&#60;r' type='radio'/>c&#62;b&#60;a</label>");
165  1 expectedHTML.append("<label class=\"xwiki-form-listclass\" for=\"xwiki-form-b&#38;a&#60;r-0-1\">");
166  1 expectedHTML.append("<input id='xwiki-form-b&#38;a&#60;r-0-1' checked='checked' value='1&#34;2&#39;3' ");
167  1 expectedHTML.append("name='w&#62;vb&#38;a&#60;r' type='radio'/>3&#39;2&#34;1</label>");
168  1 expectedHTML.append("<label class=\"xwiki-form-listclass\" for=\"xwiki-form-b&#38;a&#60;r-0-2\">");
169  1 expectedHTML.append("<input id='xwiki-form-b&#38;a&#60;r-0-2' value='x&#123;y&#38;z'");
170  1 expectedHTML.append(" name='w&#62;vb&#38;a&#60;r' type='radio'/>z&#38;y&#123;x</label>");
171  1 expectedHTML.append("<input name='w&#62;vb&#38;a&#60;r' type='hidden' value=''/>");
172  1 testDisplayEdit("radio", Arrays.asList(VALUES_WITH_HTML_SPECIAL_CHARS.get(1)), expectedHTML.toString());
173    }
174   
175    /**
176    * Tests the 'checkbox' display type.
177    */
 
178  1 toggle @Test
179    public void testDisplayEditCheckbox()
180    {
181  1 StringBuilder expectedHTML = new StringBuilder();
182  1 expectedHTML.append("<label class=\"xwiki-form-listclass\" for=\"xwiki-form-b&#38;a&#60;r-0-0\">");
183  1 expectedHTML.append("<input id='xwiki-form-b&#38;a&#60;r-0-0' value='a&#60;b&#62;c'");
184  1 expectedHTML.append(" name='w&#62;vb&#38;a&#60;r' type='checkbox'/>c&#62;b&#60;a</label>");
185  1 expectedHTML.append("<label class=\"xwiki-form-listclass\" for=\"xwiki-form-b&#38;a&#60;r-0-1\">");
186  1 expectedHTML.append("<input id='xwiki-form-b&#38;a&#60;r-0-1' checked='checked' value='1&#34;2&#39;3' ");
187  1 expectedHTML.append("name='w&#62;vb&#38;a&#60;r' type='checkbox'/>3&#39;2&#34;1</label>");
188  1 expectedHTML.append("<label class=\"xwiki-form-listclass\" for=\"xwiki-form-b&#38;a&#60;r-0-2\">");
189  1 expectedHTML.append("<input id='xwiki-form-b&#38;a&#60;r-0-2' value='x&#123;y&#38;z'");
190  1 expectedHTML.append(" name='w&#62;vb&#38;a&#60;r' type='checkbox'/>z&#38;y&#123;x</label>");
191  1 expectedHTML.append("<input name='w&#62;vb&#38;a&#60;r' type='hidden' value=''/>");
192  1 testDisplayEdit("checkbox", Arrays.asList(VALUES_WITH_HTML_SPECIAL_CHARS.get(1)), expectedHTML.toString());
193    }
194   
195    /**
196    * Tests the hidden display type.
197    */
 
198  1 toggle @Test
199    public void testDisplayHidden()
200    {
201  1 ListProperty listProperty = new ListProperty();
202  1 listProperty.setValue(VALUES_WITH_HTML_SPECIAL_CHARS);
203   
204    // Use special XML characters, even if they are not valid inside an XML name, just to test the XML escaping.
205  1 String propertyName = "f<o&o";
206  1 BaseObject object = new BaseObject();
207  1 object.addField(propertyName, listProperty);
208   
209  1 assertEquals(
210    "<input id='f&#60;o&#38;o' value='a&#60;b&#62;c|1&#34;2&#39;3|x&#123;y&#38;z' "
211    + "name='f&#60;o&#38;o' type='hidden'/>",
212    new StaticListClass().displayHidden(propertyName, "", object, null));
213    }
214   
215    /**
216    * Tests the suggest code generated when "use suggest" is set.
217    */
 
218  1 toggle @Test
219    public void testDisplayEditWithSuggest() throws Exception
220    {
221  1 ListProperty listProperty = new ListProperty();
222  1 listProperty.setValue(VALUES_WITH_HTML_SPECIAL_CHARS);
223   
224    // Use special XML characters, even if they are not valid inside an XML name, just to test the XML escaping.
225  1 String propertyName = "b&a<r";
226  1 String prefix = "w>v";
227  1 BaseObject object = new BaseObject();
228  1 object.addField(propertyName, listProperty);
229   
230  1 StaticListClass staticListClass = new StaticListClass();
231  1 BaseClass ownerClass = new BaseClass();
232  1 ownerClass.setDocumentReference(new DocumentReference("xwiki", "ClassSpace", "ClassName"));
233  1 staticListClass.setName(propertyName);
234  1 staticListClass.setObject(ownerClass);
235  1 staticListClass.setSize(7);
236  1 StringBuilder values = new StringBuilder();
237  1 for (String value : VALUES_WITH_HTML_SPECIAL_CHARS) {
238  3 if (values.length() > 0) {
239  2 values.append('|');
240    }
241  3 values.append(value).append('=').append(StringUtils.reverse(value));
242    }
243  1 staticListClass.setValues(values.toString());
244   
245  1 staticListClass.setDisplayType("input");
246  1 staticListClass.setPicker(true);
247  1 doReturn("/xwiki/bin/view/Main/WebHome").when(this.oldcore.getSpyXWiki()).getURL("Main.WebHome", "view",
248    this.oldcore.getXWikiContext());
249  1 String output = staticListClass.displayEdit(propertyName, prefix, object, this.oldcore.getXWikiContext());
250  1 System.err.println(output);
251  1 assertTrue(
252    output.contains("new ajaxSuggest(this, &#123;script:&#34;/xwiki/bin/view/Main/WebHome?xpage=suggest&#38;"
253    + "classname=ClassSpace.ClassName&#38;fieldname=b&#38;a&#60;r&#38;firCol=-&#38;"
254    + "secCol=-&#38;&#34;, varname:&#34;input&#34;} )"));
255    }
256    }