#249117: Documenting and testing additional parameters of @Function methods
authorJaroslav Tulach <jtulach@netbeans.org>
Tue, 09 Dec 2014 13:42:51 +0100
changeset 9014d20596b35bc
parent 898 18e17d0cd066
child 902 5c65f811cf55
#249117: Documenting and testing additional parameters of @Function methods
json/src/main/java/net/java/html/json/Function.java
json/src/test/java/net/java/html/json/MapModelTest.java
json/src/test/java/net/java/html/json/PersonImpl.java
     1.1 --- a/json/src/main/java/net/java/html/json/Function.java	Tue Dec 09 11:43:46 2014 +0100
     1.2 +++ b/json/src/main/java/net/java/html/json/Function.java	Tue Dec 09 13:42:51 2014 +0100
     1.3 @@ -42,16 +42,18 @@
     1.4   */
     1.5  package net.java.html.json;
     1.6  
     1.7 +import java.io.PrintStream;
     1.8  import java.lang.annotation.ElementType;
     1.9  import java.lang.annotation.Retention;
    1.10  import java.lang.annotation.RetentionPolicy;
    1.11  import java.lang.annotation.Target;
    1.12 +import org.netbeans.html.json.spi.FunctionBinding;
    1.13  
    1.14  /** Methods in class annotated by {@link Model} can be 
    1.15   * annotated by this annotation to signal that they should be available
    1.16   * as functions to users of the model classes. The method
    1.17   * should be non-private, static and return <code>void</code>.
    1.18 - * It may take up to two arguments. One argument can be the type of
    1.19 + * It may take few arguments. The first argument can be the type of
    1.20   * the associated model class, the other argument can be of any type,
    1.21   * but has to be named <code>data</code> - this one represents the
    1.22   * actual data the function was invoked on. Example:
    1.23 @@ -95,6 +97,21 @@
    1.24   * and after clicking on one of the names the <code>---</code> would be replaced
    1.25   * by selected name. 
    1.26   * Try <a target="_blank" href="http://dew.apidesign.org/dew/#8848505">this sample on-line</a>!
    1.27 + * <p>
    1.28 + * There can be additional arguments in the method which can extract information
    1.29 + * from (typically event object) sent as a second parameter of the function
    1.30 + * {@link FunctionBinding#call(java.lang.Object, java.lang.Object) dispatch method}.
    1.31 + * Such arguments can be of primitive types (<code>int</code>, <code>double</code>
    1.32 + * or {@link String}). Their names are used to extract values of appropriate
    1.33 + * properties from the event object. The following function...
    1.34 + * <pre>
    1.35 + * {@link Function @Function} static void <b>meaningOfWorld</b>(Names myModel, String data, int answer) {
    1.36 + *   {@link System}.out.{@link PrintStream#println(int) println}(answer);
    1.37 + * }
    1.38 + * // would print <b>42</b> if the dispatch method:
    1.39 + * {@link FunctionBinding#call(java.lang.Object, java.lang.Object) meaningOfWorld.call}(model, json)
    1.40 + * </pre>
    1.41 + * is called with equivalent of <code>var json = { 'answer' : 42 }</code>.
    1.42   * 
    1.43   * @author Jaroslav Tulach
    1.44   */
     2.1 --- a/json/src/test/java/net/java/html/json/MapModelTest.java	Tue Dec 09 11:43:46 2014 +0100
     2.2 +++ b/json/src/test/java/net/java/html/json/MapModelTest.java	Tue Dec 09 13:42:51 2014 +0100
     2.3 @@ -238,6 +238,37 @@
     2.4          assertEquals(one.changes, 2, "Snd change");
     2.5      }
     2.6  
     2.7 +    @Test public void functionWithParameters() {
     2.8 +        People p = Models.bind(new People(), c);
     2.9 +        p.getNicknames().add("One");
    2.10 +        p.getNicknames().add("Two");
    2.11 +        p.getNicknames().add("Three");
    2.12 +
    2.13 +        Map m = (Map)Models.toRaw(p);
    2.14 +        Object o = m.get("inInnerClass");
    2.15 +        assertNotNull(o, "functiton is available");
    2.16 +        assertEquals(o.getClass(), One.class);
    2.17 +        One one = (One)o;
    2.18 +        
    2.19 +        Map<String,Object> obj = new HashMap<String, Object>();
    2.20 +        obj.put("nick", "newNick");
    2.21 +        obj.put("x", 42);
    2.22 +        obj.put("y", 7.7f);
    2.23 +        final Person data = new Person("a", "b", Sex.MALE);
    2.24 +        
    2.25 +        one.fb.call(data, obj);
    2.26 +
    2.27 +        assertEquals(p.getInfo().size(), 1, "a+b is there: " + p.getInfo());
    2.28 +        assertEquals(p.getInfo().get(0), data, "Expecting data: " + p.getInfo());
    2.29 +        
    2.30 +        assertEquals(p.getNicknames().size(), 4, "One more nickname: " + p.getNicknames());
    2.31 +        assertEquals(p.getNicknames().get(3), "newNick");
    2.32 +        
    2.33 +        assertEquals(p.getAge().size(), 2, "Two new values: " + p.getAge());
    2.34 +        assertEquals(p.getAge().get(0).intValue(), 42);
    2.35 +        assertEquals(p.getAge().get(1).intValue(), 7);
    2.36 +    }
    2.37 +    
    2.38      static final class One {
    2.39          int changes;
    2.40          final PropertyBinding pb;
     3.1 --- a/json/src/test/java/net/java/html/json/PersonImpl.java	Tue Dec 09 11:43:46 2014 +0100
     3.2 +++ b/json/src/test/java/net/java/html/json/PersonImpl.java	Tue Dec 09 13:42:51 2014 +0100
     3.3 @@ -44,6 +44,7 @@
     3.4  
     3.5  import java.io.IOException;
     3.6  import java.util.Arrays;
     3.7 +import java.util.HashSet;
     3.8  import java.util.List;
     3.9  
    3.10  /**
    3.11 @@ -102,7 +103,11 @@
    3.12              p.getAge().add(42);
    3.13          }
    3.14          
    3.15 -        @Function static void inInnerClass(People p) throws IOException {
    3.16 +        @Function static void inInnerClass(People p, Person data, int x, double y, String nick) throws IOException {
    3.17 +            p.getInfo().add(data);
    3.18 +            p.getAge().add(x);
    3.19 +            p.getAge().add((int)y);
    3.20 +            p.getNicknames().add(nick);
    3.21          }
    3.22      }
    3.23  }