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  public class ActiveMQDestinationTest extends TestCase {
24      ActiveMQDestination topic1 = new ActiveMQTopic("FOO.BAR");
25      ActiveMQDestination topic2 = new ActiveMQTopic("FOO.XXX");
26      ActiveMQDestination topic3 = new ActiveMQTopic("FOO.BAR");
27      ActiveMQDestination topic4 = new ActiveMQTopic(null);
28  
29      ActiveMQDestination queue1 = new ActiveMQQueue("FOO.BAR");
30      ActiveMQDestination queue2 = new ActiveMQQueue("FOO.XXX");
31      ActiveMQDestination queue3 = new ActiveMQQueue("FOO.BAR");
32      ActiveMQDestination queue4 = new ActiveMQQueue(null);
33  
34  
35      public static void main(String[] args) {
36          TestRunner.run(ActiveMQDestinationTest.class);
37      }
38  
39      public void testCompareTopics() {
40          assertCompareTo(topic1, topic2, -1);
41          assertCompareTo(topic2, topic1, 1);
42          assertCompareTo(topic1, topic3, 0);
43          assertCompareTo(topic4, topic1, -1);
44          assertCompareTo(topic1, topic4, 1);
45      }
46  
47      public void testCompareQueues() {
48          assertCompareTo(queue1, queue2, -1);
49          assertCompareTo(queue2, queue1, 1);
50          assertCompareTo(queue1, queue3, 0);
51          assertCompareTo(queue4, queue1, -1);
52          assertCompareTo(queue1, queue4, 1);
53      }
54  
55      public void testCompareQueuesAndTopics() {
56          assertCompareTo(topic1, queue1, 1);
57          assertCompareTo(queue1, topic1, -1);
58          assertCompareTo(topic1, queue2, -1);
59          assertCompareTo(queue2, topic1, 1);
60      }
61  
62      protected void assertCompareTo(ActiveMQDestination dest1, ActiveMQDestination dest2, int expected) {
63          int value = dest1.compareTo(dest2);
64          String message = "Comparsing: " + dest1 + " to: " + dest2 + " found value: " + value + " expected: " + expected;
65          String hashCodeMessage = "HashCode for: " + dest1 + " is " + dest1.hashCode() + " for: " + dest2 + " is " + dest2.hashCode();
66  
67          // asserting different hash codes is a bit naughty as they could be equal on a different JVM maybe
68          switch (expected) {
69              case 0:
70                  assertEquals(message, 0, value);
71                  assertEquals(hashCodeMessage, dest1.hashCode(), dest2.hashCode());
72                  break;
73              case 1:
74                  assertTrue(message, value > 0);
75                  assertTrue(hashCodeMessage, dest1.hashCode() != dest2.hashCode());
76                  break;
77              default:
78                  assertTrue(message, value < 0);
79                  assertTrue(hashCodeMessage, dest1.hashCode() != dest2.hashCode());
80  
81          }
82      }
83      
84      public void testGetClientID(){
85          String clientID = "TestClientId";
86          String temp = ActiveMQDestination.createTemporaryName(clientID);
87          ActiveMQDestination tempDest = new ActiveMQTemporaryTopic(temp);
88          assertTrue(ActiveMQDestination.getClientId(tempDest).equals(clientID));
89      }
90      
91  }