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.testng.annotations.BeforeMethod;
58 import org.testng.annotations.Test;
59 import static org.testng.Assert.*;
63 * @author Jaroslav Tulach
65 public class MapModelTest {
66 private MapTechnology t;
69 @BeforeMethod public void initTechnology() {
70 t = new MapTechnology();
71 c = Contexts.newBuilder().register(Technology.class, t, 1).
72 register(Transfer.class, t, 1).build();
75 @Test public void isThereABinding() throws Exception {
76 Person p = Models.bind(new Person(), c).applyBindings();
77 p.setFirstName("Jarda");
79 Map m = (Map)Models.toRaw(p);
80 Object v = m.get("firstName");
81 assertNotNull(v, "Value should be in the map");
82 assertEquals(v.getClass(), One.class, "It is instance of One");
84 assertEquals(o.changes, 1, "One change so far");
85 assertFalse(o.pb.isReadOnly(), "Mutable property");
87 assertEquals(o.get(), "Jarda", "Value should be in the map");
91 assertEquals(p.getFirstName(), "Karle", "Model value updated");
92 assertEquals(o.changes, 2, "Snd change");
96 @Test public void dontNotifySameProperty() throws Exception {
97 Person p = Models.bind(new Person(), c);
98 p.setFirstName("Jirka");
100 Map m = (Map)Models.toRaw(p);
101 Object v = m.get("firstName");
102 assertNotNull(v, "Value should be in the map");
103 assertEquals(v.getClass(), One.class, "It is instance of One");
105 assertEquals(o.changes, 0, "No change so far the only one change happened before we connected");
107 p.setFirstName(new String("Jirka"));
108 assertEquals(o.changes, 0, "No change so far, the value is the same");
110 p.setFirstName("Jarda");
111 assertFalse(o.pb.isReadOnly(), "Mutable property");
113 assertEquals(o.get(), "Jarda", "Value should be in the map");
117 assertEquals(p.getFirstName(), "Karle", "Model value updated");
118 assertEquals(o.changes, 2, "Snd change");
121 @Test public void canSetEnumAsString() throws Exception {
122 Person p = Models.bind(new Person(), c);
123 p.setFirstName("Jirka");
126 Map m = (Map)Models.toRaw(p);
127 Object v = m.get("sex");
128 assertNotNull(v, "Value should be in the map");
129 assertEquals(v.getClass(), One.class, "It is instance of One");
134 assertEquals(p.getSex(), Sex.FEMALE, "Changed to female");
137 @Test public void derivedProperty() throws Exception {
138 Person p = Models.bind(new Person(), c);
140 Map m = (Map)Models.toRaw(p);
141 Object v = m.get("fullName");
142 assertNotNull(v, "Value should be in the map");
143 assertEquals(v.getClass(), One.class, "It is instance of One");
145 assertTrue(o.pb.isReadOnly(), "Mutable property");
148 @Test public void changeSex() {
149 Person p = Models.bind(new Person(), c);
150 p.setFirstName("Trans");
153 Map m = (Map)Models.toRaw(p);
154 Object o = m.get("changeSex");
155 assertNotNull(o, "Function registered in the model");
156 assertEquals(o.getClass(), One.class);
159 assertNotNull(one.fb, "Function binding specified");
161 one.fb.call(null, null);
163 assertEquals(p.getSex(), Sex.FEMALE, "Changed");
166 @Test public void setSex() {
167 Person p = Models.bind(new Person(), c);
168 p.setFirstName("Trans");
170 Map m = (Map)Models.toRaw(p);
171 Object o = m.get("changeSex");
172 assertNotNull(o, "Function registered in the model");
173 assertEquals(o.getClass(), One.class);
176 assertNotNull(one.fb, "Function binding specified");
178 one.fb.call("FEMALE", new Object());
180 assertEquals(p.getSex(), Sex.FEMALE, "Changed");
183 static final class One {
185 final PropertyBinding pb;
186 final FunctionBinding fb;
188 One(Object m, PropertyBinding pb) throws NoSuchMethodException {
192 One(Object m, FunctionBinding fb) throws NoSuchMethodException {
197 Object get() throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {
198 return pb.getValue();
201 void set(Object v) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {
206 static final class MapTechnology
207 implements Technology<Map<String,One>>, Transfer {
210 public Map<String, One> wrapModel(Object model) {
211 return new HashMap<String, One>();
215 public void valueHasMutated(Map<String, One> data, String propertyName) {
216 One p = data.get(propertyName);
223 public void bind(PropertyBinding b, Object model, Map<String, One> data) {
225 One o = new One(model, b);
226 data.put(b.getPropertyName(), o);
227 } catch (NoSuchMethodException ex) {
228 throw new IllegalStateException(ex);
233 public void expose(FunctionBinding fb, Object model, Map<String, One> data) {
235 data.put(fb.getFunctionName(), new One(model, fb));
236 } catch (NoSuchMethodException ex) {
237 throw new IllegalStateException(ex);
242 public void applyBindings(Map<String, One> data) {
246 public Object wrapArray(Object[] arr) {
251 public void extract(Object obj, String[] props, Object[] values) {
252 Map<?,?> map = obj instanceof Map ? (Map<?,?>)obj : null;
253 for (int i = 0; i < Math.min(props.length, values.length); i++) {
257 values[i] = map.get(props[i]);
258 if (values[i] instanceof One) {
259 values[i] = ((One)values[i]).pb.getValue();
266 public void loadJSON(JSONCall call) {
267 call.notifyError(new UnsupportedOperationException());
271 public <M> M toModel(Class<M> modelClass, Object data) {
272 return modelClass.cast(data);
276 public Object toJSON(InputStream is) throws IOException {
277 throw new IOException();
281 public void runSafe(Runnable r) {
282 throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.