|
| 1 | +// The contents of this file are subject to the Mozilla Public License |
| 2 | +// Version 1.1 (the "License"); you may not use this file except in |
| 3 | +// compliance with the License. You may obtain a copy of the License |
| 4 | +// at http://www.mozilla.org/MPL/ |
| 5 | +// |
| 6 | +// Software distributed under the License is distributed on an "AS IS" |
| 7 | +// basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See |
| 8 | +// the License for the specific language governing rights and |
| 9 | +// limitations under the License. |
| 10 | +// |
| 11 | +// The Original Code is RabbitMQ. |
| 12 | +// |
| 13 | +// The Initial Developer of the Original Code is VMware, Inc. |
| 14 | +// Copyright (c) 2007-2011 VMware, Inc. All rights reserved. |
| 15 | +// |
| 16 | + |
| 17 | +package com.rabbitmq.client.test.functional; |
| 18 | + |
| 19 | +import com.rabbitmq.client.AMQP; |
| 20 | +import com.rabbitmq.client.AMQP.BasicProperties; |
| 21 | +import com.rabbitmq.client.GetResponse; |
| 22 | +import com.rabbitmq.client.test.BrokerTestCase; |
| 23 | + |
| 24 | +import java.io.IOException; |
| 25 | +import java.util.ArrayList; |
| 26 | +import java.util.HashMap; |
| 27 | +import java.util.Arrays; |
| 28 | +import java.util.List; |
| 29 | +import java.util.Map; |
| 30 | + |
| 31 | +public class CcRoutes extends BrokerTestCase { |
| 32 | + |
| 33 | + static private String[] queues = new String[]{"queue1", "queue2", "queue3"}; |
| 34 | + protected String exDirect = "direct_cc_exchange"; |
| 35 | + protected String exTopic = "topic_cc_exchange"; |
| 36 | + protected BasicProperties props; |
| 37 | + protected Map<String, Object> headers; |
| 38 | + protected List<String> ccList; |
| 39 | + protected List<String> bccList; |
| 40 | + |
| 41 | + @Override protected void setUp() throws IOException { |
| 42 | + super.setUp(); |
| 43 | + props = new BasicProperties(); |
| 44 | + headers = new HashMap<String, Object>(); |
| 45 | + ccList = new ArrayList<String>(); |
| 46 | + bccList = new ArrayList<String>(); |
| 47 | + } |
| 48 | + |
| 49 | + @Override protected void createResources() throws IOException { |
| 50 | + super.createResources(); |
| 51 | + for (String q : queues) { |
| 52 | + channel.queueDeclare(q, false, true, true, null); |
| 53 | + } |
| 54 | + channel.exchangeDeclare(exDirect, "direct", false, true, null); |
| 55 | + channel.exchangeDeclare(exTopic, "topic", false, true, null); |
| 56 | + } |
| 57 | + |
| 58 | + public void testCcList() throws IOException { |
| 59 | + ccList.add("queue2"); |
| 60 | + ccList.add("queue3"); |
| 61 | + headerPublish("", "queue1", ccList, null); |
| 62 | + expect(new String []{"queue1", "queue2", "queue3"}, true); |
| 63 | + } |
| 64 | + |
| 65 | + public void testCcIgnoreEmptyAndInvalidRoutes() throws IOException { |
| 66 | + bccList.add("frob"); |
| 67 | + headerPublish("", "queue1", ccList, bccList); |
| 68 | + expect(new String []{"queue1"}, true); |
| 69 | + } |
| 70 | + |
| 71 | + public void testBcc() throws IOException { |
| 72 | + bccList.add("queue2"); |
| 73 | + headerPublish("", "queue1", null, bccList); |
| 74 | + expect(new String []{"queue1", "queue2"}, false); |
| 75 | + } |
| 76 | + |
| 77 | + public void testNoDuplicates() throws IOException { |
| 78 | + ccList.add("queue1"); |
| 79 | + ccList.add("queue1"); |
| 80 | + bccList.add("queue1"); |
| 81 | + headerPublish("", "queue1", ccList, bccList); |
| 82 | + expect(new String[] {"queue1"}, true); |
| 83 | + } |
| 84 | + |
| 85 | + public void testDirectExchangeWithoutBindings() throws IOException { |
| 86 | + ccList.add("queue1"); |
| 87 | + headerPublish(exDirect, "queue2", ccList, null); |
| 88 | + expect(new String[] {}, true); |
| 89 | + } |
| 90 | + |
| 91 | + public void testTopicExchange() throws IOException { |
| 92 | + ccList.add("routing_key"); |
| 93 | + channel.queueBind("queue2", exTopic, "routing_key"); |
| 94 | + headerPublish(exTopic, "", ccList, null); |
| 95 | + expect(new String[] {"queue2"}, true); |
| 96 | + } |
| 97 | + |
| 98 | + public void testBoundExchanges() throws IOException { |
| 99 | + ccList.add("routing_key1"); |
| 100 | + bccList.add("routing_key2"); |
| 101 | + channel.exchangeBind(exTopic, exDirect, "routing_key1"); |
| 102 | + channel.queueBind("queue2", exTopic, "routing_key2"); |
| 103 | + headerPublish(exDirect, "", ccList, bccList); |
| 104 | + expect(new String[] {"queue2"}, true); |
| 105 | + } |
| 106 | + |
| 107 | + public void testNonArray() throws IOException { |
| 108 | + headers.put("CC", 0); |
| 109 | + props.setHeaders(headers); |
| 110 | + channel.basicPublish("", "queue1", props, new byte[0]); |
| 111 | + try { |
| 112 | + expect(new String[] {}, false); |
| 113 | + fail(); |
| 114 | + } catch (IOException e) { |
| 115 | + checkShutdownSignal(AMQP.PRECONDITION_FAILED, e); |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + private void headerPublish(String ex, String to, List<String> cc, List<String> bcc) throws IOException { |
| 120 | + if (cc != null) { |
| 121 | + headers.put("CC", ccList); |
| 122 | + } |
| 123 | + if (bcc != null) { |
| 124 | + headers.put("BCC", bccList); |
| 125 | + } |
| 126 | + props.setHeaders(headers); |
| 127 | + channel.basicPublish(ex, to, props, new byte[0]); |
| 128 | + } |
| 129 | + |
| 130 | + private void expect(String[] expectedQueues, boolean usedCc) throws IOException { |
| 131 | + GetResponse getResponse; |
| 132 | + List<String> expectedList = Arrays.asList(expectedQueues); |
| 133 | + for (String q : queues) { |
| 134 | + getResponse = basicGet(q); |
| 135 | + if (expectedList.contains(q)) { |
| 136 | + assertNotNull(getResponse); |
| 137 | + assertEquals(0, getResponse.getMessageCount()); |
| 138 | + Map headers = getResponse.getProps().getHeaders(); |
| 139 | + if (headers != null){ |
| 140 | + assertEquals(usedCc, headers.containsKey("CC")); |
| 141 | + assertFalse(headers.containsKey("BCC")); |
| 142 | + } |
| 143 | + } else { |
| 144 | + assertNull(getResponse); |
| 145 | + } |
| 146 | + } |
| 147 | + } |
| 148 | +} |
0 commit comments