1   /***
2    *
3    * Copyright 2004 Protique Ltd
4    *
5    * Licensed under the Apache License, Version 2.0 (the "License");
6    * you may not use this file except in compliance with the License.
7    * You may obtain a copy of the License at
8    *
9    * http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   *
17   **/
18  package org.codehaus.activemq.message;
19  
20  import junit.framework.TestCase;
21  import junit.textui.TestRunner;
22  
23  import java.util.List;
24  
25  public class ActiveMQDestinationTest extends TestCase {
26      ActiveMQDestination topic1 = new ActiveMQTopic("FOO.BAR");
27      ActiveMQDestination topic2 = new ActiveMQTopic("FOO.XXX");
28      ActiveMQDestination topic3 = new ActiveMQTopic("FOO.BAR");
29      ActiveMQDestination topic4 = new ActiveMQTopic(null);
30  
31      ActiveMQDestination queue1 = new ActiveMQQueue("FOO.BAR");
32      ActiveMQDestination queue2 = new ActiveMQQueue("FOO.XXX");
33      ActiveMQDestination queue3 = new ActiveMQQueue("FOO.BAR");
34      ActiveMQDestination queue4 = new ActiveMQQueue(null);
35  
36  
37      public static void main(String[] args) {
38          TestRunner.run(ActiveMQDestinationTest.class);
39      }
40  
41      public void testCompareTopics() {
42          assertCompareTo(topic1, topic2, -1);
43          assertCompareTo(topic2, topic1, 1);
44          assertCompareTo(topic1, topic3, 0);
45          assertCompareTo(topic4, topic1, -1);
46          assertCompareTo(topic1, topic4, 1);
47      }
48  
49      public void testCompareQueues() {
50          assertCompareTo(queue1, queue2, -1);
51          assertCompareTo(queue2, queue1, 1);
52          assertCompareTo(queue1, queue3, 0);
53          assertCompareTo(queue4, queue1, -1);
54          assertCompareTo(queue1, queue4, 1);
55      }
56  
57      public void testCompareQueuesAndTopics() {
58          assertCompareTo(topic1, queue1, 1);
59          assertCompareTo(queue1, topic1, -1);
60          assertCompareTo(topic1, queue2, -1);
61          assertCompareTo(queue2, topic1, 1);
62      }
63  
64      public void testGetClientID() {
65          String clientID = "TestClientId";
66          String temp = ActiveMQDestination.createTemporaryName(clientID);
67          ActiveMQDestination tempDest = new ActiveMQTemporaryTopic(temp);
68          assertTrue(ActiveMQDestination.getClientId(tempDest).equals(clientID));
69      }
70  
71      public void testCompositeQueues() {
72          ActiveMQDestination destination = new ActiveMQQueue("FOO.BAR,COM.ACME.WHATNOT,FOO.>");
73          assertTrue(destination.isComposite());
74  
75          List destinations = destination.getChildDestinations();
76          assertEquals("Size is wrong", 3, destinations.size());
77  
78          assertEquals(new ActiveMQQueue("FOO.BAR"), destinations.get(0));
79          assertEquals(new ActiveMQQueue("COM.ACME.WHATNOT"), destinations.get(1));
80          assertEquals(new ActiveMQQueue("FOO.>"), destinations.get(2));
81  
82      }
83  
84      public void testCompositeQueueWithChildTopic() {
85          ActiveMQDestination destination = new ActiveMQQueue("FOO.BAR,topic://COM.ACME.WHATNOT,FOO.>");
86          assertTrue(destination.isComposite());
87  
88          List destinations = destination.getChildDestinations();
89          assertEquals("Size is wrong", 3, destinations.size());
90  
91          assertEquals(new ActiveMQQueue("FOO.BAR"), destinations.get(0));
92          assertEquals(new ActiveMQTopic("COM.ACME.WHATNOT"), destinations.get(1));
93          assertEquals(new ActiveMQQueue("FOO.>"), destinations.get(2));
94  
95      }
96  
97      public void testCompositeTopics() {
98          ActiveMQDestination destination = new ActiveMQTopic("FOO.BAR,COM.ACME.WHATNOT,FOO.>");
99          assertTrue(destination.isComposite());
100 
101         List destinations = destination.getChildDestinations();
102         assertEquals("Size is wrong", 3, destinations.size());
103 
104         assertEquals(new ActiveMQTopic("FOO.BAR"), destinations.get(0));
105         assertEquals(new ActiveMQTopic("COM.ACME.WHATNOT"), destinations.get(1));
106         assertEquals(new ActiveMQTopic("FOO.>"), destinations.get(2));
107 
108     }
109 
110     public void testCompositeTopicsWithChildQueue() {
111         ActiveMQDestination destination = new ActiveMQTopic("queue://FOO.BAR,COM.ACME.WHATNOT,queue://FOO.>");
112         assertTrue(destination.isComposite());
113 
114         List destinations = destination.getChildDestinations();
115         assertEquals("Size is wrong", 3, destinations.size());
116 
117         assertEquals(new ActiveMQQueue("FOO.BAR"), destinations.get(0));
118         assertEquals(new ActiveMQTopic("COM.ACME.WHATNOT"), destinations.get(1));
119         assertEquals(new ActiveMQQueue("FOO.>"), destinations.get(2));
120 
121     }
122 
123     public void testNormalDestinationsAreNotComposite() {
124         ActiveMQDestination destination = new ActiveMQQueue("FOO");
125         assertTrue(!destination.isComposite());
126 
127         destination = new ActiveMQTopic("FOO");
128         assertTrue(!destination.isComposite());
129 
130         System.out.println("Composite queues: " + destination.getChildDestinations());
131     }
132 
133     protected void assertCompareTo(ActiveMQDestination dest1, ActiveMQDestination dest2, int expected) {
134         int value = dest1.compareTo(dest2);
135         String message = "Comparsing: " + dest1 + " to: " + dest2 + " found value: " + value + " expected: " + expected;
136         String hashCodeMessage = "HashCode for: " + dest1 + " is " + dest1.hashCode() + " for: " + dest2 + " is " + dest2.hashCode();
137 
138         // asserting different hash codes is a bit naughty as they could be equal on a different JVM maybe
139         switch (expected) {
140             case 0:
141                 assertEquals(message, 0, value);
142                 assertEquals(hashCodeMessage, dest1.hashCode(), dest2.hashCode());
143                 break;
144             case 1:
145                 assertTrue(message, value > 0);
146                 assertTrue(hashCodeMessage, dest1.hashCode() != dest2.hashCode());
147                 break;
148             default:
149                 assertTrue(message, value < 0);
150                 assertTrue(hashCodeMessage, dest1.hashCode() != dest2.hashCode());
151 
152         }
153     }
154 
155 }