2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4 * Copyright 2013-2013 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-2013 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.util.ArrayList;
47 import java.util.Collections;
48 import java.util.Iterator;
49 import java.util.List;
50 import java.util.ListIterator;
51 import org.apidesign.html.context.spi.Contexts;
52 import org.apidesign.html.json.spi.FunctionBinding;
53 import org.apidesign.html.json.spi.PropertyBinding;
54 import org.apidesign.html.json.spi.Technology;
55 import static org.testng.Assert.*;
56 import org.testng.annotations.BeforeMethod;
57 import org.testng.annotations.Test;
61 * @author Jaroslav Tulach <jtulach@netbeans.org>
63 @Model(className = "Modelik", properties = {
64 @Property(name = "value", type = int.class),
65 @Property(name = "count", type = int.class),
66 @Property(name = "unrelated", type = long.class),
67 @Property(name = "names", type = String.class, array = true),
68 @Property(name = "values", type = int.class, array = true),
69 @Property(name = "people", type = Person.class, array = true),
70 @Property(name = "changedProperty", type=String.class)
72 public class ModelTest {
73 private MockTechnology my;
74 private Modelik model;
75 private static Modelik leakedModel;
78 public void createModel() {
79 my = new MockTechnology();
80 final BrwsrCtx c = Contexts.newBuilder().register(Technology.class, my, 1).build();
81 model = Models.bind(new Modelik(), c);
84 @Test public void classGeneratedWithSetterGetter() {
86 assertEquals(10, model.getValue(), "Value changed");
89 @Test public void computedMethod() {
91 assertEquals(16, model.getPowerValue());
94 @Test public void equalsAndHashCode() {
95 Modelik m1 = new Modelik(10, 20, 30, "changed", "firstName");
96 Modelik m2 = new Modelik(10, 20, 30, "changed", "firstName");
98 assertTrue(m1.equals(m2), "They are the same");
99 assertEquals(m1.hashCode(), m2.hashCode(), "Hashcode is the same");
103 assertFalse(m1.equals(m2), "No longer the same");
104 assertFalse(m1.hashCode() == m2.hashCode(), "No longe is hashcode is the same");
107 @Test public void arrayIsMutable() {
108 assertEquals(model.getNames().size(), 0, "Is empty");
109 model.getNames().add("Jarda");
110 assertEquals(model.getNames().size(), 1, "One element");
113 @Test public void arrayChangesNotNotifiedUntilInitied() {
114 model.getNames().add("Hello");
115 assertTrue(my.mutated.isEmpty(), "No change now " + my.mutated);
116 model.getNames().remove("Hello");
117 assertTrue(my.mutated.isEmpty(), "No change still " + my.mutated);
118 assertTrue(model.getNames().isEmpty(), "No empty");
121 @Test public void arrayChangesNotified() {
122 Models.applyBindings(model);
123 model.getNames().add("Hello");
125 assertFalse(my.mutated.isEmpty(), "There was a change" + my.mutated);
126 assertTrue(my.mutated.contains("names"), "Change in names property: " + my.mutated);
130 Iterator<String> it = model.getNames().iterator();
131 assertEquals(it.next(), "Hello");
134 assertFalse(my.mutated.isEmpty(), "There was a change" + my.mutated);
135 assertTrue(my.mutated.contains("names"), "Change in names property: " + my.mutated);
139 ListIterator<String> lit = model.getNames().listIterator();
142 assertFalse(my.mutated.isEmpty(), "There was a change" + my.mutated);
143 assertTrue(my.mutated.contains("names"), "Change in names property: " + my.mutated);
146 @Test public void autoboxedArray() {
147 model.getValues().add(10);
149 assertEquals(model.getValues().get(0), Integer.valueOf(10), "Really ten");
152 @Test public void derivedArrayProp() {
153 model.applyBindings();
156 List<String> arr = model.getRepeat();
157 assertEquals(arr.size(), 10, "Ten items: " + arr);
163 arr = model.getRepeat();
164 assertEquals(arr.size(), 5, "Five items: " + arr);
166 assertEquals(my.mutated.size(), 2, "Two properties changed: " + my.mutated);
167 assertTrue(my.mutated.contains("repeat"), "Array is in there: " + my.mutated);
168 assertTrue(my.mutated.contains("count"), "Count is in there: " + my.mutated);
171 @Test public void derivedPropertiesAreNotified() {
172 model.applyBindings();
176 // not interested in change of this property
177 my.mutated.remove("changedProperty");
178 my.mutated.remove("changedProperty");
180 assertEquals(my.mutated.size(), 2, "Two properties changed: " + my.mutated);
181 assertTrue(my.mutated.contains("powerValue"), "Power value is in there: " + my.mutated);
182 assertTrue(my.mutated.contains("value"), "Simple value is in there: " + my.mutated);
186 model.setUnrelated(44);
189 // not interested in change of this property
190 my.mutated.remove("changedProperty");
191 assertEquals(my.mutated.size(), 1, "One property changed: " + my.mutated);
192 assertTrue(my.mutated.contains("unrelated"), "Its name is unrelated");
195 @Test public void computedPropertyCannotWriteToModel() {
198 String res = model.getNotAllowedWrite();
199 fail("We should not be allowed to write to the model: " + res);
200 } catch (IllegalStateException ex) {
205 @Test public void computedPropertyCannotReadToModel() {
208 String res = model.getNotAllowedRead();
209 fail("We should not be allowed to read from the model: " + res);
210 } catch (IllegalStateException ex) {
215 @OnReceive(url = "{protocol}://{host}?query={query}", data = Person.class, onError = "errorState")
216 static void loadPeople(Modelik thiz, People p) {
219 m.loadPeople("http", "apidesign.org", "query", new Person());
222 static void errorState(Modelik thiz, Exception ex) {
226 @OnReceive(url = "{protocol}://{host}?callback={back}&query={query}", jsonp = "back")
227 static void loadPeopleViaJSONP(Modelik thiz, People p) {
230 m.loadPeopleViaJSONP("http", "apidesign.org", "query");
234 static void doSomething() {
238 static int powerValue(int value) {
239 return value * value;
242 @OnPropertyChange({ "powerValue", "unrelated" })
243 static void aPropertyChanged(Modelik m, String name) {
244 m.setChangedProperty(name);
247 @OnPropertyChange({ "values" })
248 static void anArrayPropertyChanged(String name, Modelik m) {
249 m.setChangedProperty(name);
252 @Test public void changeAnything() {
254 assertNull(model.getChangedProperty(), "No observed value change");
256 @Test public void changeValue() {
258 assertEquals(model.getChangedProperty(), "powerValue", "power property changed");
260 @Test public void changeUnrelated() {
261 model.setUnrelated(333);
262 assertEquals(model.getChangedProperty(), "unrelated", "unrelated changed");
265 @Test public void changeInArray() {
266 model.getValues().add(10);
267 assertNull(model.getChangedProperty(), "No change before applyBindings");
268 model.applyBindings();
269 model.getValues().add(10);
270 assertEquals(model.getChangedProperty(), "values", "Something added into the array");
274 static String notAllowedRead() {
275 return "Not allowed callback: " + leakedModel.getUnrelated();
279 static String notAllowedWrite() {
280 leakedModel.setUnrelated(11);
281 return "Not allowed callback!";
285 static List<String> repeat(int count) {
286 return Collections.nCopies(count, "Hello");
289 public @Test void hasPersonPropertyAndComputedFullName() {
290 List<Person> arr = model.getPeople();
291 assertEquals(arr.size(), 0, "By default empty");
294 String fullNameGenerated = p.getFullName();
295 assertNotNull(fullNameGenerated);
299 private static class MockTechnology implements Technology<Object> {
300 private final List<String> mutated = new ArrayList<String>();
303 public Object wrapModel(Object model) {
308 public void valueHasMutated(Object data, String propertyName) {
309 mutated.add(propertyName);
313 public void bind(PropertyBinding b, Object model, Object data) {
317 public void expose(FunctionBinding fb, Object model, Object d) {
321 public void applyBindings(Object data) {
325 public Object wrapArray(Object[] arr) {
330 public <M> M toModel(Class<M> modelClass, Object data) {
331 return modelClass.cast(data);
335 public void runSafe(Runnable r) {
336 throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.