Testing onChange behavior in union classes union
authorJaroslav Tulach <jaroslav.tulach@netbeans.org>
Wed, 29 Jan 2014 15:49:48 +0100
branchunion
changeset 505501413c8638c
parent 504 f21372e3b044
child 506 4b66c5141f84
Testing onChange behavior in union classes
json-tck/src/main/java/net/java/html/json/tests/KnockoutTest.java
json/src/main/java/org/netbeans/html/json/impl/ModelProcessor.java
json/src/test/java/net/java/html/json/MapModelTest.java
json/src/test/java/org/netbeans/html/json/impl/PlainEnumTest.java
     1.1 --- a/json-tck/src/main/java/net/java/html/json/tests/KnockoutTest.java	Wed Jan 29 15:41:16 2014 +0100
     1.2 +++ b/json-tck/src/main/java/net/java/html/json/tests/KnockoutTest.java	Wed Jan 29 15:49:48 2014 +0100
     1.3 @@ -133,6 +133,11 @@
     1.4  
     1.5              assert ones == 2 : "Two from One: " + ones;
     1.6              assert twos == 0 : "None from Two: " + twos;
     1.7 +            
     1.8 +            m.getOne().getOnes().add("Third");
     1.9 +            
    1.10 +            int ones3 = countChildren("ones");
    1.11 +            assert ones3 == 3 : "Three now from One: " + ones;
    1.12          } finally {
    1.13              Utils.exposeHTML(KnockoutTest.class, "");
    1.14          }
     2.1 --- a/json/src/main/java/org/netbeans/html/json/impl/ModelProcessor.java	Wed Jan 29 15:41:16 2014 +0100
     2.2 +++ b/json/src/main/java/org/netbeans/html/json/impl/ModelProcessor.java	Wed Jan 29 15:49:48 2014 +0100
     2.3 @@ -565,22 +565,11 @@
     2.4              if (!generateComputedProperties(body, props, e.getEnclosedElements(), propsGetSet, propsDeps)) {
     2.5                  ok = false;
     2.6              }
     2.7 -            if (!generateOnChange(e, propsDeps, props, className, functionDeps)) {
     2.8 -                ok = false;
     2.9 -            }
    2.10              if (!generateProperties(e, body, className, props, propsGetSet, null, propsDeps, functionDeps)) {
    2.11                  ok = false;
    2.12              }
    2.13 -            if (!generateFunctions(e, body, className, e.getEnclosedElements(), functions)) {
    2.14 -                ok = false;
    2.15 -            }
    2.16 -            if (!generateReceive(e, body, className, e.getEnclosedElements(), onReceiveType)) {
    2.17 -                ok = false;
    2.18 -            }
    2.19 -            if (!generateOperation(e, body, className, e.getEnclosedElements())) {
    2.20 -                ok = false;
    2.21 -            }
    2.22              List<PropInfo> mergedProps = new ArrayList<PropInfo>(propsGetSet);
    2.23 +            List<Prprt> allProps = new ArrayList<Prprt>(Arrays.asList(props));
    2.24              for (Element ec : e.getEnclosedElements()) {
    2.25                  if (ec.getKind() != ElementKind.ENUM_CONSTANT) {
    2.26                      continue;
    2.27 @@ -590,6 +579,7 @@
    2.28                      continue;
    2.29                  }
    2.30                  Prprt[] subProps = Prprt.wrap(processingEnv, ec, em.properties());
    2.31 +                allProps.addAll(Arrays.asList(subProps));
    2.32                  for (Prprt prprt : subProps) {
    2.33                      PropInfo found = null;
    2.34                      for (PropInfo existing : mergedProps) {
    2.35 @@ -620,6 +610,18 @@
    2.36                      found.addModel(em);
    2.37                  }
    2.38              }            
    2.39 +            if (!generateOnChange(e, propsDeps, allProps.toArray(new Prprt[0]), className, functionDeps)) {
    2.40 +                ok = false;
    2.41 +            }
    2.42 +            if (!generateFunctions(e, body, className, e.getEnclosedElements(), functions)) {
    2.43 +                ok = false;
    2.44 +            }
    2.45 +            if (!generateReceive(e, body, className, e.getEnclosedElements(), onReceiveType)) {
    2.46 +                ok = false;
    2.47 +            }
    2.48 +            if (!generateOperation(e, body, className, e.getEnclosedElements())) {
    2.49 +                ok = false;
    2.50 +            }
    2.51              FileObject java = processingEnv.getFiler().createSourceFile(pkg + '.' + className, e);
    2.52              w = new OutputStreamWriter(java.openOutputStream());
    2.53              try {
    2.54 @@ -1422,7 +1424,7 @@
    2.55              
    2.56              for (String pn : onPC.value()) {
    2.57                  StringBuilder call = new StringBuilder();
    2.58 -                call.append("  ").append(clazz.getSimpleName()).append(".").append(n).append("(");
    2.59 +                call.append("  ").append(inPckName(clazz)).append(".").append(n).append("(");
    2.60                  call.append(wrapPropName(e, className, "name", pn));
    2.61                  call.append(");\n");
    2.62                  
     3.1 --- a/json/src/test/java/net/java/html/json/MapModelTest.java	Wed Jan 29 15:41:16 2014 +0100
     3.2 +++ b/json/src/test/java/net/java/html/json/MapModelTest.java	Wed Jan 29 15:49:48 2014 +0100
     3.3 @@ -165,7 +165,7 @@
     3.4          assertEquals(p.getSex(), Sex.FEMALE, "Changed");
     3.5      }
     3.6  
     3.7 -    static final class One {
     3.8 +    public static final class One {
     3.9          int changes;
    3.10          final PropertyBinding pb;
    3.11          final FunctionBinding fb;
    3.12 @@ -186,6 +186,10 @@
    3.13          void set(Object v) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {
    3.14              pb.setValue(v);
    3.15          }
    3.16 +        
    3.17 +        public int getChanges() {
    3.18 +            return changes;
    3.19 +        }
    3.20      }
    3.21      
    3.22      public static final class MapTechnology 
     4.1 --- a/json/src/test/java/org/netbeans/html/json/impl/PlainEnumTest.java	Wed Jan 29 15:41:16 2014 +0100
     4.2 +++ b/json/src/test/java/org/netbeans/html/json/impl/PlainEnumTest.java	Wed Jan 29 15:49:48 2014 +0100
     4.3 @@ -47,6 +47,7 @@
     4.4  import net.java.html.json.MapModelTest;
     4.5  import net.java.html.json.Model;
     4.6  import net.java.html.json.Models;
     4.7 +import net.java.html.json.OnPropertyChange;
     4.8  import net.java.html.json.Property;
     4.9  import org.apidesign.html.context.spi.Contexts;
    4.10  import org.apidesign.html.json.spi.Technology;
    4.11 @@ -106,7 +107,12 @@
    4.12          @Model(className = "B", properties = { 
    4.13              @Property(name = "ub", type = String.class)
    4.14          })
    4.15 -        B
    4.16 +        B;
    4.17 +        
    4.18 +        @OnPropertyChange({ "ua", "ub" })
    4.19 +        static void onChange(Union u) {
    4.20 +            u.setX(u.getX() + 1);
    4.21 +        }
    4.22      }
    4.23      
    4.24      @Model(className = "EnumAndModel", properties = {
    4.25 @@ -146,7 +152,15 @@
    4.26          Map<?,?> map = (Map<?,?>)Models.toRaw(u);
    4.27          assertNotNull(map.get("x"), "Four properties: " + map);
    4.28          assertNotNull(map.get("y"), "Four properties: " + map);
    4.29 +        MapModelTest.One ub;
    4.30          assertNotNull(map.get("ua"), "Four properties: " + map);
    4.31 -        assertNotNull(map.get("ub"), "Four properties: " + map);
    4.32 +        assertNotNull(ub = (MapModelTest.One) map.get("ub"), "Four properties: " + map);
    4.33 +        
    4.34 +        assertEquals(u.getX(), 11, "Eleven");
    4.35 +        
    4.36 +        assertEquals(ub.getChanges(), 0, "No changes yet");
    4.37 +        u.getB().setUb("10");
    4.38 +        assertEquals(u.getX(), 12, "Change increases X");
    4.39 +        assertEquals(ub.getChanges(), 1, "One change");
    4.40      }
    4.41  }