1 18 package org.apache.activemq.ra; 19 20 import java.beans.IntrospectionException ; 21 import java.beans.PropertyDescriptor ; 22 import java.util.Arrays ; 23 import java.util.List ; 24 25 import javax.jms.Queue ; 26 import javax.jms.Session ; 27 import javax.jms.Topic ; 28 import javax.resource.spi.InvalidPropertyException ; 29 30 import junit.framework.TestCase; 31 32 import org.apache.activemq.command.ActiveMQDestination; 33 import org.apache.activemq.ra.ActiveMQActivationSpec; 34 35 38 public class ActiveMQActivationSpecTest extends TestCase { 39 40 private static final String DESTINATION = "defaultQueue"; 41 private static final String DESTINATION_TYPE = Queue .class.getName(); 42 43 private ActiveMQActivationSpec activationSpec; 44 private PropertyDescriptor destinationProperty; 45 private PropertyDescriptor destinationTypeProperty; 46 private PropertyDescriptor acknowledgeModeProperty; 47 private PropertyDescriptor subscriptionDurabilityProperty; 48 private PropertyDescriptor clientIdProperty; 49 private PropertyDescriptor subscriptionNameProperty; 50 private static final String EMPTY_STRING = " "; 51 52 public ActiveMQActivationSpecTest(String name) { 53 super(name); 54 } 55 56 protected void setUp() throws Exception { 57 super.setUp(); 58 59 activationSpec = new ActiveMQActivationSpec(); 60 activationSpec.setDestination(DESTINATION); 61 activationSpec.setDestinationType(DESTINATION_TYPE); 62 63 destinationProperty = new PropertyDescriptor ("destination", ActiveMQActivationSpec.class); 64 destinationTypeProperty = new PropertyDescriptor ("destinationType", ActiveMQActivationSpec.class); 65 acknowledgeModeProperty = new PropertyDescriptor ("acknowledgeMode", ActiveMQActivationSpec.class); 66 subscriptionDurabilityProperty = new PropertyDescriptor ("subscriptionDurability", ActiveMQActivationSpec.class); 67 clientIdProperty = new PropertyDescriptor ("clientId", ActiveMQActivationSpec.class); 68 subscriptionNameProperty = new PropertyDescriptor ("subscriptionName", ActiveMQActivationSpec.class); 69 } 70 71 public void testDefaultContructionValidation() throws IntrospectionException { 72 PropertyDescriptor [] expected = {destinationTypeProperty, destinationProperty}; 73 assertActivationSpecInvalid(new ActiveMQActivationSpec(), expected); 74 } 75 76 public void testMinimalSettings() { 77 assertEquals(DESTINATION, activationSpec.getDestination()); 78 assertEquals(DESTINATION_TYPE, activationSpec.getDestinationType()); 79 assertActivationSpecValid(); 80 } 81 82 public void testNoDestinationTypeFailure() { 83 activationSpec.setDestinationType(null); 84 PropertyDescriptor [] expected = {destinationTypeProperty}; 85 assertActivationSpecInvalid(expected); 86 } 87 88 public void testInvalidDestinationTypeFailure() { 89 activationSpec.setDestinationType("foobar"); 90 PropertyDescriptor [] expected = {destinationTypeProperty}; 91 assertActivationSpecInvalid(expected); 92 } 93 94 public void testQueueDestinationType() { 95 activationSpec.setDestinationType(Queue .class.getName()); 96 assertActivationSpecValid(); 97 } 98 99 public void testTopicDestinationType() { 100 activationSpec.setDestinationType(Topic .class.getName()); 101 assertActivationSpecValid(); 102 } 103 104 public void testSuccessfulCreateQueueDestination() { 105 activationSpec.setDestinationType(Queue .class.getName()); 106 activationSpec.setDestination(DESTINATION); 107 assertActivationSpecValid(); 108 ActiveMQDestination destination = activationSpec.createDestination(); 109 assertNotNull("ActiveMQDestination not created", destination); 110 assertEquals("Physical name not the same", activationSpec.getDestination(), destination.getPhysicalName()); 111 assertTrue("Destination is not a Queue", destination instanceof Queue ); 112 } 113 114 public void testSuccessfulCreateTopicDestination() { 115 activationSpec.setDestinationType(Topic .class.getName()); 116 activationSpec.setDestination(DESTINATION); 117 assertActivationSpecValid(); 118 ActiveMQDestination destination = activationSpec.createDestination(); 119 assertNotNull("ActiveMQDestination not created", destination); 120 assertEquals("Physical name not the same", activationSpec.getDestination(), destination.getPhysicalName()); 121 assertTrue("Destination is not a Topic", destination instanceof Topic ); 122 } 123 124 public void testCreateDestinationIncorrectType() { 125 activationSpec.setDestinationType(null); 126 activationSpec.setDestination(DESTINATION); 127 ActiveMQDestination destination = activationSpec.createDestination(); 128 assertNull("ActiveMQDestination should not have been created", destination); 129 } 130 131 public void testCreateDestinationIncorrectDestinationName() { 132 activationSpec.setDestinationType(Topic .class.getName()); 133 activationSpec.setDestination(null); 134 ActiveMQDestination destination = activationSpec.createDestination(); 135 assertNull("ActiveMQDestination should not have been created", destination); 136 } 137 138 public void testDefaultAcknowledgeModeSetCorrectly() { 140 assertEquals("Incorrect default value", ActiveMQActivationSpec.AUTO_ACKNOWLEDGE_MODE, 141 activationSpec.getAcknowledgeMode()); 142 assertEquals("Incorrect default value", Session.AUTO_ACKNOWLEDGE, 143 activationSpec.getAcknowledgeModeForSession()); 144 } 145 146 public void testInvalidAcknowledgeMode() { 147 activationSpec.setAcknowledgeMode("foobar"); 148 PropertyDescriptor [] expected = {acknowledgeModeProperty}; 149 assertActivationSpecInvalid(expected); 150 assertEquals("Incorrect acknowledge mode", ActiveMQActivationSpec.INVALID_ACKNOWLEDGE_MODE, 151 activationSpec.getAcknowledgeModeForSession()); 152 } 153 154 public void testNoAcknowledgeMode() { 155 activationSpec.setAcknowledgeMode(null); 156 PropertyDescriptor [] expected = {acknowledgeModeProperty}; 157 assertActivationSpecInvalid(expected); 158 assertEquals("Incorrect acknowledge mode", ActiveMQActivationSpec.INVALID_ACKNOWLEDGE_MODE, 159 activationSpec.getAcknowledgeModeForSession()); 160 } 161 162 public void testSettingAutoAcknowledgeMode() { 163 activationSpec.setAcknowledgeMode(ActiveMQActivationSpec.AUTO_ACKNOWLEDGE_MODE); 164 assertActivationSpecValid(); 165 assertEquals("Incorrect acknowledge mode", Session.AUTO_ACKNOWLEDGE, 166 activationSpec.getAcknowledgeModeForSession()); 167 } 168 169 public void testSettingDupsOkAcknowledgeMode() { 170 activationSpec.setAcknowledgeMode(ActiveMQActivationSpec.DUPS_OK_ACKNOWLEDGE_MODE); 171 assertActivationSpecValid(); 172 assertEquals("Incorrect acknowledge mode", Session.DUPS_OK_ACKNOWLEDGE, 173 activationSpec.getAcknowledgeModeForSession()); 174 } 175 176 public void testDefaultSubscriptionDurabilitySetCorrectly() { 178 assertEquals("Incorrect default value", ActiveMQActivationSpec.NON_DURABLE_SUBSCRIPTION, 179 activationSpec.getSubscriptionDurability()); 180 } 181 182 public void testInvalidSubscriptionDurability() { 183 activationSpec.setSubscriptionDurability("foobar"); 184 PropertyDescriptor [] expected = {subscriptionDurabilityProperty}; 185 assertActivationSpecInvalid(expected); 186 } 187 188 public void testNullSubscriptionDurability() { 189 activationSpec.setSubscriptionDurability(null); 190 PropertyDescriptor [] expected = {subscriptionDurabilityProperty}; 191 assertActivationSpecInvalid(expected); 192 } 193 194 public void testSettingNonDurableSubscriptionDurability() { 195 activationSpec.setSubscriptionDurability(ActiveMQActivationSpec.NON_DURABLE_SUBSCRIPTION); 196 assertActivationSpecValid(); 197 } 198 199 public void testValidDurableSubscriber() { 201 activationSpec.setDestinationType(Topic .class.getName()); 202 activationSpec.setSubscriptionDurability(ActiveMQActivationSpec.DURABLE_SUBSCRIPTION); 203 activationSpec.setClientId("foobar"); 204 activationSpec.setSubscriptionName("foobar"); 205 assertActivationSpecValid(); 206 assertTrue(activationSpec.isDurableSubscription()); 207 } 208 209 public void testDurableSubscriberWithQueueDestinationTypeFailure() { 210 activationSpec.setDestinationType(Queue .class.getName()); 211 activationSpec.setSubscriptionDurability(ActiveMQActivationSpec.DURABLE_SUBSCRIPTION); 212 activationSpec.setClientId("foobar"); 213 activationSpec.setSubscriptionName("foobar"); 214 PropertyDescriptor [] expected = {subscriptionDurabilityProperty}; 215 assertActivationSpecInvalid(expected); 216 } 217 218 public void testDurableSubscriberNoClientIdNoSubscriptionNameFailure() { 219 activationSpec.setDestinationType(Topic .class.getName()); 220 activationSpec.setSubscriptionDurability(ActiveMQActivationSpec.DURABLE_SUBSCRIPTION); 221 activationSpec.setClientId(null); 222 assertNull(activationSpec.getClientId()); 223 activationSpec.setSubscriptionName(null); 224 assertNull(activationSpec.getSubscriptionName()); 225 PropertyDescriptor [] expected = {clientIdProperty, subscriptionNameProperty}; 226 assertActivationSpecInvalid(expected); 227 } 228 229 public void testDurableSubscriberEmptyClientIdEmptySubscriptionNameFailure() { 230 activationSpec.setDestinationType(Topic .class.getName()); 231 activationSpec.setSubscriptionDurability(ActiveMQActivationSpec.DURABLE_SUBSCRIPTION); 232 activationSpec.setClientId(EMPTY_STRING); 233 assertNull(activationSpec.getClientId()); 234 activationSpec.setSubscriptionName(EMPTY_STRING); 235 assertNull(activationSpec.getSubscriptionName()); 236 PropertyDescriptor [] expected = {clientIdProperty, subscriptionNameProperty}; 237 assertActivationSpecInvalid(expected); 238 } 239 240 public void testSetEmptyStringButGetNullValue() { 241 ActiveMQActivationSpec activationSpec = new ActiveMQActivationSpec(); 242 243 activationSpec.setDestinationType(EMPTY_STRING); 244 assertNull("Property not null", activationSpec.getDestinationType()); 245 246 activationSpec.setMessageSelector(EMPTY_STRING); 247 assertNull("Property not null", activationSpec.getMessageSelector()); 248 249 activationSpec.setDestination(EMPTY_STRING); 250 assertNull("Property not null", activationSpec.getDestination()); 251 252 activationSpec.setUserName(EMPTY_STRING); 253 assertNull("Property not null", activationSpec.getUserName()); 254 255 activationSpec.setPassword(EMPTY_STRING); 256 assertNull("Property not null", activationSpec.getPassword()); 257 258 activationSpec.setClientId(EMPTY_STRING); 259 assertNull("Property not null", activationSpec.getClientId()); 260 261 activationSpec.setSubscriptionName(EMPTY_STRING); 262 assertNull("Property not null", activationSpec.getSubscriptionName()); 263 } 264 265 private void assertActivationSpecValid() { 267 try { 268 activationSpec.validate(); 269 } catch (InvalidPropertyException e) { 270 fail("InvalidPropertyException should not be thrown"); 271 } 272 } 273 274 private void assertActivationSpecInvalid(PropertyDescriptor [] expected) { 275 assertActivationSpecInvalid(activationSpec, expected); 276 } 277 278 private void assertActivationSpecInvalid(ActiveMQActivationSpec testActivationSpec, PropertyDescriptor [] expected) { 279 try { 280 testActivationSpec.validate(); 281 fail("InvalidPropertyException should have been thrown"); 282 } catch (InvalidPropertyException e) { 283 PropertyDescriptor [] actual = e.getInvalidPropertyDescriptors(); 284 assertEquals(expected, actual); 285 } 286 } 287 288 private static void assertEquals(PropertyDescriptor [] expected, PropertyDescriptor [] actual) { 289 299 assertNotNull("No PropertyDescriptors returned", expected); 300 assertEquals("PropertyDescriptor array size is incorrect ", expected.length, actual.length); 301 List expectedList = Arrays.asList(expected); 302 List actualList = Arrays.asList(actual); 303 assertTrue("Incorrect PropertyDescriptors returned", expectedList.containsAll(actualList)); 304 } 305 306 public void testSelfEquality() { 307 assertEquality(activationSpec, activationSpec); 308 } 309 310 public void testSamePropertiesButNotEqual() { 311 assertNonEquality(new ActiveMQActivationSpec(), new ActiveMQActivationSpec()); 312 } 313 314 private void assertEquality(ActiveMQActivationSpec leftSpec, ActiveMQActivationSpec rightSpec) { 315 assertTrue("ActiveMQActivationSpecs are not equal", leftSpec.equals(rightSpec)); 316 assertTrue("ActiveMQActivationSpecs are not equal", rightSpec.equals(leftSpec)); 317 assertTrue("HashCodes are not equal", leftSpec.hashCode() == rightSpec.hashCode()); 318 } 319 320 private void assertNonEquality(ActiveMQActivationSpec leftSpec, ActiveMQActivationSpec rightSpec) { 321 assertFalse("ActiveMQActivationSpecs are equal", leftSpec.equals(rightSpec)); 322 assertFalse("ActiveMQActivationSpecs are equal", rightSpec.equals(leftSpec)); 323 assertFalse("HashCodes are equal", leftSpec.hashCode() == rightSpec.hashCode()); 324 } 325 326 } 327 | Popular Tags |