1 /*
2 * Copyright (c) 2010, 2015, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
25 package test.com.sun.javafx.binding;
26
27 import com.sun.javafx.binding.Logging;
28 import java.beans.PropertyChangeListener;
29 import java.beans.PropertyChangeSupport;
30 import java.util.ArrayList;
31 import java.util.Arrays;
32 import java.util.List;
33 import java.util.Random;
34 import test.javafx.beans.Person;
35 import javafx.beans.binding.Bindings;
36 import javafx.beans.binding.BooleanBinding;
37 import javafx.beans.binding.DoubleBinding;
38 import javafx.beans.binding.FloatBinding;
39 import javafx.beans.binding.IntegerBinding;
40 import javafx.beans.binding.LongBinding;
41 import javafx.beans.binding.ObjectBinding;
42 import javafx.beans.binding.StringBinding;
43 import test.javafx.binding.Variable;
44 import javafx.collections.ObservableList;
45 import sun.util.logging.PlatformLogger.Level;
46 import org.junit.AfterClass;
47 import org.junit.Before;
48 import org.junit.BeforeClass;
49 import org.junit.Test;
50
51 import static org.junit.Assert.*;
52
53 public class SelectBindingTest {
54
55 private static final double EPSILON_DOUBLE = 1e-12;
56 private static final float EPSILON_FLOAT = 1e-6f;
57
58 public static class POJOPerson {
59
60 private String name;
61
62 public String getName() {
63 return name;
64 }
65
91 pcs.addPropertyChangeListener(property, pcl);
92 }
93
94 public void removePropertyChangeListener(String property, PropertyChangeListener pcl) {
95 pcs.removePropertyChangeListener(property, pcl);
96 }
97
98 }
99
100 private Variable a;
101 private Variable b;
102 private Variable c;
103 private Variable d;
104 private StringBinding select;
105 private ObservableList<?> dependencies;
106
107 private static final ErrorLoggingUtiltity log = new ErrorLoggingUtiltity();
108
109 @BeforeClass
110 public static void setUpClass() {
111 log.start();
112 }
113
114 @AfterClass
115 public static void tearDownClass() {
116 log.stop();
117 }
118
119 @Before
120 public void setUp() throws Exception {
121 a = new Variable("a");
122 b = new Variable("b");
123 c = new Variable("c");
124 d = new Variable("d");
125 a.setNext(b);
126 b.setNext(c);
127 select = Bindings.selectString(a.nextProperty(), "next", "name");
128 dependencies = select.getDependencies();
129 }
130
540
541 assertEquals(Arrays.asList(a.nextProperty()), dependencies);
542 }
543
544 /**
545 * This test performs 10,000 random operations on the chain of a.b.c
546 * (setting different values for each step, sometimes doing multiple
547 * assignments between get() calls, random invalidate() calls). After each
548 * random iteration, we check to see if the listeners installed on the a, b,
549 * c objects are still correct. The goal is to catch any freak situations
550 * where we might have installed two listeners on the same property, or
551 * failed to remove a listener for a property under some circumstance.
552 *
553 * Do note that the only listeners that are installed in this method are
554 * done by the select binding.
555 */
556 @SuppressWarnings("unchecked")
557 @Test
558 public void stressTestRandomOperationsResultInCorrectListenersInstalled() {
559
560 final Level logLevel = Logging.getLogger().level();
561 Logging.getLogger().setLevel(Level.SEVERE);
562 List<String> steps = new ArrayList<String>();
563
564 Random rand = new Random(System.currentTimeMillis());
565 for (int i = 0; i < 10000; i++) {
566 switch (rand.nextInt(20)) {
567 case 0:
568 a.setNext(null);
569 steps.add("Assign a.value to null");
570 break;
571 case 1:
572 a.setNext(b);
573 steps.add("Assign a.value to b");
574 break;
575 case 2:
576 b.setNext(null);
577 steps.add("Assign b.value to null");
578 break;
579 case 3:
580 b.setNext(c);
581 steps.add("Assign b.value to c");
627 assertEquals(expected, c.numChangedListenersForName);
628
629 assertEquals(0, d.numChangedListenersForName);
630 assertEquals(0, d.numChangedListenersForNext);
631
632 switch (depsCount) {
633 case 0:
634 case 1:
635 assertEquals(Arrays.asList(a.nextProperty()), dependencies);
636 break;
637 case 2:
638 assertEquals(Arrays.asList(a.nextProperty(), b.nextProperty()), dependencies);
639 break;
640 case 3:
641 assertEquals(Arrays.asList(a.nextProperty(), b.nextProperty(), c.nameProperty()), dependencies);
642 break;
643 default:
644 fail("Should not reach here");
645 }
646 }
647 Logging.getLogger().setLevel(logLevel);
648 }
649
650 private void printSteps(int iteration, List<String> steps) {
651 System.err.println("Failed on iteration " + iteration + " for the following observableArrayList of changes");
652 for (String s : steps) {
653 System.err.println("\t" + s);
654 }
655 }
656 }
|
1 /*
2 * Copyright (c) 2010, 2018, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
25 package test.com.sun.javafx.binding;
26
27 import com.sun.javafx.binding.Logging;
28 import java.beans.PropertyChangeListener;
29 import java.beans.PropertyChangeSupport;
30 import java.util.ArrayList;
31 import java.util.Arrays;
32 import java.util.List;
33 import java.util.Random;
34 import test.javafx.beans.Person;
35 import javafx.beans.binding.Bindings;
36 import javafx.beans.binding.BooleanBinding;
37 import javafx.beans.binding.DoubleBinding;
38 import javafx.beans.binding.FloatBinding;
39 import javafx.beans.binding.IntegerBinding;
40 import javafx.beans.binding.LongBinding;
41 import javafx.beans.binding.ObjectBinding;
42 import javafx.beans.binding.StringBinding;
43 import test.javafx.binding.Variable;
44 import javafx.collections.ObservableList;
45 import com.sun.javafx.logging.PlatformLogger.Level;
46 import org.junit.AfterClass;
47 import org.junit.Before;
48 import org.junit.BeforeClass;
49 import org.junit.Test;
50
51 import static org.junit.Assert.*;
52
53 public class SelectBindingTest {
54
55 private static final double EPSILON_DOUBLE = 1e-12;
56 private static final float EPSILON_FLOAT = 1e-6f;
57
58 public static class POJOPerson {
59
60 private String name;
61
62 public String getName() {
63 return name;
64 }
65
91 pcs.addPropertyChangeListener(property, pcl);
92 }
93
94 public void removePropertyChangeListener(String property, PropertyChangeListener pcl) {
95 pcs.removePropertyChangeListener(property, pcl);
96 }
97
98 }
99
100 private Variable a;
101 private Variable b;
102 private Variable c;
103 private Variable d;
104 private StringBinding select;
105 private ObservableList<?> dependencies;
106
107 private static final ErrorLoggingUtiltity log = new ErrorLoggingUtiltity();
108
109 @BeforeClass
110 public static void setUpClass() {
111 System.err.println("SelectBindingTest : log messages are expected from these tests.");
112 log.start();
113 }
114
115 @AfterClass
116 public static void tearDownClass() {
117 log.stop();
118 }
119
120 @Before
121 public void setUp() throws Exception {
122 a = new Variable("a");
123 b = new Variable("b");
124 c = new Variable("c");
125 d = new Variable("d");
126 a.setNext(b);
127 b.setNext(c);
128 select = Bindings.selectString(a.nextProperty(), "next", "name");
129 dependencies = select.getDependencies();
130 }
131
541
542 assertEquals(Arrays.asList(a.nextProperty()), dependencies);
543 }
544
545 /**
546 * This test performs 10,000 random operations on the chain of a.b.c
547 * (setting different values for each step, sometimes doing multiple
548 * assignments between get() calls, random invalidate() calls). After each
549 * random iteration, we check to see if the listeners installed on the a, b,
550 * c objects are still correct. The goal is to catch any freak situations
551 * where we might have installed two listeners on the same property, or
552 * failed to remove a listener for a property under some circumstance.
553 *
554 * Do note that the only listeners that are installed in this method are
555 * done by the select binding.
556 */
557 @SuppressWarnings("unchecked")
558 @Test
559 public void stressTestRandomOperationsResultInCorrectListenersInstalled() {
560
561 List<String> steps = new ArrayList<String>();
562
563 Random rand = new Random(System.currentTimeMillis());
564 for (int i = 0; i < 10000; i++) {
565 switch (rand.nextInt(20)) {
566 case 0:
567 a.setNext(null);
568 steps.add("Assign a.value to null");
569 break;
570 case 1:
571 a.setNext(b);
572 steps.add("Assign a.value to b");
573 break;
574 case 2:
575 b.setNext(null);
576 steps.add("Assign b.value to null");
577 break;
578 case 3:
579 b.setNext(c);
580 steps.add("Assign b.value to c");
626 assertEquals(expected, c.numChangedListenersForName);
627
628 assertEquals(0, d.numChangedListenersForName);
629 assertEquals(0, d.numChangedListenersForNext);
630
631 switch (depsCount) {
632 case 0:
633 case 1:
634 assertEquals(Arrays.asList(a.nextProperty()), dependencies);
635 break;
636 case 2:
637 assertEquals(Arrays.asList(a.nextProperty(), b.nextProperty()), dependencies);
638 break;
639 case 3:
640 assertEquals(Arrays.asList(a.nextProperty(), b.nextProperty(), c.nameProperty()), dependencies);
641 break;
642 default:
643 fail("Should not reach here");
644 }
645 }
646 }
647
648 private void printSteps(int iteration, List<String> steps) {
649 System.err.println("Failed on iteration " + iteration + " for the following observableArrayList of changes");
650 for (String s : steps) {
651 System.err.println("\t" + s);
652 }
653 }
654 }
|