KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > ra > ActiveMQActivationSpecTest


1 /**
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements. See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package org.apache.activemq.ra;
19
20 import java.beans.IntrospectionException JavaDoc;
21 import java.beans.PropertyDescriptor JavaDoc;
22 import java.util.Arrays JavaDoc;
23 import java.util.List JavaDoc;
24
25 import javax.jms.Queue JavaDoc;
26 import javax.jms.Session JavaDoc;
27 import javax.jms.Topic JavaDoc;
28 import javax.resource.spi.InvalidPropertyException JavaDoc;
29
30 import junit.framework.TestCase;
31
32 import org.apache.activemq.command.ActiveMQDestination;
33 import org.apache.activemq.ra.ActiveMQActivationSpec;
34
35 /**
36  * @version $Revision$
37  */

38 public class ActiveMQActivationSpecTest extends TestCase {
39
40     private static final String JavaDoc DESTINATION = "defaultQueue";
41     private static final String JavaDoc DESTINATION_TYPE = Queue JavaDoc.class.getName();
42
43     private ActiveMQActivationSpec activationSpec;
44     private PropertyDescriptor JavaDoc destinationProperty;
45     private PropertyDescriptor JavaDoc destinationTypeProperty;
46     private PropertyDescriptor JavaDoc acknowledgeModeProperty;
47     private PropertyDescriptor JavaDoc subscriptionDurabilityProperty;
48     private PropertyDescriptor JavaDoc clientIdProperty;
49     private PropertyDescriptor JavaDoc subscriptionNameProperty;
50     private static final String JavaDoc EMPTY_STRING = " ";
51
52     public ActiveMQActivationSpecTest(String JavaDoc name) {
53         super(name);
54     }
55
56     protected void setUp() throws Exception JavaDoc {
57         super.setUp();
58
59         activationSpec = new ActiveMQActivationSpec();
60         activationSpec.setDestination(DESTINATION);
61         activationSpec.setDestinationType(DESTINATION_TYPE);
62
63         destinationProperty = new PropertyDescriptor JavaDoc("destination", ActiveMQActivationSpec.class);
64         destinationTypeProperty = new PropertyDescriptor JavaDoc("destinationType", ActiveMQActivationSpec.class);
65         acknowledgeModeProperty = new PropertyDescriptor JavaDoc("acknowledgeMode", ActiveMQActivationSpec.class);
66         subscriptionDurabilityProperty = new PropertyDescriptor JavaDoc("subscriptionDurability", ActiveMQActivationSpec.class);
67         clientIdProperty = new PropertyDescriptor JavaDoc("clientId", ActiveMQActivationSpec.class);
68         subscriptionNameProperty = new PropertyDescriptor JavaDoc("subscriptionName", ActiveMQActivationSpec.class);
69     }
70
71     public void testDefaultContructionValidation() throws IntrospectionException JavaDoc {
72         PropertyDescriptor JavaDoc[] 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 JavaDoc[] expected = {destinationTypeProperty};
85         assertActivationSpecInvalid(expected);
86     }
87
88     public void testInvalidDestinationTypeFailure() {
89         activationSpec.setDestinationType("foobar");
90         PropertyDescriptor JavaDoc[] expected = {destinationTypeProperty};
91         assertActivationSpecInvalid(expected);
92     }
93
94     public void testQueueDestinationType() {
95         activationSpec.setDestinationType(Queue JavaDoc.class.getName());
96         assertActivationSpecValid();
97     }
98
99     public void testTopicDestinationType() {
100         activationSpec.setDestinationType(Topic JavaDoc.class.getName());
101         assertActivationSpecValid();
102     }
103
104     public void testSuccessfulCreateQueueDestination() {
105         activationSpec.setDestinationType(Queue JavaDoc.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 JavaDoc);
112     }
113
114     public void testSuccessfulCreateTopicDestination() {
115         activationSpec.setDestinationType(Topic JavaDoc.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 JavaDoc);
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 JavaDoc.class.getName());
133         activationSpec.setDestination(null);
134         ActiveMQDestination destination = activationSpec.createDestination();
135         assertNull("ActiveMQDestination should not have been created", destination);
136     }
137
138 //----------- acknowledgeMode tests
139
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 JavaDoc[] 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 JavaDoc[] 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 //----------- subscriptionDurability tests
177
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 JavaDoc[] expected = {subscriptionDurabilityProperty};
185         assertActivationSpecInvalid(expected);
186     }
187
188     public void testNullSubscriptionDurability() {
189         activationSpec.setSubscriptionDurability(null);
190         PropertyDescriptor JavaDoc[] expected = {subscriptionDurabilityProperty};
191         assertActivationSpecInvalid(expected);
192     }
193
194     public void testSettingNonDurableSubscriptionDurability() {
195         activationSpec.setSubscriptionDurability(ActiveMQActivationSpec.NON_DURABLE_SUBSCRIPTION);
196         assertActivationSpecValid();
197     }
198
199 //----------- durable subscriber tests
200
public void testValidDurableSubscriber() {
201         activationSpec.setDestinationType(Topic JavaDoc.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 JavaDoc.class.getName());
211         activationSpec.setSubscriptionDurability(ActiveMQActivationSpec.DURABLE_SUBSCRIPTION);
212         activationSpec.setClientId("foobar");
213         activationSpec.setSubscriptionName("foobar");
214         PropertyDescriptor JavaDoc[] expected = {subscriptionDurabilityProperty};
215         assertActivationSpecInvalid(expected);
216     }
217     
218     public void testDurableSubscriberNoClientIdNoSubscriptionNameFailure() {
219         activationSpec.setDestinationType(Topic JavaDoc.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 JavaDoc[] expected = {clientIdProperty, subscriptionNameProperty};
226         assertActivationSpecInvalid(expected);
227     }
228     
229     public void testDurableSubscriberEmptyClientIdEmptySubscriptionNameFailure() {
230         activationSpec.setDestinationType(Topic JavaDoc.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 JavaDoc[] 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 //----------- helper methods
266
private void assertActivationSpecValid() {
267         try {
268             activationSpec.validate();
269         } catch (InvalidPropertyException JavaDoc e) {
270             fail("InvalidPropertyException should not be thrown");
271         }
272     }
273     
274     private void assertActivationSpecInvalid(PropertyDescriptor JavaDoc[] expected) {
275         assertActivationSpecInvalid(activationSpec, expected);
276     }
277
278     private void assertActivationSpecInvalid(ActiveMQActivationSpec testActivationSpec, PropertyDescriptor JavaDoc[] expected) {
279         try {
280             testActivationSpec.validate();
281             fail("InvalidPropertyException should have been thrown");
282         } catch (InvalidPropertyException JavaDoc e) {
283             PropertyDescriptor JavaDoc[] actual = e.getInvalidPropertyDescriptors();
284             assertEquals(expected, actual);
285         }
286     }
287
288     private static void assertEquals(PropertyDescriptor JavaDoc[] expected, PropertyDescriptor JavaDoc[] actual) {
289         /*
290         * This is kind of ugly. I originally created two HashSets and did an assertEquals(set1, set2)
291         * but because of a bug in the PropertyDescriptor class, it incorrectly fails. The problem is that the
292         * PropertyDescriptor class implements the equals() method but not the hashCode() method and almost all
293         * of the java collection classes use hashCode() for testing equality. The one exception I found was
294         * the ArrayList class which uses equals() for testing equality. Since Arrays.asList(...) returns an
295         * ArrayList, I use it below. Yes, ugly ... I know.
296         *
297         * see http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4634390
298         */

299         assertNotNull("No PropertyDescriptors returned", expected);
300         assertEquals("PropertyDescriptor array size is incorrect ", expected.length, actual.length);
301         List JavaDoc expectedList = Arrays.asList(expected);
302         List JavaDoc 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