001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.geronimo.gbean.runtime;
017:
018: import junit.framework.TestCase;
019: import org.apache.geronimo.gbean.AbstractName;
020: import org.apache.geronimo.gbean.GAttributeInfo;
021: import org.apache.geronimo.gbean.GBeanData;
022: import org.apache.geronimo.gbean.InvalidConfigurationException;
023: import org.apache.geronimo.kernel.Kernel;
024: import org.apache.geronimo.kernel.KernelFactory;
025: import org.apache.geronimo.kernel.MockGBean;
026: import org.apache.geronimo.kernel.repository.Artifact;
027:
028: /**
029: * @version $Rev: 476049 $ $Date: 2006-11-16 20:35:17 -0800 (Thu, 16 Nov 2006) $
030: */
031: public class GBeanAttributeTest extends TestCase {
032:
033: private static final String attributeName = "Name";
034:
035: private static final String persistentPrimitiveAttributeName = "MutableInt";
036:
037: /**
038: * Wraps GBean
039: */
040: private GBeanInstance gbeanInstance = null;
041:
042: private MethodInvoker getInvoker = null;
043:
044: private MethodInvoker setInvoker = null;
045:
046: private GAttributeInfo persistentPrimitiveAttributeInfo = null;
047: private GAttributeInfo attributeInfo = null;
048: private Kernel kernel;
049:
050: public final void testGBeanAttributStringClassMethodInvokerMethodInvoker() {
051: try {
052: GBeanAttribute.createFrameworkAttribute(null, null, null,
053: null);
054: fail("IllegalArgumentException expected");
055: } catch (IllegalArgumentException expected) {
056: }
057:
058: GBeanAttribute attribute;
059: attribute = GBeanAttribute.createFrameworkAttribute(
060: gbeanInstance, attributeName, String.class, getInvoker);
061: assertEquals(String.class, attribute.getType());
062: assertEquals(attributeName, attribute.getName());
063: assertTrue(attribute.isReadable());
064: assertFalse(attribute.isWritable());
065: assertFalse(attribute.isPersistent());
066: attribute = GBeanAttribute.createFrameworkAttribute(
067: gbeanInstance, attributeName, String.class, null,
068: setInvoker, false, null, false);
069: assertEquals(String.class, attribute.getType());
070: assertEquals(attributeName, attribute.getName());
071: assertFalse(attribute.isReadable());
072: assertTrue(attribute.isWritable());
073: assertFalse(attribute.isPersistent());
074: attribute = GBeanAttribute.createFrameworkAttribute(
075: gbeanInstance, attributeName, String.class, getInvoker,
076: setInvoker, false, null, false);
077: assertEquals(String.class, attribute.getType());
078: assertEquals(attributeName, attribute.getName());
079: assertTrue(attribute.isReadable());
080: assertTrue(attribute.isWritable());
081: assertFalse(attribute.isPersistent());
082: }
083:
084: public final void testGBeanAttributeInfoClass() {
085: try {
086: new GBeanAttribute(null, null, false);
087: fail("IllegalArgumentException expected");
088: } catch (IllegalArgumentException expected) {
089: }
090:
091: // 2. @todo BUG An attribute must be readable, writable, or persistent
092: // GBeanAttribute ctor doesn't check if readable/writable are
093: // null's
094: try {
095: new GBeanAttribute(gbeanInstance, attributeInfo, false);
096: // till Dain sorts out the question of ctor
097: // fail("InvalidConfigurationException expected");
098: } catch (InvalidConfigurationException expected) {
099: }
100:
101: try {
102: GAttributeInfo invalidAttributeInfo = new GAttributeInfo(
103: attributeName, String.class.getName(), false,
104: false, null, null);
105:
106: new GBeanAttribute(gbeanInstance, invalidAttributeInfo,
107: false);
108: fail("InvalidConfigurationException expected");
109: } catch (InvalidConfigurationException expected) {
110: }
111:
112: {
113: final GAttributeInfo attributeInfo = new GAttributeInfo(
114: attributeName, String.class.getName(), false,
115: false, true, false, null, null);
116: GBeanAttribute attribute = new GBeanAttribute(
117: gbeanInstance, attributeInfo, false);
118: assertTrue(attribute.isReadable());
119: assertFalse(attribute.isWritable());
120: }
121:
122: {
123: final GAttributeInfo attributeInfo = new GAttributeInfo(
124: persistentPrimitiveAttributeName, int.class
125: .getName(), false, false, false, true,
126: null, null);
127: GBeanAttribute attribute = new GBeanAttribute(
128: gbeanInstance, attributeInfo, false);
129: assertFalse(attribute.isReadable());
130: assertTrue(attribute.isWritable());
131: }
132:
133: {
134: // the attribute name and getter name are different, yet both
135: // exist.
136: // getYetAnotherFinalInt doesn't exist
137: final GAttributeInfo attributeInfo = new GAttributeInfo(
138: "YetAnotherFinalInt", int.class.getName(), true,
139: false, "getFinalInt", null);
140: GBeanAttribute attribute = new GBeanAttribute(
141: gbeanInstance, attributeInfo, false);
142: assertNotNull(attribute);
143: }
144:
145: {
146: final GAttributeInfo attributeInfo = new GAttributeInfo(
147: "YetAnotherFinalInt", int.class.getName(), true,
148: false, null, "setCharAsYetAnotherFinalInt");
149: try {
150: new GBeanAttribute(gbeanInstance, attributeInfo, false);
151: fail("Expected InvalidConfigurationException due to invalid setter parameter type");
152: } catch (InvalidConfigurationException expected) {
153: }
154: }
155:
156: {
157: final GAttributeInfo attributeInfo = new GAttributeInfo(
158: "YetAnotherFinalInt", int.class.getName(), true,
159: false, null, "setBooleanAsYetAnotherFinalInt");
160: try {
161: new GBeanAttribute(gbeanInstance, attributeInfo, false);
162: fail("Expected InvalidConfigurationException due to invalid setter parameter type");
163: } catch (InvalidConfigurationException expected) {
164: }
165: }
166:
167: {
168: final GAttributeInfo attributeInfo = new GAttributeInfo(
169: "YetAnotherFinalInt", int.class.getName(), true,
170: false, null, "setByteAsYetAnotherFinalInt");
171: try {
172: new GBeanAttribute(gbeanInstance, attributeInfo, false);
173: fail("Expected InvalidConfigurationException due to invalid setter parameter type");
174: } catch (InvalidConfigurationException expected) {
175: }
176: }
177:
178: {
179: final GAttributeInfo attributeInfo = new GAttributeInfo(
180: "YetAnotherFinalInt", int.class.getName(), true,
181: false, null, "setShortAsYetAnotherFinalInt");
182: try {
183: new GBeanAttribute(gbeanInstance, attributeInfo, false);
184: fail("Expected InvalidConfigurationException due to invalid setter parameter type");
185: } catch (InvalidConfigurationException expected) {
186: }
187: }
188:
189: {
190: final GAttributeInfo attributeInfo = new GAttributeInfo(
191: "YetAnotherFinalInt", int.class.getName(), true,
192: false, null, "setLongAsYetAnotherFinalInt");
193: try {
194: new GBeanAttribute(gbeanInstance, attributeInfo, false);
195: fail("Expected InvalidConfigurationException due to invalid setter parameter type");
196: } catch (InvalidConfigurationException expected) {
197: }
198: }
199:
200: {
201: final GAttributeInfo attributeInfo = new GAttributeInfo(
202: "YetAnotherFinalInt", int.class.getName(), true,
203: false, null, "setFloatAsYetAnotherFinalInt");
204: try {
205: new GBeanAttribute(gbeanInstance, attributeInfo, false);
206: fail("Expected InvalidConfigurationException due to invalid setter parameter type");
207: } catch (InvalidConfigurationException expected) {
208: }
209: }
210:
211: {
212: final GAttributeInfo attributeInfo = new GAttributeInfo(
213: "YetAnotherFinalInt", int.class.getName(), true,
214: false, null, "setDoubleAsYetAnotherFinalInt");
215: try {
216: new GBeanAttribute(gbeanInstance, attributeInfo, false);
217: fail("Expected InvalidConfigurationException due to invalid setter parameter type");
218: } catch (InvalidConfigurationException expected) {
219: }
220: }
221:
222: {
223: final GAttributeInfo attributeInfo = new GAttributeInfo(
224: "YetAnotherFinalInt", int.class.getName(), true,
225: false, "getVoidGetterOfFinalInt", null);
226: try {
227: new GBeanAttribute(gbeanInstance, attributeInfo, false);
228: fail("Getter method not found on target; InvalidConfigurationException expected");
229: } catch (InvalidConfigurationException expected) {
230: }
231: }
232:
233: {
234: final GAttributeInfo attributeInfo = new GAttributeInfo(
235: "YetAnotherFinalInt", int.class.getName(), true,
236: false, null, "setThatDoesntExist");
237: try {
238: new GBeanAttribute(gbeanInstance, attributeInfo, false);
239: fail("Setter method not found on target; InvalidConfigurationException expected");
240: } catch (InvalidConfigurationException expected) {
241: }
242: }
243: }
244:
245: public final void testGetValue() throws Exception {
246: {
247: // attribute that isn't readable and persistent
248: final GBeanAttribute attribute = GBeanAttribute
249: .createFrameworkAttribute(gbeanInstance,
250: attributeName, String.class, null,
251: setInvoker, false, null, false);
252: try {
253: attribute.getValue(gbeanInstance);
254: fail("Only persistent attributes can be accessed while offline; exception expected");
255: } catch (/* IllegalState */Exception expected) {
256: }
257: }
258:
259: {
260: final GBeanAttribute attribute = GBeanAttribute
261: .createFrameworkAttribute(gbeanInstance,
262: attributeName, String.class, null,
263: setInvoker, false, null, false);
264:
265: try {
266: gbeanInstance.start();
267:
268: attribute.getValue(gbeanInstance);
269: fail("This attribute is not readable; exception expected");
270: } catch (/* IllegalArgument */Throwable expected) {
271: } finally {
272: gbeanInstance.stop();
273: }
274: }
275: }
276:
277: public final void testSetValue() throws Exception {
278:
279: // 1. (offline) attribute that isn't readable and persistent
280: {
281: final GBeanAttribute attribute = GBeanAttribute
282: .createFrameworkAttribute(gbeanInstance,
283: attributeName, String.class, null,
284: setInvoker, false, null, false);
285: try {
286: attribute.setValue(gbeanInstance, null);
287: fail("Only persistent attributes can be modified while offline; exception expected");
288: } catch (/* IllegalState */Exception expected) {
289: }
290: }
291:
292: // 2. (offline) attribute that is of primitive type, writable and
293: // persistent, but not readable
294: {
295: final GBeanAttribute persistentAttribute = new GBeanAttribute(
296: gbeanInstance, persistentPrimitiveAttributeInfo,
297: false);
298: try {
299: persistentAttribute.setValue(gbeanInstance, null);
300: fail("Cannot assign null to a primitive attribute; exception expected");
301: } catch (/* IllegalArgument */Exception expected) {
302: }
303: }
304:
305: // 3. (online) attribute that is immutable and not persistent
306: {
307: final GBeanAttribute immutableAttribute = GBeanAttribute
308: .createFrameworkAttribute(gbeanInstance,
309: attributeName, String.class, getInvoker);
310:
311: try {
312: gbeanInstance.start();
313:
314: immutableAttribute.setValue(gbeanInstance, null);
315: fail("This attribute is not writable; exception expected");
316: } catch (/* IllegalArgument */Exception expected) {
317: } finally {
318: gbeanInstance.stop();
319: }
320: }
321:
322: // 4. (online) attribute that is mutable and of primitive type
323: {
324: final GBeanAttribute mutablePersistentAttribute = new GBeanAttribute(
325: gbeanInstance, persistentPrimitiveAttributeInfo,
326: false);
327:
328: try {
329: gbeanInstance.start();
330:
331: mutablePersistentAttribute
332: .setValue(gbeanInstance, null);
333: fail("Cannot assign null to a primitive attribute; exception expected");
334: } catch (/* IllegalArgument */Exception expected) {
335: } finally {
336: gbeanInstance.stop();
337: }
338: }
339:
340: // 4a. @todo BUG: It's possible to set a value to a persistent
341: // attribute while online; IllegalStateException expected
342: {
343: final GBeanAttribute mutablePersistentAttribute = new GBeanAttribute(
344: gbeanInstance, persistentPrimitiveAttributeInfo,
345: false);
346:
347: try {
348: gbeanInstance.start();
349:
350: mutablePersistentAttribute.setValue(gbeanInstance,
351: new Integer(4));
352: //fail("Cannot assign a value to a persistent attribute while
353: // online; exception expected");
354: } catch (/* IllegalState */Exception expected) {
355: } finally {
356: gbeanInstance.stop();
357: }
358: }
359:
360: // 5. Invoke setValue so that exception is thrown
361: {
362: final GBeanAttribute attribute = GBeanAttribute
363: .createFrameworkAttribute(gbeanInstance,
364: attributeName, int.class, null, setInvoker,
365: false, null, false);
366:
367: try {
368: gbeanInstance.start();
369:
370: attribute.setValue(gbeanInstance, new Integer(4));
371: fail("Exception expected upon setValue's call");
372: } catch (/* IllegalState */Exception expected) {
373: } finally {
374: gbeanInstance.stop();
375: }
376: }
377: }
378:
379: protected void setUp() throws Exception {
380: super .setUp();
381: kernel = KernelFactory.newInstance().createKernel("test");
382: kernel.boot();
383:
384: AbstractName name = kernel.getNaming()
385: .createRootName(
386: new Artifact("test", "foo", "1", "car"),
387: "test", "test");
388: gbeanInstance = new GBeanInstance(new GBeanData(name, MockGBean
389: .getGBeanInfo()), kernel,
390: kernel.getDependencyManager(),
391: new MyLifecycleBroadcaster(), MockGBean.class
392: .getClassLoader());
393:
394: getInvoker = new MethodInvoker() {
395: public Object invoke(Object target, Object[] arguments)
396: throws Exception {
397: throw new UnsupportedOperationException(
398: "Throws exception to rise test coverage");
399: }
400: };
401:
402: setInvoker = new MethodInvoker() {
403: public Object invoke(Object target, Object[] arguments)
404: throws Exception {
405: throw new UnsupportedOperationException(
406: "Throws exception to rise test coverage");
407: }
408: };
409:
410: attributeInfo = new GAttributeInfo(attributeName, String.class
411: .getName(), false, false, "getName", "setName");
412: persistentPrimitiveAttributeInfo = new GAttributeInfo(
413: persistentPrimitiveAttributeName, int.class.getName(),
414: true, false, "getMutableInt", "setMutableInt");
415: }
416:
417: protected void tearDown() throws Exception {
418: kernel.shutdown();
419: gbeanInstance = null;
420: super .tearDown();
421: }
422:
423: private static class MyLifecycleBroadcaster implements
424: LifecycleBroadcaster {
425: public void fireLoadedEvent() {
426: }
427:
428: public void fireStartingEvent() {
429: }
430:
431: public void fireRunningEvent() {
432: }
433:
434: public void fireStoppingEvent() {
435: }
436:
437: public void fireStoppedEvent() {
438: }
439:
440: public void fireFailedEvent() {
441: }
442:
443: public void fireUnloadedEvent() {
444: }
445: }
446: }
|