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.ComponentTypes;
24  import org.apache.myfaces.tobago.component.RendererTypes;
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.util.CreateComponentUtils;
29  import org.junit.Assert;
30  import org.junit.Test;
31  
32  import java.util.Locale;
33  
34  // using ' instead of " to make it better readable.
35  
36  public class JsonUtilsUnitTest extends AbstractTobagoTestBase {
37  
38    @Test
39    public void empty() {
40      final CommandMap map = new CommandMap();
41      Assert.assertEquals("{}", JsonUtils.encode(map));
42    }
43  
44    @Test
45    public void click() {
46      final CommandMap map = new CommandMap();
47      map.setClick(new Command(null, null, null, null, new String[0], null, null, null, null, null));
48      final String expected = "{'click':{}}".replaceAll("'", "\"");
49      Assert.assertEquals(expected, JsonUtils.encode(map));
50    }
51  
52    @Test
53    public void change() {
54      final CommandMap map = new CommandMap();
55      map.addCommand("change", new Command(null, null, null, null, new String[0], null, null, null, null, null));
56      final String expected = "{'change':{}}".replaceAll("'", "\"");
57      Assert.assertEquals(expected, JsonUtils.encode(map));
58    }
59  
60    @Test
61    public void two() {
62      final CommandMap map = new CommandMap();
63      map.addCommand("click", new Command(null, null, "target", null, new String[0], null, null, null, null, null));
64      map.addCommand("change", new Command(null, null, null, null, new String[0], null, null, null, null, null));
65      final String expected = "{'click':{'target':'target'},'change':{}}".replaceAll("'", "\"");
66      Assert.assertEquals(expected, JsonUtils.encode(map));
67    }
68  
69    @Test
70    public void transition() {
71      final CommandMap commandMap = new CommandMap();
72      commandMap.setClick(new Command(null, false, null, null, new String[0], null, null, null, null, null));
73      final String expected = "{'click':{'transition':false}}".replaceAll("'", "\"");
74      Assert.assertEquals(expected, JsonUtils.encode(commandMap));
75    }
76  
77    @Test
78    public void more() {
79      final CommandMap map = new CommandMap();
80      final AbstractUICommand command = (AbstractUICommand)
81          CreateComponentUtils.createComponent(facesContext, ComponentTypes.BUTTON, RendererTypes.BUTTON, "command");
82      command.getAttributes().put(Attributes.POPUP_CLOSE, "immediate");
83      command.setRenderedPartially(new String[]{"popup"});
84  
85      map.setClick(new Command(
86          "ns:actionId", false, "_blank", "http://www.apache.org/", new String[]{"id1", "id2"}, "id_focus",
87          "Really?", 1000, Popup.createPopup(command), true));
88      final String expected = (
89          "{"
90              + "'click':{"
91              + "'action':'ns:actionId',"
92              + "'transition':false,"
93              + "'target':'_blank',"
94              + "'url':'http://www.apache.org/',"
95              + "'partially':['id1','id2'],"
96              + "'focus':'id_focus',"
97              + "'confirmation':'Really?',"
98              + "'delay':1000,"
99              + "'popup':{"
100             + "'command':'close',"
101             + "'immediate':true"
102             + "},"
103             + "'omit':true"
104             + "}"
105             + "}").replaceAll("'", "\"");
106     Assert.assertEquals(expected, JsonUtils.encode(map));
107   }
108 
109   @Test
110   public void monthNames() {
111     final DateTimeI18n dateTimeI18n = DateTimeI18n.valueOf(Locale.GERMANY);
112     final String expected
113         = ("{'monthNames':['Januar','Februar','März','April','Mai','Juni',"
114         + "'Juli','August','September','Oktober','November','Dezember'],"
115         + "'monthNamesShort':['Jan','Feb','Mrz','Apr','Mai','Jun','Jul','Aug','Sep','Okt','Nov','Dez'],"
116         + "'dayNames':['Sonntag','Montag','Dienstag','Mittwoch','Donnerstag','Freitag','Samstag'],"
117         + "'dayNamesShort':['So','Mo','Di','Mi','Do','Fr','Sa'],"
118         + "'dayNamesMin':['So','Mo','Di','Mi','Do','Fr','Sa'],"
119         + "'firstDay':1}").replaceAll("'", "\"");
120 
121     Assert.assertEquals(expected, JsonUtils.encode(dateTimeI18n));
122   }
123 
124 }