@Override public JMSProducer createProducer() { if (connectionRefCount.get() == 0) { throw new IllegalStateRuntimeException("The Connection is closed"); } try { if (sharedProducer == null) { synchronized (this) { if (sharedProducer == null) { sharedProducer = (JmsPoolMessageProducer) getSession().createProducer(null); } } } return new JmsPoolJMSProducer(getSession(), sharedProducer); } catch (JMSException jmse) { throw JMSExceptionSupport.createRuntimeException(jmse); } }
@Test public void testStringBodyIsApplied() throws JMSException { JMSProducer producer = context.createProducer(); final String bodyValue = "String-Value"; final AtomicBoolean bodyValidated = new AtomicBoolean(); MockJMSConnection connection = (MockJMSConnection) context.getConnection(); connection.addConnectionListener(new MockJMSDefaultConnectionListener() { @Override public void onMessageSend(MockJMSSession session, Message message) throws JMSException { assertEquals(bodyValue, message.getBody(String.class)); bodyValidated.set(true); } }); producer.send(JMS_DESTINATION, bodyValue); assertTrue(bodyValidated.get()); }
@Test public void testMapBodyIsApplied() throws JMSException { JMSProducer producer = context.createProducer(); final Map<String, Object> bodyValue = new HashMap<String, Object>(); bodyValue.put("Value-1", "First"); bodyValue.put("Value-2", "Second"); final AtomicBoolean bodyValidated = new AtomicBoolean(); MockJMSConnection connection = (MockJMSConnection) context.getConnection(); connection.addConnectionListener(new MockJMSDefaultConnectionListener() { @Override public void onMessageSend(MockJMSSession session, Message message) throws JMSException { assertEquals(bodyValue, message.getBody(Map.class)); bodyValidated.set(true); } }); producer.send(JMS_DESTINATION, bodyValue); assertTrue(bodyValidated.get()); }
@Test public void testSerializableBodyIsApplied() throws JMSException { JMSProducer producer = context.createProducer(); final UUID bodyValue = UUID.randomUUID(); final AtomicBoolean bodyValidated = new AtomicBoolean(); MockJMSConnection connection = (MockJMSConnection) context.getConnection(); connection.addConnectionListener(new MockJMSDefaultConnectionListener() { @Override public void onMessageSend(MockJMSSession session, Message message) throws JMSException { assertEquals(bodyValue, message.getBody(UUID.class)); bodyValidated.set(true); } }); producer.send(JMS_DESTINATION, bodyValue); assertTrue(bodyValidated.get()); }
@Test public void testRuntimeExceptionFromSendByteBody() throws JMSException { JMSProducer producer = context.createProducer(); MockJMSConnection connection = (MockJMSConnection) context.getConnection(); connection.addConnectionListener(new MockJMSDefaultConnectionListener() { @Override public void onMessageSend(MockJMSSession session, Message message) throws JMSException { throw new IllegalStateException("Send Failed"); } }); try { producer.send(context.createTemporaryQueue(), new byte[0]); fail("Should have thrown an exception"); } catch (IllegalStateRuntimeException isre) {} }
@Test public void testRuntimeExceptionFromSendMapBody() throws JMSException { JMSProducer producer = context.createProducer(); MockJMSConnection connection = (MockJMSConnection) context.getConnection(); connection.addConnectionListener(new MockJMSDefaultConnectionListener() { @Override public void onMessageSend(MockJMSSession session, Message message) throws JMSException { throw new IllegalStateException("Send Failed"); } }); try { producer.send(context.createTemporaryQueue(), Collections.<String, Object>emptyMap()); fail("Should have thrown an exception"); } catch (IllegalStateRuntimeException isre) {} }
@Test public void testRuntimeExceptionFromSendStringBody() throws JMSException { JMSProducer producer = context.createProducer(); MockJMSConnection connection = (MockJMSConnection) context.getConnection(); connection.addConnectionListener(new MockJMSDefaultConnectionListener() { @Override public void onMessageSend(MockJMSSession session, Message message) throws JMSException { throw new IllegalStateException("Send Failed"); } }); try { producer.send(context.createTemporaryQueue(), "test"); fail("Should have thrown an exception"); } catch (IllegalStateRuntimeException isre) {} }
@Override public JMSProducer createProducer() { if (connectionRefCount.get() == 0) { throw new IllegalStateRuntimeException("The Connection is closed"); } try { if (sharedProducer == null) { synchronized (this) { if (sharedProducer == null) { sharedProducer = (MockJMSMessageProducer) getSession().createProducer(null); } } } return new MockJMSProducer(getSession(), sharedProducer); } catch (JMSException jmse) { throw JMSExceptionSupport.createRuntimeException(jmse); } }
@Override public JMSProducer createProducer() { try { if (sharedProducer == null) { synchronized (this) { if (sharedProducer == null) { sharedProducer = getSession().createProducer(null); } } } return new JMSProducerImpl(this, sharedProducer); } catch (JMSException jmse) { throw Utils.convertToRuntimeException(jmse); } }
@Override public JMSProducer send(Destination destination, byte[] body) { try { BytesMessage message = session.createBytesMessage(); message.writeBytes(body); doSend(destination, message); } catch (JMSException jmse) { throw JMSExceptionSupport.createRuntimeException(jmse); } return this; }
@Override public JMSProducer send(Destination destination, String body) { try { TextMessage message = session.createTextMessage(body); doSend(destination, message); } catch (JMSException jmse) { throw JMSExceptionSupport.createRuntimeException(jmse); } return this; }
@Override public JMSProducer send(Destination destination, Serializable body) { try { ObjectMessage message = context.createObjectMessage(); message.setObject(body); doSend(destination, message); } catch (JMSException jmse) { throw Utils.convertToRuntimeException(jmse); } return this; }
@Override public JMSProducer setDeliveryMode(int deliveryMode) { switch (deliveryMode) { case DeliveryMode.PERSISTENT: case DeliveryMode.NON_PERSISTENT: this.deliveryMode = deliveryMode; return this; default: throw new JMSRuntimeException(String.format("Invalid DeliveryMode specified: %d", deliveryMode)); } }
@Override public JMSProducer setPriority(int priority) { if (priority < 0 || priority > 9) { throw new JMSRuntimeException(String.format("Priority value given {%d} is out of range (0..9)", priority)); } this.priority = priority; return this; }
private JMSProducer setObjectProperty(String name, Object value) { try { checkPropertyNameIsValid(name, true); checkValidObject(value); messageProperties.put(name, value); return this; } catch (JMSException e) { throw JMSExceptionSupport.createRuntimeException(e); } }
@Test public void testGetPropertyNames() { JMSProducer producer = context.createProducer(); producer.setProperty("Property_1", "1"); producer.setProperty("Property_2", "2"); producer.setProperty("Property_3", "3"); assertEquals(3, producer.getPropertyNames().size()); assertTrue(producer.getPropertyNames().contains("Property_1")); assertTrue(producer.getPropertyNames().contains("Property_2")); assertTrue(producer.getPropertyNames().contains("Property_3")); }
@Test public void testPropertyExists() { JMSProducer producer = context.createProducer(); producer.setProperty("Property_1", "1"); producer.setProperty("Property_2", "2"); producer.setProperty("Property_3", "3"); assertEquals(3, producer.getPropertyNames().size()); assertTrue(producer.propertyExists("Property_1")); assertTrue(producer.propertyExists("Property_2")); assertTrue(producer.propertyExists("Property_3")); assertFalse(producer.propertyExists("Property_4")); }
@Test public void testJMSReplyTo() { JMSContext context = cf.createContext(); JMSProducer producer = context.createProducer(); producer.setJMSReplyTo(JMS_REPLY_TO); assertTrue(JMS_REPLY_TO.equals(producer.getJMSReplyTo())); }
@Test public void testJMSType() { JMSProducer producer = context.createProducer(); producer.setJMSType(JMS_TYPE_STRING); assertEquals(JMS_TYPE_STRING, producer.getJMSType()); }
@Override public JMSProducer send(Destination destination, Message message) { try { doSend(destination, message); } catch (JMSException jmse) { throw Utils.convertToRuntimeException(jmse); } return this; }
@Override public JMSProducer send(Destination destination, Map<String, Object> body) { try { MapMessage message = context.createMapMessage(); for (Map.Entry<String, Object> entry : body.entrySet()) { message.setObject(entry.getKey(), entry.getValue()); } doSend(destination, message); } catch (JMSException jmse) { throw Utils.convertToRuntimeException(jmse); } return this; }
@Test public void testSetStringPropetryWithBadPropetyName() { JMSProducer producer = context.createProducer(); try { producer.setProperty(BAD_PROPERTY_NAME, "X"); fail("Should not accept invalid property name"); } catch (IllegalArgumentException iae) {} }
@Test public void testSetBytePropetryWithBadPropetyName() { JMSProducer producer = context.createProducer(); try { producer.setProperty(BAD_PROPERTY_NAME, (byte) 1); fail("Should not accept invalid property name"); } catch (IllegalArgumentException iae) {} }
@Test public void testSetBooleanPropetryWithBadPropetyName() { JMSProducer producer = context.createProducer(); try { producer.setProperty(BAD_PROPERTY_NAME, true); fail("Should not accept invalid property name"); } catch (IllegalArgumentException iae) {} }
@Test public void testSetDoublePropetryWithBadPropetyName() { JMSProducer producer = context.createProducer(); try { producer.setProperty(BAD_PROPERTY_NAME, 100.0); fail("Should not accept invalid property name"); } catch (IllegalArgumentException iae) {} }
@Test public void testSetFloatPropetryWithBadPropetyName() { JMSProducer producer = context.createProducer(); try { producer.setProperty(BAD_PROPERTY_NAME, 100.0f); fail("Should not accept invalid property name"); } catch (IllegalArgumentException iae) {} }
@Test public void testSetShortPropetryWithBadPropetyName() { JMSProducer producer = context.createProducer(); try { producer.setProperty(BAD_PROPERTY_NAME, (short) 100); fail("Should not accept invalid property name"); } catch (IllegalArgumentException iae) {} }
@Test public void testSetIntPropetryWithBadPropetyName() { JMSProducer producer = context.createProducer(); try { producer.setProperty(BAD_PROPERTY_NAME, 100); fail("Should not accept invalid property name"); } catch (IllegalArgumentException iae) {} }
@Test public void testSetLongPropetryWithBadPropetyName() { JMSProducer producer = context.createProducer(); try { producer.setProperty(BAD_PROPERTY_NAME, 100l); fail("Should not accept invalid property name"); } catch (IllegalArgumentException iae) {} }
@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 testAsync() { JMSProducer producer = context.createProducer(); TestJmsCompletionListener listener = new TestJmsCompletionListener(); producer.setAsync(listener); assertEquals(listener, producer.getAsync()); }
@Test public void testDeliveryMode() { JMSProducer producer = context.createProducer(); producer.setDeliveryMode(DeliveryMode.PERSISTENT); assertEquals(DeliveryMode.PERSISTENT, producer.getDeliveryMode()); producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT); assertEquals(DeliveryMode.NON_PERSISTENT, producer.getDeliveryMode()); }
@Test public void testDeliveryDelay() { JMSProducer producer = context.createProducer(); assertEquals(0, producer.getDeliveryDelay()); try { producer.setDeliveryDelay(2000); fail("Pool JMSProducer can't modify shared session delay mode."); } catch (JMSRuntimeException jmsre) { } }
@Test public void testPriority() { JMSProducer producer = context.createProducer(); producer.setPriority(1); assertEquals(1, producer.getPriority()); producer.setPriority(4); assertEquals(4, producer.getPriority()); }
@Test public void testTimeToLive() { JMSProducer producer = context.createProducer(); producer.setTimeToLive(2000); assertEquals(2000, producer.getTimeToLive()); }
@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 void doTestSendAppliesDeliveryModeWithMessageBody(Class<?> bodyType) throws JMSException { JMSProducer producer = context.createProducer(); final AtomicBoolean nonPersistentMessage = new AtomicBoolean(); final AtomicBoolean persistentMessage = new AtomicBoolean(); MockJMSConnection connection = (MockJMSConnection) context.getConnection(); connection.addConnectionListener(new MockJMSDefaultConnectionListener() { @Override public void onMessageSend(MockJMSSession session, Message message) throws JMSException { if (!persistentMessage.get()) { assertEquals(DeliveryMode.PERSISTENT, message.getJMSDeliveryMode()); persistentMessage.set(true); } else { assertEquals(DeliveryMode.NON_PERSISTENT, message.getJMSDeliveryMode()); nonPersistentMessage.set(true); } } }); producer.setDeliveryMode(DeliveryMode.PERSISTENT); producer.send(JMS_DESTINATION, "text"); producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT); producer.send(JMS_DESTINATION, "text"); assertTrue(persistentMessage.get()); assertTrue(nonPersistentMessage.get()); }
private void doTestSendAppliesPriorityWithMessageBody(Class<?> bodyType) throws JMSException { JMSProducer producer = context.createProducer(); final AtomicBoolean lowPriority = new AtomicBoolean(); final AtomicBoolean highPriority = new AtomicBoolean(); MockJMSConnection connection = (MockJMSConnection) context.getConnection(); connection.addConnectionListener(new MockJMSDefaultConnectionListener() { @Override public void onMessageSend(MockJMSSession session, Message message) throws JMSException { if (!lowPriority.get()) { assertEquals(0, message.getJMSPriority()); lowPriority.set(true); } else { assertEquals(7, message.getJMSPriority()); highPriority.set(true); } } }); producer.setPriority(0); producer.send(JMS_DESTINATION, "text"); producer.setPriority(7); producer.send(JMS_DESTINATION, "text"); assertTrue(lowPriority.get()); assertTrue(highPriority.get()); }