|
| 1 | +/* |
| 2 | + * Copyright (c) 2023, 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 |
| 23 | + * questions. |
| 24 | + */ |
| 25 | +package test.javafx.scene.control; |
| 26 | + |
| 27 | +import java.lang.reflect.Method; |
| 28 | +import java.lang.reflect.Modifier; |
| 29 | +import java.util.Set; |
| 30 | +import javafx.scene.control.Accordion; |
| 31 | +import javafx.scene.control.Button; |
| 32 | +import javafx.scene.control.ButtonBar; |
| 33 | +import javafx.scene.control.ButtonBase; |
| 34 | +import javafx.scene.control.Cell; |
| 35 | +import javafx.scene.control.CheckBox; |
| 36 | +import javafx.scene.control.ChoiceBox; |
| 37 | +import javafx.scene.control.ColorPicker; |
| 38 | +import javafx.scene.control.ComboBox; |
| 39 | +import javafx.scene.control.ComboBoxBase; |
| 40 | +import javafx.scene.control.DateCell; |
| 41 | +import javafx.scene.control.DatePicker; |
| 42 | +import javafx.scene.control.Hyperlink; |
| 43 | +import javafx.scene.control.IndexedCell; |
| 44 | +import javafx.scene.control.Label; |
| 45 | +import javafx.scene.control.Labeled; |
| 46 | +import javafx.scene.control.ListCell; |
| 47 | +import javafx.scene.control.ListView; |
| 48 | +import javafx.scene.control.MenuBar; |
| 49 | +import javafx.scene.control.MenuButton; |
| 50 | +import javafx.scene.control.Pagination; |
| 51 | +import javafx.scene.control.PasswordField; |
| 52 | +import javafx.scene.control.ProgressIndicator; |
| 53 | +import javafx.scene.control.RadioButton; |
| 54 | +import javafx.scene.control.ScrollBar; |
| 55 | +import javafx.scene.control.ScrollPane; |
| 56 | +import javafx.scene.control.Separator; |
| 57 | +import javafx.scene.control.SeparatorMenuItem; |
| 58 | +import javafx.scene.control.Slider; |
| 59 | +import javafx.scene.control.Spinner; |
| 60 | +import javafx.scene.control.SplitPane; |
| 61 | +import javafx.scene.control.TabPane; |
| 62 | +import javafx.scene.control.TableRow; |
| 63 | +import javafx.scene.control.TableView; |
| 64 | +import javafx.scene.control.TextArea; |
| 65 | +import javafx.scene.control.TextField; |
| 66 | +import javafx.scene.control.TitledPane; |
| 67 | +import javafx.scene.control.ToggleButton; |
| 68 | +import javafx.scene.control.TreeCell; |
| 69 | +import javafx.scene.control.TreeTableCell; |
| 70 | +import javafx.scene.control.TreeTableRow; |
| 71 | +import javafx.scene.control.TreeTableView; |
| 72 | +import javafx.scene.control.cell.CheckBoxListCell; |
| 73 | +import javafx.scene.control.cell.CheckBoxTableCell; |
| 74 | +import javafx.scene.control.cell.CheckBoxTreeCell; |
| 75 | +import javafx.scene.control.cell.CheckBoxTreeTableCell; |
| 76 | +import javafx.scene.control.cell.ChoiceBoxTreeCell; |
| 77 | +import javafx.scene.control.cell.ChoiceBoxTreeTableCell; |
| 78 | +import javafx.scene.control.cell.ComboBoxTreeCell; |
| 79 | +import javafx.scene.control.cell.ComboBoxTreeTableCell; |
| 80 | +import javafx.scene.control.cell.ProgressBarTreeTableCell; |
| 81 | +import javafx.scene.control.cell.TextFieldTreeCell; |
| 82 | +import javafx.scene.control.cell.TextFieldTreeTableCell; |
| 83 | +import org.junit.jupiter.api.Test; |
| 84 | +import com.sun.javafx.scene.control.DoubleField; |
| 85 | +import com.sun.javafx.scene.control.InputField; |
| 86 | +import com.sun.javafx.scene.control.IntegerField; |
| 87 | +import com.sun.javafx.scene.control.WebColorField; |
| 88 | +import com.sun.javafx.scene.control.skin.FXVK; |
| 89 | + |
| 90 | +/** |
| 91 | + * Tests contract for properties and their accessors and mutators |
| 92 | + * in the Control type hierarchy. |
| 93 | + * |
| 94 | + * Currently uses a list of classes, so any new Controls must be added manually. |
| 95 | + * Perhaps the test should scan modulepath and find all the Controls automagically. |
| 96 | + */ |
| 97 | +public class ControlPropertiesTest { |
| 98 | + /** |
| 99 | + * controls whether the test fails with assertion (true, default) or |
| 100 | + * outputs all violations to stderr (false). |
| 101 | + */ |
| 102 | + private static final boolean FAIL_FAST = true; |
| 103 | + |
| 104 | + // list all current descendants of Control class. |
| 105 | + private Set<Class<?>> allControlClasses() { |
| 106 | + return Set.of( |
| 107 | + Accordion.class, |
| 108 | + ButtonBar.class, |
| 109 | + ButtonBase.class, |
| 110 | + Button.class, |
| 111 | + Cell.class, |
| 112 | + CheckBox.class, |
| 113 | + CheckBoxListCell.class, |
| 114 | + CheckBoxTableCell.class, |
| 115 | + CheckBoxTreeCell.class, |
| 116 | + CheckBoxTreeTableCell.class, |
| 117 | + ChoiceBox.class, |
| 118 | + ChoiceBoxTreeCell.class, |
| 119 | + ChoiceBoxTreeTableCell.class, |
| 120 | + ColorPicker.class, |
| 121 | + ComboBox.class, |
| 122 | + ComboBoxBase.class, |
| 123 | + ComboBoxTreeCell.class, |
| 124 | + ComboBoxTreeTableCell.class, |
| 125 | + DateCell.class, |
| 126 | + DatePicker.class, |
| 127 | + DoubleField.class, |
| 128 | + FXVK.class, |
| 129 | + //HTMLEditor.class, |
| 130 | + Hyperlink.class, |
| 131 | + IndexedCell.class, |
| 132 | + InputField.class, |
| 133 | + IntegerField.class, |
| 134 | + Labeled.class, |
| 135 | + Label.class, |
| 136 | + ListCell.class, |
| 137 | + ListView.class, |
| 138 | + MenuBar.class, |
| 139 | + MenuButton.class, |
| 140 | + Pagination.class, |
| 141 | + PasswordField.class, |
| 142 | + ProgressIndicator.class, |
| 143 | + ProgressBarTreeTableCell.class, |
| 144 | + RadioButton.class, |
| 145 | + ScrollBar.class, |
| 146 | + ScrollPane.class, |
| 147 | + Separator.class, |
| 148 | + SeparatorMenuItem.class, |
| 149 | + Slider.class, |
| 150 | + Spinner.class, |
| 151 | + SplitPane.class, |
| 152 | + TableRow.class, |
| 153 | + TableView.class, |
| 154 | + TabPane.class, |
| 155 | + TextArea.class, |
| 156 | + TextField.class, |
| 157 | + TextFieldTreeCell.class, |
| 158 | + TextFieldTreeTableCell.class, |
| 159 | + TitledPane.class, |
| 160 | + ToggleButton.class, |
| 161 | + TreeCell.class, |
| 162 | + TreeTableCell.class, |
| 163 | + TreeTableRow.class, |
| 164 | + TreeTableView.class, |
| 165 | + WebColorField.class |
| 166 | + ); |
| 167 | + } |
| 168 | + |
| 169 | + /** |
| 170 | + * Tests for missing final keyword in Control properties and their accessors/mutators. |
| 171 | + */ |
| 172 | + @Test |
| 173 | + public void testMissingFinalMethods() { |
| 174 | + for (Class<?> c : allControlClasses()) { |
| 175 | + check(c); |
| 176 | + } |
| 177 | + } |
| 178 | + |
| 179 | + private void check(Class<?> cls) { |
| 180 | + Method[] publicMethods = cls.getMethods(); |
| 181 | + for (Method m : publicMethods) { |
| 182 | + String name = m.getName(); |
| 183 | + if (name.endsWith("Property") && (m.getParameterCount() == 0)) { |
| 184 | + checkModifiers(m); |
| 185 | + |
| 186 | + String propName = name.substring(0, name.length() - "Property".length()); |
| 187 | + check(publicMethods, propName, "get", 0); |
| 188 | + check(publicMethods, propName, "set", 1); |
| 189 | + check(publicMethods, propName, "is", 0); |
| 190 | + } |
| 191 | + } |
| 192 | + } |
| 193 | + |
| 194 | + private void check(Method[] methods, String propName, String prefix, int numArgs) { |
| 195 | + StringBuilder sb = new StringBuilder(64); |
| 196 | + sb.append(prefix); |
| 197 | + sb.append(Character.toUpperCase(propName.charAt(0))); |
| 198 | + sb.append(propName, 1, propName.length()); |
| 199 | + |
| 200 | + String name = sb.toString(); |
| 201 | + for (Method m : methods) { |
| 202 | + if (m.getParameterCount() == numArgs) { |
| 203 | + if (name.equals(m.getName())) { |
| 204 | + checkModifiers(m); |
| 205 | + return; |
| 206 | + } |
| 207 | + } |
| 208 | + } |
| 209 | + } |
| 210 | + |
| 211 | + private void checkModifiers(Method m) { |
| 212 | + int mod = m.getModifiers(); |
| 213 | + if (Modifier.isPublic(mod) && !Modifier.isFinal(mod)) { |
| 214 | + String msg = m + " is not final."; |
| 215 | + if (FAIL_FAST) { |
| 216 | + throw new AssertionError(msg); |
| 217 | + } else { |
| 218 | + System.err.println(msg); |
| 219 | + } |
| 220 | + } |
| 221 | + } |
| 222 | +} |
0 commit comments