View Javadoc

1   package org.apache.velocity.tools.generic;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.junit.*;
23  import static org.junit.Assert.*;
24  import java.util.ArrayList;
25  import java.util.Arrays;
26  import java.util.Collection;
27  import java.util.HashMap;
28  import java.util.List;
29  import java.util.Map;
30  import org.apache.velocity.tools.generic.ValueParser;
31  
32  /**
33   * <p>Tests for DisplayTool</p>
34   *
35   * @author Nathan Bubna
36   * @since VelocityTools 2.0
37   * @version $Id$
38   */
39  public class DisplayToolTests {
40  
41      public @Test void methodAlt_Object() throws Exception
42      {
43          DisplayTool display = new DisplayTool();
44          assertSame(display.alt(null), display.getDefaultAlternate());
45          Object notnull = new Object();
46          assertSame(display.alt(notnull), notnull);
47      }
48  
49      public @Test void methodAlt_ObjectObject() throws Exception
50      {
51          DisplayTool display = new DisplayTool();
52          Object notnull = new Object();
53          Object result = display.alt(null, notnull);
54          assertSame(result, notnull);
55          result = display.alt(notnull, "foo");
56          assertSame(result, notnull);
57      }
58  
59      public @Test void methodCapitalize_Object() throws Exception
60      {
61          DisplayTool display = new DisplayTool();
62          assertEquals("FOO", display.capitalize("fOO"));
63          assertEquals("Foo", display.capitalize("Foo"));
64          assertEquals("Foo", display.capitalize("foo"));
65          assertEquals("F", display.capitalize("f"));
66          assertEquals("", display.capitalize(""));
67          assertEquals(null, display.capitalize(null));
68      }
69  
70      public @Test void methodCell_Object() throws Exception
71      {
72          DisplayTool display = new DisplayTool();
73          display.setCellLength(4);
74          assertEquals(null, display.cell(null));
75          assertEquals("foo ", display.cell("foo"));
76          assertEquals("f...", display.cell("foobar"));
77          assertEquals("foob", display.cell("foob"));
78      }
79  
80      public @Test void methodCell_ObjectString() throws Exception
81      {
82          DisplayTool display = new DisplayTool();
83          display.setCellLength(5);
84          assertEquals("test>", display.cell("testing", ">"));
85      }
86  
87      public @Test void methodCell_Objectint() throws Exception
88      {
89          DisplayTool display = new DisplayTool();
90          assertEquals("testing", display.cell("testing", 7));
91          assertEquals("testing ", display.cell("testing", 8));
92          assertEquals("tes...", display.cell("testing", 6));
93      }
94  
95      public @Test void methodCell_ObjectintString() throws Exception
96      {
97          DisplayTool display = new DisplayTool();
98          assertEquals("f", display.cell("foo", 1, null));
99          assertEquals("f", display.cell("foo", 1, "bar"));
100         assertEquals("fbar", display.cell("foobar", 4, "bar"));
101         assertEquals(null, display.cell("foo", 0, "bar"));
102         assertEquals(null, display.cell("foo", -1, "bar"));
103     }
104 
105     public @Test void methodConfigure_Map() throws Exception
106     {
107         DisplayTool display = new DisplayTool();
108         // change the inspected type to Map
109         Map<String,Object> conf = new HashMap<String,Object>();
110         conf.put(DisplayTool.LIST_DELIM_KEY, ";");
111         conf.put(DisplayTool.LIST_FINAL_DELIM_KEY, " und ");
112         conf.put(DisplayTool.TRUNCATE_LENGTH_KEY, "5");
113         conf.put(DisplayTool.TRUNCATE_SUFFIX_KEY, ">");
114         conf.put(DisplayTool.TRUNCATE_AT_WORD_KEY, "true");
115         conf.put(DisplayTool.CELL_LENGTH_KEY, "4");
116         conf.put(DisplayTool.CELL_SUFFIX_KEY, "~");
117         conf.put(DisplayTool.DEFAULT_ALTERNATE_KEY, "n/a");
118         conf.put(DisplayTool.ALLOWED_TAGS_KEY, "img,br");
119         display.configure(conf);
120         assertEquals(";", display.getListDelimiter());
121         assertEquals(" und ", display.getListFinalDelimiter());
122         assertEquals(5, display.getTruncateLength());
123         assertEquals(">", display.getTruncateSuffix());
124         assertEquals(true, display.getTruncateAtWord());
125         assertEquals("~", display.getCellSuffix());
126         assertEquals(4, display.getCellLength());
127         assertEquals("n/a", display.getDefaultAlternate());
128         String[] tags = display.getAllowedTags();
129         assertNotNull(tags);
130         assertEquals("img", tags[0]);
131         assertEquals("br", tags[1]);
132 
133         // ensure that configure is locked now
134         conf.put(DisplayTool.LIST_DELIM_KEY, " & ");
135         display.configure(conf);
136         assertEquals(";", display.getListDelimiter());
137     }
138 
139     public @Test void methodMeasure_String() throws Exception
140     {
141         DisplayTool display = new DisplayTool();
142         assertNull(display.measure(null));
143         DisplayTool.Measurements dims = display.measure("");
144         assertNotNull(dims);
145         assertEquals(1, dims.getHeight());
146         assertEquals(0, dims.getWidth());
147         dims = display.measure("twelve chars");
148         assertEquals(12, dims.getWidth());
149         assertEquals(1, dims.getHeight());
150         dims = display.measure("one\ntwo\nthree");
151         assertEquals(5, dims.getWidth());
152         assertEquals(3, dims.getHeight());
153     }
154 
155     public @Test void methodMessage_StringObjectVarArgs() throws Exception
156     {
157         DisplayTool display = new DisplayTool();
158         assertNull(display.message(null));
159         assertEquals("foo", display.message("foo"));
160         assertEquals("foo", display.message("foo", (Object[])null));
161         assertEquals("foo", display.message("foo", new Object[] {}));
162         assertEquals("foo", display.message("foo", new ArrayList()));
163         assertEquals("foo", display.message("foo", 1));
164         assertEquals("foo bar", display.message("foo {0}", "bar"));
165         assertEquals("foo 2 bar", display.message("foo {1} {0}", "bar", 2));
166     }
167 
168     public @Test void methodPrintf_StringObjectVarArgs() throws Exception
169     {
170         DisplayTool display = new DisplayTool();
171         assertNull(display.printf(null));
172         assertEquals("foo", display.printf("foo"));
173         assertEquals("foo", display.printf("foo", (Object[])null));
174         assertEquals("foo", display.printf("foo", new Object[] {}));
175         assertEquals("foo", display.printf("foo", new ArrayList()));
176         assertEquals("foo", display.printf("foo", 1));
177         assertEquals("foo bar", display.printf("foo %s", "bar"));
178         assertEquals("foo 2 bar", display.printf("foo %2$d %1$s", "bar", 2));
179     }
180 
181     public @Test void methodList_Object() throws Exception
182     {
183         DisplayTool display = new DisplayTool();
184         int[] nums = new int[] { 1, 2, 3 };
185         assertEquals("1, 2 and 3", display.list(nums));
186         display.setListDelimiter(" & ");
187         assertEquals("1 & 2 and 3", display.list(nums));
188         display.setListFinalDelimiter(" & ");
189         assertEquals("1 & 2 & 3", display.list(nums));
190     }
191 
192     public @Test void methodList_ObjectString() throws Exception
193     {
194         DisplayTool display = new DisplayTool();
195         List<Integer> nums = new ArrayList<Integer>();
196         nums.add(1);
197         nums.add(2);
198         nums.add(3);
199         assertEquals(null, display.list(null, null));
200         assertEquals("1null2null3", display.list(nums, null));
201         assertEquals("1, 2, 3", display.list(nums, ", "));
202     }
203 
204     public @Test void methodList_ObjectStringString() throws Exception
205     {
206         DisplayTool display = new DisplayTool();
207         int[] nums = new int[] { 1, 2, 3 };
208         assertEquals(null, display.list(null, null, null));
209         assertEquals("1null2null3", display.list(nums, null, null));
210         assertEquals("1 & 2null3", display.list(nums, " & ", null));
211         assertEquals("1null2 & 3", display.list(nums, null, " & "));
212         assertEquals("123", display.list(nums, "", ""));
213         assertEquals("1; 2 und 3", display.list(nums, "; ", " und "));
214     }
215     
216     public @Test void methodList_ObjectStringStringString() throws Exception
217     {
218         TestBean bean1 = new TestBean(1, "one");
219         TestBean bean2 = new TestBean(2, "two");
220         TestBean bean3 = new TestBean(3, "three");
221         TestBean[] beanArray = new TestBean[] { bean1, bean2, bean3 };
222         List<TestBean> beanList = new ArrayList<TestBean>();
223         beanList.addAll(Arrays.asList(beanArray));
224         
225         DisplayTool display = new DisplayTool();
226         assertEquals(null, display.list(null, null, null, null));
227         assertEquals("1null2null3", display.list(beanArray, null, null, "num"));
228         assertEquals("123", display.list(beanList, "", "", "num"));
229         assertEquals("one, two or three", display.list(beanList, ", ", " or ", "str"));
230     }
231 
232     public @Test void methodSetAllowedTags_StringArray() throws Exception
233     {
234         DisplayTool display = new DisplayTool();
235         assertNull(display.getAllowedTags());
236         String[] tags = new String[] { "img" };
237         display.setAllowedTags(tags);
238         assertEquals(tags, display.getAllowedTags());
239     }
240 
241     public @Test void methodSetCellLength_int() throws Exception
242     {
243         DisplayTool display = new DisplayTool();
244         display.setCellLength(10);
245         assertEquals(10, display.getCellLength());
246     }
247 
248     public @Test void methodSetCellSuffix_String() throws Exception
249     {
250         DisplayTool display = new DisplayTool();
251         display.setCellSuffix("foo");
252         assertEquals("foo", display.getCellSuffix());
253     }
254 
255     public @Test void methodSetDefaultAlternate_String() throws Exception
256     {
257         DisplayTool display = new DisplayTool();
258         display.setDefaultAlternate("foo");
259         assertEquals("foo", display.getDefaultAlternate());
260     }
261 
262     public @Test void methodSetListDelimiter_String() throws Exception
263     {
264         DisplayTool display = new DisplayTool();
265         display.setListDelimiter("foo");
266         assertEquals("foo", display.getListDelimiter());
267     }
268 
269     public @Test void methodSetListFinalDelimiter_String() throws Exception
270     {
271         DisplayTool display = new DisplayTool();
272         display.setListFinalDelimiter("foo");
273         assertEquals("foo", display.getListFinalDelimiter());
274     }
275 
276     public @Test void methodSetTruncateLength_int() throws Exception
277     {
278         DisplayTool display = new DisplayTool();
279         display.setTruncateLength(5);
280         assertEquals(5, display.getTruncateLength());
281     }
282 
283     public @Test void methodSetTruncateSuffix_String() throws Exception
284     {
285         DisplayTool display = new DisplayTool();
286         display.setTruncateSuffix("foo");
287         assertEquals("foo", display.getTruncateSuffix());
288     }
289 
290     public @Test void methodSetTruncateAtWord_String() throws Exception
291     {
292         DisplayTool display = new DisplayTool();
293         assertEquals(false, display.getTruncateAtWord());
294         display.setTruncateAtWord(true);
295         assertEquals(true, display.getTruncateAtWord());
296     }
297 
298     public @Test void methodSpace_int() throws Exception
299     {
300         DisplayTool display = new DisplayTool();
301         assertEquals(null, display.space(-1));
302         assertEquals("", display.space(0));
303         assertEquals(" ", display.space(1));
304         assertEquals("     ", display.space(5));
305     }
306 
307     public @Test void methodTruncate_Object() throws Exception
308     {
309         DisplayTool display = new DisplayTool();
310         display.setTruncateLength(4);
311         assertEquals(null, display.truncate(null));
312         assertEquals("f...", display.truncate("foobar"));
313         assertEquals("foob", display.truncate("foob"));
314         assertEquals("foo", display.truncate("foo"));
315     }
316 
317     public @Test void methodTruncate_ObjectString() throws Exception
318     {
319         DisplayTool display = new DisplayTool();
320         display.setTruncateLength(4);
321         assertEquals(null, display.truncate(null, ">"));
322         assertEquals("foo>", display.truncate("foobar", ">"));
323         assertEquals("foob", display.truncate("foobar", null));
324         assertEquals("foob", display.truncate("foobar", "woogie"));
325         assertEquals("foo", display.truncate("foo", ">"));
326     }
327 
328     public @Test void methodTruncate_Objectint() throws Exception
329     {
330         DisplayTool display = new DisplayTool();
331         assertEquals(null, display.truncate(null, 1));
332         assertEquals(null, display.truncate("foobar", -1));
333         assertEquals(null, display.truncate("foobar", 0));
334         assertEquals("f", display.truncate("foobar", 1));
335         assertEquals("fo", display.truncate("foobar", 2));
336         assertEquals("foo", display.truncate("foobar", 3));
337         assertEquals("f...", display.truncate("foobar", 4));
338         assertEquals("fo...", display.truncate("foobar", 5));
339         assertEquals("foobar", display.truncate("foobar", 6));
340         assertEquals("foobar", display.truncate("foobar", 7));
341     }
342 
343     public @Test void methodTruncate_ObjectintString() throws Exception
344     {
345         DisplayTool display = new DisplayTool();
346         assertEquals(null, display.truncate(null, 0, null));
347         assertEquals(null, display.truncate("foo", 0, null));
348         assertEquals("f", display.truncate("foo", 1, null));
349         assertEquals("foob", display.truncate("foobar", 4, null));
350         assertEquals("foo>", display.truncate("foobar", 4, ">"));
351     }
352     
353     public @Test void methodTruncate_ObjectintStringboolean() throws Exception
354     {
355         DisplayTool display = new DisplayTool();
356         assertEquals(null, display.truncate(null, 0, null, true));
357         assertEquals(null, display.truncate("foo", 0, null, false));
358         assertEquals("f", display.truncate("foo", 1, null, true));
359         assertEquals("long stri>", display.truncate("long string", 10, ">", false));
360         assertEquals("long>", display.truncate("long string", 10, ">", true));
361     }
362 
363     public @Test void methodUncapitalize_Object() throws Exception
364     {
365         DisplayTool display = new DisplayTool();
366         assertEquals(null, display.uncapitalize(null));
367         assertEquals("", display.uncapitalize(""));
368         assertEquals("test", display.uncapitalize("test"));
369         assertEquals("test", display.uncapitalize("Test"));
370         assertEquals("tEST", display.uncapitalize("TEST"));
371     }
372     
373     public @Test void methodBr_Object() throws Exception
374     {
375         DisplayTool display = new DisplayTool();
376         assertEquals(null, display.br(null));
377         assertEquals("", display.br(""));
378         assertEquals("<br />\n", display.br("\n"));
379         assertEquals("line1 <br />\n LINE2", display.br("line1 \n LINE2"));
380     }
381     
382     public @Test void methodStripTags_Object() throws Exception
383     {
384         DisplayTool display = new DisplayTool();
385         String html = "<p>paragraph <a href=\"url\" target='t'>link</a></p> "
386                       + "<h1>header1</h1> <h2>header2</h2> "
387                       + "<br><br/><br  /><b>bold</b>";
388         assertEquals(null, display.stripTags(null));
389         assertEquals("", display.stripTags(""));
390         assertEquals("paragraph link header1 header2 bold", display.stripTags(html));
391     }
392     
393     public @Test void methodStripTags_ObjectStringVarArgs() throws Exception
394     {
395         DisplayTool display = new DisplayTool();
396         String html = "<p>paragraph <a href=\"url\" target='t'>link</a></p> "
397                       + "<h1>header1</h1> <h2>header2</h2> "
398                       + "<br><br/><br  /><b>bold</b>";
399         assertEquals(null, display.stripTags(null, (String[])null));
400         assertEquals("", display.stripTags("","",""));
401         assertEquals("paragraph link <h1>header1</h1> <h2>header2</h2> bold", 
402                 display.stripTags(html, "h1", "h2"));
403         assertEquals("paragraph <a href=\"url\" target='t'>link</a> header1 header2 bold", 
404                 display.stripTags(html, "a"));
405         assertEquals("paragraph link header1 header2 <br><br/><br  /><b>bold</b>", 
406                 display.stripTags(html, "b", "", null, "br"));
407     }
408     
409     public @Test void methodPlural_intString() throws Exception
410     {
411         DisplayTool display = new DisplayTool();
412         assertEquals(null, display.plural(1,null));
413         assertEquals("", display.plural(2,""));
414         assertEquals("items", display.plural(0,"item"));
415         assertEquals("item", display.plural(-1,"item"));
416         assertEquals("555s", display.plural(2,"555"));
417         assertEquals("TOYS", display.plural(2,"TOY"));
418         assertEquals("ladies", display.plural(2,"lady"));
419         assertEquals("foxes", display.plural(2,"fox"));
420         assertEquals("churches", display.plural(2,"church"));
421     }
422     
423     public @Test void methodPlural_intStringString() throws Exception
424     {
425         DisplayTool display = new DisplayTool();
426         assertEquals(null, display.plural(1,null,null));
427         assertEquals("", display.plural(2,"empty",""));
428         assertEquals("men", display.plural(0,"man","men"));
429         assertEquals("mouse", display.plural(-1,"mouse", "mice"));
430     }
431 
432     public class TestBean {
433         private int num;
434         private String str;
435         
436         public TestBean(int num, String str)
437         {
438             this.num = num;
439             this.str = str;
440         }
441         
442         public int getNum()
443         {
444             return num;
445         }
446         public void setNum(int num)
447         {
448             this.num = num;
449         }
450         public String getStr()
451         {
452             return str;
453         }
454         public void setStr(String str)
455         {
456             this.str = str;
457         }
458     }
459 
460 }