Added: qpid/trunk/qpid/java/management/client/src/test/java/org/apache/qpid/management/wsdm/capabilities/MBeanCapabilityBuilderTest.java URL: http://svn.apache.org/viewvc/qpid/trunk/qpid/java/management/client/src/test/java/org/apache/qpid/management/wsdm/capabilities/MBeanCapabilityBuilderTest.java?rev=737182&view=auto ============================================================================== --- qpid/trunk/qpid/java/management/client/src/test/java/org/apache/qpid/management/wsdm/capabilities/MBeanCapabilityBuilderTest.java (added) +++ qpid/trunk/qpid/java/management/client/src/test/java/org/apache/qpid/management/wsdm/capabilities/MBeanCapabilityBuilderTest.java Fri Jan 23 12:44:48 2009 @@ -0,0 +1,278 @@ +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ +package org.apache.qpid.management.wsdm.capabilities; + +import java.lang.management.ManagementFactory; +import java.util.ArrayList; +import java.util.List; + +import javassist.CtClass; +import javassist.CtMethod; + +import javax.management.MBeanAttributeInfo; +import javax.management.MBeanInfo; +import javax.management.MBeanOperationInfo; +import javax.management.MBeanParameterInfo; +import javax.management.MBeanServer; +import javax.management.ObjectName; + +import junit.framework.TestCase; + +import org.apache.qpid.management.domain.handler.impl.QpidDomainObject; +import org.apache.qpid.management.wsdm.common.EntityInstanceNotFoundFault; +import org.apache.qpid.management.wsdm.common.MethodInvocationFault; +import org.apache.qpid.management.wsdm.common.NoSuchAttributeFault; +import org.apache.qpid.management.wsdm.common.QManFault; + +/** + * Test case for MBean capability builder. + * + * @author Andrea Gazzarini + */ +public class MBeanCapabilityBuilderTest extends TestCase +{ + + private MBeanCapabilityBuilder _builder; + private ObjectName _objectName; + + /** + * Set up fixture for this test case. + */ + protected void setUp() throws Exception + { + _builder = new MBeanCapabilityBuilder(); + _objectName = new ObjectName("Test:Name=aName,class=DomainObject"); + } + + /** + * Tests that state change that occcurs on the begin() method when the requested + * class has not been defined. + */ + public void testBegin_withClassNotYetDefined() throws Exception + { + _builder.begin(_objectName); + assertEquals(_builder._state,_builder._classNotAvailable); + } + + /** + * Tests that state change that occcurs on the begin() method when the requested + * class has not been defined. + */ + public void testBegin_withClassAlreadyDefined() throws Exception + { + _objectName = new ObjectName("Test:Name=aString,class=MBeanCapabilityBuilder"); + _builder.begin(_objectName); + + assertTrue(_builder._state instanceof DummyCapabilityBuilder); + assertSame(_builder._endAttributeHandler, _builder._noPropertyHasBeenDefined); + } + + /** + * Tests the generateGetter method(). + */ + public void testGenerateGetter() + { + String name ="MyProperty"; + String type = Long.class.getName(); + String expected = + "public "+ + type+ + " get"+ + name+ + "() throws NoSuchAttributeFault,EntityInstanceNotFoundFault,QManFault { return ("+ + type+ + ") getAttribute(\""+ + name+ + "\"); }"; + + String result = _builder.generateGetter(type, name); + assertEquals(expected,result); + } + + /** + * Tests the generateGetter method(). + */ + public void testGenerateSetter() + { + String name ="MyProperty"; + String type = Long.class.getName(); + String expected = + "public void setMyProperty("+ + type+ + " newValue) throws NoSuchAttributeFault,EntityInstanceNotFoundFault,QManFault { setAttribute(\""+ + name+ + "\", newValue); }"; + + String result = _builder.generateSetter(type, name); + assertEquals(expected,result); + } + + /** + * Tests the whole execution of the builder. + */ + @SuppressWarnings("unchecked") + public void testBuildOK() throws Exception + { + MBeanServer server = ManagementFactory.getPlatformMBeanServer(); + QpidDomainObject mbean = new QpidDomainObject(); + server.registerMBean(mbean, _objectName); + + _builder.begin(_objectName); + + CtClass definition = _builder._capabilityClassDefinition; + assertEquals( + MBeanCapability.class.getName(), + definition.getSuperclass().getName()); + + MBeanInfo metadata = server.getMBeanInfo(_objectName); + + for (MBeanAttributeInfo attribute : metadata.getAttributes()) + { + _builder.onAttribute(attribute); + checkAttribute(attribute, definition); + + assertSame( + _builder._endAttributeHandler, + _builder._atLeastThereIsOneProperty); + } + + for (MBeanOperationInfo operation : metadata.getOperations()) + { + _builder.onOperation(operation); + checkOperation(operation,definition); + } + + _builder.endAttributes(); + _builder.endOperations(); + + assertNotNull(_builder.getCapabilityClass()); + } + + /** + * Checks an operation / method after that it has been declared on + * capability definition. + * + * @param operation the (JMX) operation metadata. + * @param definition the capability class definition. + * @throws Exception when something goes wrong during introspection. + */ + private void checkOperation(MBeanOperationInfo operation, CtClass definition) throws Exception + { + CtMethod method = definition.getDeclaredMethod(operation.getName()); + assertNotNull(method); + + checkExceptionTypes( + method.getExceptionTypes(), + new String[]{ + QManFault.class.getName(), + EntityInstanceNotFoundFault.class.getName(), + MethodInvocationFault.class.getName()}); + + assertEquals(Result.class.getName(),method.getReturnType().getName()); + + CtClass [] parameterTypes = method.getParameterTypes(); + MBeanParameterInfo [] parameterMetadata = operation.getSignature(); + + assertEquals(parameterTypes.length, parameterMetadata.length); + for (int i = 0; i < parameterMetadata.length; i++) + { + assertEquals( + parameterTypes[i].getName(), + Class.forName(parameterMetadata[i].getType()).getCanonicalName()); + } + } + + /** + * Checks the exception types associated with a method. + * + * @param exceptionTypes the exception types actually thrown. + * @param expectedExceptionNames the expected exception types (as strings). + */ + private void checkExceptionTypes(CtClass [] exceptionTypes, String [] expectedExceptionNames) + { + List<String> exceptionNames = new ArrayList<String>(exceptionTypes.length); + for (CtClass exception : exceptionTypes) + { + exceptionNames.add(exception.getName()); + } + + for (String expectedExceptionName : expectedExceptionNames) + { + exceptionNames.remove(expectedExceptionName); + } + + assertTrue(exceptionNames.isEmpty()); + } + + /** + * Checks an attribute after that it has been declared on capability definition. + * + * @param attribute the (JMX) attribute metadata. + * @param definition the capability class definition. + * @throws Exception when something goes wrong during introspection. + */ + private void checkAttribute(MBeanAttributeInfo attribute, CtClass definition) throws Exception + { + String name = _builder.getNameForAccessors(attribute.getName()); + + String newPropertyDeclaration = + new StringBuilder("new QName(Names.NAMESPACE_URI, \"") + .append(attribute.getName()) + .append("\", Names.PREFIX),") + .toString(); + assertTrue(_builder._properties.indexOf(newPropertyDeclaration) != -1); + + if (attribute.isReadable()) + { + CtMethod getter = definition.getDeclaredMethod("get"+name); + assertNotNull(getter); + + checkExceptionTypes( + getter.getExceptionTypes(), + new String[]{ + QManFault.class.getName(), + NoSuchAttributeFault.class.getName(), + EntityInstanceNotFoundFault.class.getName()}); + + assertEquals(0,getter.getParameterTypes().length); + assertEquals(attribute.getType(),getter.getReturnType().getName()); + } + + if (attribute.isWritable()) + { + CtMethod setter = definition.getDeclaredMethod("set"+name); + assertNotNull(setter); + + checkExceptionTypes( + setter.getExceptionTypes(), + new String[]{ + QManFault.class.getName(), + NoSuchAttributeFault.class.getName(), + EntityInstanceNotFoundFault.class.getName()}); + + CtClass [] parameterTypes = setter.getParameterTypes(); + + assertEquals(1,parameterTypes.length); + assertEquals(attribute.getType(),parameterTypes[0].getName()); + assertEquals(void.class.getName(),setter.getReturnType().getName()); + } + } +} \ No newline at end of file
Added: qpid/trunk/qpid/java/management/client/src/test/java/org/apache/qpid/management/wsdm/capabilities/MBeanCapabilityTest.java URL: http://svn.apache.org/viewvc/qpid/trunk/qpid/java/management/client/src/test/java/org/apache/qpid/management/wsdm/capabilities/MBeanCapabilityTest.java?rev=737182&view=auto ============================================================================== --- qpid/trunk/qpid/java/management/client/src/test/java/org/apache/qpid/management/wsdm/capabilities/MBeanCapabilityTest.java (added) +++ qpid/trunk/qpid/java/management/client/src/test/java/org/apache/qpid/management/wsdm/capabilities/MBeanCapabilityTest.java Fri Jan 23 12:44:48 2009 @@ -0,0 +1,204 @@ +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ +package org.apache.qpid.management.wsdm.capabilities; + +import java.lang.management.ManagementFactory; +import java.net.URI; + +import javax.management.ObjectName; + +import junit.framework.TestCase; + +import org.apache.muse.ws.addressing.EndpointReference; +import org.apache.muse.ws.resource.WsResource; +import org.apache.muse.ws.resource.impl.SimpleWsResource; +import org.apache.qpid.management.domain.handler.impl.QpidDomainObject; +import org.apache.qpid.management.wsdm.common.EntityInstanceNotFoundFault; +import org.apache.qpid.management.wsdm.common.NoSuchAttributeFault; +import org.apache.qpid.management.wsdm.common.QManFault; + +/** + * Test case for MBeanCapability supertype layer.. + * + * @author Andrea Gazzarini + */ +public class MBeanCapabilityTest extends TestCase +{ + private final String _typeAttributeName = "Type"; + private final String _newTypeValue = "DomainObject"; + + private ObjectName _objectName; + private ObjectName _unknownObjectName; + + private MBeanCapability _capability; + + @Override + protected void setUp() throws Exception + { + _objectName = new ObjectName("Test:Name=aName"); + _unknownObjectName = new ObjectName("Test:Type=unknown"); + + _capability = new MBeanCapability(){ + @Override + public WsResource getWsResource() + { + return new SimpleWsResource(){ + @Override + public EndpointReference getEndpointReference() + { + return new EndpointReference(URI.create("http://qpid.apache.org/qman")); + } + }; + } + }; + _capability.setResourceObjectName(_objectName); + ManagementFactory.getPlatformMBeanServer().registerMBean(new QpidDomainObject(), _objectName); + } + + /** + * Tests the execution of the getAttribute() and setAttribute() method. + * + * <br>precondition : the mbean is registered and a _capability is associated with it. + * <br>postcondition : the set value of the requested attribute is correctly returned. + */ + public void testGetAndSetAttributeOK() throws Exception + { + Object name = _capability.getAttribute(_typeAttributeName); + assertNull("Name has an initial value of null so how is possibile that is not null?",name); + + _capability.setAttribute(_typeAttributeName,_newTypeValue); + + name = _capability.getAttribute(_typeAttributeName); + assertEquals("Now the name attribute must be set to \""+_newTypeValue+"\"",_newTypeValue,name); + } + + /** + * Tests the execution of the getAttribute() and setAttribte() methods when an unknown attribute is given.. + * + * <br>precondition : the mbean is registered, a _capability is associated with it and the requested attribute doesn't exist. + * <br>postcondition : an exception is thrown indicating the failure. + */ + public void testNoSuchAttributeFault() throws Exception + { + // I suppose that we shouldn't have an attribute with this name... + String unknownAttribute = String.valueOf(System.currentTimeMillis()); + + try + { + _capability.getAttribute(unknownAttribute); + fail("An exception must be thrown here in order to indicate that the attribute is unknown."); + } catch(NoSuchAttributeFault expected) + { + } + + try + { + _capability.setAttribute(unknownAttribute,null); + fail("An exception must be thrown here in order to indicate that the attribute is unknown."); + } catch(NoSuchAttributeFault expected) + { + } + } + + /** + * Tests the execution of the setAttribute,getAttribute and invoke methods when the target mbean + * doesn't exists. + * + * <br>precondition : the object name associated with the capability is not pointing to an existent MBean. + * <br>postcondition : an exception is thrown indicating the failure. + */ + public void testEntityInstanceNotFoundFault() throws Exception + { + _capability.setResourceObjectName(_unknownObjectName); + + try + { + _capability.getAttribute(_typeAttributeName); + fail("An exception must be thrown here in order to indicate that the attribute is unknown."); + } catch(EntityInstanceNotFoundFault expected) + { + } + + try + { + _capability.setAttribute(_typeAttributeName,_newTypeValue); + fail("An exception must be thrown here in order to indicate that the attribute is unknown."); + } catch(EntityInstanceNotFoundFault expected) + { + } + + try + { + _capability.invoke("operationName", null,null); + fail("An exception must be thrown here in order to indicate that the attribute is unknown."); + } catch(EntityInstanceNotFoundFault expected) + { + } + } + + /** + * Tests the execution of the setAttribute,getAttribute and invoke methods when an unknown / unexpected + * exception is thrown. + * + * <br>precondition : the mbean is registered and a capability is associated with it. Something + * unexpected happens during method invocation. + * <br>postcondition : an exception is thrown indicating the failure. + */ + public void testQManFault() throws Exception + { + // Emulate a RuntimeException (which is the best example of uncaught exception... :) ) + _capability.setResourceObjectName(null); + + try + { + _capability.getAttribute(_typeAttributeName); + fail("An exception must be thrown here in order to indicate that the attribute is unknown."); + } catch(QManFault expected) + { + } + + try + { + _capability.setAttribute(_typeAttributeName,_newTypeValue); + fail("An exception must be thrown here in order to indicate that the attribute is unknown."); + } catch(QManFault expected) + { + } + + try + { + _capability.invoke("operationName", null,null); + fail("An exception must be thrown here in order to indicate that the attribute is unknown."); + } catch(QManFault expected) + { + } + } + + + /** + * Shutdown procedure for this test case. + */ + @Override + protected void tearDown() throws Exception + { + ManagementFactory.getPlatformMBeanServer().unregisterMBean(_objectName); + } +} Added: qpid/trunk/qpid/java/management/client/src/test/java/org/apache/qpid/management/wsdm/capabilities/RmdBuilderTest.java URL: http://svn.apache.org/viewvc/qpid/trunk/qpid/java/management/client/src/test/java/org/apache/qpid/management/wsdm/capabilities/RmdBuilderTest.java?rev=737182&view=auto ============================================================================== --- qpid/trunk/qpid/java/management/client/src/test/java/org/apache/qpid/management/wsdm/capabilities/RmdBuilderTest.java (added) +++ qpid/trunk/qpid/java/management/client/src/test/java/org/apache/qpid/management/wsdm/capabilities/RmdBuilderTest.java Fri Jan 23 12:44:48 2009 @@ -0,0 +1,110 @@ +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ +package org.apache.qpid.management.wsdm.capabilities; + +import java.lang.management.ManagementFactory; + +import javax.management.MBeanAttributeInfo; +import javax.management.MBeanInfo; +import javax.management.MBeanServer; +import javax.management.ObjectName; + +import org.apache.qpid.management.Names; +import org.apache.qpid.management.domain.handler.impl.QpidDomainObject; +import org.w3c.dom.Element; + +import junit.framework.TestCase; + +/** + * Test case for Resource Metadata Descriptor Builder. + * + * @author Andrea Gazzarini + */ +public class RmdBuilderTest extends TestCase +{ + private MBeanInfo _metadata; + private RmdBuilder _builder; + private ObjectName _objectName; + + @Override + protected void setUp() throws Exception + { + MBeanServer server = ManagementFactory.getPlatformMBeanServer(); + _objectName = new ObjectName("Test:Name=QpidDomainObject"); + + server.registerMBean(new QpidDomainObject(), _objectName); + _metadata = server.getMBeanInfo(_objectName); + + _builder = new RmdBuilder(); + _builder.begin(_objectName); + + assertEquals(_objectName,_builder._objectName); + } + + /** + * Tests the execution of the onOperation() method. + */ + public void testOnOperation() throws Exception + { + MBeanAttributeInfo [] attributes = _metadata.getAttributes(); + for (MBeanAttributeInfo attribute : attributes) + { + _builder.onAttribute(attribute); + } + + Element [] rmd = _builder.getResourceMetadataDescriptor(); + + assertEquals(attributes.length,rmd.length); + + for (MBeanAttributeInfo attribute: _metadata.getAttributes()) + { + Element propertyMetadataDescriptor = getPropertyMetadatDescriptor(attribute.getName(), rmd); + + String modifiability = propertyMetadataDescriptor.getAttribute(Names.MODIFIABILITY); + String expectedValue = + attribute.isWritable() + ? Names.READ_WRITE + : Names.READ_ONLY; + assertEquals(expectedValue,modifiability); + } + } + + /** + * Returns the property metadata descriptor associated with the given attribute name. + * + * @param name the attribute name. + * @param rmd the resource metadata descriptor. + * @return the property metadata descriptor associated with the given attribute name. + * @throws RuntimeException if metadata for the given attribute is not found. + */ + private Element getPropertyMetadatDescriptor(String name, Element [] rmd) + { + for (Element propertyMetadataDescriptor : rmd) + { + if ((Names.PREFIX+":"+name).equals( + propertyMetadataDescriptor.getAttribute(Names.NAME_ATTRIBUTE))) + { + return propertyMetadataDescriptor; + } + } + throw new RuntimeException("Property MetadataDescriptor not found for attribute "+name); + } +} \ No newline at end of file Added: qpid/trunk/qpid/java/management/client/src/test/java/org/apache/qpid/management/wsdm/web.xml URL: http://svn.apache.org/viewvc/qpid/trunk/qpid/java/management/client/src/test/java/org/apache/qpid/management/wsdm/web.xml?rev=737182&view=auto ============================================================================== --- qpid/trunk/qpid/java/management/client/src/test/java/org/apache/qpid/management/wsdm/web.xml (added) +++ qpid/trunk/qpid/java/management/client/src/test/java/org/apache/qpid/management/wsdm/web.xml Fri Jan 23 12:44:48 2009 @@ -0,0 +1,11 @@ +<?xml version="1.0" encoding="UTF-8"?> +<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> + <servlet> + <display-name>Qpid emulator startip</display-name> + <servlet-name>QEmu</servlet-name> + <servlet-class>org.apache.qpid.management.wsdm.QEmuInitializer</servlet-class> + <load-on-startup>1</load-on-startup> + </servlet> +</web-app> \ No newline at end of file --------------------------------------------------------------------- Apache Qpid - AMQP Messaging Implementation Project: http://qpid.apache.org Use/Interact: mailto:commits-subscr...@qpid.apache.org