1 17 package org.apache.geronimo.gbean.runtime; 18 19 import junit.framework.TestCase; 20 import org.apache.geronimo.gbean.AbstractName; 21 import org.apache.geronimo.gbean.GAttributeInfo; 22 import org.apache.geronimo.gbean.GBeanData; 23 import org.apache.geronimo.gbean.InvalidConfigurationException; 24 import org.apache.geronimo.kernel.Kernel; 25 import org.apache.geronimo.kernel.KernelFactory; 26 import org.apache.geronimo.kernel.MockGBean; 27 import org.apache.geronimo.kernel.repository.Artifact; 28 29 32 public class GBeanAttributeTest extends TestCase { 33 34 private static final String attributeName = "Name"; 35 36 private static final String persistentPrimitiveAttributeName = "MutableInt"; 37 38 41 private GBeanInstance gbeanInstance = null; 42 43 private MethodInvoker getInvoker = null; 44 45 private MethodInvoker setInvoker = null; 46 47 private GAttributeInfo persistentPrimitiveAttributeInfo = null; 48 private GAttributeInfo attributeInfo = null; 49 private Kernel kernel; 50 51 public final void testGBeanAttributStringClassMethodInvokerMethodInvoker() { 52 try { 53 GBeanAttribute.createFrameworkAttribute(null, null, null, null); 54 fail("IllegalArgumentException expected"); 55 } catch (IllegalArgumentException expected) { 56 } 57 58 GBeanAttribute attribute; 59 attribute = GBeanAttribute.createFrameworkAttribute(gbeanInstance, attributeName, String .class, getInvoker); 60 assertEquals(String .class, attribute.getType()); 61 assertEquals(attributeName, attribute.getName()); 62 assertTrue(attribute.isReadable()); 63 assertFalse(attribute.isWritable()); 64 assertFalse(attribute.isPersistent()); 65 attribute = GBeanAttribute.createFrameworkAttribute(gbeanInstance, attributeName, String .class, null, setInvoker, false, null, false); 66 assertEquals(String .class, attribute.getType()); 67 assertEquals(attributeName, attribute.getName()); 68 assertFalse(attribute.isReadable()); 69 assertTrue(attribute.isWritable()); 70 assertFalse(attribute.isPersistent()); 71 attribute = GBeanAttribute.createFrameworkAttribute(gbeanInstance, attributeName, String .class, getInvoker, setInvoker, false, null, false); 72 assertEquals(String .class, attribute.getType()); 73 assertEquals(attributeName, attribute.getName()); 74 assertTrue(attribute.isReadable()); 75 assertTrue(attribute.isWritable()); 76 assertFalse(attribute.isPersistent()); 77 } 78 79 public final void testGBeanAttributeInfoClass() { 80 try { 81 new GBeanAttribute(null, null, false); 82 fail("IllegalArgumentException expected"); 83 } catch (IllegalArgumentException expected) { 84 } 85 86 try { 90 new GBeanAttribute(gbeanInstance, attributeInfo, false); 91 } catch (InvalidConfigurationException expected) { 94 } 95 96 try { 97 GAttributeInfo invalidAttributeInfo = new GAttributeInfo(attributeName, String .class.getName(), false, false, null, null); 98 99 new GBeanAttribute(gbeanInstance, invalidAttributeInfo, false); 100 fail("InvalidConfigurationException expected"); 101 } catch (InvalidConfigurationException expected) { 102 } 103 104 { 105 final GAttributeInfo attributeInfo = new GAttributeInfo(attributeName, String .class.getName(), false, false, true, false, null, null); 106 GBeanAttribute attribute = new GBeanAttribute(gbeanInstance, attributeInfo, false); 107 assertTrue(attribute.isReadable()); 108 assertFalse(attribute.isWritable()); 109 } 110 111 { 112 final GAttributeInfo attributeInfo = new GAttributeInfo(persistentPrimitiveAttributeName, int.class.getName(), false, false, false, true, null, null); 113 GBeanAttribute attribute = new GBeanAttribute(gbeanInstance, attributeInfo, false); 114 assertFalse(attribute.isReadable()); 115 assertTrue(attribute.isWritable()); 116 } 117 118 { 119 final GAttributeInfo attributeInfo = new GAttributeInfo("YetAnotherFinalInt", int.class.getName(), true, false, "getFinalInt", null); 123 GBeanAttribute attribute = new GBeanAttribute(gbeanInstance, attributeInfo, false); 124 assertNotNull(attribute); 125 } 126 127 { 128 final GAttributeInfo attributeInfo = new GAttributeInfo("YetAnotherFinalInt", int.class.getName(), true, false, null, "setCharAsYetAnotherFinalInt"); 129 try { 130 new GBeanAttribute(gbeanInstance, attributeInfo, false); 131 fail("Expected InvalidConfigurationException due to invalid setter parameter type"); 132 } catch (InvalidConfigurationException expected) { 133 } 134 } 135 136 { 137 final GAttributeInfo attributeInfo = new GAttributeInfo("YetAnotherFinalInt", int.class.getName(), true, false, null, "setBooleanAsYetAnotherFinalInt"); 138 try { 139 new GBeanAttribute(gbeanInstance, attributeInfo, false); 140 fail("Expected InvalidConfigurationException due to invalid setter parameter type"); 141 } catch (InvalidConfigurationException expected) { 142 } 143 } 144 145 { 146 final GAttributeInfo attributeInfo = new GAttributeInfo("YetAnotherFinalInt", int.class.getName(), true, false, null, "setByteAsYetAnotherFinalInt"); 147 try { 148 new GBeanAttribute(gbeanInstance, attributeInfo, false); 149 fail("Expected InvalidConfigurationException due to invalid setter parameter type"); 150 } catch (InvalidConfigurationException expected) { 151 } 152 } 153 154 { 155 final GAttributeInfo attributeInfo = new GAttributeInfo("YetAnotherFinalInt", int.class.getName(), true, false, null, "setShortAsYetAnotherFinalInt"); 156 try { 157 new GBeanAttribute(gbeanInstance, attributeInfo, false); 158 fail("Expected InvalidConfigurationException due to invalid setter parameter type"); 159 } catch (InvalidConfigurationException expected) { 160 } 161 } 162 163 { 164 final GAttributeInfo attributeInfo = new GAttributeInfo("YetAnotherFinalInt", int.class.getName(), true, false, null, "setLongAsYetAnotherFinalInt"); 165 try { 166 new GBeanAttribute(gbeanInstance, attributeInfo, false); 167 fail("Expected InvalidConfigurationException due to invalid setter parameter type"); 168 } catch (InvalidConfigurationException expected) { 169 } 170 } 171 172 { 173 final GAttributeInfo attributeInfo = new GAttributeInfo("YetAnotherFinalInt", int.class.getName(), true, false, null, "setFloatAsYetAnotherFinalInt"); 174 try { 175 new GBeanAttribute(gbeanInstance, attributeInfo, false); 176 fail("Expected InvalidConfigurationException due to invalid setter parameter type"); 177 } catch (InvalidConfigurationException expected) { 178 } 179 } 180 181 { 182 final GAttributeInfo attributeInfo = new GAttributeInfo("YetAnotherFinalInt", int.class.getName(), true, false, null, "setDoubleAsYetAnotherFinalInt"); 183 try { 184 new GBeanAttribute(gbeanInstance, attributeInfo, false); 185 fail("Expected InvalidConfigurationException due to invalid setter parameter type"); 186 } catch (InvalidConfigurationException expected) { 187 } 188 } 189 190 { 191 final GAttributeInfo attributeInfo = new GAttributeInfo("YetAnotherFinalInt", int.class.getName(), true, false, "getVoidGetterOfFinalInt", null); 192 try { 193 new GBeanAttribute(gbeanInstance, attributeInfo, false); 194 fail("Getter method not found on target; InvalidConfigurationException expected"); 195 } catch (InvalidConfigurationException expected) { 196 } 197 } 198 199 { 200 final GAttributeInfo attributeInfo = new GAttributeInfo("YetAnotherFinalInt", int.class.getName(), true, false, null, "setThatDoesntExist"); 201 try { 202 new GBeanAttribute(gbeanInstance, attributeInfo, false); 203 fail("Setter method not found on target; InvalidConfigurationException expected"); 204 } catch (InvalidConfigurationException expected) { 205 } 206 } 207 } 208 209 public final void testGetValue() throws Exception { 210 { 211 final GBeanAttribute attribute = GBeanAttribute.createFrameworkAttribute(gbeanInstance, attributeName, String .class, null, setInvoker, false, null, false); 213 try { 214 attribute.getValue(gbeanInstance); 215 fail("Only persistent attributes can be accessed while offline; exception expected"); 216 } catch (Exception expected) { 217 } 218 } 219 220 { 221 final GBeanAttribute attribute = GBeanAttribute.createFrameworkAttribute(gbeanInstance, attributeName, String .class, null, setInvoker, false, null, false); 222 223 try { 224 gbeanInstance.start(); 225 226 attribute.getValue(gbeanInstance); 227 fail("This attribute is not readable; exception expected"); 228 } catch (Throwable expected) { 229 } finally { 230 gbeanInstance.stop(); 231 } 232 } 233 } 234 235 public final void testSetValue() throws Exception { 236 237 { 239 final GBeanAttribute attribute = GBeanAttribute.createFrameworkAttribute(gbeanInstance, attributeName, String .class, null, setInvoker, false, null, false); 240 try { 241 attribute.setValue(gbeanInstance, null); 242 fail("Only persistent attributes can be modified while offline; exception expected"); 243 } catch (Exception expected) { 244 } 245 } 246 247 { 250 final GBeanAttribute persistentAttribute = new GBeanAttribute(gbeanInstance, persistentPrimitiveAttributeInfo, false); 251 try { 252 persistentAttribute.setValue(gbeanInstance, null); 253 fail("Cannot assign null to a primitive attribute; exception expected"); 254 } catch (Exception expected) { 255 } 256 } 257 258 { 260 final GBeanAttribute immutableAttribute = GBeanAttribute.createFrameworkAttribute(gbeanInstance, attributeName, String .class, getInvoker); 261 262 try { 263 gbeanInstance.start(); 264 265 immutableAttribute.setValue(gbeanInstance, null); 266 fail("This attribute is not writable; exception expected"); 267 } catch (Exception expected) { 268 } finally { 269 gbeanInstance.stop(); 270 } 271 } 272 273 { 275 final GBeanAttribute mutablePersistentAttribute = new GBeanAttribute(gbeanInstance, persistentPrimitiveAttributeInfo, false); 276 277 try { 278 gbeanInstance.start(); 279 280 mutablePersistentAttribute.setValue(gbeanInstance, null); 281 fail("Cannot assign null to a primitive attribute; exception expected"); 282 } catch (Exception expected) { 283 } finally { 284 gbeanInstance.stop(); 285 } 286 } 287 288 { 291 final GBeanAttribute mutablePersistentAttribute = new GBeanAttribute(gbeanInstance, persistentPrimitiveAttributeInfo, false); 292 293 try { 294 gbeanInstance.start(); 295 296 mutablePersistentAttribute.setValue(gbeanInstance, new Integer (4)); 297 } catch (Exception expected) { 300 } finally { 301 gbeanInstance.stop(); 302 } 303 } 304 305 { 307 final GBeanAttribute attribute = GBeanAttribute.createFrameworkAttribute(gbeanInstance, 308 attributeName, 309 int.class, 310 null, 311 setInvoker, 312 false, 313 null, 314 false); 315 316 try { 317 gbeanInstance.start(); 318 319 attribute.setValue(gbeanInstance, new Integer (4)); 320 fail("Exception expected upon setValue's call"); 321 } catch (Exception expected) { 322 } finally { 323 gbeanInstance.stop(); 324 } 325 } 326 } 327 328 protected void setUp() throws Exception { 329 super.setUp(); 330 kernel = KernelFactory.newInstance().createKernel("test"); 331 kernel.boot(); 332 333 AbstractName name = kernel.getNaming().createRootName(new Artifact("test", "foo", "1", "car"), "test", "test"); 334 gbeanInstance = new GBeanInstance(new GBeanData(name, MockGBean.getGBeanInfo()), 335 kernel, 336 kernel.getDependencyManager(), 337 new MyLifecycleBroadcaster(), 338 MockGBean.class.getClassLoader()); 339 340 getInvoker = new MethodInvoker() { 341 public Object invoke(Object target, Object [] arguments) throws Exception { 342 throw new UnsupportedOperationException ("Throws exception to rise test coverage"); 343 } 344 }; 345 346 setInvoker = new MethodInvoker() { 347 public Object invoke(Object target, Object [] arguments) throws Exception { 348 throw new UnsupportedOperationException ("Throws exception to rise test coverage"); 349 } 350 }; 351 352 attributeInfo = new GAttributeInfo(attributeName, String .class.getName(), false, false, "getName", "setName"); 353 persistentPrimitiveAttributeInfo = new GAttributeInfo(persistentPrimitiveAttributeName, int.class.getName(), true, false, "getMutableInt", "setMutableInt"); 354 } 355 356 protected void tearDown() throws Exception { 357 kernel.shutdown(); 358 gbeanInstance = null; 359 super.tearDown(); 360 } 361 362 private static class MyLifecycleBroadcaster implements LifecycleBroadcaster { 363 public void fireLoadedEvent() { 364 } 365 366 public void fireStartingEvent() { 367 } 368 369 public void fireRunningEvent() { 370 } 371 372 public void fireStoppingEvent() { 373 } 374 375 public void fireStoppedEvent() { 376 } 377 378 public void fireFailedEvent() { 379 } 380 381 public void fireUnloadedEvent() { 382 } 383 } 384 } 385 | Popular Tags |