@Test public void testSetters() { long v = random.nextLong(); producer.setDeliveryDelay(v); Assert.assertEquals(v, producer.getDeliveryDelay()); long l = random.nextLong(); producer.setTimeToLive(l); Assert.assertEquals(l, producer.getTimeToLive()); String id = "ID: jms2-tests-correlation-id" + random.nextLong(); producer.setJMSCorrelationID(id); Assert.assertEquals(id, producer.getJMSCorrelationID()); //set a property of an invalid type (ArrayList) try { producer.setProperty("name1", new ArrayList<String>(2)); fail("didn't get expected MessageFormatRuntimeException"); } catch (MessageFormatRuntimeException e) { //expected. } }
@Override public Set<String> getPropertyNames() { try { Set<SimpleString> simplePropNames = properties.getPropertyNames(); Set<String> propNames = new HashSet<>(simplePropNames.size()); for (SimpleString str : simplePropNames) { propNames.add(str.toString()); } return propNames; } catch (ActiveMQPropertyConversionException ce) { throw new MessageFormatRuntimeException(ce.getMessage()); } catch (RuntimeException e) { throw new JMSRuntimeException(e.getMessage()); } }
@Test public void testSetPropertyConversions() { JMSProducer producer = context.createProducer(); producer.setProperty(STRING_PROPERTY_NAME, STRING_PROPERTY_VALUE); producer.setProperty(BYTE_PROPERTY_NAME, Byte.valueOf(BYTE_PROPERTY_VALUE)); producer.setProperty(BOOLEAN_PROPERTY_NAME, Boolean.valueOf(BOOLEAN_PROPERTY_VALUE)); producer.setProperty(SHORT_PROPERTY_NAME, Short.valueOf(SHORT_PROPERTY_VALUE)); producer.setProperty(INTEGER_PROPERTY_NAME, Integer.valueOf(INTEGER_PROPERTY_VALUE)); producer.setProperty(LONG_PROPERTY_NAME, Long.valueOf(LONG_PROPERTY_VALUE)); producer.setProperty(FLOAT_PROPERTY_NAME, Float.valueOf(FLOAT_PROPERTY_VALUE)); producer.setProperty(DOUBLE_PROPERTY_NAME, Double.valueOf(DOUBLE_PROPERTY_VALUE)); try { producer.setProperty(STRING_PROPERTY_NAME, UUID.randomUUID()); fail("Should not be able to set non-primitive type"); } catch (MessageFormatRuntimeException mfe) { } assertNull(producer.getObjectProperty("Unknown")); assertEquals(STRING_PROPERTY_VALUE, producer.getStringProperty(STRING_PROPERTY_NAME)); assertEquals(BYTE_PROPERTY_VALUE, producer.getByteProperty(BYTE_PROPERTY_NAME)); assertEquals(BOOLEAN_PROPERTY_VALUE, producer.getBooleanProperty(BOOLEAN_PROPERTY_NAME)); assertEquals(SHORT_PROPERTY_VALUE, producer.getShortProperty(SHORT_PROPERTY_NAME)); assertEquals(INTEGER_PROPERTY_VALUE, producer.getIntProperty(INTEGER_PROPERTY_NAME)); assertEquals(LONG_PROPERTY_VALUE, producer.getLongProperty(LONG_PROPERTY_NAME)); assertEquals(FLOAT_PROPERTY_VALUE, producer.getFloatProperty(FLOAT_PROPERTY_NAME), 0.0); assertEquals(DOUBLE_PROPERTY_VALUE, producer.getDoubleProperty(DOUBLE_PROPERTY_NAME), 0.0); assertEquals(STRING_PROPERTY_VALUE, producer.getObjectProperty(STRING_PROPERTY_NAME)); assertEquals(BYTE_PROPERTY_VALUE, producer.getObjectProperty(BYTE_PROPERTY_NAME)); assertEquals(BOOLEAN_PROPERTY_VALUE, producer.getObjectProperty(BOOLEAN_PROPERTY_NAME)); assertEquals(SHORT_PROPERTY_VALUE, producer.getObjectProperty(SHORT_PROPERTY_NAME)); assertEquals(INTEGER_PROPERTY_VALUE, producer.getObjectProperty(INTEGER_PROPERTY_NAME)); assertEquals(LONG_PROPERTY_VALUE, producer.getObjectProperty(LONG_PROPERTY_NAME)); assertEquals(FLOAT_PROPERTY_VALUE, producer.getObjectProperty(FLOAT_PROPERTY_NAME)); assertEquals(DOUBLE_PROPERTY_VALUE, producer.getObjectProperty(DOUBLE_PROPERTY_NAME)); }
@Test public void testSetObjectPropetryWithInvalidObject() { JMSProducer producer = context.createProducer(); try { producer.setProperty(GOOD_PROPERTY_NAME, UUID.randomUUID()); fail("Should not accept invalid property name"); } catch (MessageFormatRuntimeException mfre) {} }
@Test public void testSendNullMessageThrowsMFRE() throws JMSException { JMSProducer producer = context.createProducer(); try { producer.send(JMS_DESTINATION, (Message) null); fail("Should throw a MessageFormatRuntimeException"); } catch (MessageFormatRuntimeException mfre) { } catch (Exception e) { fail("Should throw a MessageFormatRuntimeException"); } }
public static JMSRuntimeException convertToRuntimeException(JMSException e) { if (e instanceof javax.jms.IllegalStateException) { return new IllegalStateRuntimeException(e.getMessage(), e.getErrorCode(), e); } if (e instanceof InvalidClientIDException) { return new InvalidClientIDRuntimeException(e.getMessage(), e.getErrorCode(), e); } if (e instanceof InvalidDestinationException) { return new InvalidDestinationRuntimeException(e.getMessage(), e.getErrorCode(), e); } if (e instanceof InvalidSelectorException) { return new InvalidSelectorRuntimeException(e.getMessage(), e.getErrorCode(), e); } if (e instanceof JMSSecurityException) { return new JMSSecurityRuntimeException(e.getMessage(), e.getErrorCode(), e); } if (e instanceof MessageFormatException) { return new MessageFormatRuntimeException(e.getMessage(), e.getErrorCode(), e); } if (e instanceof MessageNotWriteableException) { return new MessageNotWriteableRuntimeException(e.getMessage(), e.getErrorCode(), e); } if (e instanceof ResourceAllocationException) { return new ResourceAllocationRuntimeException(e.getMessage(), e.getErrorCode(), e); } if (e instanceof TransactionInProgressException) { return new TransactionInProgressRuntimeException(e.getMessage(), e.getErrorCode(), e); } if (e instanceof TransactionRolledBackException) { return new TransactionRolledBackRuntimeException(e.getMessage(), e.getErrorCode(), e); } return new JMSRuntimeException(e.getMessage(), e.getErrorCode(), e); }
@Test public void testInvalidMessage() { JMSProducer producer = context.createProducer(); try { producer.send(queue1, (Message) null); Assert.fail("null msg"); } catch (MessageFormatRuntimeException expected) { // no-op } }
@Override public JMSProducer send(Destination destination, Message message) { if (message == null) { throw new MessageFormatRuntimeException("null message"); } try { if (jmsHeaderCorrelationID != null) { message.setJMSCorrelationID(jmsHeaderCorrelationID); } if (jmsHeaderCorrelationIDAsBytes != null && jmsHeaderCorrelationIDAsBytes.length > 0) { message.setJMSCorrelationIDAsBytes(jmsHeaderCorrelationIDAsBytes); } if (jmsHeaderReplyTo != null) { message.setJMSReplyTo(jmsHeaderReplyTo); } if (jmsHeaderType != null) { message.setJMSType(jmsHeaderType); } // XXX HORNETQ-1209 "JMS 2.0" can this be a foreign msg? // if so, then "SimpleString" properties will trigger an error. setProperties(message); if (completionListener != null) { CompletionListener wrapped = new CompletionListenerWrapper(completionListener); producer.send(destination, message, wrapped); } else { producer.send(destination, message); } } catch (JMSException e) { throw JmsExceptionUtils.convertToRuntimeException(e); } return this; }
@Override public JMSProducer send(Destination destination, Map<String, Object> body) { MapMessage message = context.createMapMessage(); if (body != null) { try { for (Entry<String, Object> entry : body.entrySet()) { final String name = entry.getKey(); final Object v = entry.getValue(); if (v instanceof String) { message.setString(name, (String) v); } else if (v instanceof Long) { message.setLong(name, (Long) v); } else if (v instanceof Double) { message.setDouble(name, (Double) v); } else if (v instanceof Integer) { message.setInt(name, (Integer) v); } else if (v instanceof Character) { message.setChar(name, (Character) v); } else if (v instanceof Short) { message.setShort(name, (Short) v); } else if (v instanceof Boolean) { message.setBoolean(name, (Boolean) v); } else if (v instanceof Float) { message.setFloat(name, (Float) v); } else if (v instanceof Byte) { message.setByte(name, (Byte) v); } else if (v instanceof byte[]) { byte[] array = (byte[]) v; message.setBytes(name, array, 0, array.length); } else { message.setObject(name, v); } } } catch (JMSException e) { throw new MessageFormatRuntimeException(e.getMessage()); } } send(destination, message); return this; }
@Override public JMSProducer send(Destination destination, byte[] body) { BytesMessage message = context.createBytesMessage(); if (body != null) { try { message.writeBytes(body); } catch (JMSException e) { throw new MessageFormatRuntimeException(e.getMessage()); } } send(destination, message); return this; }
@Override public JMSProducer setProperty(String name, Object value) { checkName(name); try { TypedProperties.setObjectProperty(new SimpleString(name), value, properties); } catch (ActiveMQPropertyConversionException amqe) { throw new MessageFormatRuntimeException(amqe.getMessage()); } catch (RuntimeException e) { throw new JMSRuntimeException(e.getMessage()); } return this; }
@Override public boolean getBooleanProperty(String name) { try { return properties.getBooleanProperty(new SimpleString(name)); } catch (ActiveMQPropertyConversionException ce) { throw new MessageFormatRuntimeException(ce.getMessage()); } catch (RuntimeException e) { throw new JMSRuntimeException(e.getMessage()); } }
@Override public byte getByteProperty(String name) { try { return properties.getByteProperty(new SimpleString(name)); } catch (ActiveMQPropertyConversionException ce) { throw new MessageFormatRuntimeException(ce.getMessage()); } }
@Override public short getShortProperty(String name) { try { return properties.getShortProperty(new SimpleString(name)); } catch (ActiveMQPropertyConversionException ce) { throw new MessageFormatRuntimeException(ce.getMessage()); } }
@Override public int getIntProperty(String name) { try { return properties.getIntProperty(new SimpleString(name)); } catch (ActiveMQPropertyConversionException ce) { throw new MessageFormatRuntimeException(ce.getMessage()); } }
@Override public long getLongProperty(String name) { try { return properties.getLongProperty(new SimpleString(name)); } catch (ActiveMQPropertyConversionException ce) { throw new MessageFormatRuntimeException(ce.getMessage()); } }
@Override public float getFloatProperty(String name) { try { return properties.getFloatProperty(new SimpleString(name)); } catch (ActiveMQPropertyConversionException ce) { throw new MessageFormatRuntimeException(ce.getMessage()); } }
@Override public double getDoubleProperty(String name) { try { return properties.getDoubleProperty(new SimpleString(name)); } catch (ActiveMQPropertyConversionException ce) { throw new MessageFormatRuntimeException(ce.getMessage()); } }
@Override public String getStringProperty(String name) { try { SimpleString prop = properties.getSimpleStringProperty(new SimpleString(name)); if (prop == null) return null; return prop.toString(); } catch (ActiveMQPropertyConversionException ce) { throw new MessageFormatRuntimeException(ce.getMessage()); } catch (RuntimeException e) { throw new JMSRuntimeException(e.getMessage()); } }
@Override public Object getObjectProperty(String name) { try { SimpleString key = new SimpleString(name); Object property = properties.getProperty(key); if (stringPropertyNames.contains(key)) { property = property.toString(); } return property; } catch (ActiveMQPropertyConversionException ce) { throw new MessageFormatRuntimeException(ce.getMessage()); } catch (RuntimeException e) { throw new JMSRuntimeException(e.getMessage()); } }
/** * Converts instances of sub-classes of {@link JMSException} into the corresponding sub-class of * {@link JMSRuntimeException}. * * @param e * @return */ public static JMSRuntimeException convertToRuntimeException(JMSException e) { if (e instanceof javax.jms.IllegalStateException) { return new IllegalStateRuntimeException(e.getMessage(), e.getErrorCode(), e); } if (e instanceof InvalidClientIDException) { return new InvalidClientIDRuntimeException(e.getMessage(), e.getErrorCode(), e); } if (e instanceof InvalidDestinationException) { return new InvalidDestinationRuntimeException(e.getMessage(), e.getErrorCode(), e); } if (e instanceof InvalidSelectorException) { return new InvalidSelectorRuntimeException(e.getMessage(), e.getErrorCode(), e); } if (e instanceof JMSSecurityException) { return new JMSSecurityRuntimeException(e.getMessage(), e.getErrorCode(), e); } if (e instanceof MessageFormatException) { return new MessageFormatRuntimeException(e.getMessage(), e.getErrorCode(), e); } if (e instanceof MessageNotWriteableException) { return new MessageNotWriteableRuntimeException(e.getMessage(), e.getErrorCode(), e); } if (e instanceof ResourceAllocationException) { return new ResourceAllocationRuntimeException(e.getMessage(), e.getErrorCode(), e); } if (e instanceof TransactionInProgressException) { return new TransactionInProgressRuntimeException(e.getMessage(), e.getErrorCode(), e); } if (e instanceof TransactionRolledBackException) { return new TransactionRolledBackRuntimeException(e.getMessage(), e.getErrorCode(), e); } return new JMSRuntimeException(e.getMessage(), e.getErrorCode(), e); }
public static <T> T castTo(Class<T> c, Message message) throws JMSException { if (message == null) { throw new MessageFormatRuntimeException("empty body"); } if (!isBodyAssignableTo(message, c)) { throw new MessageFormatRuntimeException("the message body cannot be assigned to the specified type " + c + " and the Message type " + StreamMessage.class + " cannot be used"); } if (message instanceof ObjectMessage) { return c.cast(((ObjectMessage) message).getObject()); } else if (message instanceof MapMessage) { return c.cast(resolveMapMessage((MapMessage) message)); } else if (message instanceof BytesMessage) { BytesMessage bytesMessage = (BytesMessage) message; bytesMessage.reset(); try { byte[] bytes = new byte[(int) bytesMessage.getBodyLength()]; for (int i = 0; i < bytes.length; i++) { bytes[i] = bytesMessage.readByte(); } return c.cast(bytes); } finally { bytesMessage.reset(); } } else if (message instanceof TextMessage) { return c.cast(((TextMessage) message).getText()); } else { throw new MessageFormatRuntimeException("internal error. unknown message type " + message.getClass() + " or empty body"); } }
@Test public void testSendNullMessageThrowsMFRE() throws JMSException { JmsSession session = Mockito.mock(JmsSession.class); JmsMessageProducer messageProducer = Mockito.mock(JmsMessageProducer.class); JmsProducer producer = new JmsProducer(session, messageProducer); try { producer.send(JMS_DESTINATION, (Message) null); fail("Should throw a MessageFormatRuntimeException"); } catch (MessageFormatRuntimeException mfre) { } catch (Exception e) { fail("Should throw a MessageFormatRuntimeException"); } }
public void doTestReceiveBodyFailsDoesNotAcceptMessage(int sessionMode) throws Exception { try (TestAmqpPeer testPeer = new TestAmqpPeer();) { JMSContext context = testFixture.createJMSContext(testPeer); testPeer.expectBegin(); final String content = "Message-Content"; Queue queue = context.createQueue("myQueue"); DescribedType amqpValueContent = new AmqpValueDescribedType(content); testPeer.expectReceiverAttach(); testPeer.expectLinkFlowRespondWithTransfer(null, null, null, null, amqpValueContent); testPeer.expectEnd(); testPeer.expectClose(); JMSConsumer messageConsumer = context.createConsumer(queue); try { messageConsumer.receiveBody(Boolean.class, 3000); fail("Should not read as Boolean type"); } catch (MessageFormatRuntimeException mfre) { } context.close(); testPeer.waitForAllHandlersToComplete(3000); } }
public static JMSRuntimeException toRuntimeException(final JMSException e) { if (e instanceof javax.jms.IllegalStateException) { return new IllegalStateRuntimeException(e.getMessage(), e.getErrorCode(), e); } if (e instanceof InvalidClientIDException) { return new InvalidClientIDRuntimeException(e.getMessage(), e.getErrorCode(), e); } if (e instanceof InvalidDestinationException) { return new InvalidDestinationRuntimeException(e.getMessage(), e.getErrorCode(), e); } if (e instanceof InvalidSelectorException) { return new InvalidSelectorRuntimeException(e.getMessage(), e.getErrorCode(), e); } if (e instanceof JMSSecurityException) { return new JMSSecurityRuntimeException(e.getMessage(), e.getErrorCode(), e); } if (e instanceof MessageFormatException) { return new MessageFormatRuntimeException(e.getMessage(), e.getErrorCode(), e); } if (e instanceof MessageNotWriteableException) { return new MessageNotWriteableRuntimeException(e.getMessage(), e.getErrorCode(), e); } if (e instanceof ResourceAllocationException) { return new ResourceAllocationRuntimeException(e.getMessage(), e.getErrorCode(), e); } if (e instanceof TransactionInProgressException) { return new TransactionInProgressRuntimeException(e.getMessage(), e.getErrorCode(), e); } if (e instanceof TransactionRolledBackException) { return new TransactionRolledBackRuntimeException(e.getMessage(), e.getErrorCode(), e); } return new JMSRuntimeException(e.getMessage(), e.getErrorCode(), e); }
private <T> T getProperty(final String key, final Class<T> type) { final Object val = properties.get(key); if (val == null || type.isInstance(val)) { return type.cast(val); } try { return type.cast(PropertyEditors.getValue(type, val.toString())); } catch (final PropertyEditorException pee) { throw new MessageFormatRuntimeException(pee.getMessage()); } }
@Override public JMSProducer send(final Destination destination, final Message message) { if (message == null) { throw new MessageFormatRuntimeException("null message"); } try { if (jmsHeaderCorrelationID != null) { message.setJMSCorrelationID(jmsHeaderCorrelationID); } if (jmsHeaderCorrelationIDAsBytes != null && jmsHeaderCorrelationIDAsBytes.length > 0) { message.setJMSCorrelationIDAsBytes(jmsHeaderCorrelationIDAsBytes); } if (jmsHeaderReplyTo != null) { message.setJMSReplyTo(jmsHeaderReplyTo); } if (jmsHeaderType != null) { message.setJMSType(jmsHeaderType); } setProperties(message); if (completionListener != null) { producer.send(destination, message, completionListener); } else { producer.send(destination, message); } } catch (final JMSException e) { throw toRuntimeException(e); } return this; }
@Override public JMSProducer send(final Destination destination, final Map<String, Object> body) { final MapMessage message = wrap(context.createMapMessage()); if (body != null) { try { for (final Map.Entry<String, Object> entry : body.entrySet()) { final String name = entry.getKey(); final Object v = entry.getValue(); if (v instanceof String) { message.setString(name, (String) v); } else if (v instanceof Long) { message.setLong(name, (Long) v); } else if (v instanceof Double) { message.setDouble(name, (Double) v); } else if (v instanceof Integer) { message.setInt(name, (Integer) v); } else if (v instanceof Character) { message.setChar(name, (Character) v); } else if (v instanceof Short) { message.setShort(name, (Short) v); } else if (v instanceof Boolean) { message.setBoolean(name, (Boolean) v); } else if (v instanceof Float) { message.setFloat(name, (Float) v); } else if (v instanceof Byte) { message.setByte(name, (Byte) v); } else if (v instanceof byte[]) { byte[] array = (byte[]) v; message.setBytes(name, array, 0, array.length); } else { message.setObject(name, v); } } } catch (final JMSException e) { throw new MessageFormatRuntimeException(e.getMessage()); } } send(destination, message); return this; }
@Override public JMSProducer send(final Destination destination, final byte[] body) { final BytesMessage message = wrap(context.createBytesMessage()); if (body != null) { try { message.writeBytes(body); } catch (final JMSException e) { throw new MessageFormatRuntimeException(e.getMessage()); } } send(destination, message); return this; }
@Override public JMSProducer setProperty(final String name, final Object value) { validName(name); if (value != null && !Boolean.class.isInstance(value) && !Byte.class.isInstance(value) && !Character.class.isInstance(value) && !Short.class.isInstance(value) && !Integer.class.isInstance(value) && !Long.class.isInstance(value) && !Float.class.isInstance(value) && !Double.class.isInstance(value) && !String.class.isInstance(value) && !byte[].class.isInstance(value)) { throw new MessageFormatRuntimeException("Unsupported type: " + value); } properties.put(name, value); return this; }
@Test(expected = MessageFormatRuntimeException.class) public void testConvertsMessageFormatExceptionToMessageFormatRuntimeException() { throw JMSExceptionSupport.createRuntimeException(new MessageFormatException("error")); }
public void doTestReceiveBodyFailsThenCalledWithCorrectType(int sessionMode) throws Exception { try (TestAmqpPeer testPeer = new TestAmqpPeer();) { JMSContext context = testFixture.createJMSContext(testPeer); testPeer.expectBegin(); final String content = "Message-Content"; Queue queue = context.createQueue("myQueue"); DescribedType amqpValueContent = new AmqpValueDescribedType(content); testPeer.expectReceiverAttach(); testPeer.expectLinkFlowRespondWithTransfer(null, null, null, null, amqpValueContent); JMSConsumer messageConsumer = context.createConsumer(queue); try { messageConsumer.receiveBody(Boolean.class, 3000); fail("Should not read as Boolean type"); } catch (MessageFormatRuntimeException mfre) { } testPeer.waitForAllHandlersToComplete(3000); if (sessionMode == JMSContext.AUTO_ACKNOWLEDGE || sessionMode == JMSContext.DUPS_OK_ACKNOWLEDGE) { testPeer.expectDispositionThatIsAcceptedAndSettled(); } String received = messageConsumer.receiveBody(String.class, 3000); if (sessionMode == JMSContext.AUTO_ACKNOWLEDGE || sessionMode == JMSContext.DUPS_OK_ACKNOWLEDGE) { assertNotNull(received); assertEquals(content, received); } else { assertNull(received); } testPeer.expectEnd(); testPeer.expectClose(); context.close(); testPeer.waitForAllHandlersToComplete(3000); } }
@Test(expected = MessageFormatRuntimeException.class) public void testConvertsMessageFormatExceptionToMessageFormatRuntimeException() { throw JmsExceptionSupport.createRuntimeException(new MessageFormatException("error")); }