2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4 * Copyright 2013-2014 Oracle and/or its affiliates. All rights reserved.
6 * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
7 * Other names may be trademarks of their respective owners.
9 * The contents of this file are subject to the terms of either the GNU
10 * General Public License Version 2 only ("GPL") or the Common
11 * Development and Distribution License("CDDL") (collectively, the
12 * "License"). You may not use this file except in compliance with the
13 * License. You can obtain a copy of the License at
14 * http://www.netbeans.org/cddl-gplv2.html
15 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
16 * specific language governing permissions and limitations under the
17 * License. When distributing the software, include this License Header
18 * Notice in each file and include the License file at
19 * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this
20 * particular file as subject to the "Classpath" exception as provided
21 * by Oracle in the GPL Version 2 section of the License file that
22 * accompanied this code. If applicable, add the following below the
23 * License Header, with the fields enclosed by brackets [] replaced by
24 * your own identifying information:
25 * "Portions Copyrighted [year] [name of copyright owner]"
29 * The Original Software is NetBeans. The Initial Developer of the Original
30 * Software is Oracle. Portions Copyright 2013-2014 Oracle. All Rights Reserved.
32 * If you wish your version of this file to be governed by only the CDDL
33 * or only the GPL Version 2, indicate your decision by adding
34 * "[Contributor] elects to include this software in this distribution
35 * under the [CDDL or GPL Version 2] license." If you do not indicate a
36 * single choice of license, a recipient has the option to distribute
37 * your version of this file under either the CDDL, the GPL Version 2 or
38 * to extend the choice of license to its licensees as provided above.
39 * However, if you add GPL Version 2 code and therefore, elected the GPL
40 * Version 2 license, then the option applies only if the new code is
41 * made subject to such option by the copyright holder.
43 package net.java.html.json;
45 import net.java.html.BrwsrCtx;
46 import java.io.IOException;
47 import java.io.InputStream;
48 import java.lang.reflect.InvocationTargetException;
49 import java.util.HashMap;
51 import org.apidesign.html.context.spi.Contexts;
52 import org.apidesign.html.json.spi.FunctionBinding;
53 import org.apidesign.html.json.spi.JSONCall;
54 import org.apidesign.html.json.spi.PropertyBinding;
55 import org.apidesign.html.json.spi.Technology;
56 import org.apidesign.html.json.spi.Transfer;
57 import org.netbeans.html.json.impl.JSON;
58 import org.testng.annotations.BeforeMethod;
59 import org.testng.annotations.Test;
60 import static org.testng.Assert.*;
64 * @author Jaroslav Tulach <jtulach@netbeans.org>
66 public class MapModelTest {
67 private MapTechnology t;
70 @BeforeMethod public void initTechnology() {
71 t = new MapTechnology();
72 c = Contexts.newBuilder().register(Technology.class, t, 1).
73 register(Transfer.class, t, 1).build();
76 @Test public void isThereABinding() throws Exception {
77 Person p = Models.bind(new Person(), c).applyBindings();
78 p.setFirstName("Jarda");
80 Map m = (Map)Models.toRaw(p);
81 Object v = m.get("firstName");
82 assertNotNull(v, "Value should be in the map");
83 assertEquals(v.getClass(), One.class, "It is instance of One");
85 assertEquals(o.changes, 1, "One change so far");
86 assertFalse(o.pb.isReadOnly(), "Mutable property");
88 assertEquals(o.get(), "Jarda", "Value should be in the map");
92 assertEquals(p.getFirstName(), "Karle", "Model value updated");
93 assertEquals(o.changes, 2, "Snd change");
97 @Test public void dontNotifySameProperty() throws Exception {
98 Person p = Models.bind(new Person(), c);
99 p.setFirstName("Jirka");
101 Map m = (Map)Models.toRaw(p);
102 Object v = m.get("firstName");
103 assertNotNull(v, "Value should be in the map");
104 assertEquals(v.getClass(), One.class, "It is instance of One");
106 assertEquals(o.changes, 0, "No change so far the only one change happened before we connected");
108 p.setFirstName(new String("Jirka"));
109 assertEquals(o.changes, 0, "No change so far, the value is the same");
111 p.setFirstName("Jarda");
112 assertFalse(o.pb.isReadOnly(), "Mutable property");
114 assertEquals(o.get(), "Jarda", "Value should be in the map");
118 assertEquals(p.getFirstName(), "Karle", "Model value updated");
119 assertEquals(o.changes, 2, "Snd change");
122 @Test public void derivedProperty() throws Exception {
123 Person p = Models.bind(new Person(), c);
125 Map m = (Map)Models.toRaw(p);
126 Object v = m.get("fullName");
127 assertNotNull(v, "Value should be in the map");
128 assertEquals(v.getClass(), One.class, "It is instance of One");
130 assertTrue(o.pb.isReadOnly(), "Mutable property");
133 @Test public void changeSex() {
134 Person p = Models.bind(new Person(), c);
135 p.setFirstName("Trans");
138 Map m = (Map)Models.toRaw(p);
139 Object o = m.get("changeSex");
140 assertNotNull(o, "Function registered in the model");
141 assertEquals(o.getClass(), One.class);
144 assertNotNull(one.fb, "Function binding specified");
146 one.fb.call(null, null);
148 assertEquals(p.getSex(), Sex.FEMALE, "Changed");
151 @Test public void setSex() {
152 Person p = Models.bind(new Person(), c);
153 p.setFirstName("Trans");
155 Map m = (Map)Models.toRaw(p);
156 Object o = m.get("changeSex");
157 assertNotNull(o, "Function registered in the model");
158 assertEquals(o.getClass(), One.class);
161 assertNotNull(one.fb, "Function binding specified");
163 one.fb.call("FEMALE", new Object());
165 assertEquals(p.getSex(), Sex.FEMALE, "Changed");
168 static final class One {
170 final PropertyBinding pb;
171 final FunctionBinding fb;
173 One(Object m, PropertyBinding pb) throws NoSuchMethodException {
177 One(Object m, FunctionBinding fb) throws NoSuchMethodException {
182 Object get() throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {
183 return pb.getValue();
186 void set(Object v) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {
191 static final class MapTechnology
192 implements Technology<Map<String,One>>, Transfer {
195 public Map<String, One> wrapModel(Object model) {
196 return new HashMap<String, One>();
200 public void valueHasMutated(Map<String, One> data, String propertyName) {
201 One p = data.get(propertyName);
208 public void bind(PropertyBinding b, Object model, Map<String, One> data) {
210 One o = new One(model, b);
211 data.put(b.getPropertyName(), o);
212 } catch (NoSuchMethodException ex) {
213 throw new IllegalStateException(ex);
218 public void expose(FunctionBinding fb, Object model, Map<String, One> data) {
220 data.put(fb.getFunctionName(), new One(model, fb));
221 } catch (NoSuchMethodException ex) {
222 throw new IllegalStateException(ex);
227 public void applyBindings(Map<String, One> data) {
231 public Object wrapArray(Object[] arr) {
236 public void extract(Object obj, String[] props, Object[] values) {
237 Map<?,?> map = obj instanceof Map ? (Map<?,?>)obj : null;
238 for (int i = 0; i < Math.min(props.length, values.length); i++) {
242 values[i] = map.get(props[i]);
243 if (values[i] instanceof One) {
244 values[i] = ((One)values[i]).pb.getValue();
251 public void loadJSON(JSONCall call) {
252 call.notifyError(new UnsupportedOperationException());
256 public <M> M toModel(Class<M> modelClass, Object data) {
257 return modelClass.cast(data);
261 public Object toJSON(InputStream is) throws IOException {
262 throw new IOException();
266 public void runSafe(Runnable r) {
267 throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.