added the capability to add an optional configuration
[utils] / wicket / components / src / test / java / org / wamblee / wicket / jquery / AbstractJQueryBehaviorTest.java
1 /*
2  * Copyright 2005-2010 the original author or authors.
3  * 
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  * 
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  * 
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.wamblee.wicket.jquery;
17
18 import org.apache.wicket.Component;
19 import org.apache.wicket.markup.html.basic.Label;
20 import org.apache.wicket.markup.html.form.Form;
21 import org.apache.wicket.util.tester.TagTester;
22 import org.apache.wicket.util.tester.WicketTester;
23 import org.junit.After;
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.wamblee.wicket.MyPage;
27
28 import flexjson.JSONSerializer;
29
30 import static junit.framework.TestCase.*;
31
32 public class AbstractJQueryBehaviorTest {
33     private WicketTester wicket;
34
35     @Before
36     public void setUp() {
37         wicket = new WicketTester();
38     }
39
40     @After
41     public void tearDown() {
42         wicket.destroy();
43     }
44
45     @Test
46     public void testVerifyBasicFunc() {
47         MyPage page = new MyPage();
48         page.getLabel().add(new AbstractJQueryBehavior("initfunc"));
49         wicket.startPage(page);
50         wicket.assertRenderedPage(MyPage.class);
51         TagTester tag = wicket.getTagByWicketId("label");
52         assertNotNull(tag.getAttribute("id"));
53         String doc = wicket.getServletResponse().getDocument();
54         assertTrue(doc.contains("jquery-noconflict"));
55         assertTrue(doc.contains("initfunc"));
56     }
57
58     @Test
59     public void testCreateReadyJavascript() {
60         Component component = new Label("label");
61         component.setOutputMarkupId(true);
62         AbstractJQueryBehavior behavior = new AbstractJQueryBehavior("myfunc");
63         behavior.bind(component);
64         String readyHandler = behavior.createReadyFunction();
65         assertEquals("jQuery(function(){myfunc(\"#" + component.getMarkupId() +
66             "\",null);});", readyHandler);
67     }
68
69     @Test
70     public void testCreateReadyJavaScriptWithCustomConfigJavascript() {
71         Component component = new Label("label");
72         component.setOutputMarkupId(false);
73         AbstractJQueryBehavior behavior = new AbstractJQueryBehavior("myfunc") {
74             @Override
75             protected String getConfigurationJavascript() {
76                 return "bla";
77             }
78         };
79         behavior.bind(component);
80         String readyHandler = behavior.createReadyFunction();
81         assertEquals("jQuery(function(){myfunc(\"#" + component.getMarkupId() +
82             "\",bla);});", readyHandler);
83     }
84
85     public static final class X {
86         private int x = 10;
87         public int getX() {
88             return x;
89         }
90     }
91
92     @Test
93     public void testCreateReadyJavaScriptWithConfig() {
94         Component component = new Label("label");
95         component.setOutputMarkupId(false);
96         AbstractJQueryBehavior behavior = new AbstractJQueryBehavior("myfunc") {
97             @Override
98             protected Object getConfigurationObject() {
99                return new X();
100             }
101         };
102         behavior.bind(component);
103         String readyHandler = behavior.createReadyFunction();
104         String config = new JSONSerializer().serialize(new X()); 
105         assertEquals("jQuery(function(){myfunc(\"#" + component.getMarkupId() +
106             "\"," + config + ");});", readyHandler);
107     }
108     
109     @Test
110     public void testCreateReadyJavaScriptWithConfigWithCustomSerializer() {
111         Component component = new Label("label");
112         component.setOutputMarkupId(false);
113         AbstractJQueryBehavior behavior = new AbstractJQueryBehavior("myfunc") {
114             @Override
115             protected Object getConfigurationObject() {
116                return new X();
117             }
118             @Override
119             protected JSONSerializer getCustomSerializer() {
120                 return new JSONSerializer().exclude("x");
121             }
122         };
123         behavior.bind(component);
124         String readyHandler = behavior.createReadyFunction();
125         String config = new JSONSerializer().exclude("x").serialize(new X());
126         assertEquals("jQuery(function(){myfunc(\"#" + component.getMarkupId() +
127             "\"," + config + ");});", readyHandler);
128     }
129     
130
131 }