<directory>${basedir}/src/main/java</directory>
</resource>
<resource>
- <directory>${basedir}/src/main/resources</directory>
+ <directory>${basedir}/src/main/super</directory>
</resource>
</resources>
<testResources>
<module>
<inherits name='com.google.gwt.user.User'/>
+ <source path="client"/>
+ <super-source path="super"/>
+
<!-- Browser flags -->
<generate-with class="com.google.gwt.query.rebind.BrowserGenerator">
<when-type-assignable class="com.google.gwt.query.client.Browser"/>
<generate-with class="com.google.gwt.query.rebind.JsonBuilderGenerator">
<when-type-assignable class="com.google.gwt.query.client.builders.JsonBuilder"/>
</generate-with>
+ <generate-with class="com.google.gwt.query.rebind.JsonBuilderGenerator">
+ <when-type-assignable class="com.google.gwt.query.client.builders.JsonFactory"/>
+ </generate-with>
<generate-with class="com.google.gwt.query.rebind.XmlBuilderGenerator">
<when-type-assignable class="com.google.gwt.query.client.builders.XmlBuilder"/>
</generate-with>
<!-- Inherit GQuery module -->
<inherits name='com.google.gwt.query.Query'/>
-
+
+ <source path="client"/>
+ <super-source path="super"/>
+
<!-- Detect whether querySelectorAll is available -->
<define-property name="selectorCapability" values="native,js"/>
<property-provider name="selectorCapability">
</any>
</set-property>
- <!-- JSNI Generator -->
- <generate-with class="com.google.gwt.query.rebind.JsniBundleGenerator">
- <when-type-assignable class="com.google.gwt.query.client.builders.JsniBundle"/>
- </generate-with>
-
- <!-- Browser flags -->
- <generate-with class="com.google.gwt.query.rebind.BrowserGenerator">
- <when-type-assignable class="com.google.gwt.query.client.Browser"/>
- </generate-with>
-
- <!-- GQuery.console -->
- <replace-with class="com.google.gwt.query.client.impl.ConsoleBrowser">
- <when-type-assignable class="com.google.gwt.query.client.Console"/>
- </replace-with>
-
<!-- Selector Engines -->
<replace-with class="com.google.gwt.query.client.impl.SelectorEngineNativeMin">
<when-type-assignable class="com.google.gwt.query.client.impl.SelectorEngineImpl"/>
--- /dev/null
+/*
+ * Copyright 2013, The gwtquery team.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+package com.google.gwt.query.client;
+
+import com.google.gwt.core.shared.GWT;
+import com.google.gwt.query.client.builders.JsonBuilder;
+import com.google.gwt.query.client.builders.JsonFactory;
+import com.google.gwt.query.vm.JsonFactoryJre;
+
+public class GQ {
+
+ private static JsonFactory jsonFactory;
+
+ public static <T extends JsonBuilder> T create(Class<T> clz) {
+ if (jsonFactory == null) {
+ jsonFactory = GWT.isClient() ?
+ GWT.<JsonFactory>create(JsonFactory.class) :
+ new JsonFactoryJre();
+ }
+ return jsonFactory.create(clz);
+ }
+
+ public static <T extends JsonBuilder> T create(Class<T> clz, String payload) {
+ T ret = create(clz);
+ ret.load(payload);
+ return ret;
+ }
+}
*/
package com.google.gwt.query.client.builders;
-import com.google.gwt.query.client.Properties;
/**
* Tagging interface used to generate JsonBuilder classes.
<J> J parse(String json, boolean fix);
/**
- * Returns the javascript properties object.
+ * Returns the wrapped object, normally a Properties jso in client
+ * but can be used to return the underlying Json implementation in JVM
*/
- Properties getProperties();
+ <J> J getProperties();
/**
* return a list of field names.
if (r.length > 0 && r[0] instanceof JsonBuilder) {
JsArray<JavaScriptObject> a = JavaScriptObject.createArray().cast();
for (T o : r) {
- a.push(((JsonBuilder)o).getProperties());
+ a.push(((JsonBuilder)o).<Properties>getProperties());
}
p.set(n, a);
} else {
return p.tostring();
}
+ @SuppressWarnings("unchecked")
public Properties getProperties() {
return p;
}
--- /dev/null
+package com.google.gwt.query.client.builders;
+
+public interface JsonFactory {
+ <T extends JsonBuilder> T create(Class<T> clz);
+}
*/
package com.google.gwt.query.rebind;
+import java.io.PrintWriter;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Set;
+
import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.core.ext.Generator;
import com.google.gwt.core.ext.GeneratorContext;
import com.google.gwt.core.ext.TreeLogger;
+import com.google.gwt.core.ext.TreeLogger.Type;
import com.google.gwt.core.ext.UnableToCompleteException;
import com.google.gwt.core.ext.typeinfo.JArrayType;
import com.google.gwt.core.ext.typeinfo.JClassType;
import com.google.gwt.query.client.Function;
import com.google.gwt.query.client.Properties;
import com.google.gwt.query.client.builders.JsonBuilder;
+import com.google.gwt.query.client.builders.JsonBuilderBase;
+import com.google.gwt.query.client.builders.JsonFactory;
import com.google.gwt.query.client.builders.Name;
import com.google.gwt.user.rebind.ClassSourceFileComposerFactory;
import com.google.gwt.user.rebind.SourceWriter;
-import java.io.PrintWriter;
-import java.util.Collection;
-import java.util.HashSet;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Set;
-
/**
*/
public class JsonBuilderGenerator extends Generator {
static JClassType jsType;
static JClassType listType;
static JClassType stringType;
+ static JClassType jsonCreatorType;
+
+
public static String capitalize(String s) {
if (s.length() == 0)
return s;
public String generate(TreeLogger treeLogger,
GeneratorContext generatorContext, String requestedClass)
throws UnableToCompleteException {
+
oracle = generatorContext.getTypeOracle();
JClassType clazz = oracle.findType(requestedClass);
+
jsonBuilderType = oracle.findType(JsonBuilder.class.getName());
stringType = oracle.findType(String.class.getName());
jsType = oracle.findType(JavaScriptObject.class.getName());
listType = oracle.findType(List.class.getName());
functionType = oracle.findType(Function.class.getName());
+ jsonCreatorType = oracle.findType(JsonFactory.class.getName());
String t[] = generateClassName(clazz);
- SourceWriter sw = getSourceWriter(treeLogger, generatorContext, t[0], t[1],
- requestedClass);
+ boolean isFactory = clazz.isAssignableTo(jsonCreatorType);
+
+ SourceWriter sw = getSourceWriter(treeLogger, generatorContext, t[0], t[1], isFactory, requestedClass);
if (sw != null) {
- Set<String> attrs = new HashSet<String>();
- for (JMethod method : clazz.getInheritableMethods()) {
- String methName = method.getName();
- //skip method from JsonBuilder
- if(jsonBuilderType.findMethod(method.getName(), method.getParameterTypes()) != null){
- continue;
- }
+ if (isFactory) {
+ generateCreateMethod(sw, treeLogger);
+ } else {
+ Set<String> attrs = new HashSet<String>();
+ for (JMethod method : clazz.getInheritableMethods()) {
+ String methName = method.getName();
+ //skip method from JsonBuilder
+ if(jsonBuilderType.findMethod(method.getName(), method.getParameterTypes()) != null) {
+ continue;
+ }
- Name nameAnnotation = method.getAnnotation(Name.class);
- String name = nameAnnotation != null
- ? nameAnnotation.value()
- : methName.replaceFirst("^(get|set)", "");
- if (nameAnnotation == null) {
- name = name.substring(0, 1).toLowerCase() + name.substring(1);
+ Name nameAnnotation = method.getAnnotation(Name.class);
+ String name = nameAnnotation != null
+ ? nameAnnotation.value()
+ : methName.replaceFirst("^(get|set)", "");
+ if (nameAnnotation == null) {
+ name = name.substring(0, 1).toLowerCase() + name.substring(1);
+ }
+ attrs.add(name);
+ generateMethod(sw, method, name, treeLogger);
}
- attrs.add(name);
- generateMethod(sw, method, name, treeLogger);
+ generateFieldNamesMethod(sw, attrs, treeLogger);
+ generateToJsonMethod(sw, t[3], treeLogger);
}
- generateFieldNamesMethod(sw, attrs, treeLogger);
- generateToJsonMethod(sw, t[3], treeLogger);
sw.commit(treeLogger);
}
return t[2];
}
protected SourceWriter getSourceWriter(TreeLogger logger,
- GeneratorContext context, String packageName, String className,
+ GeneratorContext context, String packageName, String className, boolean isFactory,
String... interfaceNames) {
PrintWriter printWriter = context.tryCreate(logger, packageName, className);
if (printWriter == null) {
}
ClassSourceFileComposerFactory composerFactory = new ClassSourceFileComposerFactory(
packageName, className);
- composerFactory.setSuperclass("com.google.gwt.query.client.builders.JsonBuilderBase<"
- + packageName + "." + className + ">");
+ if (!isFactory) {
+ composerFactory.setSuperclass(JsonBuilderBase.class.getName() +
+ "<" + packageName + "." + className + ">");
+ }
composerFactory.addImport("com.google.gwt.query.client.js.*");
composerFactory.addImport("com.google.gwt.query.client.*");
composerFactory.addImport("com.google.gwt.core.client.*");
JClassType c = t.isClassOrInterface();
return (c != null && c.isAssignableTo(o));
}
+
+ private void generateCreateMethod(SourceWriter sw, TreeLogger logger) {
+ sw.println("public <T extends " + JsonBuilder.class.getName() + "> T create(Class<T> clz) {");
+ sw.indent();
+ ArrayList<JClassType> types = new ArrayList<JClassType>();
+ for (JClassType t : oracle.getTypes()) {
+ if (t.isInterface() != null && t.isAssignableTo(jsonBuilderType) ) {
+ if (t.isPublic()) {
+ sw.println("if (clz == " + t.getQualifiedSourceName() + ".class) return GWT.<T>create(" + t.getQualifiedSourceName() + ".class);");
+ } else {
+ logger.log(Type.WARN, t.getQualifiedSourceName() + " is not public");
+ }
+ types.add(t);
+ }
+ }
+ sw.println("return null;");
+ sw.outdent();
+ sw.println("}");
+ }
}
--- /dev/null
+package com.google.gwt.query.vm;
+
+import java.lang.reflect.Array;
+import java.lang.reflect.InvocationHandler;
+import java.lang.reflect.Method;
+import java.lang.reflect.ParameterizedType;
+import java.lang.reflect.Proxy;
+import java.lang.reflect.Type;
+import java.util.ArrayList;
+import java.util.Date;
+import java.util.List;
+
+import org.json.JSONArray;
+import org.json.JSONObject;
+
+import com.google.gwt.query.client.builders.JsonBuilder;
+import com.google.gwt.query.client.builders.JsonFactory;
+import com.google.gwt.query.client.builders.Name;
+import com.google.gwt.query.rebind.JsonBuilderGenerator;
+
+public class JsonFactoryJre implements JsonFactory {
+
+ static JsonFactoryJre jsonFactory = new JsonFactoryJre();
+
+ public static class JsonBuilderHandler implements InvocationHandler {
+ private JSONObject jsonObject;
+
+ public JsonBuilderHandler() {
+ jsonObject = new JSONObject();
+ }
+
+ public JsonBuilderHandler(JSONObject j) {
+ jsonObject = j;
+ }
+
+ public JsonBuilderHandler(String payload) throws Throwable {
+ jsonObject = new JSONObject(payload);
+ }
+
+ @SuppressWarnings("unchecked")
+ private <T> Object jsonArrayToList(JSONArray j, Class<T> ctype, boolean isArray) throws Throwable {
+ List<T> l = new ArrayList<T>();
+ for (int i = 0; j != null && i < j.length() ; i++) {
+ l.add((T)getValue(j, i, null, null, ctype, null));
+ }
+ return l.isEmpty() ? null : isArray ? l.toArray((T[])Array.newInstance(ctype, l.size())) : l;
+ }
+
+ private Object getValue(JSONArray arr, int idx, JSONObject obj, String attr, Class<?> clz, Method method) {
+ Object ret = null;
+ try {
+ if (clz.isArray() || clz.equals(List.class)) {
+ Class<?> ctype = Object.class;
+ if (clz.isArray()) {
+ ctype = clz.getComponentType();
+ } else {
+ Type returnType = method.getGenericReturnType();
+ if (returnType instanceof ParameterizedType) {
+ ctype = (Class<?>)((ParameterizedType) returnType).getActualTypeArguments()[0];
+ }
+ }
+ ret = jsonArrayToList(obj.getJSONArray(attr), ctype, clz.isArray());
+ } else if (clz.equals(Date.class)) {
+ ret = new Date(obj != null ? obj.getLong(attr): arr.getLong(idx));
+ } else if (clz.equals(String.class)) {
+ ret = obj != null ? obj.getString(attr) : arr.getString(idx);
+ } else if (clz.equals(Boolean.class) || clz.isPrimitive() && clz == Boolean.TYPE) {
+ try {
+ ret = obj != null ? obj.getBoolean(attr): arr.getBoolean(idx);
+ } catch (Exception e) {
+ return Boolean.FALSE;
+ }
+ } else if (clz.equals(Byte.class) || clz.equals(Short.class) || clz.equals(Integer.class)
+ || clz.isPrimitive() && (clz == Byte.TYPE || clz == Short.TYPE || clz == Integer.TYPE)) {
+ try {
+ ret = obj != null ? obj.getInt(attr): arr.getInt(idx);
+ } catch (Exception e) {
+ return 0;
+ }
+ } else if (clz.equals(Double.class) || clz.equals(Float.class)
+ || clz.isPrimitive() && (clz == Double.TYPE || clz == Float.TYPE)) {
+ try {
+ ret = obj != null ? obj.getDouble(attr): arr.getDouble(idx);
+ } catch (Exception e) {
+ return .0;
+ }
+ } else if (clz.equals(Long.class)
+ || clz.isPrimitive() && clz == Long.TYPE) {
+ try {
+ ret = obj != null ? obj.getLong(attr): arr.getLong(idx);
+ } catch (Exception e) {
+ return 0l;
+ }
+ } else {
+ ret = obj != null ? obj.get(attr): arr.get(idx);
+ if (ret instanceof JSONObject && JsonBuilder.class.isAssignableFrom(clz) && !clz.isAssignableFrom(ret.getClass())) {
+ ret = jsonFactory.create(clz, (JSONObject)ret);
+ }
+ }
+ } catch (Throwable e) {
+ System.out.println(this.getClass().getSimpleName() + " ERROR getting attr=" + attr + " idx=" + idx + " Exception=" + e.getMessage());
+ }
+ return ret;
+ }
+
+ private <T> JSONArray listToJsonArray(Object...l) throws Throwable {
+ JSONArray ret = new JSONArray();
+ for (Object o: l) {
+ setValue(ret, null, null, o, null);
+ }
+ return ret;
+ }
+
+ private Object setValue(JSONArray arr, JSONObject obj, String attr, Object o, Method method) {
+ try {
+ if (o == null) {
+ return o;
+ }
+ if (o instanceof String) {
+ return obj != null ? obj.put(attr, o) : arr.put(o);
+ } else if (o instanceof Boolean) {
+ return obj != null ? obj.put(attr, o) : arr.put(o);
+ } else if (o instanceof Number) {
+ return obj != null ? obj.put(attr, o) : arr.put(o);
+ } else if (o instanceof Date) {
+ return obj != null ? obj.put(attr, ((Date) o).getTime()) : arr.put(((Date) o).getTime());
+ } else if (o instanceof JsonBuilder) {
+ return obj != null ? obj.put(attr, ((JsonBuilder) o).getProperties()) : arr.put(((JsonBuilder) o).getProperties());
+ } else if (o.getClass().isArray() || o instanceof List) {
+ Object[] arg;
+ if (o.getClass().isArray()) {
+ arg = (Object[])o;
+ } else {
+ arg = ((List<?>)o).toArray();
+ }
+ JSONArray a = listToJsonArray(arg);
+ return obj != null ? obj.put(attr, a) : arr.put(a);
+ } else {
+ System.out.println("Unkown setter object " + attr + " " + o.getClass().getName() + " " + o);
+ return obj != null ? obj.put(attr, o) : arr.put(o);
+ }
+ } catch (Throwable e) {
+ e.printStackTrace();
+ }
+ return null;
+ }
+
+ @Override
+ public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
+ String mname = method.getName();
+ Class<?>[] classes = method.getParameterTypes();
+ int largs = classes.length;
+
+ Name name = method.getAnnotation(Name.class);
+ String attr = name != null ? name.value() : deCapitalize(mname.replaceFirst("^[gs]et", ""));
+
+ if ("getFieldNames".equals(mname)) {
+ return JSONObject.getNames(jsonObject);
+ } else if ("getProperties".equals(mname)) {
+ return jsonObject;
+ } else if (largs > 0 && ("parse".equals(mname) || "load".equals(mname))) {
+ jsonObject = new JSONObject(String.valueOf(args[0]));
+ } else if ("toString".equals(mname) || "toJson".equals(mname)) {
+ String jsonName = JsonBuilderGenerator.classNameToJsonName(getDataBindingClassName(proxy.getClass()));
+ return "{\"" + jsonName + "\":"+ jsonObject.toString();
+ } else if (largs == 0 || mname.startsWith("get")) {
+ Class<?> ret = method.getReturnType();
+ return getValue(null, 0, jsonObject, attr, ret, method);
+ } else if (largs == 1 || mname.startsWith("set")) {
+ setValue(null, jsonObject, attr, args[0], method);
+ return proxy;
+ }
+ return null;
+ }
+ }
+
+ @SuppressWarnings("unchecked")
+ public <T> T create(Class<T> clz, JSONObject jso) {
+ InvocationHandler handler = new JsonBuilderHandler(jso);
+ return (T) Proxy.newProxyInstance(clz.getClassLoader(), new Class[] {clz}, handler);
+ }
+
+ @SuppressWarnings("unchecked")
+ public <T extends JsonBuilder> T create(Class<T> clz) {
+ InvocationHandler handler = new JsonBuilderHandler();
+ return (T) Proxy.newProxyInstance(clz.getClassLoader(), new Class[] {clz}, handler);
+ }
+
+ private static String deCapitalize(String s) {
+ return s != null && s.length() > 0 ? s.substring(0, 1).toLowerCase() + s.substring(1) : s;
+ }
+
+ private static String getDataBindingClassName(Class<?> type) {
+ for (Class<?> c : type.getInterfaces()) {
+ if (c.equals(JsonBuilder.class)) {
+ return type.getName();
+ } else {
+ return getDataBindingClassName(c);
+ }
+ }
+ return null;
+ }
+}
--- /dev/null
+/*
+ * Copyright 2013, The gwtquery team.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+package com.google.gwt.query.client;
+
+import com.google.gwt.core.shared.GWT;
+import com.google.gwt.query.client.builders.JsonBuilder;
+import com.google.gwt.query.client.builders.JsonFactory;
+import com.google.gwt.query.vm.JsonFactoryJre;
+
+public class GQ {
+
+ private static JsonFactory jsonFactory;
+
+ public static <T extends JsonBuilder> T create(Class<T> clz) {
+ if (jsonFactory == null) {
+ jsonFactory = GWT.<JsonFactory>create(JsonFactory.class);
+ }
+ return jsonFactory.create(clz);
+ }
+
+ public static <T extends JsonBuilder> T create(Class<T> clz, String payload) {
+ T ret = create(clz);
+ ret.load(payload);
+ return ret;
+ }
+}
/*
- * Copyright 2011, The gwtquery team.
+ * Copyright 2013, The gwtquery team.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
import static com.google.gwt.query.client.GQuery.$;
-import java.util.Arrays;
-import java.util.Date;
-import java.util.List;
-
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Element;
import com.google.gwt.junit.DoNotRunWith;
import com.google.gwt.junit.Platform;
import com.google.gwt.junit.client.GWTTestCase;
-import com.google.gwt.query.client.builders.JsonBuilder;
import com.google.gwt.query.client.builders.Name;
import com.google.gwt.query.client.builders.XmlBuilder;
import com.google.gwt.query.client.plugins.ajax.Ajax;
}
}
- interface Item extends JsonBuilder {
- Date getDate();
- void setDate(Date d);
- }
-
- interface JsonExample extends JsonBuilder {
- int getA();
- JsonExample getB();
- @Name("M")
- int getM();
- @Name("u")
- String getUrl();
- long getD();
- Boolean getZ();
- String[] getT();
- JsonExample setT(String[] strings);
- JsonExample setZ(Boolean b);
- JsonExample setD(long l);
- List<Item> getItems();
- void setItems(List<Item> a);
- String y();
- void y(String s);
- Function getF();
- void setF(Function f);
- }
-
- boolean functionRun = false;
- public void testJsonBuilder() {
- String json = "{M:0, a:1, b:{a:2,b:{a:3}},u:url, d:'2','t':['hola','adios'], 'z': true}";
- JsonExample c = GWT.create(JsonExample.class);
- assertEquals(0, c.getA());
- c.parse(json, true);
- assertEquals(0, c.getM());
- assertEquals(1, c.getA());
- assertNotNull(c.getB());
- assertEquals(2, c.getB().getA());
- assertEquals(3, c.getB().getB().getA());
- assertTrue(c.getZ());
- assertEquals("hola", c.getT()[0]);
- assertEquals("adios", c.getT()[1]);
- assertEquals("url", c.getUrl());
- c.setT(new String[]{"foo", "bar"})
- .setZ(false).setD(1234);
- assertFalse(c.getZ());
- assertEquals("foo", c.getT()[0]);
- assertEquals("bar", c.getT()[1]);
- assertEquals(1234l, c.getD());
- c.y("y");
- assertEquals("y", c.y());
-
- c.setF(new Function() {
- public void f() {
- functionRun = true;
- }
- });
- assertFalse(functionRun);
- c.getF().f();
- assertTrue(functionRun);
-
- Item i1 = GWT.create(Item.class);
- Item i2 = GWT.create(Item.class);
- i1.setDate(new Date(2000));
- i2.setDate(new Date(3000));
- Item[] items = new Item[]{i1, i2};
- c.setItems(Arrays.asList(items));
- assertEquals(2000l, c.getItems().get(0).getDate().getTime());
- assertEquals(3000l, c.getItems().get(1).getDate().getTime());
- String s = "{'M':0,'a':1,'b':{'a':2,'b':{'a':3}},'u':'url','d':1234,'t':['foo','bar'],'z':false,'y':'y','items':[{'date':2000},{'date':3000}]";
- assertEquals(s, c.toString().replaceAll("\"", "'"));
- }
-
interface XmlExample extends XmlBuilder {
interface T extends XmlBuilder {
}
Ajax.ajax(s);
}
-
+
public void testAjaxError() {
delayTestFinish(5000);
String url = "http://127.0.0.1/nopage";
-
+
Ajax.ajax(Ajax.createSettings().setTimeout(1000).setUrl(url))
.done(new Function(){
public void f() {
--- /dev/null
+/*
+ * Copyright 2013, The gwtquery team.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+package com.google.gwt.query.client.dbinding;
+
+import java.util.Arrays;
+import java.util.Date;
+import java.util.List;
+
+import com.google.gwt.junit.client.GWTTestCase;
+import com.google.gwt.query.client.Function;
+import com.google.gwt.query.client.GQ;
+import com.google.gwt.query.client.builders.JsonBuilder;
+import com.google.gwt.query.client.builders.Name;
+
+/**
+ * Tests for Deferred which can run either in JVM and GWT
+ */
+public class DataBindingTest extends GWTTestCase {
+
+ public String getModuleName() {
+ return null;
+ }
+
+ public interface Item extends JsonBuilder {
+ Date getDate();
+ void setDate(Date d);
+ }
+
+ public interface JsonExample extends JsonBuilder {
+ int getA();
+ JsonExample getB();
+ @Name("M")
+ int getM();
+ @Name("u")
+ String getUrl();
+ long getD();
+ Boolean getZ();
+ String[] getT();
+ JsonExample setT(String[] strings);
+ JsonExample setZ(Boolean b);
+ JsonExample setD(long l);
+ List<Item> getItems();
+ void setItems(List<Item> a);
+ String y();
+ void y(String s);
+ Function getF();
+ void setF(Function f);
+ }
+
+ boolean functionRun = false;
+ public void testJsonBuilder() {
+ String json = "{M:0, a:1, b:{a:2,b:{a:3}},u:url, d:'2','t':['hola','adios'], 'z': true, 'items':[{'date':100}]}";
+
+ JsonExample c = GQ.create(JsonExample.class);
+ assertEquals(0, c.getA());
+ c.parse(json, true);
+
+ assertEquals(0, c.getM());
+ assertEquals(1, c.getA());
+ assertNotNull(c.getB());
+ assertEquals(2, c.getB().getA());
+ assertEquals(3, c.getB().getB().getA());
+ assertTrue(c.getZ());
+ assertEquals("hola", c.getT()[0]);
+ assertEquals("adios", c.getT()[1]);
+ assertEquals("url", c.getUrl());
+ c.setT(new String[]{"foo", "bar"})
+ .setZ(false).setD(1234);
+ assertFalse(c.getZ());
+ assertEquals("foo", c.getT()[0]);
+ assertEquals("bar", c.getT()[1]);
+ assertEquals(1234l, c.getD());
+ c.y("y");
+ assertEquals("y", c.y());
+ assertEquals(1, c.getItems().size());
+
+ c.setF(new Function() {
+ public void f() {
+ functionRun = true;
+ }
+ });
+ assertFalse(functionRun);
+ c.getF().f();
+ assertTrue(functionRun);
+
+ Item i1 = GQ.create(Item.class);
+ Item i2 = GQ.create(Item.class);
+ i1.setDate(new Date(2000));
+ i2.setDate(new Date(3000));
+ Item[] items = new Item[]{i1, i2};
+ c.setItems(Arrays.asList(items));
+ System.out.println(c.toJson());
+ System.out.println(c.getItems().get(0).getDate());
+ assertEquals(2000l, c.getItems().get(0).getDate().getTime());
+ assertEquals(3000l, c.getItems().get(1).getDate().getTime());
+
+ assertTrue(c.toJson().startsWith("{\"jsonExample\":{"));
+ assertTrue(c.toJson().contains("\"items\":[{\"date\":"));
+ }
+}
\ No newline at end of file
--- /dev/null
+/*
+ * Copyright 2013, The gwtquery team.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+package com.google.gwt.query.client.dbinding;
+
+
+/**
+ * Test for data binding shared code run in gwt
+ */
+public class DataBindingTestGwt extends DataBindingTest {
+
+ @Override
+ public String getModuleName() {
+ return "com.google.gwt.query.Query";
+ }
+
+}