Arrays get properly wrapped by technology classes beans
authorJaroslav Tulach <jtulach@netbeans.org>
Mon, 04 Aug 2014 07:08:46 +0200
branchbeans
changeset 8059e9e0c10dff4
parent 804 c9774f95da96
child 806 3aa239b9922d
Arrays get properly wrapped by technology classes
json-beans/src/main/java/net/java/html/beans/JSONBeans.java
json-beans/src/test/java/net/java/html/beans/MapModelTest.java
json-beans/src/test/java/net/java/html/beans/People.java
     1.1 --- a/json-beans/src/main/java/net/java/html/beans/JSONBeans.java	Mon Aug 04 06:51:19 2014 +0200
     1.2 +++ b/json-beans/src/main/java/net/java/html/beans/JSONBeans.java	Mon Aug 04 07:08:46 2014 +0200
     1.3 @@ -59,6 +59,7 @@
     1.4  import net.java.html.BrwsrCtx;
     1.5  import net.java.html.json.Function;
     1.6  import net.java.html.json.Model;
     1.7 +import net.java.html.json.Models;
     1.8  import net.java.html.json.Property;
     1.9  import org.apidesign.html.json.spi.Proto;
    1.10  
    1.11 @@ -151,7 +152,18 @@
    1.12          @Override
    1.13          protected Object getValue(Object model, int index) {
    1.14              try {
    1.15 -                return properties[index].getReadMethod().invoke(model);
    1.16 +                Object ret = properties[index].getReadMethod().invoke(model);
    1.17 +                if (ret instanceof Object[]) {
    1.18 +                    Object[] arr = new Object[((Object[])ret).length];
    1.19 +                    for (int i = 0; i < arr.length; i++) {
    1.20 +                        Object ith = ((Object[])ret)[i];
    1.21 +                        if (ith != null) {
    1.22 +                            arr[i] = Models.toRaw(ith);
    1.23 +                        }
    1.24 +                    }
    1.25 +                    return arr;
    1.26 +                }
    1.27 +                return ret;
    1.28              } catch (Exception ex) {
    1.29                  throw toRuntime(ex, RuntimeException.class);
    1.30              }
     2.1 --- a/json-beans/src/test/java/net/java/html/beans/MapModelTest.java	Mon Aug 04 06:51:19 2014 +0200
     2.2 +++ b/json-beans/src/test/java/net/java/html/beans/MapModelTest.java	Mon Aug 04 07:08:46 2014 +0200
     2.3 @@ -71,6 +71,7 @@
     2.4      
     2.5      @BeforeClass public static void registerBeanAsModel() throws IntrospectionException {
     2.6          JSONBeans.register(Person.class);
     2.7 +        JSONBeans.register(People.class);
     2.8      }
     2.9  
    2.10      @BeforeMethod public void initTechnology() {
    2.11 @@ -203,6 +204,33 @@
    2.12          
    2.13          assertEquals(p.getSex(), Sex.FEMALE, "Changed");
    2.14      }
    2.15 +    
    2.16 +    @Test public void workWithArray() throws Exception {
    2.17 +        People people = Models.bind(new People(), c);
    2.18 +        Person p1 = Models.bind(new Person(), c);
    2.19 +        p1.setFirstName("Ahoj");
    2.20 +        Person p2 = Models.bind(new Person(), c);
    2.21 +        p2.setFirstName("Hi");
    2.22 +        
    2.23 +        people.setPeople(p1, p2);
    2.24 +        
    2.25 +        Map m = (Map) Models.toRaw(people);
    2.26 +        Object o = m.get("people");
    2.27 +        assertNotNull(o, "People array registered in the model");
    2.28 +        assertEquals(o.getClass(), One.class);
    2.29 +
    2.30 +        One one = (One) o;
    2.31 +        assertNotNull(one.pb, "binding specified");
    2.32 +        
    2.33 +        Object value = one.get();
    2.34 +        assertTrue(value instanceof Object[], "It is an array: " + value);
    2.35 +        Object[] arrValue = (Object[]) value;
    2.36 +        assertEquals(arrValue.length, 2, "Two values");
    2.37 +        assertFalse(arrValue[0] instanceof Person, "Model classes are not directly in the array");
    2.38 +        assertTrue(arrValue[0] instanceof Map, "Technology represeting class is found");
    2.39 +        
    2.40 +        assertEquals(arrValue[0], Models.toRaw(p1), "Object behind the scene is the same");
    2.41 +    }
    2.42  
    2.43      static final class One {
    2.44          int changes;
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/json-beans/src/test/java/net/java/html/beans/People.java	Mon Aug 04 07:08:46 2014 +0200
     3.3 @@ -0,0 +1,78 @@
     3.4 +/*
     3.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     3.6 + *
     3.7 + * Copyright 2014 Oracle and/or its affiliates. All rights reserved.
     3.8 + *
     3.9 + * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
    3.10 + * Other names may be trademarks of their respective owners.
    3.11 + *
    3.12 + * The contents of this file are subject to the terms of either the GNU
    3.13 + * General Public License Version 2 only ("GPL") or the Common
    3.14 + * Development and Distribution License("CDDL") (collectively, the
    3.15 + * "License"). You may not use this file except in compliance with the
    3.16 + * License. You can obtain a copy of the License at
    3.17 + * http://www.netbeans.org/cddl-gplv2.html
    3.18 + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
    3.19 + * specific language governing permissions and limitations under the
    3.20 + * License.  When distributing the software, include this License Header
    3.21 + * Notice in each file and include the License file at
    3.22 + * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
    3.23 + * particular file as subject to the "Classpath" exception as provided
    3.24 + * by Oracle in the GPL Version 2 section of the License file that
    3.25 + * accompanied this code. If applicable, add the following below the
    3.26 + * License Header, with the fields enclosed by brackets [] replaced by
    3.27 + * your own identifying information:
    3.28 + * "Portions Copyrighted [year] [name of copyright owner]"
    3.29 + *
    3.30 + * If you wish your version of this file to be governed by only the CDDL
    3.31 + * or only the GPL Version 2, indicate your decision by adding
    3.32 + * "[Contributor] elects to include this software in this distribution
    3.33 + * under the [CDDL or GPL Version 2] license." If you do not indicate a
    3.34 + * single choice of license, a recipient has the option to distribute
    3.35 + * your version of this file under either the CDDL, the GPL Version 2 or
    3.36 + * to extend the choice of license to its licensees as provided above.
    3.37 + * However, if you add GPL Version 2 code and therefore, elected the GPL
    3.38 + * Version 2 license, then the option applies only if the new code is
    3.39 + * made subject to such option by the copyright holder.
    3.40 + *
    3.41 + * Contributor(s):
    3.42 + *
    3.43 + * Portions Copyrighted 2014 Sun Microsystems, Inc.
    3.44 + */
    3.45 +
    3.46 +package net.java.html.beans;
    3.47 +
    3.48 +import java.beans.PropertyChangeListener;
    3.49 +import java.beans.PropertyChangeSupport;
    3.50 +
    3.51 +/**
    3.52 + *
    3.53 + * @author Jaroslav Tulach
    3.54 + */
    3.55 +public class People {
    3.56 +    
    3.57 +    private Person[] people;
    3.58 +
    3.59 +    public static final String PROP_PEOPLE = "people";
    3.60 +
    3.61 +    public Person[] getPeople() {
    3.62 +        return people;
    3.63 +    }
    3.64 +
    3.65 +    public void setPeople(Person... people) {
    3.66 +        Person[] oldPeople = this.people;
    3.67 +        this.people = people;
    3.68 +        propertyChangeSupport.firePropertyChange(PROP_PEOPLE, oldPeople, people);
    3.69 +    }
    3.70 +
    3.71 +    private transient final PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport(this);
    3.72 +
    3.73 +    public void addPropertyChangeListener(PropertyChangeListener listener) {
    3.74 +        propertyChangeSupport.addPropertyChangeListener(listener);
    3.75 +    }
    3.76 +
    3.77 +    public void removePropertyChangeListener(PropertyChangeListener listener) {
    3.78 +        propertyChangeSupport.removePropertyChangeListener(listener);
    3.79 +    }
    3.80 +
    3.81 +}