@@ -46,8 +46,20 @@ public class AlternateExchange extends BrokerTestCase
4646 static private String [] resources = new String []{"x" ,"u" ,"v" };
4747 static private String [] keys = new String []{"x" ,"u" ,"v" ,"z" };
4848
49+ static private boolean unrouted [] = new boolean [] {false , false , false };
50+
4951 private AtomicBoolean gotReturn = new AtomicBoolean ();
5052
53+ /**
54+ * Determine which of the queues in our test configuration we
55+ * expect a message with routing key <code>key</code> to get
56+ * delivered to: the queue (if any) named <code>key</code>.
57+ *
58+ * @param key the routing key of the message
59+ * @return an array of booleans that when zipped with {@link
60+ * #resources} indicates whether the messages is expected to be
61+ * routed to the respective queue
62+ */
5163 private static boolean [] expected (String key ) {
5264 boolean [] expected = new boolean [resources .length ];
5365 for (int i = 0 ; i < resources .length ; i ++) {
@@ -83,11 +95,33 @@ public void handleBasicReturn(int replyCode,
8395 }
8496 }
8597
86- protected void setupRouting (String x , String ae ) throws IOException {
98+ /**
99+ * Declare an direct exchange <code>name</code> with an
100+ * alternate-exchange <code>ae</code> and bind the queue
101+ * <code>name</code> to it with a binding key of
102+ * <code>name</code>.
103+ *
104+ * @param name the name of the exchange to be created, and queue
105+ * to be bound
106+ * @param ae the name of the alternate-exchage
107+ */
108+ protected void setupRouting (String name , String ae ) throws IOException {
87109 Map <String , Object > args = new HashMap <String , Object >();
88110 if (ae != null ) args .put ("alternate-exchange" , ae );
89- channel .exchangeDeclare (x , "direct" , false , false , false , args );
90- channel .queueBind (x , x , x );
111+ channel .exchangeDeclare (name , "direct" , false , false , false , args );
112+ channel .queueBind (name , name , name );
113+ }
114+
115+ protected void setupRouting () throws IOException {
116+ setupRouting ("x" , "u" );
117+ setupRouting ("u" , "v" );
118+ setupRouting ("v" , "x" );
119+ }
120+
121+ protected void cleanup () throws IOException {
122+ for (String e : resources ) {
123+ channel .exchangeDelete (e );
124+ }
91125 }
92126
93127 protected void publish (String key , boolean mandatory , boolean immediate )
@@ -100,6 +134,15 @@ protected void publish(String key) throws IOException {
100134 publish (key , false , false );
101135 }
102136
137+ /**
138+ * Perform an auto-acking 'basic.get' on each of the queues named
139+ * in {@link #resources} and check whether a message can be
140+ * retrieved when expected.
141+ *
142+ * @param expected an array of booleans that is zipped with {@link
143+ * #resources} and indicates whether a messages is expected
144+ * to be retrievable from the respective queue
145+ */
103146 protected void checkGet (boolean [] expected ) throws IOException {
104147 for (int i = 0 ; i < resources .length ; i ++) {
105148 String q = resources [i ];
@@ -108,6 +151,24 @@ protected void checkGet(boolean[] expected) throws IOException {
108151 }
109152 }
110153
154+ /**
155+ * Test whether a message is routed as expected.
156+ *
157+ * We publish a message to exchange 'x' with a routing key of
158+ * <code>key</code>, check whether the message (actually, any
159+ * message) can be retrieved from the queues named in {@link
160+ * #resources} when expected, and whether a 'basic.return' is
161+ * received when expected.
162+ *
163+ * @param key the routing key of the message to be sent
164+ * @param mandatory whether the message should be marked as 'mandatory'
165+ * @param immediate whether the message should be marked as 'immediate'
166+ * @param expected indicates which queues we expect the message to
167+ * get routed to
168+ * @param ret whether a 'basic.return' is expected
169+ *
170+ * @see #checkGet(boolean[])
171+ */
111172 protected void check (String key , boolean mandatory , boolean immediate ,
112173 boolean [] expected , boolean ret )
113174 throws IOException {
@@ -132,13 +193,11 @@ protected void check(String key, boolean ret) throws IOException {
132193 check (key , false , false , ret );
133194 }
134195
135- public void testAe () throws IOException {
136-
137- //check various cases of missing AEs - we expect to see some
138- //warnings in the server logs
139-
140- boolean unrouted [] = new boolean [] {false , false , false };
141-
196+ /**
197+ * check various cases of missing AEs - we expect to see some
198+ * warnings in the server logs
199+ */
200+ public void testMissing () throws IOException {
142201 setupRouting ("x" , "u" );
143202 check ("x" , false ); //no warning
144203 check ("u" , unrouted , false ); //warning
@@ -151,7 +210,12 @@ public void testAe() throws IOException {
151210 check ("v" , false ); //no warning
152211 check ("z" , unrouted , false ); //no warning
153212
154- //routing with AEs in place
213+ cleanup ();
214+ }
215+
216+ public void testAe () throws IOException {
217+ setupRouting ();
218+
155219 for (String k : keys ) {
156220 //ordinary
157221 check (k , false );
@@ -174,23 +238,13 @@ public void testAe() throws IOException {
174238 checkGet (expected (k ));
175239 }
176240
177- //cleanup
178- for (String e : resources ) {
179- channel .exchangeDelete (e );
180- }
181-
241+ cleanup ();
182242 }
183243
184244 public void testCycleBreaking () throws IOException {
185- setupRouting ("x" , "u" );
186- setupRouting ("u" , "v" );
187- setupRouting ("v" , "x" );
188-
245+ setupRouting ();
189246 check ("z" , false );
190-
191- for (String e : resources ) {
192- channel .exchangeDelete (e );
193- }
247+ cleanup ();
194248 }
195249
196250}
0 commit comments