1 /* 2 * Copyright (c) 2012, 2014, 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 26 package test.com.sun.javafx.property.adapter; 27 28 import com.sun.javafx.property.adapter.JavaBeanPropertyBuilderHelper; 29 import com.sun.javafx.property.adapter.PropertyDescriptor; 30 import org.junit.Before; 31 import org.junit.Test; 32 33 import java.lang.reflect.InvocationTargetException; 34 import java.lang.reflect.Method; 35 36 import static org.junit.Assert.assertEquals; 37 import static org.junit.Assert.fail; 38 39 /** 40 */ 41 public class JavaBeanPropertyBuilderHelperTest { 42 43 private JavaBeanPropertyBuilderHelper helperPOJOBean; 44 private JavaBeanPropertyBuilderHelper helperPOJOBeanWithNonStandardNames; 45 46 @Before 47 public void setUp() { 48 helperPOJOBean = new JavaBeanPropertyBuilderHelper(); 49 helperPOJOBean.beanClass(POJOBean.class); 50 helperPOJOBean.name("x"); 51 52 helperPOJOBeanWithNonStandardNames = new JavaBeanPropertyBuilderHelper(); 53 helperPOJOBeanWithNonStandardNames.beanClass(POJOBeanWithNonStandardNames.class); 54 helperPOJOBeanWithNonStandardNames.name("x"); 55 helperPOJOBeanWithNonStandardNames.getterName("readX"); 56 helperPOJOBeanWithNonStandardNames.setterName("writeX"); 57 } 58 59 @Test(expected = NullPointerException.class) 60 public void testSetup_WithNameIsNull() { 61 try { 62 helperPOJOBean.name(null); 63 helperPOJOBean.getDescriptor(); 64 } catch (NoSuchMethodException e) { 65 fail(); 66 } 67 } 68 69 @Test(expected = IllegalArgumentException.class) 70 public void testSetup_WithNameIsEmpty() { 71 try { 72 helperPOJOBean.name(""); 73 helperPOJOBean.getDescriptor(); 74 } catch (NoSuchMethodException e) { 75 fail(); 76 } 77 } 78 79 @Test(expected = NullPointerException.class) 80 public void testSetup_WithBeanClassIsNull() { 81 try { 82 helperPOJOBean.beanClass(null); 83 helperPOJOBean.getDescriptor(); 84 } catch (NoSuchMethodException e) { 85 fail(); 86 } 87 } 88 89 @Test(expected = NullPointerException.class) 90 public void testSetup_WithNonStandardNames_WithNameIsNull() { 91 try { 92 helperPOJOBeanWithNonStandardNames.name(null); 93 helperPOJOBeanWithNonStandardNames.getDescriptor(); 94 } catch (NoSuchMethodException e) { 95 fail(); 96 } 97 } 98 99 @Test(expected = NullPointerException.class) 100 public void testSetup_WithNonStandardNames_WithBeanClassIsNull() { 101 try { 102 helperPOJOBeanWithNonStandardNames.beanClass(null); 103 helperPOJOBeanWithNonStandardNames.getDescriptor(); 104 } catch (NoSuchMethodException e) { 105 fail(); 106 } 107 } 108 109 @Test(expected = NoSuchMethodException.class) 110 public void testSetup_WithNonStandardNames_WithGetterNameIsNull() throws NoSuchMethodException { 111 helperPOJOBeanWithNonStandardNames.getterName(null); 112 helperPOJOBeanWithNonStandardNames.getDescriptor(); 113 } 114 115 @Test(expected = NoSuchMethodException.class) 116 public void testSetup_WithNonStandardNames_WithSetterNameIsNull() throws NoSuchMethodException { 117 helperPOJOBeanWithNonStandardNames.setterName(null); 118 helperPOJOBeanWithNonStandardNames.getDescriptor(); 119 } 120 121 @Test(expected = IllegalArgumentException.class) 122 public void testSetup_WithNonStandardNames_WithNameIsEmpty() { 123 try { 124 helperPOJOBeanWithNonStandardNames.name(""); 125 helperPOJOBeanWithNonStandardNames.getDescriptor(); 126 } catch (NoSuchMethodException e) { 127 fail(); 128 } 129 } 130 131 @Test(expected = NoSuchMethodException.class) 132 public void testSetup_WithNonStandardNames_WithGetterNameIsEmpty() throws NoSuchMethodException { 133 helperPOJOBeanWithNonStandardNames.getterName(""); 134 helperPOJOBeanWithNonStandardNames.getDescriptor(); 135 } 136 137 @Test(expected = NoSuchMethodException.class) 138 public void testSetup_WithNonStandardNames_WithSetterNameIsEmpty() throws NoSuchMethodException { 139 helperPOJOBeanWithNonStandardNames.setterName(""); 140 helperPOJOBeanWithNonStandardNames.getDescriptor(); 141 } 142 143 @Test(expected = NullPointerException.class) 144 public void testSetup_WithNonStandardAccessors_WithNameIsNull() throws NoSuchMethodException { 145 helperPOJOBeanWithNonStandardNames.getterName(null); 146 helperPOJOBeanWithNonStandardNames.setterName(null); 147 try { 148 final Method getter = POJOBeanWithNonStandardNames.class.getMethod("readX"); 149 final Method setter = POJOBeanWithNonStandardNames.class.getMethod("writeX", Object.class); 150 helperPOJOBeanWithNonStandardNames.getter(getter); 151 helperPOJOBeanWithNonStandardNames.setter(setter); 152 153 helperPOJOBeanWithNonStandardNames.name(null); 154 } catch (NoSuchMethodException e) { 155 fail("Error in test code. Should not happen."); 156 } 157 helperPOJOBeanWithNonStandardNames.getDescriptor(); 158 } 159 160 @Test(expected = NoSuchMethodException.class) 161 public void testSetup_WithNonStandardAccessors_WithGetterIsNull() throws NoSuchMethodException { 162 helperPOJOBeanWithNonStandardNames.getterName(null); 163 helperPOJOBeanWithNonStandardNames.setterName(null); 164 try { 165 final Method setter = POJOBeanWithNonStandardNames.class.getMethod("writeX", Object.class); 166 helperPOJOBeanWithNonStandardNames.setter(setter); 167 168 helperPOJOBeanWithNonStandardNames.getter(null); 169 } catch (NoSuchMethodException e) { 170 fail("Error in test code. Should not happen."); 171 } 172 helperPOJOBeanWithNonStandardNames.getDescriptor(); 173 } 174 175 @Test(expected = NoSuchMethodException.class) 176 public void testSetup_WithNonStandardAccessors_WithSetterIsNull() throws NoSuchMethodException { 177 helperPOJOBeanWithNonStandardNames.getterName(null); 178 helperPOJOBeanWithNonStandardNames.setterName(null); 179 try { 180 final Method getter = POJOBeanWithNonStandardNames.class.getMethod("readX"); 181 helperPOJOBeanWithNonStandardNames.getter(getter); 182 183 helperPOJOBeanWithNonStandardNames.setter(null); 184 } catch (NoSuchMethodException e) { 185 fail("Error in test code. Should not happen."); 186 } 187 helperPOJOBeanWithNonStandardNames.getDescriptor(); 188 } 189 190 @Test(expected = IllegalArgumentException.class) 191 public void testSetup_WithNonStandardAccessors_WithNameIsEmpty() throws NoSuchMethodException { 192 helperPOJOBeanWithNonStandardNames.getterName(null); 193 helperPOJOBeanWithNonStandardNames.setterName(null); 194 try { 195 final Method getter = POJOBeanWithNonStandardNames.class.getMethod("readX"); 196 final Method setter = POJOBeanWithNonStandardNames.class.getMethod("writeX", Object.class); 197 helperPOJOBeanWithNonStandardNames.getter(getter); 198 helperPOJOBeanWithNonStandardNames.setter(setter); 199 200 helperPOJOBeanWithNonStandardNames.name(""); 201 } catch (NoSuchMethodException e) { 202 fail("Error in test code. Should not happen."); 203 } 204 helperPOJOBeanWithNonStandardNames.getDescriptor(); 205 } 206 207 @Test 208 public void testReusabilityWhenChangeOfBeanClass() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { 209 Object x = new Object(); 210 211 PropertyDescriptor descriptor = helperPOJOBean.getDescriptor(); 212 assertEquals(x, descriptor.getGetter().invoke(new POJOBean(x))); 213 descriptor.getSetter().invoke(new POJOBean(x), new Object()); 214 215 helperPOJOBean.beanClass(POJOBean2.class); 216 217 descriptor = helperPOJOBean.getDescriptor(); 218 assertEquals(x, descriptor.getGetter().invoke(new POJOBean2(x))); 219 descriptor.getSetter().invoke(new POJOBean2(x), new Object()); 220 } 221 222 public static class POJOBean { 223 private Object x; 224 225 public POJOBean(Object x) {this.x = x;} 226 227 public Object getX() {return x;} 228 public void setX(Object x) {this.x = x;} 229 } 230 231 public static class POJOBean2 { 232 private Object x; 233 234 public POJOBean2(Object x) {this.x = x;} 235 236 public Object getX() {return x;} 237 public void setX(Object x) {this.x = x;} 238 239 } 240 241 public static class POJOBeanWithNonStandardNames { 242 private Object x; 243 244 public POJOBeanWithNonStandardNames(Object x) {this.x = x;} 245 246 public Object readX() {return x;} 247 public void writeX(Object x) {this.x = x;} 248 } 249 250 }