Representation of JSON objects can be different in each technology. The actual implementation of the TCK needs to create such object then.
authorJaroslav Tulach <jaroslav.tulach@apidesign.org>
Wed, 24 Apr 2013 18:52:42 +0200
changeset 35d13b8b8c9a81
parent 34 485f222358de
child 36 d283d6c6fba9
Representation of JSON objects can be different in each technology. The actual implementation of the TCK needs to create such object then.
json-tck/src/main/java/net/java/html/json/tests/ConvertTypesTest.java
json-tck/src/main/java/net/java/html/json/tests/JSONTest.java
json-tck/src/main/java/net/java/html/json/tests/KnockoutTest.java
json-tck/src/main/java/net/java/html/json/tests/Utils.java
json-tck/src/main/java/org/apidesign/html/json/tck/KnockoutTCK.java
     1.1 --- a/json-tck/src/main/java/net/java/html/json/tests/ConvertTypesTest.java	Wed Apr 24 18:34:31 2013 +0200
     1.2 +++ b/json-tck/src/main/java/net/java/html/json/tests/ConvertTypesTest.java	Wed Apr 24 18:52:42 2013 +0200
     1.3 @@ -20,8 +20,8 @@
     1.4   */
     1.5  package net.java.html.json.tests;
     1.6  
     1.7 -import org.apidesign.html.json.tck.KnockoutTCK;
     1.8 -import org.apidesign.bck2brwsr.core.JavaScriptBody;
     1.9 +import java.util.HashMap;
    1.10 +import java.util.Map;
    1.11  import org.apidesign.bck2brwsr.vmtest.BrwsrTest;
    1.12  import org.apidesign.bck2brwsr.vmtest.VMTest;
    1.13  import org.apidesign.html.json.impl.JSON;
    1.14 @@ -31,19 +31,21 @@
    1.15   * @author Jaroslav Tulach <jtulach@netbeans.org>
    1.16   */
    1.17  public final class ConvertTypesTest {
    1.18 -    @JavaScriptBody(args = { "includeSex" }, body = "var json = new Object();"
    1.19 -        + "json.firstName = 'son';\n"
    1.20 -        + "json.lastName = 'dj';\n"
    1.21 -        + "if (includeSex) json.sex = 'MALE';\n"
    1.22 -        + "return json;"
    1.23 -    )
    1.24 -    private static native Object createJSON(boolean includeSex);
    1.25 +    private static Object createJSON(boolean includeSex) {
    1.26 +        Map<String,Object> map = new HashMap<>();
    1.27 +        map.put("firstName", "son");
    1.28 +        map.put("lastName", "dj");
    1.29 +        if (includeSex) {
    1.30 +            map.put("sex", "MALE");
    1.31 +        }
    1.32 +        return Utils.createObject(map);
    1.33 +    }
    1.34      
    1.35      @BrwsrTest
    1.36      public void testConvertToPeople() throws Exception {
    1.37          final Object o = createJSON(true);
    1.38          
    1.39 -        Person p = JSON.read(KnockoutTCK.newContext(), Person.class, o);
    1.40 +        Person p = JSON.read(Utils.newContext(), Person.class, o);
    1.41          
    1.42          assert "son".equals(p.getFirstName()) : "First name: " + p.getFirstName();
    1.43          assert "dj".equals(p.getLastName()) : "Last name: " + p.getLastName();
    1.44 @@ -54,7 +56,7 @@
    1.45      public void testConvertToPeopleWithoutSex() throws Exception {
    1.46          final Object o = createJSON(false);
    1.47          
    1.48 -        Person p = JSON.read(KnockoutTCK.newContext(), Person.class, o);
    1.49 +        Person p = JSON.read(Utils.newContext(), Person.class, o);
    1.50          
    1.51          assert "son".equals(p.getFirstName()) : "First name: " + p.getFirstName();
    1.52          assert "dj".equals(p.getLastName()) : "Last name: " + p.getLastName();
     2.1 --- a/json-tck/src/main/java/net/java/html/json/tests/JSONTest.java	Wed Apr 24 18:34:31 2013 +0200
     2.2 +++ b/json-tck/src/main/java/net/java/html/json/tests/JSONTest.java	Wed Apr 24 18:52:42 2013 +0200
     2.3 @@ -23,7 +23,6 @@
     2.4  import net.java.html.json.Model;
     2.5  import net.java.html.json.OnReceive;
     2.6  import net.java.html.json.Property;
     2.7 -import org.apidesign.html.json.tck.KnockoutTCK;
     2.8  import org.apidesign.bck2brwsr.core.JavaScriptBody;
     2.9  import org.apidesign.bck2brwsr.vmtest.BrwsrTest;
    2.10  import org.apidesign.bck2brwsr.vmtest.Http;
    2.11 @@ -44,7 +43,7 @@
    2.12      private Integer orig;
    2.13      
    2.14      @BrwsrTest public void toJSONInABrowser() throws Throwable {
    2.15 -        Person p = new Person(KnockoutTCK.newContext());
    2.16 +        Person p = new Person(Utils.newContext());
    2.17          p.setSex(Sex.MALE);
    2.18          p.setFirstName("Jarda");
    2.19          p.setLastName("Tulach");
    2.20 @@ -56,7 +55,7 @@
    2.21              throw new IllegalStateException("Can't parse " + p).initCause(ex);
    2.22          }
    2.23          
    2.24 -        Person p2 = JSON.read(KnockoutTCK.newContext(), Person.class, json);
    2.25 +        Person p2 = JSON.read(Utils.newContext(), Person.class, json);
    2.26          
    2.27          assert p2.getFirstName().equals(p.getFirstName()) : 
    2.28              "Should be the same: " + p.getFirstName() + " != " + p2.getFirstName();
    2.29 @@ -95,7 +94,7 @@
    2.30      ))
    2.31      @BrwsrTest public void loadAndParseJSON() throws InterruptedException {
    2.32          if (js == null) {
    2.33 -            js = new JSONik(KnockoutTCK.newContext());
    2.34 +            js = new JSONik(Utils.newContext());
    2.35              js.applyBindings();
    2.36  
    2.37              js.fetch("person.json");
    2.38 @@ -127,7 +126,7 @@
    2.39              orig = scriptElements();
    2.40              assert orig > 0 : "There should be some scripts on the page";
    2.41              
    2.42 -            js = new JSONik(KnockoutTCK.newContext());
    2.43 +            js = new JSONik(Utils.newContext());
    2.44              js.applyBindings();
    2.45  
    2.46              js.fetchViaJSONP("person.json");
    2.47 @@ -159,7 +158,7 @@
    2.48      ))
    2.49      @BrwsrTest public void loadAndParseJSONSentToArray() throws InterruptedException {
    2.50          if (js == null) {
    2.51 -            js = new JSONik(KnockoutTCK.newContext());
    2.52 +            js = new JSONik(Utils.newContext());
    2.53              js.applyBindings();
    2.54  
    2.55              js.fetchArray("person.json");
    2.56 @@ -181,7 +180,7 @@
    2.57      ))
    2.58      @BrwsrTest public void loadAndParseJSONArraySingle() throws InterruptedException {
    2.59          if (js == null) {
    2.60 -            js = new JSONik(KnockoutTCK.newContext());
    2.61 +            js = new JSONik(Utils.newContext());
    2.62              js.applyBindings();
    2.63          
    2.64              js.fetch("person.json");
    2.65 @@ -203,7 +202,7 @@
    2.66      ))
    2.67      @BrwsrTest public void loadAndParseArrayInPeople() throws InterruptedException {
    2.68          if (js == null) {
    2.69 -            js = new JSONik(KnockoutTCK.newContext());
    2.70 +            js = new JSONik(Utils.newContext());
    2.71              js.applyBindings();
    2.72          
    2.73              js.fetchPeople("people.json");
    2.74 @@ -229,7 +228,7 @@
    2.75      ))
    2.76      @BrwsrTest public void loadAndParseArrayOfIntegers() throws InterruptedException {
    2.77          if (js == null) {
    2.78 -            js = new JSONik(KnockoutTCK.newContext());
    2.79 +            js = new JSONik(Utils.newContext());
    2.80              js.applyBindings();
    2.81          
    2.82              js.fetchPeopleAge("people.json");
    2.83 @@ -256,7 +255,7 @@
    2.84      ))
    2.85      @BrwsrTest public void loadAndParseArrayOfEnums() throws InterruptedException {
    2.86          if (js == null) {
    2.87 -            js = new JSONik(KnockoutTCK.newContext());
    2.88 +            js = new JSONik(Utils.newContext());
    2.89              js.applyBindings();
    2.90          
    2.91              js.fetchPeopleSex("people.json");
    2.92 @@ -283,7 +282,7 @@
    2.93      ))
    2.94      @BrwsrTest public void loadAndParseJSONArray() throws InterruptedException {
    2.95          if (js == null) {
    2.96 -            js = new JSONik(KnockoutTCK.newContext());
    2.97 +            js = new JSONik(Utils.newContext());
    2.98              js.applyBindings();
    2.99              js.fetchArray("person.json");
   2.100          }
     3.1 --- a/json-tck/src/main/java/net/java/html/json/tests/KnockoutTest.java	Wed Apr 24 18:34:31 2013 +0200
     3.2 +++ b/json-tck/src/main/java/net/java/html/json/tests/KnockoutTest.java	Wed Apr 24 18:52:42 2013 +0200
     3.3 @@ -25,7 +25,6 @@
     3.4  import net.java.html.json.Function;
     3.5  import net.java.html.json.Model;
     3.6  import net.java.html.json.Property;
     3.7 -import org.apidesign.html.json.tck.KnockoutTCK;
     3.8  import org.apidesign.bck2brwsr.core.JavaScriptBody;
     3.9  import org.apidesign.bck2brwsr.vmtest.BrwsrTest;
    3.10  import org.apidesign.bck2brwsr.vmtest.HtmlFragment;
    3.11 @@ -49,7 +48,7 @@
    3.12          "<button id=\"hello\">Say Hello!</button>\n"
    3.13      )
    3.14      @BrwsrTest public void modifyValueAssertChangeInModel() {
    3.15 -        KnockoutModel m = new KnockoutModel(KnockoutTCK.newContext());
    3.16 +        KnockoutModel m = new KnockoutModel(Utils.newContext());
    3.17          m.setName("Kukuc");
    3.18          m.applyBindings();
    3.19          
    3.20 @@ -80,7 +79,7 @@
    3.21          + "</ul>\n"
    3.22      )
    3.23      @BrwsrTest public void displayContentOfArray() {
    3.24 -        KnockoutModel m = new KnockoutModel(KnockoutTCK.newContext());
    3.25 +        KnockoutModel m = new KnockoutModel(Utils.newContext());
    3.26          m.getResults().add("Ahoj");
    3.27          m.applyBindings();
    3.28          
    3.29 @@ -104,7 +103,7 @@
    3.30          + "</ul>\n"
    3.31      )
    3.32      @BrwsrTest public void displayContentOfDerivedArray() {
    3.33 -        KnockoutModel m = new KnockoutModel(KnockoutTCK.newContext());
    3.34 +        KnockoutModel m = new KnockoutModel(Utils.newContext());
    3.35          m.getResults().add("Ahoj");
    3.36          m.applyBindings();
    3.37          
    3.38 @@ -123,9 +122,9 @@
    3.39          + "</ul>\n"
    3.40      )
    3.41      @BrwsrTest public void displayContentOfArrayOfPeople() {
    3.42 -        KnockoutModel m = new KnockoutModel(KnockoutTCK.newContext());
    3.43 +        KnockoutModel m = new KnockoutModel(Utils.newContext());
    3.44          
    3.45 -        final Person first = new Person(KnockoutTCK.newContext());
    3.46 +        final Person first = new Person(Utils.newContext());
    3.47          first.setFirstName("first");
    3.48          m.getPeople().add(first);
    3.49          
    3.50 @@ -134,7 +133,7 @@
    3.51          int cnt = countChildren("ul");
    3.52          assert cnt == 1 : "One child, but was " + cnt;
    3.53          
    3.54 -        final Person second = new Person(KnockoutTCK.newContext());
    3.55 +        final Person second = new Person(Utils.newContext());
    3.56          second.setFirstName("second");
    3.57          m.getPeople().add(second);
    3.58  
    3.59 @@ -181,9 +180,9 @@
    3.60      }
    3.61      
    3.62      private void trasfertToFemale() {
    3.63 -        KnockoutModel m = new KnockoutModel(KnockoutTCK.newContext());
    3.64 +        KnockoutModel m = new KnockoutModel(Utils.newContext());
    3.65  
    3.66 -        final Person first = new Person(KnockoutTCK.newContext());
    3.67 +        final Person first = new Person(Utils.newContext());
    3.68          first.setFirstName("first");
    3.69          first.setSex(Sex.MALE);
    3.70          m.getPeople().add(first);
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/json-tck/src/main/java/net/java/html/json/tests/Utils.java	Wed Apr 24 18:52:42 2013 +0200
     4.3 @@ -0,0 +1,55 @@
     4.4 +/**
     4.5 + * HTML via Java(tm) Language Bindings
     4.6 + * Copyright (C) 2013 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     4.7 + *
     4.8 + * This program is free software: you can redistribute it and/or modify
     4.9 + * it under the terms of the GNU General Public License as published by
    4.10 + * the Free Software Foundation, version 2 of the License.
    4.11 + *
    4.12 + * This program is distributed in the hope that it will be useful,
    4.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    4.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    4.15 + * GNU General Public License for more details. apidesign.org
    4.16 + * designates this particular file as subject to the
    4.17 + * "Classpath" exception as provided by apidesign.org
    4.18 + * in the License file that accompanied this code.
    4.19 + *
    4.20 + * You should have received a copy of the GNU General Public License
    4.21 + * along with this program. Look for COPYING file in the top folder.
    4.22 + * If not, see http://wiki.apidesign.org/wiki/GPLwithClassPathException
    4.23 + */
    4.24 +package net.java.html.json.tests;
    4.25 +
    4.26 +import java.util.Map;
    4.27 +import java.util.ServiceLoader;
    4.28 +import net.java.html.json.Context;
    4.29 +import org.apidesign.html.json.tck.KnockoutTCK;
    4.30 +
    4.31 +/**
    4.32 + *
    4.33 + * @author Jaroslav Tulach <jtulach@netbeans.org>
    4.34 + */
    4.35 +final class Utils {
    4.36 +    private Utils() {
    4.37 +    }
    4.38 +
    4.39 +    static Context newContext() {
    4.40 +        for (KnockoutTCK tck : ServiceLoader.load(KnockoutTCK.class)) {
    4.41 +            Context c = tck.createContext();
    4.42 +            if (c != null) {
    4.43 +                return c;
    4.44 +            }
    4.45 +        }
    4.46 +        throw new AssertionError("Can't find appropriate Context in ServiceLoader!");
    4.47 +    }
    4.48 +    static Object createObject(Map<String,Object> values) {
    4.49 +        for (KnockoutTCK tck : ServiceLoader.load(KnockoutTCK.class)) {
    4.50 +            Object o = tck.createJSON(values);
    4.51 +            if (o != null) {
    4.52 +                return o;
    4.53 +            }
    4.54 +        }
    4.55 +        throw new AssertionError("Can't find appropriate Context in ServiceLoader!");
    4.56 +    }
    4.57 +    
    4.58 +}
     5.1 --- a/json-tck/src/main/java/org/apidesign/html/json/tck/KnockoutTCK.java	Wed Apr 24 18:34:31 2013 +0200
     5.2 +++ b/json-tck/src/main/java/org/apidesign/html/json/tck/KnockoutTCK.java	Wed Apr 24 18:52:42 2013 +0200
     5.3 @@ -20,6 +20,7 @@
     5.4   */
     5.5  package org.apidesign.html.json.tck;
     5.6  
     5.7 +import java.util.Map;
     5.8  import net.java.html.json.tests.ConvertTypesTest;
     5.9  import net.java.html.json.tests.KnockoutTest;
    5.10  import net.java.html.json.tests.JSONTest;
    5.11 @@ -55,7 +56,12 @@
    5.12      /** Implement to create new context for the test. 
    5.13       * Use {@link ContextBuilder} to implement context for your technology.
    5.14       */
    5.15 -    protected abstract Context createContext();
    5.16 +    public abstract Context createContext();
    5.17 +    
    5.18 +    /** Create a JSON object as seen by the technology
    5.19 +     * @param values mapping from names to values of properties
    5.20 +     */
    5.21 +    public abstract Object createJSON(Map<String,Object> values);
    5.22      
    5.23      
    5.24      /** Gives you list of classes included in the TCK. Send them
    5.25 @@ -71,21 +77,4 @@
    5.26          };
    5.27      }
    5.28      
    5.29 -
    5.30 -    /** Finds registered implementation of {@link KnockoutTCK} and obtains
    5.31 -     * new context. 
    5.32 -     * 
    5.33 -     * @return context to use for currently running test
    5.34 -     * @throws AssertionError if no context has been found
    5.35 -     */
    5.36 -    public static Context newContext() {
    5.37 -        for (KnockoutTCK tck : ServiceLoader.load(KnockoutTCK.class)) {
    5.38 -            Context c = tck.createContext();
    5.39 -            if (c != null) {
    5.40 -                return c;
    5.41 -            }
    5.42 -        }
    5.43 -        throw new AssertionError("Can't find appropriate Context in ServiceLoader!");
    5.44 -    }
    5.45 -    
    5.46  }