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.junit.Assert;
23  import org.junit.Test;
24  
25  import java.lang.reflect.Field;
26  import java.util.ArrayList;
27  import java.util.Arrays;
28  import java.util.Iterator;
29  import java.util.List;
30  
31  public class MarkupUnitTest {
32  
33    private static final String[] AB = new String[]{"a", "b"};
34  
35    @Test
36    public void testString() {
37      Assert.assertNull(Markup.valueOf((String) null));
38  
39      Assert.assertArrayEquals(new String[]{"accent"}, toArray(Markup.valueOf("accent").iterator()));
40  
41      Assert.assertArrayEquals(AB, toArray(Markup.valueOf("a,b").iterator()));
42  
43      Assert.assertArrayEquals(AB, toArray(Markup.valueOf("a, b").iterator()));
44  
45      Assert.assertArrayEquals(AB, toArray(Markup.valueOf("a b").iterator()));
46  
47      Assert.assertArrayEquals(AB, toArray(Markup.valueOf(", \ta , ,\n b ,").iterator()));
48    }
49  
50    @Test
51    public void testStringArray() {
52      Assert.assertNull(Markup.valueOf((String[]) null));
53  
54      Assert.assertNull(Markup.valueOf(new String[]{}));
55  
56      Assert.assertArrayEquals(AB, toArray(Markup.valueOf(new String[]{"a", "b"}).iterator()));
57  
58      Assert.assertArrayEquals(AB, toArray(Markup.valueOf(new String[]{" a ", " b "}).iterator()));
59    }
60  
61    @Test
62    public void testObject() {
63      Assert.assertNull(Markup.valueOf((Object) null));
64  
65      Assert.assertArrayEquals(AB, toArray(Markup.valueOf((Object) ", \ta , ,\n b ,").iterator()));
66  
67      Assert.assertArrayEquals(AB, toArray(Markup.valueOf((Object) new String[]{"a", "b"}).iterator()));
68  
69      Assert.assertArrayEquals(AB,
70          toArray(Markup.valueOf((Object) new String[]{" a ", " b "}).iterator()));
71  
72      Assert.assertArrayEquals(AB, toArray(Markup.valueOf(new StringBuilder("a, b")).iterator()));
73  
74      Assert.assertArrayEquals(AB, toArray(Markup.valueOf(AB).iterator()));
75  
76      Assert.assertArrayEquals(AB, toArray(Markup.valueOf(Arrays.asList(AB)).iterator()));
77    }
78  
79    @Test
80    public void testMarkup() {
81      Assert.assertNull(Markup.valueOf((Markup) null));
82  
83      final Markup accent = Markup.valueOf("accent");
84      Assert.assertSame(accent, Markup.valueOf(accent));
85    }
86  
87    @Test
88    public void testAdd() {
89      final Markup a = Markup.valueOf("a");
90      final Markup b = Markup.valueOf("b");
91      final Markup c = Markup.valueOf("c");
92      final Markup ab = Markup.valueOf("a,b");
93      final Markup abc = Markup.valueOf("a,b,c");
94      Assert.assertEquals(a, Markup.NULL.add(a));
95      Assert.assertEquals(ab, a.add(b));
96      Assert.assertEquals(abc, ab.add(c));
97      Assert.assertEquals(abc, ab.add(abc));
98      Assert.assertSame(a, a.add(a));
99      Assert.assertSame(ab, ab.add(a));
100     Assert.assertSame(ab, ab.add(ab));
101   }
102 
103   @Test
104   public void testRemove() {
105     final Markup a = Markup.valueOf("a");
106     final Markup b = Markup.valueOf("b");
107     final Markup c = Markup.valueOf("c");
108     final Markup ab = Markup.valueOf("a,b");
109     final Markup bc = Markup.valueOf("b,c");
110     final Markup abc = Markup.valueOf("a,b,c");
111     Assert.assertEquals(Markup.NULL, Markup.NULL.remove(a));
112     Assert.assertEquals(a, a.remove(b));
113     Assert.assertEquals(Markup.NULL, a.remove(a));
114     Assert.assertEquals(b, ab.remove(a));
115     Assert.assertEquals(a, ab.remove(b));
116     Assert.assertEquals(c, abc.remove(ab));
117     Assert.assertEquals(a, ab.remove(bc));
118     Assert.assertEquals(Markup.NULL, abc.remove(abc));
119     Assert.assertSame(b, b.remove(a));
120     Assert.assertSame(ab, ab.remove(c));
121   }
122 
123   @Test
124   public void testContainsString() {
125     final Markup a = Markup.valueOf("a");
126     final Markup ab = Markup.valueOf("a,b");
127     Assert.assertFalse(Markup.NULL.contains("a"));
128     Assert.assertTrue(a.contains("a"));
129     Assert.assertTrue(a.contains((String) null));
130     Assert.assertFalse(a.contains("b"));
131     Assert.assertTrue(ab.contains("a"));
132     Assert.assertTrue(ab.contains("b"));
133     Assert.assertFalse(ab.contains("c"));
134   }
135 
136   @Test
137   public void testContainsMarkup() {
138     final Markup a = Markup.valueOf("a");
139     final Markup ab = Markup.valueOf("a,b");
140     Assert.assertFalse(Markup.NULL.contains(Markup.valueOf("a")));
141     Assert.assertTrue(a.contains(Markup.NULL));
142     Assert.assertTrue(a.contains((Markup) null));
143     Assert.assertTrue(a.contains(Markup.valueOf("a")));
144     Assert.assertFalse(a.contains(Markup.valueOf("b")));
145     Assert.assertTrue(ab.contains(Markup.valueOf("a")));
146     Assert.assertTrue(ab.contains(Markup.valueOf("b")));
147     Assert.assertFalse(ab.contains(Markup.valueOf("c")));
148     Assert.assertTrue(ab.contains(Markup.valueOf("a,b")));
149     Assert.assertTrue(ab.contains(Markup.valueOf("a,a")));
150     Assert.assertFalse(ab.contains(Markup.valueOf("a,c")));
151     Assert.assertFalse(ab.contains(Markup.valueOf("a,c,d,e,c,f,e,f")));
152   }
153 
154   @Test
155   public void testIsEmpty() {
156     final Markup a = Markup.valueOf("a");
157     final Markup ab = Markup.valueOf("a,b");
158 
159     Assert.assertFalse(a.isEmpty());
160     Assert.assertFalse(ab.isEmpty());
161     Assert.assertTrue(Markup.NULL.isEmpty());
162   }
163 
164   public static Object[] toArray(final Iterator<?> iterator) {
165     final List<Object> list = new ArrayList<Object>(10);
166     while (iterator.hasNext()) {
167       list.add(iterator.next());
168     }
169     return list.toArray();
170   }
171 
172   @Test
173   public void testNames() throws IllegalAccessException {
174     final List<Field> markups = new ArrayList<>();
175     final List<Field> strings = new ArrayList<>();
176 
177     for (final Field field : Markup.class.getFields()) {
178       if (field.getName().startsWith("STRING")) {
179         strings.add(field);
180       } else if (!field.getName().equals("NULL")) {
181         markups.add(field);
182       }
183     }
184 
185     Assert.assertEquals("Is for every markup a string constant defined?", markups.size(), strings.size());
186 
187     for (final Field markupField : markups) {
188       final Markup markup = (Markup) markupField.get(null);
189 
190       boolean pendantFound = false;
191       for (final Field stringField : strings) {
192         if (stringField.getName().equals("STRING_" + markupField.getName())) {
193           final String string = (String) stringField.get(null);
194 
195           Assert.assertEquals(markup.toString(), string);
196           pendantFound = true;
197         }
198       }
199 
200       Assert.assertTrue("Could be a string pendant found for " + markup + "?", pendantFound);
201     }
202   }
203 }