Modified: incubator/qpid/trunk/qpid/java/management/client/src/main/java/org/apache/qpid/management/messages/MethodInvocationRequestMessage.java URL: http://svn.apache.org/viewvc/incubator/qpid/trunk/qpid/java/management/client/src/main/java/org/apache/qpid/management/messages/MethodInvocationRequestMessage.java?rev=707640&r1=707639&r2=707640&view=diff ============================================================================== --- incubator/qpid/trunk/qpid/java/management/client/src/main/java/org/apache/qpid/management/messages/MethodInvocationRequestMessage.java (original) +++ incubator/qpid/trunk/qpid/java/management/client/src/main/java/org/apache/qpid/management/messages/MethodInvocationRequestMessage.java Fri Oct 24 07:09:26 2008 @@ -20,25 +20,131 @@ */ package org.apache.qpid.management.messages; +import org.apache.qpid.management.Messages; import org.apache.qpid.management.Protocol; +import org.apache.qpid.management.configuration.Configuration; import org.apache.qpid.management.domain.model.QpidMethod; import org.apache.qpid.management.domain.model.type.Binary; +import org.apache.qpid.transport.DeliveryProperties; +import org.apache.qpid.transport.Header; +import org.apache.qpid.transport.MessageProperties; +import org.apache.qpid.transport.ReplyTo; +import org.apache.qpid.transport.util.Logger; +/** + * Abstract representation of a method invocation request message. + * Concrete subclasses must supply the values needed to build & encode the message. + * + * @author Andrea Gazzarini + */ public abstract class MethodInvocationRequestMessage extends ManagementMessage { + private final static Logger LOGGER = Logger.get(MethodInvocationRequestMessage.class); + + private DeliveryProperties _deliveryProperties; + private MessageProperties _messageProperties; + private Header _header; + + /** + * Builds a new method invocation request message with the given target identifiers. + * + * @param bankId the bank identifier. + * @param brokerId the broker identifier. + */ + public MethodInvocationRequestMessage(long bankId, long brokerId) + { + ReplyTo replyTo=new ReplyTo(); + replyTo.setRoutingKey(Configuration.getInstance().getMethodReplyQueueName()); + _messageProperties = new MessageProperties(); + _messageProperties.setReplyTo(replyTo); + + String routingKey = String.format("agent.%s.%s", brokerId,bankId); + + LOGGER.debug(Messages.QMAN_200032_COMMAND_MESSAGE_ROUTING_KEY, routingKey); + + _deliveryProperties = new DeliveryProperties(); + _deliveryProperties.setRoutingKey(routingKey); + _header = new Header(_deliveryProperties, _messageProperties); + } + @Override char opcode () { return Protocol.OPERATION_INVOCATION_REQUEST_OPCODE; } - + + /** + * Returns the package name. + * + * @return the package name. + */ protected abstract String packageName(); + + /** + * Returns the class name. + * + * @return the class name. + */ protected abstract String className(); + + /** + * Returns the schema hash. + * + * @return the schema hash. + */ protected abstract Binary schemaHash(); + + /** + * Returns the object identifier. + * + * @return the object identifier. + */ protected abstract Binary objectId(); + + /** + * Returns the method to be invoked. + * + * @return the method to be invoked. + */ protected abstract QpidMethod method(); + + /** + * Returns the parameters used for method invocation. + * + * @return the parameters used for method invocation. + */ protected abstract Object[] parameters(); + /** + * Returns the delivery properties of this message. + * + * @return the delivery properties of this message. + */ + public DeliveryProperties getDeliveryProperties () + { + return _deliveryProperties; + } + + /** + * Returns the header of this message. + * + * @return the header of this message. + */ + public Header getHeader () + { + return _header; + } + + /** + * Returns the messages header properties of this message. + * + * @return the message header properties of this message. + */ + public MessageProperties getMessageProperties () + { + return _messageProperties; + } + @Override void specificMessageEncoding () { @@ -51,4 +157,4 @@ _codec.packStr8(method.getName()); method.encodeParameters(parameters(), _codec); } -} \ No newline at end of file +}
Modified: incubator/qpid/trunk/qpid/java/management/client/src/main/java/org/apache/qpid/management/messages/SchemaRequestMessage.java URL: http://svn.apache.org/viewvc/incubator/qpid/trunk/qpid/java/management/client/src/main/java/org/apache/qpid/management/messages/SchemaRequestMessage.java?rev=707640&r1=707639&r2=707640&view=diff ============================================================================== --- incubator/qpid/trunk/qpid/java/management/client/src/main/java/org/apache/qpid/management/messages/SchemaRequestMessage.java (original) +++ incubator/qpid/trunk/qpid/java/management/client/src/main/java/org/apache/qpid/management/messages/SchemaRequestMessage.java Fri Oct 24 07:09:26 2008 @@ -23,6 +23,12 @@ import org.apache.qpid.management.Protocol; import org.apache.qpid.management.domain.model.type.Binary; +/** + * Abstract representation of a schema request message. + * Concrete subclasses must supply the values needed to build & encode the message. + * + * @author Andrea Gazzarini + */ public abstract class SchemaRequestMessage extends ManagementMessage { @Override @@ -31,10 +37,25 @@ return Protocol.SCHEMA_REQUEST_OPCODE; } + /** + * Returns the package name. + * + * @return the package name. + */ protected abstract String packageName(); + /** + * Returns the class name. + * + * @return the class name. + */ protected abstract String className(); + /** + * Returns the schema hash. + * + * @return the schema hash. + */ protected abstract Binary schemaHash(); @Override @@ -44,4 +65,4 @@ _codec.packStr8(className()); schemaHash().encode(_codec); } -} \ No newline at end of file +} Modified: incubator/qpid/trunk/qpid/java/management/client/src/test/java/org/apache/qpid/management/domain/model/QpidClassTest.java URL: http://svn.apache.org/viewvc/incubator/qpid/trunk/qpid/java/management/client/src/test/java/org/apache/qpid/management/domain/model/QpidClassTest.java?rev=707640&r1=707639&r2=707640&view=diff ============================================================================== --- incubator/qpid/trunk/qpid/java/management/client/src/test/java/org/apache/qpid/management/domain/model/QpidClassTest.java (original) +++ incubator/qpid/trunk/qpid/java/management/client/src/test/java/org/apache/qpid/management/domain/model/QpidClassTest.java Fri Oct 24 07:09:26 2008 @@ -1,6 +1,5 @@ package org.apache.qpid.management.domain.model; - import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; @@ -19,6 +18,11 @@ import org.apache.qpid.management.domain.handler.impl.MethodOrEventDataTransferObject; import org.apache.qpid.management.domain.model.QpidClass.QpidManagedObject; +/** + * Test case for Qpid Class. + * + * @author Andrea Gazzarini + */ public class QpidClassTest extends TestCase { private QpidClass _class; @@ -197,8 +201,6 @@ TestConstants._1, true, TestConstants._1)); - - List<Map<String,Object>> statisticDefinitions = new ArrayList<Map<String,Object>>(2); _class.setSchema(propertyDefinitions, TestConstants.EMPTY_STATISTICS_SCHEMA, TestConstants.EMPTY_METHODS_SCHEMA); Modified: incubator/qpid/trunk/qpid/java/management/client/src/test/java/org/apache/qpid/management/domain/services/BrokerMessageListenerTest.java URL: http://svn.apache.org/viewvc/incubator/qpid/trunk/qpid/java/management/client/src/test/java/org/apache/qpid/management/domain/services/BrokerMessageListenerTest.java?rev=707640&r1=707639&r2=707640&view=diff ============================================================================== --- incubator/qpid/trunk/qpid/java/management/client/src/test/java/org/apache/qpid/management/domain/services/BrokerMessageListenerTest.java (original) +++ incubator/qpid/trunk/qpid/java/management/client/src/test/java/org/apache/qpid/management/domain/services/BrokerMessageListenerTest.java Fri Oct 24 07:09:26 2008 @@ -23,6 +23,7 @@ import java.io.IOException; import java.util.HashMap; import java.util.Map; +import java.util.Random; import junit.framework.TestCase; @@ -167,4 +168,74 @@ _listener.onMessage(message); } + + /** + * Tests the execution of the onMessage() method when the incoming message is a compound message. + * + * <br>precondition : the incoming message is a compound message. + * <br>postcondition : each tokenized message is forwarded to the appropriate handler. + */ + public void testOnMessageOK_WithCompoundMessage() throws Exception + { + final Map<Character,IMessageHandler> handlersMap = new HashMap<Character,IMessageHandler>(); + char [] opcodes = {'a','b','c','d','e'}; + + class MockMessageHandler implements IMessageHandler + { + private final char _opcode; + + public MockMessageHandler(char opcode) + { + this._opcode = opcode; + } + + public void process (ManagementDecoder decoder, int sequenceNumber) + { + handlersMap.remove(_opcode); + } + + public void setDomainModel (DomainModel domainModel) + { + // Do nothing here. It's just a mock handler. + } + }; + + for (char opcode : opcodes) + { + handlersMap.put(opcode, new MockMessageHandler(opcode)); + } + + // Removes previously injected handlers (i.e. x & y) + _listener._handlers.clear(); + _listener.setHandlers(handlersMap); + + Message compoundMessage = createCompoundMessage(opcodes); + _listener.onMessage(compoundMessage); + + assertTrue(handlersMap.isEmpty()); + } + + // Creates a (non valid) compound message. + private Message createCompoundMessage(char[] opcodes) throws IOException { + byte [] compoundMessageData = new byte [12 * opcodes.length]; + Random randomizer = new Random(); + int position = 0; + + for (char opcode : opcodes) { + System.arraycopy(MessageTokenizer.MAGIC_NUMBER_BYTES, 0, compoundMessageData, position, MessageTokenizer.MAGIC_NUMBER_BYTES.length); + position+=MessageTokenizer.MAGIC_NUMBER_BYTES.length; + + compoundMessageData[position++] = (byte)opcode; + + for (int c = 4; c < 12; c++) + { + byte aByte = (byte)randomizer.nextInt(127); + compoundMessageData[position++] = aByte; + } + } + + Message compoundMessage = new ByteBufferMessage(); + compoundMessage.appendData(compoundMessageData); + return compoundMessage; + } } Added: incubator/qpid/trunk/qpid/java/management/client/src/test/java/org/apache/qpid/management/domain/services/MessageTokenizerTest.java URL: http://svn.apache.org/viewvc/incubator/qpid/trunk/qpid/java/management/client/src/test/java/org/apache/qpid/management/domain/services/MessageTokenizerTest.java?rev=707640&view=auto ============================================================================== --- incubator/qpid/trunk/qpid/java/management/client/src/test/java/org/apache/qpid/management/domain/services/MessageTokenizerTest.java (added) +++ incubator/qpid/trunk/qpid/java/management/client/src/test/java/org/apache/qpid/management/domain/services/MessageTokenizerTest.java Fri Oct 24 07:09:26 2008 @@ -0,0 +1,120 @@ +package org.apache.qpid.management.domain.services; + +import java.io.IOException; +import java.nio.ByteBuffer; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.*; +import java.util.Random; + +import junit.framework.TestCase; + +import org.apache.qpid.api.Message; +import org.apache.qpid.nclient.util.ByteBufferMessage; +import org.apache.qpid.transport.codec.ManagementDecoder; + +/** + * Tests case for messaeg tokenizer. + * + * @author Andrea Gazzarini + */ +public class MessageTokenizerTest extends TestCase { + + /** + * Tests the execution of the message tokenizer when the given message is not a valid AMQP message. + * + * <br>precondition : the incoming message is not a valid AMQP message. + * <br>postcondition : no exception is thrown and there will be exactly one token with the given message. + */ + public void testOK_WithNoMessage() throws IOException{ + byte [] noMessage = {2,10,120,23,23,23,4,10,11,12,2,1,3,-22}; + + Message multiMessage = new ByteBufferMessage(); + multiMessage.appendData(noMessage); + MessageTokenizer tokenizer = new MessageTokenizer(multiMessage); + + assertEquals(1, tokenizer.countTokens()); + assertEquals(tokenizer.nextElement(),noMessage); + assertFalse(tokenizer.hasMoreElements()); + } + + /** + * Tests the execution of the message tokenizer when the given message contains only one message. + * + * <br>precondition : the incoming message contains only one message. + * <br>postcondition : no exception is thrown and there will be exactly one token with the given message. + */ + public void testOK_WithOneMessage() throws IOException{ + byte [] oneEncodedMessage = {'A','M','2',23,23,23,4,10,11,12,2,1,3,-22}; + + Message multiMessage = new ByteBufferMessage(); + multiMessage.appendData(oneEncodedMessage); + MessageTokenizer tokenizer = new MessageTokenizer(multiMessage); + + assertEquals(1, tokenizer.countTokens()); + assertEquals(tokenizer.nextElement(),oneEncodedMessage); + assertFalse(tokenizer.hasMoreElements()); + } + + /** + * Tests the execution of the message tokenizer when the given message contains a random number of messages. + * + * <br>precondition : the incoming message contains a random number of messages. + * <br>postcondition : no exception is thrown and each built token is a valid message starting with right header. + */ + public void testOK_WithRandomNUmberOfMessages() throws IOException{ + Random randomizer = new Random(); + + int howManyLoops = randomizer.nextInt(10000); + byte [] compoundMessageData = new byte [12 * howManyLoops]; + + List<byte []> messages = new ArrayList<byte[]>(howManyLoops); + + int position = 0; + for (int i = 0; i < howManyLoops; i++) + { + byte [] message = new byte[12]; + System.arraycopy(MessageTokenizer.MAGIC_NUMBER_BYTES, 0, compoundMessageData, position, MessageTokenizer.MAGIC_NUMBER_BYTES.length); + System.arraycopy(MessageTokenizer.MAGIC_NUMBER_BYTES, 0, message, 0, MessageTokenizer.MAGIC_NUMBER_BYTES.length); + position+=MessageTokenizer.MAGIC_NUMBER_BYTES.length; + + for (int c = 3; c < 12; c++) + { + byte aByte = (byte)randomizer.nextInt(127); + compoundMessageData[position++] = aByte; + message[c] = aByte; + } + messages.add(message); + } + + Message multiMessage = new ByteBufferMessage(); + multiMessage.appendData(compoundMessageData); + MessageTokenizer tokenizer = new MessageTokenizer(multiMessage); + + int howManyTokens = tokenizer.countTokens(); + assertEquals(howManyLoops, howManyTokens); + + int index = 0; + while (tokenizer.hasMoreElements()) + { + assertEquals(tokenizer.nextElement(),messages.get(index++)); + } + + assertEquals((index),howManyTokens); + } + + /** + * Internal method used for comparison of two messages. + * + * @param message the token message just built by the tokenizer. + * @param expected the expected result. + */ + private void assertEquals(Message message, byte [] expected) throws IOException + { + ByteBuffer messageContent = message.readData(); + ManagementDecoder decoder = new ManagementDecoder(); + decoder.init(messageContent); + byte [] content = decoder.readReaminingBytes(); + assertTrue(Arrays.equals(content, expected)); + } +}
