View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  package org.apache.myfaces.tobago.context;
21  
22  import org.apache.commons.collections.IteratorUtils;
23  import org.junit.Assert;
24  import org.junit.Test;
25  
26  import java.util.Arrays;
27  
28  public class MarkupUnitTest {
29    
30    private static final String[] AB = new String[] {"a", "b"};
31  
32    @Test
33    public void testString() {
34      Assert.assertNull(Markup.valueOf((String) null));
35      
36      Assert.assertArrayEquals(new String[] {"accent"}, IteratorUtils.toArray(Markup.valueOf("accent").iterator()));
37      
38      Assert.assertArrayEquals(AB, IteratorUtils.toArray(Markup.valueOf("a,b").iterator()));
39      
40      Assert.assertArrayEquals(AB, IteratorUtils.toArray(Markup.valueOf("a, b").iterator()));
41      
42      Assert.assertArrayEquals(AB, IteratorUtils.toArray(Markup.valueOf(", \ta , ,\n b ,").iterator()));
43    }
44    
45    @Test
46    public void testStringArray() {
47      Assert.assertNull(Markup.valueOf((String[]) null));
48      
49      Assert.assertNull(Markup.valueOf(new String[] {}));
50      
51      Assert.assertArrayEquals(AB, IteratorUtils.toArray(Markup.valueOf(new String[] {"a", "b"}).iterator()));
52      
53      Assert.assertArrayEquals(AB, IteratorUtils.toArray(Markup.valueOf(new String[] {" a ", " b "}).iterator()));
54    }
55    
56    @Test
57    public void testObject() {
58      Assert.assertNull(Markup.valueOf((Object) null));
59  
60      Assert.assertArrayEquals(AB, IteratorUtils.toArray(Markup.valueOf((Object) ", \ta , ,\n b ,").iterator()));
61  
62      Assert.assertArrayEquals(AB, IteratorUtils.toArray(Markup.valueOf((Object) new String[] {"a", "b"}).iterator()));
63      
64      Assert.assertArrayEquals(AB,
65          IteratorUtils.toArray(Markup.valueOf((Object) new String[] {" a ", " b "}).iterator()));
66      
67      Assert.assertArrayEquals(AB, IteratorUtils.toArray(Markup.valueOf(new StringBuilder("a, b")).iterator()));
68      
69      Assert.assertArrayEquals(AB, IteratorUtils.toArray(Markup.valueOf(AB).iterator()));
70      
71      Assert.assertArrayEquals(AB, IteratorUtils.toArray(Markup.valueOf(Arrays.asList(AB)).iterator()));
72    }
73    
74    @Test
75    public void testMarkup() {
76      Assert.assertNull(Markup.valueOf((Markup) null));
77      
78      Markup accent = Markup.valueOf("accent");
79      Assert.assertSame(accent, Markup.valueOf(accent));
80    }
81  
82    @Test
83    public void testAdd() {
84      Markup a = Markup.valueOf("a");
85      Markup b = Markup.valueOf("b");
86      Markup c = Markup.valueOf("c");
87      Markup ab = Markup.valueOf("a,b");
88      Markup abc = Markup.valueOf("a,b,c");
89      Assert.assertEquals(a, Markup.NULL.add(a));
90      Assert.assertEquals(ab, a.add(b));
91      Assert.assertEquals(abc, ab.add(c));
92      Assert.assertEquals(abc, ab.add(abc));
93      Assert.assertSame(a, a.add(a));
94      Assert.assertSame(ab, ab.add(a));
95      Assert.assertSame(ab, ab.add(ab));
96    }
97  
98    @Test
99    public void testRemove() {
100     Markup a = Markup.valueOf("a");
101     Markup b = Markup.valueOf("b");
102     Markup c = Markup.valueOf("c");
103     Markup ab = Markup.valueOf("a,b");
104     Markup bc = Markup.valueOf("b,c");
105     Markup abc = Markup.valueOf("a,b,c");
106     Assert.assertEquals(Markup.NULL, Markup.NULL.remove(a));
107     Assert.assertEquals(a, a.remove(b));
108     Assert.assertEquals(Markup.NULL, a.remove(a));
109     Assert.assertEquals(b, ab.remove(a));
110     Assert.assertEquals(a, ab.remove(b));
111     Assert.assertEquals(c, abc.remove(ab));
112     Assert.assertEquals(a, ab.remove(bc));
113     Assert.assertEquals(Markup.NULL, abc.remove(abc));
114     Assert.assertSame(b, b.remove(a));
115     Assert.assertSame(ab, ab.remove(c));
116   }
117 
118   @Test
119   public void testContains() {
120     Markup a = Markup.valueOf("a");
121     Markup ab = Markup.valueOf("a,b");
122     Assert.assertFalse(Markup.NULL.contains("a"));
123     Assert.assertTrue(a.contains("a"));
124     Assert.assertFalse(a.contains("b"));
125     Assert.assertTrue(ab.contains("a"));
126     Assert.assertTrue(ab.contains("b"));
127     Assert.assertFalse(ab.contains("c"));
128   }
129 }