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.renderkit.html;
21  
22  import org.apache.myfaces.tobago.component.Attributes;
23  import org.apache.myfaces.tobago.component.RendererTypes;
24  import org.apache.myfaces.tobago.component.UIButton;
25  import org.apache.myfaces.tobago.internal.component.AbstractUICommand;
26  import org.apache.myfaces.tobago.internal.config.AbstractTobagoTestBase;
27  import org.apache.myfaces.tobago.internal.context.DateTimeI18n;
28  import org.apache.myfaces.tobago.internal.util.StringUtils;
29  import org.apache.myfaces.tobago.util.ComponentUtils;
30  import org.apache.myfaces.tobago.util.CreateComponentUtils;
31  import org.junit.Assert;
32  import org.junit.Test;
33  
34  import java.util.Arrays;
35  import java.util.Locale;
36  
37  // using ' instead of " to make it better readable.
38  
39  public class JsonUtilsUnitTest extends AbstractTobagoTestBase {
40  
41    @Test
42    public void empty() {
43      final CommandMap map = new CommandMap();
44      Assert.assertEquals("{}", JsonUtils.encode(map));
45    }
46  
47    @Test
48    public void click() {
49      final CommandMap map = new CommandMap();
50      map.setClick(new Command(null, null, null, null, "", null, null, null, null, null));
51      final String expected = "{'click':{}}".replaceAll("'", "\"");
52      Assert.assertEquals(expected, JsonUtils.encode(map));
53    }
54  
55    @Test
56    public void change() {
57      final CommandMap map = new CommandMap();
58      map.addCommand("change", new Command(null, null, null, null, null, null, null, null, null, null));
59      final String expected = "{'change':{}}".replaceAll("'", "\"");
60      Assert.assertEquals(expected, JsonUtils.encode(map));
61    }
62  
63    @Test
64    public void two() {
65      final CommandMap map = new CommandMap();
66      map.addCommand("click", new Command(null, null, "target", null, null, null, null, null, null, null));
67      map.addCommand("change", new Command(null, null, null, null, null, null, null, null, null, null));
68      final String expected = "{'click':{'target':'target'},'change':{}}".replaceAll("'", "\"");
69      Assert.assertEquals(expected, JsonUtils.encode(map));
70    }
71  
72    @Test
73    public void transition() {
74      final CommandMap commandMap = new CommandMap();
75      commandMap.setClick(new Command(null, false, null, null, null, null, null, null, null, null));
76      final String expected = "{'click':{'transition':false}}".replaceAll("'", "\"");
77      Assert.assertEquals(expected, JsonUtils.encode(commandMap));
78    }
79  
80    @Test
81    public void more() {
82      final CommandMap map = new CommandMap();
83      final AbstractUICommand command = (AbstractUICommand)
84          CreateComponentUtils.createComponent(facesContext, UIButton.COMPONENT_TYPE, RendererTypes.Button, "command");
85      ComponentUtils.setAttribute(command, Attributes.popupClose, "immediate");
86      command.setRenderedPartially(new String[]{"popup"});
87  
88      map.setClick(new Command(
89          "ns:actionId",
90          false,
91          "_blank", "http://www.apache.org/",
92          StringUtils.join(Arrays.asList("id1", "id2"), ' '),
93          "id_focus",
94          "Really?", 1000, Popup.createPopup(command), true));
95      final String expected = (
96          "{"
97              + "'click':{"
98              + "'action':'ns:actionId',"
99              + "'transition':false,"
100             + "'target':'_blank',"
101             + "'url':'http://www.apache.org/',"
102             + "'partially':'id1 id2',"
103             + "'focus':'id_focus',"
104             + "'confirmation':'Really?',"
105             + "'delay':1000,"
106             + "'popup':{"
107             + "'command':'close',"
108             + "'immediate':true"
109             + "},"
110             + "'omit':true"
111             + "}"
112             + "}").replaceAll("'", "\"");
113     Assert.assertEquals(expected, JsonUtils.encode(map));
114   }
115 
116   @Test
117   public void monthNames() {
118     final DateTimeI18n dateTimeI18n = DateTimeI18n.valueOf(Locale.GERMANY);
119     final String marchShort = dateTimeI18n.getMonthNamesShort()[2]; // different with JDK 1.8.0_51 and 1.8.0_60
120     final String expected
121         = ("{'monthNames':['Januar','Februar','März','April','Mai','Juni',"
122         + "'Juli','August','September','Oktober','November','Dezember'],"
123         + "'monthNamesShort':['Jan','Feb','" + marchShort + "','Apr','Mai','Jun','Jul','Aug','Sep','Okt','Nov','Dez'],"
124         + "'dayNames':['Sonntag','Montag','Dienstag','Mittwoch','Donnerstag','Freitag','Samstag'],"
125         + "'dayNamesShort':['So','Mo','Di','Mi','Do','Fr','Sa'],"
126         + "'dayNamesMin':['So','Mo','Di','Mi','Do','Fr','Sa'],"
127         + "'firstDay':1}").replaceAll("'", "\"");
128 
129     Assert.assertEquals(expected, JsonUtils.encode(dateTimeI18n));
130   }
131 
132   @Test
133   public void decodeIntegerArray() {
134 
135     Assert.assertEquals(Arrays.asList(1, 2, 3, 4), JsonUtils.decodeIntegerArray("[1,2,3,4]"));
136 
137     Assert.assertEquals(Arrays.asList(1, 2, 3, 4), JsonUtils.decodeIntegerArray(" [ 1 , 2 , 3 , 4 ] "));
138 
139     Assert.assertEquals(Arrays.asList(1), JsonUtils.decodeIntegerArray("[1]"));
140 
141     Assert.assertEquals(Arrays.asList(), JsonUtils.decodeIntegerArray("[]"));
142 
143     Assert.assertEquals(Arrays.asList(1000000000, 2, 3, 4), JsonUtils.decodeIntegerArray("[1000000000,2,3,4]"));
144 
145     Assert.assertEquals(Arrays.asList(2, 3, 4), JsonUtils.decodeIntegerArray("[null,2,3,4]"));
146 
147     Assert.assertEquals(null, JsonUtils.decodeIntegerArray("1,2,3,4"));
148   }
149 
150 }