1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package com.xpn.xwiki.objects.classes

File StaticListClassTest.java

 

Code metrics

4
99
10
1
284
185
12
0.12
9.9
10
1.2

Classes

Class Line # Actions
StaticListClassTest 48 99 0% 12 0
1.0100%
 

Contributing tests

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