added the capability to add an optional configuration
[utils] / wicket / components / src / test / java / org / wamblee / wicket / jquery / AbstractJQueryBehaviorTest.java
index 92fcf16e38e6da9563e8a242288613dcc8cc68d2..e6c001fc3c1d88c137d79c4c811a84be0a163bcf 100644 (file)
@@ -25,6 +25,8 @@ import org.junit.Before;
 import org.junit.Test;
 import org.wamblee.wicket.MyPage;
 
+import flexjson.JSONSerializer;
+
 import static junit.framework.TestCase.*;
 
 public class AbstractJQueryBehaviorTest {
@@ -39,9 +41,9 @@ public class AbstractJQueryBehaviorTest {
     public void tearDown() {
         wicket.destroy();
     }
-    
+
     @Test
-    public void testVerifyBasicFunc() { 
+    public void testVerifyBasicFunc() {
         MyPage page = new MyPage();
         page.getLabel().add(new AbstractJQueryBehavior("initfunc"));
         wicket.startPage(page);
@@ -52,19 +54,78 @@ public class AbstractJQueryBehaviorTest {
         assertTrue(doc.contains("jquery-noconflict"));
         assertTrue(doc.contains("initfunc"));
     }
-    
+
     @Test
-    public void testCreateReadyJavascript() { 
+    public void testCreateReadyJavascript() {
         Component component = new Label("label");
         component.setOutputMarkupId(true);
-        String readyHandler = AbstractJQueryBehavior.createReadyFunction("myfunc", component);
-        assertEquals("jQuery(function(){org.wamblee.myfunc(\"#" + component.getMarkupId() + "\");});", readyHandler);
+        AbstractJQueryBehavior behavior = new AbstractJQueryBehavior("myfunc");
+        behavior.bind(component);
+        String readyHandler = behavior.createReadyFunction();
+        assertEquals("jQuery(function(){myfunc(\"#" + component.getMarkupId() +
+            "\",null);});", readyHandler);
+    }
+
+    @Test
+    public void testCreateReadyJavaScriptWithCustomConfigJavascript() {
+        Component component = new Label("label");
+        component.setOutputMarkupId(false);
+        AbstractJQueryBehavior behavior = new AbstractJQueryBehavior("myfunc") {
+            @Override
+            protected String getConfigurationJavascript() {
+                return "bla";
+            }
+        };
+        behavior.bind(component);
+        String readyHandler = behavior.createReadyFunction();
+        assertEquals("jQuery(function(){myfunc(\"#" + component.getMarkupId() +
+            "\",bla);});", readyHandler);
+    }
+
+    public static final class X {
+        private int x = 10;
+        public int getX() {
+            return x;
+        }
+    }
+
+    @Test
+    public void testCreateReadyJavaScriptWithConfig() {
+        Component component = new Label("label");
+        component.setOutputMarkupId(false);
+        AbstractJQueryBehavior behavior = new AbstractJQueryBehavior("myfunc") {
+            @Override
+            protected Object getConfigurationObject() {
+               return new X();
+            }
+        };
+        behavior.bind(component);
+        String readyHandler = behavior.createReadyFunction();
+        String config = new JSONSerializer().serialize(new X()); 
+        assertEquals("jQuery(function(){myfunc(\"#" + component.getMarkupId() +
+            "\"," + config + ");});", readyHandler);
     }
     
-    @Test(expected = IllegalStateException.class)
-    public void testCreateReadyJavascriptNoMarkupId() { 
+    @Test
+    public void testCreateReadyJavaScriptWithConfigWithCustomSerializer() {
         Component component = new Label("label");
-        String readyHandler = AbstractJQueryBehavior.createReadyFunction("myfunc", component);
+        component.setOutputMarkupId(false);
+        AbstractJQueryBehavior behavior = new AbstractJQueryBehavior("myfunc") {
+            @Override
+            protected Object getConfigurationObject() {
+               return new X();
+            }
+            @Override
+            protected JSONSerializer getCustomSerializer() {
+                return new JSONSerializer().exclude("x");
+            }
+        };
+        behavior.bind(component);
+        String readyHandler = behavior.createReadyFunction();
+        String config = new JSONSerializer().exclude("x").serialize(new X());
+        assertEquals("jQuery(function(){myfunc(\"#" + component.getMarkupId() +
+            "\"," + config + ");});", readyHandler);
     }
     
+
 }