DeliveryImpl implements Delivery { @Override public void confirm() throws StateException { final String methodName = "confirm"; logger.entry(this, methodName); if (deliveryRequest == null) { if (qos == QOS.AT_MOST_ONCE) { throw new StateException("Confirming the receipt of delivery is applicable only when 'at least once' quality of service has been requested"); } else { throw new StateException("Subscription has autoConfirm option set to true"); } } else { if (confirmed) { throw new StateException("Delivery has already been confirmed"); } else if (!client.doDelivery(deliveryRequest)) { throw new StateException("Cannot confirm delivery because of either an interruption to the network " + "connection to the MQ Light server, or because the client is no longer subscribed to the " + "destination that the message was received from"); } else { confirmed = true; } } logger.exit(this, methodName); } protected  DeliveryImpl(NonBlockingClientImpl client, QOS qos, String share, String topic, String topicPattern, long ttl, Map<String, Object> properties, DeliveryRequest deliveryRequest); @Override abstract Type getType(); @Override void confirm(); @Override QOS getQOS(); @Override String getShare(); @Override String getTopic(); @Override String getTopicPattern(); @Override long getTtl(); @Override Map<String, Object> getProperties();  }
@Test public void confirmWhenAutoConfirm() { MockClient client = new MockClient(true); MockDelivery delivery = new MockDelivery(client, QOS.AT_LEAST_ONCE, null, "topic", "topic", 0, null, null); try { delivery.confirm(); fail("Expected StateException to be thrown"); } catch(StateException e) { } }
@Test public void confirmWhenQos0() { MockClient client = new MockClient(true); MockDelivery delivery = new MockDelivery(client, QOS.AT_MOST_ONCE, null, "topic", "topic", 0, null, null); try { delivery.confirm(); fail("Expected StateException to be thrown"); } catch(StateException e) { } }
@Test public void confirmSuccessful() { MockClient client = new MockClient(true); DeliveryRequest deliveryRequest = new DeliveryRequest(null, null, null, null, null); MockDelivery delivery = new MockDelivery(client, QOS.AT_LEAST_ONCE, null, "topic", "topic", 0, null, deliveryRequest); delivery.confirm(); }
@Test public void confirmNetworkLost() { MockClient client = new MockClient(false); DeliveryRequest deliveryRequest = new DeliveryRequest(null, null, null, null, null); MockDelivery delivery = new MockDelivery(client, QOS.AT_LEAST_ONCE, null, "topic", "topic", 0, null, deliveryRequest); try { delivery.confirm(); fail("Expected StateException to be thrown"); } catch(StateException e) { } }
@Test public void duplicateConfirm() { MockClient client = new MockClient(true); DeliveryRequest deliveryRequest = new DeliveryRequest(null, null, null, null, null); MockDelivery delivery = new MockDelivery(client, QOS.AT_LEAST_ONCE, null, "topic", "topic", 0, null, deliveryRequest); delivery.confirm(); try { delivery.confirm(); fail("Expected StateException to be thrown"); } catch(StateException e) { } }