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.internal.util;
21  
22  import org.apache.myfaces.tobago.component.Attributes;
23  import org.apache.myfaces.tobago.component.ClientBehaviors;
24  import org.apache.myfaces.tobago.component.RendererTypes;
25  import org.apache.myfaces.tobago.component.Tags;
26  import org.apache.myfaces.tobago.context.Markup;
27  import org.apache.myfaces.tobago.internal.component.AbstractUICommand;
28  import org.apache.myfaces.tobago.internal.config.AbstractTobagoTestBase;
29  import org.apache.myfaces.tobago.internal.context.DateTimeI18n;
30  import org.apache.myfaces.tobago.internal.renderkit.Collapse;
31  import org.apache.myfaces.tobago.internal.renderkit.Command;
32  import org.apache.myfaces.tobago.internal.renderkit.CommandMap;
33  import org.apache.myfaces.tobago.layout.Measure;
34  import org.apache.myfaces.tobago.layout.MeasureList;
35  import org.apache.myfaces.tobago.util.ComponentUtils;
36  import org.junit.jupiter.api.Assertions;
37  import org.junit.jupiter.api.Test;
38  
39  import java.util.ArrayList;
40  import java.util.Arrays;
41  import java.util.List;
42  import java.util.Locale;
43  
44  // using ' instead of " to make it better readable.
45  
46  public class JsonUtilsUnitTest extends AbstractTobagoTestBase {
47  
48    @Test
49    public void empty() {
50      final CommandMap map = new CommandMap();
51      Assertions.assertEquals("{}", JsonUtils.encode(map));
52  
53      Assertions.assertNull(JsonUtils.encode((CommandMap) null));
54    }
55  
56    @Test
57    public void click() {
58      final CommandMap map = new CommandMap();
59      map.setClick(new Command(null, null, null, "", null, null, null, null, null, null));
60      final String expected = "{'click':{}}".replaceAll("'", "\"");
61      Assertions.assertEquals(expected, JsonUtils.encode(map));
62    }
63  
64    @Test
65    public void change() {
66      final CommandMap map = new CommandMap();
67      map.addCommand(ClientBehaviors.change, new Command(null, null, null, null, null, null, null, null, null, null));
68      final String expected = "{'change':{}}".replaceAll("'", "\"");
69      Assertions.assertEquals(expected, JsonUtils.encode(map));
70    }
71  
72    @Test
73    public void two() {
74      final CommandMap map = new CommandMap();
75      map.addCommand(ClientBehaviors.click, new Command(null, null, "target", null, null, null, null, null, null, null));
76      map.addCommand(ClientBehaviors.change, new Command(null, null, null, null, null, null, null, null, null, null));
77      final String expected = "{'click':{'target':'target'},'change':{}}".replaceAll("'", "\"");
78      Assertions.assertEquals(expected, JsonUtils.encode(map));
79    }
80  
81    @Test
82    public void transition() {
83      final CommandMap commandMap = new CommandMap();
84      commandMap.setClick(new Command(null, false, null, null, null, null, null, null, null, null));
85      final String expected = "{'click':{'transition':false}}".replaceAll("'", "\"");
86      Assertions.assertEquals(expected, JsonUtils.encode(commandMap));
87    }
88  
89    @Test
90    public void more() {
91      final CommandMap map = new CommandMap();
92      final AbstractUICommand command = (AbstractUICommand)
93          ComponentUtils.createComponent(facesContext, Tags.button.componentType(), RendererTypes.Button, "command");
94      ComponentUtils.setAttribute(command, Attributes.popupClose, "immediate");
95  
96      map.setClick(new Command(
97          "ns:actionId",
98          false,
99          "_blank",
100         StringUtils.join(Arrays.asList("id1", "id2"), ' '),
101         StringUtils.join(Arrays.asList("id1", "id2"), ' '),
102         "id_focus",
103         "Really?", 1000, new Collapse(Collapse.Action.show, "myId"), true));
104     final String expected = (
105         "{"
106             + "'click':{"
107             + "'action':'ns:actionId',"
108             + "'transition':false,"
109             + "'target':'_blank',"
110             + "'execute':'id1 id2',"
111             + "'render':'id1 id2',"
112             + "'collapse':{"
113             + "'transition':'show',"
114             + "'forId':'myId'"
115             + "},"
116             + "'focus':'id_focus',"
117             + "'confirmation':'Really?',"
118             + "'delay':1000,"
119             + "'omit':true"
120             + "}"
121             + "}").replaceAll("'", "\"");
122     Assertions.assertEquals(expected, JsonUtils.encode(map));
123   }
124 
125   @Test
126   public void monthNames() {
127     final DateTimeI18n dateTimeI18n = DateTimeI18n.valueOf(Locale.GERMANY);
128     final String marchShort = dateTimeI18n.getMonthNamesShort()[2]; // different with JDK 1.8.0_51 and 1.8.0_60
129     final String[] dayNamesShort = dateTimeI18n.getDayNamesShort(); // different with JDK 1.8.0 and 1.9.0
130     final String[] dayNamesMin = dateTimeI18n.getDayNamesMin(); // different with JDK 1.8.0 and 1.9.0
131     final String expected
132         = ("{'monthNames':['Januar','Februar','März','April','Mai','Juni',"
133         + "'Juli','August','September','Oktober','November','Dezember'],"
134         + "'monthNamesShort':['Jan','Feb','" + marchShort + "','Apr','Mai','Jun','Jul','Aug','Sep','Okt','Nov','Dez'],"
135         + "'dayNames':['Sonntag','Montag','Dienstag','Mittwoch','Donnerstag','Freitag','Samstag'],"
136         + "'dayNamesShort':['" + dayNamesShort[0] + "','" + dayNamesShort[1] + "','" + dayNamesShort[2] + "','"
137         + dayNamesShort[3] + "','" + dayNamesShort[4] + "','" + dayNamesShort[5] + "','" + dayNamesShort[6] + "'],"
138         + "'dayNamesMin':['" + dayNamesMin[0] + "','" + dayNamesMin[1] + "','" + dayNamesMin[2] + "','"
139         + dayNamesMin[3] + "','" + dayNamesMin[4] + "','" + dayNamesMin[5] + "','" + dayNamesMin[6] + "'],"
140         + "'firstDay':1,'minDays':4,'today':'Today','cancel':'Cancel','clear':'Clear','week':'Week'}")
141         .replaceAll("'", "\"");
142 
143     Assertions.assertEquals(expected, JsonUtils.encode(dateTimeI18n));
144   }
145 
146   @Test
147   public void decodeIntegerArray() {
148 
149     Assertions.assertEquals(Arrays.asList(1, 2, 3, 4), JsonUtils.decodeIntegerArray("[1,2,3,4]"));
150 
151     Assertions.assertEquals(Arrays.asList(1, 2, 3, 4), JsonUtils.decodeIntegerArray(" [ 1 , 2 , 3 , 4 ] "));
152 
153     Assertions.assertEquals(Arrays.asList(1), JsonUtils.decodeIntegerArray("[1]"));
154 
155     Assertions.assertEquals(Arrays.asList(), JsonUtils.decodeIntegerArray("[]"));
156 
157     Assertions.assertEquals(Arrays.asList(1000000000, 2, 3, 4), JsonUtils.decodeIntegerArray("[1000000000,2,3,4]"));
158 
159     Assertions.assertEquals(Arrays.asList(2, 3, 4), JsonUtils.decodeIntegerArray("[null,2,3,4]"));
160 
161     Assertions.assertEquals(Arrays.asList(), JsonUtils.decodeIntegerArray("1,2,3,4"));
162 
163     Assertions.assertEquals(null, JsonUtils.decodeIntegerArray(null));
164   }
165 
166   @Test
167   public void encodeStringArray() {
168     Assertions.assertEquals("[\"A-rập Xê-út (Tiếng A-rập)\"]",
169         JsonUtils.encode(new String[]{"A-rập Xê-út (Tiếng A-rập)"}));
170 
171     Assertions.assertEquals("[\"foo\\\"bar\"]", JsonUtils.encode(new String[]{"foo\"bar"}));
172 
173     Assertions.assertEquals(null, JsonUtils.encode((String[]) null));
174   }
175 
176   @Test
177   public void encodeMarkup() {
178     final Markup a = Markup.valueOf("a");
179     final Markup ab = Markup.valueOf("a,b");
180 
181     final String expectedA = "['a']".replaceAll("'", "\"");
182     final String expectedAb = "['a','b']".replaceAll("'", "\"");
183 
184     Assertions.assertEquals(expectedA, JsonUtils.encode(a));
185     Assertions.assertEquals(expectedAb, JsonUtils.encode(ab));
186     Assertions.assertNull(JsonUtils.encode(Markup.NULL));
187     Assertions.assertNull(JsonUtils.encode((Markup) null));
188 
189   }
190 
191   @Test
192   public void testCommandMap() {
193     CommandMap map = new CommandMap();
194     map.addCommand(
195         ClientBehaviors.blur,
196         new Command(
197             "doit", false, "field", "execute", "render", "focus", "Do \"you\" want?", 100,
198             new Collapse(Collapse.Action.hide, "box"), false));
199 
200     final String expected
201         = ("{'blur':"
202         + "{'action':'doit',"
203         + "'transition':false,"
204         + "'target':'field',"
205         + "'execute':'execute',"
206         + "'render':'render',"
207         + "'collapse':{'transition':'hide','forId':'box'},"
208         + "'focus':'focus',"
209         + "'confirmation':'Do \\'you\\' want?',"
210         + "'delay':100}}").replaceAll("'", "\"");
211     Assertions.assertEquals(expected, JsonUtils.encode(map), "command map");
212   }
213 
214   @Test
215   public void encodeMeasureList() {
216     final MeasureList measureList = new MeasureList();
217     measureList.add(Measure.AUTO);
218     measureList.add(Measure.FRACTION1);
219     measureList.add(Measure.valueOf(100));
220 
221     Assertions.assertEquals(
222         "{\"name\":[\"auto\",1.0,{\"measure\":\"100px\"}]}",
223         JsonUtils.encode(measureList, "name"));
224   }
225 
226   @Test
227   public void encodeBooleanArray() {
228     final Boolean[] array = new Boolean[]{true, false, true};
229     Assertions.assertEquals("[true,false,true]", JsonUtils.encode(array));
230 
231     Assertions.assertEquals(null, JsonUtils.encode((Boolean[]) null));
232 
233     Assertions.assertEquals("[]", JsonUtils.encode(new Boolean[0]));
234   }
235 
236   @Test
237   public void encodeIntegerArray() {
238     final Integer[] array = new Integer[]{-1_000_000_000, 0, 42};
239     Assertions.assertEquals("[-1000000000,0,42]", JsonUtils.encode(array));
240 
241     Assertions.assertEquals(null, JsonUtils.encode((Integer[]) null));
242 
243     Assertions.assertEquals("[]", JsonUtils.encode(new Integer[0]));
244   }
245 
246   @Test
247   public void encodeIntegerList() {
248     final List<Integer> list = Arrays.asList(-1_000_000_000, 0, 42);
249     Assertions.assertEquals("[-1000000000,0,42]", JsonUtils.encode(list));
250 
251     Assertions.assertEquals(null, JsonUtils.encode((List<Integer>) null));
252 
253     Assertions.assertEquals("[]", JsonUtils.encode(new ArrayList<Integer>()));
254   }
255 
256   @Test
257   public void encodeEmptyArray() {
258     Assertions.assertEquals("[]", JsonUtils.encodeEmptyArray());
259   }
260 
261 }