From: Manuel Carrasco MoƱino Date: Fri, 27 Dec 2013 20:44:49 +0000 (+0100) Subject: Implementation of JsonBuilders for the JVM. Moved tests to class which can be run... X-Git-Tag: gwtquery-project-1.4.3~44^2~35 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=ad30b3e7f61bcf25cce84d46f9527f6e075c8b1e;p=gwtquery.git Implementation of JsonBuilders for the JVM. Moved tests to class which can be run either in JVM and in client side. Added an utility class to create Json objects so as we can use it in server and client, we dont depend on ClientFactories nor Injectors like autoBeans and other implementations do --- diff --git a/gwtquery-core/pom.xml b/gwtquery-core/pom.xml index a90c6a82..c44b7f9b 100644 --- a/gwtquery-core/pom.xml +++ b/gwtquery-core/pom.xml @@ -51,7 +51,7 @@ ${basedir}/src/main/java - ${basedir}/src/main/resources + ${basedir}/src/main/super diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/Query.gwt.xml b/gwtquery-core/src/main/java/com/google/gwt/query/Query.gwt.xml index 520ceaa0..5398aa58 100644 --- a/gwtquery-core/src/main/java/com/google/gwt/query/Query.gwt.xml +++ b/gwtquery-core/src/main/java/com/google/gwt/query/Query.gwt.xml @@ -17,6 +17,9 @@ + + + @@ -36,6 +39,9 @@ + + + diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/QueryMin.gwt.xml b/gwtquery-core/src/main/java/com/google/gwt/query/QueryMin.gwt.xml index 6453aba3..f3e2e72a 100644 --- a/gwtquery-core/src/main/java/com/google/gwt/query/QueryMin.gwt.xml +++ b/gwtquery-core/src/main/java/com/google/gwt/query/QueryMin.gwt.xml @@ -22,7 +22,10 @@ - + + + + @@ -42,21 +45,6 @@ - - - - - - - - - - - - - - - diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/GQ.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/GQ.java new file mode 100644 index 00000000..0791feaf --- /dev/null +++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/GQ.java @@ -0,0 +1,41 @@ +/* + * 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 create(Class clz) { + if (jsonFactory == null) { + jsonFactory = GWT.isClient() ? + GWT.create(JsonFactory.class) : + new JsonFactoryJre(); + } + return jsonFactory.create(clz); + } + + public static T create(Class clz, String payload) { + T ret = create(clz); + ret.load(payload); + return ret; + } +} diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/builders/JsonBuilder.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/builders/JsonBuilder.java index 41a7171c..596d5473 100644 --- a/gwtquery-core/src/main/java/com/google/gwt/query/client/builders/JsonBuilder.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/builders/JsonBuilder.java @@ -15,7 +15,6 @@ */ package com.google.gwt.query.client.builders; -import com.google.gwt.query.client.Properties; /** * Tagging interface used to generate JsonBuilder classes. @@ -40,9 +39,10 @@ public interface JsonBuilder { 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 getProperties(); /** * return a list of field names. diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/builders/JsonBuilderBase.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/builders/JsonBuilderBase.java index e0d48c26..66de0898 100644 --- a/gwtquery-core/src/main/java/com/google/gwt/query/client/builders/JsonBuilderBase.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/builders/JsonBuilderBase.java @@ -51,7 +51,7 @@ public abstract class JsonBuilderBase> implements J if (r.length > 0 && r[0] instanceof JsonBuilder) { JsArray a = JavaScriptObject.createArray().cast(); for (T o : r) { - a.push(((JsonBuilder)o).getProperties()); + a.push(((JsonBuilder)o).getProperties()); } p.set(n, a); } else { @@ -88,6 +88,7 @@ public abstract class JsonBuilderBase> implements J return p.tostring(); } + @SuppressWarnings("unchecked") public Properties getProperties() { return p; } diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/builders/JsonFactory.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/builders/JsonFactory.java new file mode 100644 index 00000000..30f08bb9 --- /dev/null +++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/builders/JsonFactory.java @@ -0,0 +1,5 @@ +package com.google.gwt.query.client.builders; + +public interface JsonFactory { + T create(Class clz); +} diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/rebind/JsonBuilderGenerator.java b/gwtquery-core/src/main/java/com/google/gwt/query/rebind/JsonBuilderGenerator.java index 51543454..ead7ecd0 100644 --- a/gwtquery-core/src/main/java/com/google/gwt/query/rebind/JsonBuilderGenerator.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/rebind/JsonBuilderGenerator.java @@ -15,10 +15,19 @@ */ 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; @@ -30,17 +39,12 @@ import com.google.gwt.core.ext.typeinfo.TypeOracle; 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 { @@ -50,6 +54,9 @@ 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; @@ -70,39 +77,47 @@ public class JsonBuilderGenerator extends Generator { 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 attrs = new HashSet(); - 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 attrs = new HashSet(); + 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]; @@ -255,7 +270,7 @@ public class JsonBuilderGenerator extends Generator { } 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) { @@ -263,8 +278,10 @@ public class JsonBuilderGenerator extends Generator { } 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.*"); @@ -281,4 +298,23 @@ public class JsonBuilderGenerator extends Generator { JClassType c = t.isClassOrInterface(); return (c != null && c.isAssignableTo(o)); } + + private void generateCreateMethod(SourceWriter sw, TreeLogger logger) { + sw.println("public T create(Class clz) {"); + sw.indent(); + ArrayList types = new ArrayList(); + for (JClassType t : oracle.getTypes()) { + if (t.isInterface() != null && t.isAssignableTo(jsonBuilderType) ) { + if (t.isPublic()) { + sw.println("if (clz == " + t.getQualifiedSourceName() + ".class) return GWT.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("}"); + } } diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/vm/JsonFactoryJre.java b/gwtquery-core/src/main/java/com/google/gwt/query/vm/JsonFactoryJre.java new file mode 100644 index 00000000..49b5877f --- /dev/null +++ b/gwtquery-core/src/main/java/com/google/gwt/query/vm/JsonFactoryJre.java @@ -0,0 +1,203 @@ +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 Object jsonArrayToList(JSONArray j, Class ctype, boolean isArray) throws Throwable { + List l = new ArrayList(); + 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 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 create(Class clz, JSONObject jso) { + InvocationHandler handler = new JsonBuilderHandler(jso); + return (T) Proxy.newProxyInstance(clz.getClassLoader(), new Class[] {clz}, handler); + } + + @SuppressWarnings("unchecked") + public T create(Class 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; + } +} diff --git a/gwtquery-core/src/main/super/com/google/gwt/query/super/com/google/gwt/query/client/GQ.java b/gwtquery-core/src/main/super/com/google/gwt/query/super/com/google/gwt/query/client/GQ.java new file mode 100644 index 00000000..edad9154 --- /dev/null +++ b/gwtquery-core/src/main/super/com/google/gwt/query/super/com/google/gwt/query/client/GQ.java @@ -0,0 +1,39 @@ +/* + * 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 create(Class clz) { + if (jsonFactory == null) { + jsonFactory = GWT.create(JsonFactory.class); + } + return jsonFactory.create(clz); + } + + public static T create(Class clz, String payload) { + T ret = create(clz); + ret.load(payload); + return ret; + } +} diff --git a/gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryAjaxTestGwt.java b/gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryAjaxTestGwt.java index 695e1d01..945a7fa4 100644 --- a/gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryAjaxTestGwt.java +++ b/gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryAjaxTestGwt.java @@ -1,5 +1,5 @@ /* - * 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 @@ -18,16 +18,11 @@ package com.google.gwt.query.client; 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; @@ -64,77 +59,6 @@ public class GQueryAjaxTestGwt extends GWTTestCase { } } - 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 getItems(); - void setItems(List 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 { } @@ -292,11 +216,11 @@ public class GQueryAjaxTestGwt extends GWTTestCase { 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() { diff --git a/gwtquery-core/src/test/java/com/google/gwt/query/client/dbinding/DataBindingTest.java b/gwtquery-core/src/test/java/com/google/gwt/query/client/dbinding/DataBindingTest.java new file mode 100644 index 00000000..05054b75 --- /dev/null +++ b/gwtquery-core/src/test/java/com/google/gwt/query/client/dbinding/DataBindingTest.java @@ -0,0 +1,113 @@ +/* + * 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 getItems(); + void setItems(List 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 diff --git a/gwtquery-core/src/test/java/com/google/gwt/query/client/dbinding/DataBindingTestGwt.java b/gwtquery-core/src/test/java/com/google/gwt/query/client/dbinding/DataBindingTestGwt.java new file mode 100644 index 00000000..c01e6c5c --- /dev/null +++ b/gwtquery-core/src/test/java/com/google/gwt/query/client/dbinding/DataBindingTestGwt.java @@ -0,0 +1,29 @@ +/* + * 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"; + } + +}