| /** |
| * Licensed to the Apache Software Foundation (ASF) under one or more |
| * contributor license agreements. See the NOTICE file distributed with |
| * this work for additional information regarding copyright ownership. |
| * The ASF licenses this file to You under the Apache License, Version 2.0 |
| * (the "License"); you may not use this file except in compliance with |
| * the License. You may obtain a copy of the License at |
| * |
| * http://www.apache.org/licenses/LICENSE-2.0 |
| * |
| * Unless required by applicable law or agreed to in writing, software |
| * distributed under the License is distributed on an "AS IS" BASIS, |
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| * See the License for the specific language governing permissions and |
| * limitations under the License. |
| */ |
| package org.apache.camel.util; |
| |
| import java.net.URI; |
| import java.util.HashMap; |
| import java.util.LinkedHashMap; |
| import java.util.Map; |
| |
| import org.apache.camel.ContextTestSupport; |
| |
| /** |
| * @version |
| */ |
| public class URISupportTest extends ContextTestSupport { |
| |
| public void testNormalizeEndpointUri() throws Exception { |
| String out1 = URISupport.normalizeUri("smtp://localhost?username=davsclaus&password=secret"); |
| String out2 = URISupport.normalizeUri("smtp://localhost?password=secret&username=davsclaus"); |
| assertEquals(out1, out2); |
| |
| out1 = URISupport.normalizeUri("smtp://localhost?username=davsclaus&password=secret"); |
| out2 = URISupport.normalizeUri("smtp:localhost?password=secret&username=davsclaus"); |
| assertEquals(out1, out2); |
| |
| out1 = URISupport.normalizeUri("smtp:localhost?password=secret&username=davsclaus"); |
| out2 = URISupport.normalizeUri("smtp://localhost?username=davsclaus&password=secret"); |
| assertEquals(out1, out2); |
| |
| out1 = URISupport.normalizeUri("seda:foo?concurrentConsumer=2"); |
| out2 = URISupport.normalizeUri("seda:foo?concurrentConsumer=2"); |
| assertEquals(out1, out2); |
| |
| out1 = URISupport.normalizeUri("seda:foo?concurrentConsumer=2"); |
| out2 = URISupport.normalizeUri("seda:foo"); |
| assertNotSame(out1, out2); |
| } |
| |
| public void testNormalizeEndpointUriNoParam() throws Exception { |
| String out1 = URISupport.normalizeUri("direct:foo"); |
| String out2 = URISupport.normalizeUri("direct:foo"); |
| assertEquals(out1, out2); |
| |
| out1 = URISupport.normalizeUri("direct://foo"); |
| out2 = URISupport.normalizeUri("direct://foo"); |
| assertEquals(out1, out2); |
| |
| out1 = URISupport.normalizeUri("direct:foo"); |
| out2 = URISupport.normalizeUri("direct://foo"); |
| assertEquals(out1, out2); |
| |
| out1 = URISupport.normalizeUri("direct://foo"); |
| out2 = URISupport.normalizeUri("direct:foo"); |
| assertEquals(out1, out2); |
| |
| out1 = URISupport.normalizeUri("direct://foo"); |
| out2 = URISupport.normalizeUri("direct:bar"); |
| assertNotSame(out1, out2); |
| } |
| |
| public void testNormalizeEndpointUriWithFragments() throws Exception { |
| String out1 = URISupport.normalizeUri("irc://someserver/#camel?user=davsclaus"); |
| String out2 = URISupport.normalizeUri("irc:someserver/#camel?user=davsclaus"); |
| assertEquals(out1, out2); |
| |
| out1 = URISupport.normalizeUri("irc://someserver/#camel?user=davsclaus"); |
| out2 = URISupport.normalizeUri("irc:someserver/#camel?user=hadrian"); |
| assertNotSame(out1, out2); |
| } |
| |
| public void testNormalizeHttpEndpoint() throws Exception { |
| String out1 = URISupport.normalizeUri("http://www.google.com?q=Camel"); |
| String out2 = URISupport.normalizeUri("http:www.google.com?q=Camel"); |
| assertEquals(out1, out2); |
| assertTrue("Should have //", out1.startsWith("http://")); |
| assertTrue("Should have //", out2.startsWith("http://")); |
| } |
| |
| public void testNormalizeUriWhereParamererIsFaulty() throws Exception { |
| String out = URISupport.normalizeUri("stream:uri?file:///d:/temp/data/log/quickfix.log&scanStream=true"); |
| assertNotNull(out); |
| } |
| |
| public void testCreateRemaingURI() throws Exception { |
| URI original = new URI("http://camel.apache.org"); |
| Map<Object, Object> param = new HashMap<Object, Object>(); |
| param.put("foo", "123"); |
| URI newUri = URISupport.createRemainingURI(original, param); |
| assertNotNull(newUri); |
| |
| String s = newUri.toString(); |
| assertEquals("http://camel.apache.org?foo=123", s); |
| } |
| |
| public void testNormalizeEndpointWithEqualSignInParameter() throws Exception { |
| String out = URISupport.normalizeUri("jms:queue:foo?selector=somekey='somevalue'&foo=bar"); |
| assertNotNull(out); |
| // Camel will safe encode the URI |
| assertEquals("jms://queue:foo?foo=bar&selector=somekey%3D%27somevalue%27", out); |
| } |
| |
| public void testParseParameters() throws Exception { |
| URI u = new URI("quartz:myGroup/myTimerName?cron=0+0+*+*+*+?"); |
| Map<String, Object> params = URISupport.parseParameters(u); |
| assertEquals(1, params.size()); |
| assertEquals("0 0 * * * ?", params.get("cron")); |
| |
| u = new URI("quartz:myGroup/myTimerName?cron=0+0+*+*+*+?&bar=123"); |
| params = URISupport.parseParameters(u); |
| assertEquals(2, params.size()); |
| assertEquals("0 0 * * * ?", params.get("cron")); |
| assertEquals("123", params.get("bar")); |
| } |
| |
| public void testCreateRemainingURIEncoding() throws Exception { |
| // the uri is already encoded, but we create a new one with new query parameters |
| String uri = "http://localhost:23271/myapp/mytest?columns=name%2Ctotalsens%2Cupsens&username=apiuser"; |
| |
| // these are the parameters which is tricky to encode |
| Map<Object, Object> map = new LinkedHashMap<Object, Object>(); |
| map.put("foo", "abc def"); |
| map.put("bar", "123,456"); |
| map.put("name", "S\u00F8ren"); // danish letter |
| |
| // create new uri with the parameters |
| URI out = URISupport.createRemainingURI(new URI(uri), map); |
| assertNotNull(out); |
| assertEquals("http://localhost:23271/myapp/mytest?foo=abc+def&bar=123%2C456&name=S%C3%B8ren", out.toString()); |
| assertEquals("http://localhost:23271/myapp/mytest?foo=abc+def&bar=123%2C456&name=S%C3%B8ren", out.toASCIIString()); |
| } |
| |
| public void testNormalizeEndpointUriWithDualParameters() throws Exception { |
| String out1 = URISupport.normalizeUri("smtp://localhost?to=foo&to=bar&from=me"); |
| assertEquals("smtp://localhost?from=me&to=foo&to=bar", out1); |
| |
| String out2 = URISupport.normalizeUri("smtp://localhost?to=foo&to=bar&from=me&from=you"); |
| assertEquals("smtp://localhost?from=me&from=you&to=foo&to=bar", out2); |
| } |
| |
| } |