|
| 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) 2011 VMware, Inc. All rights reserved. |
| 15 | +// |
| 16 | +// |
| 17 | +package com.rabbitmq.client.test; |
| 18 | + |
| 19 | +import com.rabbitmq.client.ConnectionFactory; |
| 20 | + |
| 21 | +import java.security.KeyManagementException; |
| 22 | +import java.security.NoSuchAlgorithmException; |
| 23 | +import java.net.URISyntaxException; |
| 24 | + |
| 25 | +public class AmqpUriTest extends BrokerTestCase |
| 26 | +{ |
| 27 | + public void testUriParsing() |
| 28 | + throws URISyntaxException, NoSuchAlgorithmException, KeyManagementException |
| 29 | + { |
| 30 | + /* From the spec (subset of the tests) */ |
| 31 | + parseSuccess("amqp://user:pass@host:10000/vhost", |
| 32 | + "user", "pass", "host", 10000, "vhost"); |
| 33 | + parseSuccess("aMQps://user%61:%61pass@host:10000/v%2fhost", |
| 34 | + "usera", "apass", "host", 10000, "v/host"); |
| 35 | + parseSuccess("amqp://host", "guest", "guest", "host", 5672, "/"); |
| 36 | + parseSuccess("amqp:///vhost", |
| 37 | + "guest", "guest", "localhost", 5672, "vhost"); |
| 38 | + parseSuccess("amqp://host/", "guest", "guest", "host", 5672, ""); |
| 39 | + parseSuccess("amqp://host/%2f", "guest", "guest", "host", 5672, "/"); |
| 40 | + parseSuccess("amqp://[::1]", "guest", "guest", "[::1]", 5672, "/"); |
| 41 | + |
| 42 | + /* Various other success cases */ |
| 43 | + parseSuccess("amqp://host:100", "guest", "guest", "host", 100, "/"); |
| 44 | + parseSuccess("amqp://[::1]:100", "guest", "guest", "[::1]", 100, "/"); |
| 45 | + |
| 46 | + parseSuccess("amqp://host/blah", |
| 47 | + "guest", "guest", "host", 5672, "blah"); |
| 48 | + parseSuccess("amqp://host:100/blah", |
| 49 | + "guest", "guest", "host", 100, "blah"); |
| 50 | + parseSuccess("amqp://[::1]/blah", |
| 51 | + "guest", "guest", "[::1]", 5672, "blah"); |
| 52 | + parseSuccess("amqp://[::1]:100/blah", |
| 53 | + "guest", "guest", "[::1]", 100, "blah"); |
| 54 | + |
| 55 | + parseSuccess("amqp://user:pass@host", |
| 56 | + "user", "pass", "host", 5672, "/"); |
| 57 | + parseSuccess("amqp://user:pass@[::1]", |
| 58 | + "user", "pass", "[::1]", 5672, "/"); |
| 59 | + parseSuccess("amqp://user:pass@[::1]:100", |
| 60 | + "user", "pass", "[::1]", 100, "/"); |
| 61 | + |
| 62 | + /* Various failure cases */ |
| 63 | + parseFail("http://www.rabbitmq.com"); |
| 64 | + parseFail("amqp://foo[::1]"); |
| 65 | + parseFail("amqp://foo:[::1]"); |
| 66 | + parseFail("amqp://[::1]foo"); |
| 67 | + |
| 68 | + parseFail("amqp://foo%1"); |
| 69 | + parseFail("amqp://foo%1x"); |
| 70 | + parseFail("amqp://foo%xy"); |
| 71 | + } |
| 72 | + |
| 73 | + private void parseSuccess(String uri, String user, String password, |
| 74 | + String host, int port, String vhost) |
| 75 | + throws URISyntaxException, NoSuchAlgorithmException, KeyManagementException |
| 76 | + { |
| 77 | + ConnectionFactory cf = new ConnectionFactory(); |
| 78 | + cf.setUri(uri); |
| 79 | + |
| 80 | + assertEquals(user, cf.getUsername()); |
| 81 | + assertEquals(password, cf.getPassword()); |
| 82 | + assertEquals(host, cf.getHost()); |
| 83 | + assertEquals(port, cf.getPort()); |
| 84 | + assertEquals(vhost, cf.getVirtualHost()); |
| 85 | + } |
| 86 | + |
| 87 | + private void parseFail(String uri) { |
| 88 | + try { |
| 89 | + (new ConnectionFactory()).setUri(uri); |
| 90 | + fail("URI parse didn't fail: '" + uri + "'"); |
| 91 | + } catch (Exception e) { |
| 92 | + // whoosh! |
| 93 | + } |
| 94 | + } |
| 95 | +} |
0 commit comments