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.AbstractUICommandBase;
26  import org.apache.myfaces.tobago.internal.mock.faces.AbstractTobagoTestBase;
27  import org.apache.myfaces.tobago.util.CreateComponentUtils;
28  import org.junit.Assert;
29  import org.junit.Test;
30  
31  // using ' instead of " to make it better readable.
32  
33  public class JsonUtilsUnitTest extends AbstractTobagoTestBase {
34  
35    @Test
36    public void empty() {
37      CommandMap map = new CommandMap();
38      Assert.assertEquals("{}", JsonUtils.encode(map));
39    }
40  
41    @Test
42    public void click() {
43      CommandMap map = new CommandMap();
44      map.setClick(new Command(null, null, null, null, new String[0], null, null, null, null, null));
45      final String expected = "{'click':{}}".replaceAll("'", "\"");
46      Assert.assertEquals(expected, JsonUtils.encode(map));
47    }
48  
49    @Test
50    public void change() {
51      CommandMap map = new CommandMap();
52      map.addCommand("change", new Command(null, null, null, null, new String[0], null, null, null, null, null));
53      final String expected = "{'change':{}}".replaceAll("'", "\"");
54      Assert.assertEquals(expected, JsonUtils.encode(map));
55    }
56  
57    @Test
58    public void two() {
59      CommandMap map = new CommandMap();
60      map.addCommand("click", new Command(null, null, "target", null, new String[0], null, null, null, null, null));
61      map.addCommand("change", new Command(null, null, null, null, new String[0], null, null, null, null, null));
62      final String expected = "{'click':{'target':'target'},'change':{}}".replaceAll("'", "\"");
63      Assert.assertEquals(expected, JsonUtils.encode(map));
64    }
65  
66    @Test
67    public void transition() {
68      CommandMap commandMap = new CommandMap();
69      commandMap.setClick(new Command(null, false, null, null, new String[0], null, null, null, null, null));
70      final String expected = "{'click':{'transition':false}}".replaceAll("'", "\"");
71      Assert.assertEquals(expected, JsonUtils.encode(commandMap));
72    }
73  
74    @Test
75    public void more() {
76      CommandMap map = new CommandMap();
77      final AbstractUICommandBase command = (AbstractUICommandBase)
78          CreateComponentUtils.createComponent(facesContext, ComponentTypes.BUTTON, RendererTypes.BUTTON, "command");
79      command.getAttributes().put(Attributes.POPUP_CLOSE, "immediate");
80      command.setRenderedPartially(new String[] {"popup"});
81  
82      map.setClick(new Command(
83          "ns:actionId", false, "_blank", "http://www.apache.org/", new String[]{"id1", "id2"}, "id_focus",
84          "Really?", 1000, Popup.createPopup(command), true));
85      final String expected = (
86          "{"
87              + "'click':{"
88              + "'action':'ns:actionId',"
89              + "'transition':false,"
90              + "'target':'_blank',"
91              + "'url':'http://www.apache.org/',"
92              + "'partially':'id1,id2',"
93              + "'focus':'id_focus',"
94              + "'confirmation':'Really?',"
95              + "'delay':1000,"
96              + "'popup':{"
97              + "'command':'close',"
98              + "'immediate':true"
99              + "},"
100             + "'omit':true"
101             + "}"
102             + "}").replaceAll("'", "\"");
103     Assert.assertEquals(expected, JsonUtils.encode(map));
104   }
105 
106 }