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 java.util.ArrayList;
46 import java.util.Collections;
47 import java.util.Iterator;
48 import java.util.List;
49 import java.util.ListIterator;
50 import net.java.html.BrwsrCtx;
51 import org.netbeans.html.context.spi.Contexts;
52 import org.netbeans.html.json.spi.FunctionBinding;
53 import org.netbeans.html.json.spi.PropertyBinding;
54 import org.netbeans.html.json.spi.Technology;
55 import static org.testng.Assert.assertEquals;
56 import static org.testng.Assert.assertFalse;
57 import static org.testng.Assert.assertNotNull;
58 import static org.testng.Assert.assertNull;
59 import static org.testng.Assert.assertTrue;
60 import static org.testng.Assert.fail;
61 import org.testng.annotations.BeforeMethod;
62 import org.testng.annotations.Test;
66 * @author Jaroslav Tulach
68 @Model(className = "Modelik", builder = "change", targetId = "", properties = {
69 @Property(name = "value", type = int.class),
70 @Property(name = "count", type = int.class),
71 @Property(name = "unrelated", type = long.class),
72 @Property(name = "names", type = String.class, array = true),
73 @Property(name = "values", type = int.class, array = true),
74 @Property(name = "people", type = Person.class, array = true),
75 @Property(name = "changedProperty", type=String.class)
77 public class ModelTest {
78 private MockTechnology my;
79 private Modelik model;
80 private static Modelik leakedModel;
83 public void createModel() {
84 my = new MockTechnology();
85 final BrwsrCtx c = Contexts.newBuilder().register(Technology.class, my, 1).build();
86 model = Models.bind(new Modelik(), c);
89 @Test public void classGeneratedWithSetterGetter() {
91 assertEquals(10, model.getValue(), "Value changed");
94 @Test public void computedMethod() {
96 assertEquals(16, model.getPowerValue());
99 @Test public void equalsAndHashCode() {
100 Modelik m1 = new Modelik();
104 m1.setChangedProperty("changed");
105 m1.getNames().add("firstName");
106 Modelik m2 = new Modelik().
110 changeChangedProperty("changed").
111 changeNames("firstName");
113 assertTrue(m1.equals(m2), "They are the same");
114 assertEquals(m1.hashCode(), m2.hashCode(), "Hashcode is the same");
118 assertFalse(m1.equals(m2), "No longer the same");
119 assertFalse(m1.hashCode() == m2.hashCode(), "No longe is hashcode is the same");
122 @Test public void arrayIsMutable() {
123 assertEquals(model.getNames().size(), 0, "Is empty");
124 model.getNames().add("Jarda");
125 assertEquals(model.getNames().size(), 1, "One element");
128 @Test public void arrayChangesNotNotifiedUntilInitied() {
129 model.getNames().add("Hello");
130 assertTrue(my.mutated.isEmpty(), "No change now " + my.mutated);
131 model.getNames().remove("Hello");
132 assertTrue(my.mutated.isEmpty(), "No change still " + my.mutated);
133 assertTrue(model.getNames().isEmpty(), "No empty");
136 @Test public void arrayChangesNotified() {
137 Models.applyBindings(model);
138 model.getNames().add("Hello");
140 assertFalse(my.mutated.isEmpty(), "There was a change" + my.mutated);
141 assertTrue(my.mutated.contains("names"), "Change in names property: " + my.mutated);
145 Iterator<String> it = model.getNames().iterator();
146 assertEquals(it.next(), "Hello");
149 assertFalse(my.mutated.isEmpty(), "There was a change" + my.mutated);
150 assertTrue(my.mutated.contains("names"), "Change in names property: " + my.mutated);
154 ListIterator<String> lit = model.getNames().listIterator();
157 assertFalse(my.mutated.isEmpty(), "There was a change" + my.mutated);
158 assertTrue(my.mutated.contains("names"), "Change in names property: " + my.mutated);
161 @Test public void autoboxedArray() {
162 model.getValues().add(10);
164 assertEquals(model.getValues().get(0), Integer.valueOf(10), "Really ten");
167 @Test public void derivedArrayProp() {
168 model.applyBindings();
171 List<String> arr = model.getRepeat();
172 assertEquals(arr.size(), 10, "Ten items: " + arr);
178 arr = model.getRepeat();
179 assertEquals(arr.size(), 5, "Five items: " + arr);
181 assertEquals(my.mutated.size(), 2, "Two properties changed: " + my.mutated);
182 assertTrue(my.mutated.contains("repeat"), "Array is in there: " + my.mutated);
183 assertTrue(my.mutated.contains("count"), "Count is in there: " + my.mutated);
186 @Test public void derivedArrayPropChange() {
187 model.applyBindings();
190 List<String> arr = model.getRepeat();
191 assertEquals(arr.size(), 5, "Five items: " + arr);
194 assertEquals(model.getCount(), 10, "Changing repeat changes count");
197 @Test public void derivedPropertiesAreNotified() {
198 model.applyBindings();
202 // not interested in change of this property
203 my.mutated.remove("changedProperty");
205 assertEquals(my.mutated.size(), 2, "Two properties changed: " + my.mutated);
206 assertTrue(my.mutated.contains("powerValue"), "Power value is in there: " + my.mutated);
207 assertTrue(my.mutated.contains("value"), "Simple value is in there: " + my.mutated);
211 model.setUnrelated(44);
214 // not interested in change of this property
215 my.mutated.remove("changedProperty");
216 assertEquals(my.mutated.size(), 1, "One property changed: " + my.mutated);
217 assertTrue(my.mutated.contains("unrelated"), "Its name is unrelated");
220 @Test public void computedPropertyCannotWriteToModel() {
223 String res = model.getNotAllowedWrite();
224 fail("We should not be allowed to write to the model: " + res);
225 } catch (IllegalStateException ex) {
230 @Test public void computedPropertyCannotReadToModel() {
233 String res = model.getNotAllowedRead();
234 fail("We should not be allowed to read from the model: " + res);
235 } catch (IllegalStateException ex) {
240 @OnReceive(url = "{protocol}://{host}?query={query}", data = Person.class, onError = "errorState")
241 static void loadPeople(Modelik thiz, People p) {
244 m.loadPeople("http", "apidesign.org", "query", new Person());
247 static void errorState(Modelik thiz, Exception ex) {
251 @OnReceive(url="{url}", headers={
253 "H-a+r!d?e.r: {harder}",
254 "H-a+r!d?e's\"t: {harder}",
259 static void fetchPeopleWithHeaders(Modelik model, People p) {
260 model.fetchPeopleWithHeaders("url", "easy", "harder", "rep");
263 @OnReceive(url = "{protocol}://{host}?callback={back}&query={query}", jsonp = "back")
264 static void loadPeopleViaJSONP(Modelik thiz, People p) {
267 m.loadPeopleViaJSONP("http", "apidesign.org", "query");
270 @OnReceive(url = "{rep}://{rep}")
271 static void repeatedTest(Modelik thiz, People p) {
272 thiz.repeatedTest("justOneParameterRep");
276 static void doSomething() {
279 @ComputedProperty(write = "setPowerValue")
280 static int powerValue(int value) {
281 return value * value;
284 static void setPowerValue(Modelik m, int value) {
285 m.setValue((int)Math.sqrt(value));
288 @OnPropertyChange({ "powerValue", "unrelated" })
289 static void aPropertyChanged(Modelik m, String name) {
290 m.setChangedProperty(name);
293 @OnPropertyChange({ "values" })
294 static void anArrayPropertyChanged(String name, Modelik m) {
295 m.setChangedProperty(name);
298 @Test public void changeAnything() {
300 assertNull(model.getChangedProperty(), "No observed value change");
302 @Test public void changeValue() {
304 assertEquals(model.getChangedProperty(), "powerValue", "power property changed");
306 @Test public void changePowerValue() {
308 assertEquals(model.getPowerValue(), 9, "Square");
309 model.setPowerValue(16);
310 assertEquals(model.getValue(), 4, "Square root");
311 assertEquals(model.getPowerValue(), 16, "Square changed");
313 @Test public void changeUnrelated() {
314 model.setUnrelated(333);
315 assertEquals(model.getChangedProperty(), "unrelated", "unrelated changed");
318 @Test public void changeInArray() {
319 model.getValues().add(10);
320 assertNull(model.getChangedProperty(), "No change before applyBindings");
321 model.applyBindings();
322 model.getValues().add(10);
323 assertEquals(model.getChangedProperty(), "values", "Something added into the array");
327 static String notAllowedRead() {
328 return "Not allowed callback: " + leakedModel.getUnrelated();
332 static String notAllowedWrite() {
333 leakedModel.setUnrelated(11);
334 return "Not allowed callback!";
337 @ComputedProperty(write="parseRepeat")
338 static List<String> repeat(int count) {
339 return Collections.nCopies(count, "Hello");
341 static void parseRepeat(Modelik m, Object v) {
342 m.setCount((Integer)v);
345 public @Test void hasPersonPropertyAndComputedFullName() {
346 List<Person> arr = model.getPeople();
347 assertEquals(arr.size(), 0, "By default empty");
350 String fullNameGenerated = p.getFullName();
351 assertNotNull(fullNameGenerated);
355 public @Test void computedListIsOfTypeString() {
356 Person p = new Person("1st", "2nd", Sex.MALE);
357 String first = p.getBothNames().get(0);
358 String last = p.getBothNames().get(1);
359 assertEquals(first, "1st");
360 assertEquals(last, "2nd");
363 @Model(className = "Inner", properties = {
364 @Property(name = "x", type = int.class)
366 static final class InnerCntrl {
367 @OnPropertyChange("x")
368 static void increment(Inner model) {
369 model.setX(model.getX() + 1);
373 private static class MockTechnology implements Technology<Object> {
374 private final List<String> mutated = new ArrayList<String>();
377 public Object wrapModel(Object model) {
382 public void valueHasMutated(Object data, String propertyName) {
383 mutated.add(propertyName);
387 public void bind(PropertyBinding b, Object model, Object data) {
391 public void expose(FunctionBinding fb, Object model, Object d) {
395 public void applyBindings(Object data) {
399 public Object wrapArray(Object[] arr) {
404 public <M> M toModel(Class<M> modelClass, Object data) {
405 return modelClass.cast(data);
409 public void runSafe(Runnable r) {
410 throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.