001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package test.compliance.notcompliant;
023:
024: import javax.management.MBeanInfo;
025: import javax.management.MBeanServer;
026: import javax.management.MBeanServerFactory;
027: import javax.management.NotCompliantMBeanException;
028: import javax.management.ObjectName;
029:
030: import junit.framework.TestCase;
031: import test.compliance.notcompliant.support.DynamicAndStandard;
032: import test.compliance.notcompliant.support.InterfaceProblems;
033: import test.compliance.notcompliant.support.NullDynamic;
034: import test.compliance.notcompliant.support.OverloadedAttribute1;
035: import test.compliance.notcompliant.support.OverloadedAttribute2;
036: import test.compliance.notcompliant.support.OverloadedAttribute3;
037: import test.compliance.notcompliant.support.OverloadedAttribute4;
038: import test.compliance.notcompliant.support.OverloadedAttribute5;
039:
040: public class NCMBeanTEST extends TestCase {
041: public NCMBeanTEST(String s) {
042: super (s);
043: }
044:
045: public void testOverloadedAttribute1() {
046: registerAndTest(new OverloadedAttribute1());
047: }
048:
049: public void testOverloadedAttribute2() {
050: registerAndTest(new OverloadedAttribute2());
051: }
052:
053: public void testOverloadedAttribute3() {
054: registerAndTest(new OverloadedAttribute3());
055: }
056:
057: public void testOverloadedAttribute4() {
058: registerAndTest(new OverloadedAttribute4());
059: }
060:
061: public void testOverloadedAttribute5() {
062: registerAndTest(new OverloadedAttribute5());
063: }
064:
065: public void testMixedDynamicStandard() {
066: MBeanServer server = MBeanServerFactory.createMBeanServer();
067: try {
068: server.registerMBean(new DynamicAndStandard(),
069: new ObjectName("test:foo=bar"));
070: MBeanInfo info = server.getMBeanInfo(new ObjectName(
071: "test:foo=bar"));
072: assertTrue(
073: "A mixed dynamic and standard mbean should be dynamic",
074: info.getDescription().equals(
075: DynamicAndStandard.DESCRIPTION));
076: } catch (NotCompliantMBeanException e) {
077: fail("A mixed dynamic and standardmbean is allowed from jmx 1.1");
078: } catch (Exception e) {
079: fail("unexpected exception when registering "
080: + DynamicAndStandard.class.getName() + ": "
081: + e.getMessage());
082: } finally {
083: MBeanServerFactory.releaseMBeanServer(server);
084: }
085: }
086:
087: public void testNoConstructor() {
088: try {
089: registerAndDontTest(NoConstructor.getInstance());
090: } catch (NotCompliantMBeanException e) {
091: fail("An MBean without a public constructor is allowed from jmx 1.1");
092: }
093: }
094:
095: public void testInterfaceProblems() {
096: try {
097: registerAndDontTest(new InterfaceProblems());
098: } catch (NotCompliantMBeanException e) {
099: fail("FAILS IN RI: Cannot cope with overriden get/is in interfaces");
100: }
101: }
102:
103: public void testNullDynamic() throws Exception {
104: MBeanServer server = MBeanServerFactory.newMBeanServer();
105: ObjectName name = new ObjectName("test:test=test");
106: boolean caught = false;
107: try {
108: server.registerMBean(new NullDynamic(), name);
109: } catch (NotCompliantMBeanException e) {
110: caught = true;
111: }
112: assertTrue("Expected NCME for null MBeanInfo", caught);
113: }
114:
115: private void registerAndTest(Object mbean) {
116: MBeanServer server = MBeanServerFactory.createMBeanServer();
117: try {
118: server.registerMBean(mbean, new ObjectName("test:foo=bar"));
119: fail("expected a NotCompliantMBeanException for "
120: + mbean.getClass().getName());
121: } catch (NotCompliantMBeanException e) {
122: // this is what we want
123: } catch (Exception e) {
124: fail("unexpected exception when registering "
125: + mbean.getClass().getName() + ": " + e);
126: } finally {
127: MBeanServerFactory.releaseMBeanServer(server);
128: }
129: }
130:
131: private void registerAndDontTest(Object mbean)
132: throws NotCompliantMBeanException {
133: MBeanServer server = MBeanServerFactory.createMBeanServer();
134: try {
135: server.registerMBean(mbean, new ObjectName("test:foo=bar"));
136: } catch (NotCompliantMBeanException e) {
137: throw e;
138: } catch (Exception e) {
139: fail("unexpected exception when registering "
140: + mbean.getClass().getName() + ": "
141: + e.getMessage());
142: } finally {
143: MBeanServerFactory.releaseMBeanServer(server);
144: }
145: }
146: }
|