git-svn-id: https://svn.apache.org/repos/asf/activemq/trunk@1349527 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Timothy A. Bish 2012-06-12 21:09:37 +00:00
parent 4bba15a763
commit ba4a9ed841
2 changed files with 101 additions and 58 deletions

View File

@ -219,6 +219,37 @@ public class URISupport {
} }
} }
public static int indexOfParenthesisMatch(String str, int first) throws URISyntaxException {
int index = -1;
if (first < 0 || first > str.length()) {
throw new IllegalArgumentException("Invalid position for first parenthesis: " + first);
}
if (str.charAt(first) != '(') {
throw new IllegalArgumentException("character at indicated position is not a parenthesis");
}
int depth = 1;
char[] array = str.toCharArray();
for (index = first + 1; index < array.length; ++index) {
char current = array[index];
if (current == '(') {
depth++;
} else if (current == ')') {
if (--depth == 0) {
break;
}
}
}
if (depth != 0) {
throw new URISyntaxException(str, "URI did not contain a matching parenthesis.");
}
return index;
}
/** /**
* @param uri * @param uri
* @param rc * @param rc
@ -234,16 +265,19 @@ public class URISupport {
} }
int p; int p;
int intialParen = ssp.indexOf("("); int initialParen = ssp.indexOf("(");
if (intialParen == 0) { if (initialParen == 0) {
rc.host = ssp.substring(0, intialParen);
rc.host = ssp.substring(0, initialParen);
p = rc.host.indexOf("/"); p = rc.host.indexOf("/");
if (p >= 0) { if (p >= 0) {
rc.path = rc.host.substring(p); rc.path = rc.host.substring(p);
rc.host = rc.host.substring(0, p); rc.host = rc.host.substring(0, p);
} }
p = ssp.lastIndexOf(")");
componentString = ssp.substring(intialParen + 1, p); p = indexOfParenthesisMatch(ssp, initialParen);
componentString = ssp.substring(initialParen + 1, p);
params = ssp.substring(p + 1).trim(); params = ssp.substring(p + 1).trim();
} else { } else {
@ -364,6 +398,7 @@ public class URISupport {
.getPath(), bindAddr.getQuery(), bindAddr.getFragment()); .getPath(), bindAddr.getQuery(), bindAddr.getFragment());
} }
public static boolean checkParenthesis(String str) { public static boolean checkParenthesis(String str) {
boolean result = true; boolean result = true;
if (str != null) { if (str != null) {
@ -384,11 +419,4 @@ public class URISupport {
} }
return result; return result;
} }
public int indexOfParenthesisMatch(String str) {
int result = -1;
return result;
}
} }

View File

@ -22,22 +22,19 @@ import java.util.HashMap;
import java.util.Map; import java.util.Map;
import junit.framework.TestCase; import junit.framework.TestCase;
import org.apache.activemq.util.URISupport.CompositeData; import org.apache.activemq.util.URISupport.CompositeData;
/**
*
*
*/
public class URISupportTest extends TestCase { public class URISupportTest extends TestCase {
public void testEmptyCompositePath() throws Exception { public void testEmptyCompositePath() throws Exception {
CompositeData data = URISupport.parseComposite(new URI("broker:()/localhost?persistent=false")); CompositeData data = URISupport.parseComposite(new URI("broker:()/localhost?persistent=false"));
assertEquals(0, data.getComponents().length); assertEquals(0, data.getComponents().length);
} }
public void testCompositePath() throws Exception { public void testCompositePath() throws Exception {
CompositeData data = URISupport.parseComposite(new URI("test:(path)/path")); CompositeData data = URISupport.parseComposite(new URI("test:(path)/path"));
assertEquals("path", data.getPath()); assertEquals("path", data.getPath());
data = URISupport.parseComposite(new URI("test:path")); data = URISupport.parseComposite(new URI("test:path"));
assertNull(data.getPath()); assertNull(data.getPath());
} }
@ -48,61 +45,79 @@ public class URISupportTest extends TestCase {
} }
public void testComposite() throws Exception { public void testComposite() throws Exception {
CompositeData data = URISupport.parseComposite(new URI("test:(part1://host,part2://(sub1://part,sube2:part))")); URI uri = new URI("test:(part1://host,part2://(sub1://part,sube2:part))");
CompositeData data = URISupport.parseComposite(uri);
assertEquals(2, data.getComponents().length); assertEquals(2, data.getComponents().length);
} }
public void testEmptyCompositeWithParenthesisInParam() throws Exception {
URI uri = new URI("failover://()?updateURIsURL=file:/C:/Dir(1)/a.csv");
CompositeData data = URISupport.parseComposite(uri);
assertEquals(0, data.getComponents().length);
assertEquals(1, data.getParameters().size());
assertTrue(data.getParameters().containsKey("updateURIsURL"));
assertEquals("file:/C:/Dir(1)/a.csv", data.getParameters().get("updateURIsURL"));
}
public void testCompositeWithParenthesisInParam() throws Exception {
URI uri = new URI("failover://(test)?updateURIsURL=file:/C:/Dir(1)/a.csv");
CompositeData data = URISupport.parseComposite(uri);
assertEquals(1, data.getComponents().length);
assertEquals(1, data.getParameters().size());
assertTrue(data.getParameters().containsKey("updateURIsURL"));
assertEquals("file:/C:/Dir(1)/a.csv", data.getParameters().get("updateURIsURL"));
}
public void testCompositeWithComponentParam() throws Exception { public void testCompositeWithComponentParam() throws Exception {
CompositeData data = URISupport.parseComposite(new URI("test:(part1://host?part1=true)?outside=true")); CompositeData data = URISupport.parseComposite(new URI("test:(part1://host?part1=true)?outside=true"));
assertEquals(1, data.getComponents().length); assertEquals(1, data.getComponents().length);
assertEquals(1, data.getParameters().size()); assertEquals(1, data.getParameters().size());
Map part1Params = URISupport.parseParameters(data.getComponents()[0]); Map<String, String> part1Params = URISupport.parseParameters(data.getComponents()[0]);
assertEquals(1, part1Params.size()); assertEquals(1, part1Params.size());
assertTrue(part1Params.containsKey("part1")); assertTrue(part1Params.containsKey("part1"));
} }
public void testParsingURI() throws Exception { public void testParsingURI() throws Exception {
URI source = new URI("tcp://localhost:61626/foo/bar?cheese=Edam&x=123"); URI source = new URI("tcp://localhost:61626/foo/bar?cheese=Edam&x=123");
Map map = URISupport.parseParameters(source); Map<String, String> map = URISupport.parseParameters(source);
assertEquals("Size: " + map, 2, map.size()); assertEquals("Size: " + map, 2, map.size());
assertMapKey(map, "cheese", "Edam"); assertMapKey(map, "cheese", "Edam");
assertMapKey(map, "x", "123"); assertMapKey(map, "x", "123");
URI result = URISupport.removeQuery(source); URI result = URISupport.removeQuery(source);
assertEquals("result", new URI("tcp://localhost:61626/foo/bar"), result); assertEquals("result", new URI("tcp://localhost:61626/foo/bar"), result);
} }
protected void assertMapKey(Map map, String key, Object expected) { protected void assertMapKey(Map<String, String> map, String key, Object expected) {
assertEquals("Map key: " + key, map.get(key), expected); assertEquals("Map key: " + key, map.get(key), expected);
} }
public void testParsingCompositeURI() throws URISyntaxException { public void testParsingCompositeURI() throws URISyntaxException {
CompositeData data = URISupport.parseComposite(new URI("broker://(tcp://localhost:61616)?name=foo")); CompositeData data = URISupport.parseComposite(new URI("broker://(tcp://localhost:61616)?name=foo"));
assertEquals("one component", 1, data.getComponents().length); assertEquals("one component", 1, data.getComponents().length);
assertEquals("Size: " + data.getParameters(), 1, data.getParameters().size()); assertEquals("Size: " + data.getParameters(), 1, data.getParameters().size());
} }
public void testCheckParenthesis() throws Exception { public void testCheckParenthesis() throws Exception {
String str = "fred:(((ddd))"; String str = "fred:(((ddd))";
assertFalse(URISupport.checkParenthesis(str)); assertFalse(URISupport.checkParenthesis(str));
str += ")"; str += ")";
assertTrue(URISupport.checkParenthesis(str)); assertTrue(URISupport.checkParenthesis(str));
} }
public void testCreateWithQuery() throws Exception { public void testCreateWithQuery() throws Exception {
URI source = new URI("vm://localhost"); URI source = new URI("vm://localhost");
URI dest = URISupport.createURIWithQuery(source, "network=true&one=two"); URI dest = URISupport.createURIWithQuery(source, "network=true&one=two");
assertEquals("correct param count", 2, URISupport.parseParameters(dest).size()); assertEquals("correct param count", 2, URISupport.parseParameters(dest).size());
assertEquals("same uri, host", source.getHost(), dest.getHost()); assertEquals("same uri, host", source.getHost(), dest.getHost());
assertEquals("same uri, scheme", source.getScheme(), dest.getScheme()); assertEquals("same uri, scheme", source.getScheme(), dest.getScheme());
assertFalse("same uri, ssp", dest.getQuery().equals(source.getQuery())); assertFalse("same uri, ssp", dest.getQuery().equals(source.getQuery()));
} }
public void testParsingParams() throws Exception { public void testParsingParams() throws Exception {
URI uri = new URI("static:(http://localhost:61617?proxyHost=jo&proxyPort=90)?proxyHost=localhost&proxyPort=80"); URI uri = new URI("static:(http://localhost:61617?proxyHost=jo&proxyPort=90)?proxyHost=localhost&proxyPort=80");
Map<String,String>parameters = URISupport.parseParameters(uri); Map<String,String>parameters = URISupport.parseParameters(uri);
@ -113,27 +128,27 @@ public class URISupportTest extends TestCase {
uri = new URI("http://0.0.0.0:61616"); uri = new URI("http://0.0.0.0:61616");
parameters = URISupport.parseParameters(uri); parameters = URISupport.parseParameters(uri);
} }
public void testCompositeCreateURIWithQuery() throws Exception { public void testCompositeCreateURIWithQuery() throws Exception {
String queryString = "query=value"; String queryString = "query=value";
URI originalURI = new URI("outerscheme:(innerscheme:innerssp)"); URI originalURI = new URI("outerscheme:(innerscheme:innerssp)");
URI querylessURI = originalURI; URI querylessURI = originalURI;
assertEquals(querylessURI, URISupport.createURIWithQuery(originalURI, null)); assertEquals(querylessURI, URISupport.createURIWithQuery(originalURI, null));
assertEquals(querylessURI, URISupport.createURIWithQuery(originalURI, "")); assertEquals(querylessURI, URISupport.createURIWithQuery(originalURI, ""));
assertEquals(new URI(querylessURI + "?" + queryString), URISupport.createURIWithQuery(originalURI, queryString)); assertEquals(new URI(querylessURI + "?" + queryString), URISupport.createURIWithQuery(originalURI, queryString));
originalURI = new URI("outerscheme:(innerscheme:innerssp)?outerquery=0"); originalURI = new URI("outerscheme:(innerscheme:innerssp)?outerquery=0");
assertEquals(querylessURI, URISupport.createURIWithQuery(originalURI, null)); assertEquals(querylessURI, URISupport.createURIWithQuery(originalURI, null));
assertEquals(querylessURI, URISupport.createURIWithQuery(originalURI, "")); assertEquals(querylessURI, URISupport.createURIWithQuery(originalURI, ""));
assertEquals(new URI(querylessURI + "?" + queryString), URISupport.createURIWithQuery(originalURI, queryString)); assertEquals(new URI(querylessURI + "?" + queryString), URISupport.createURIWithQuery(originalURI, queryString));
originalURI = new URI("outerscheme:(innerscheme:innerssp?innerquery=0)"); originalURI = new URI("outerscheme:(innerscheme:innerssp?innerquery=0)");
querylessURI = originalURI; querylessURI = originalURI;
assertEquals(querylessURI, URISupport.createURIWithQuery(originalURI, null)); assertEquals(querylessURI, URISupport.createURIWithQuery(originalURI, null));
assertEquals(querylessURI, URISupport.createURIWithQuery(originalURI, "")); assertEquals(querylessURI, URISupport.createURIWithQuery(originalURI, ""));
assertEquals(new URI(querylessURI + "?" + queryString), URISupport.createURIWithQuery(originalURI, queryString)); assertEquals(new URI(querylessURI + "?" + queryString), URISupport.createURIWithQuery(originalURI, queryString));
originalURI = new URI("outerscheme:(innerscheme:innerssp?innerquery=0)?outerquery=0"); originalURI = new URI("outerscheme:(innerscheme:innerssp?innerquery=0)?outerquery=0");
assertEquals(querylessURI, URISupport.createURIWithQuery(originalURI, null)); assertEquals(querylessURI, URISupport.createURIWithQuery(originalURI, null));
assertEquals(querylessURI, URISupport.createURIWithQuery(originalURI, "")); assertEquals(querylessURI, URISupport.createURIWithQuery(originalURI, ""));
assertEquals(new URI(querylessURI + "?" + queryString), URISupport.createURIWithQuery(originalURI, queryString)); assertEquals(new URI(querylessURI + "?" + queryString), URISupport.createURIWithQuery(originalURI, queryString));
} }
public void testApplyParameters() throws Exception { public void testApplyParameters() throws Exception {
@ -157,10 +172,10 @@ public class URISupportTest extends TestCase {
uri = URISupport.applyParameters(uri, parameters, "t."); uri = URISupport.applyParameters(uri, parameters, "t.");
verifyParams(URISupport.parseParameters(uri)); verifyParams(URISupport.parseParameters(uri));
} }
private void verifyParams(Map<String,String> parameters) { private void verifyParams(Map<String,String> parameters) {
assertEquals(parameters.get("proxyHost"), "localhost"); assertEquals(parameters.get("proxyHost"), "localhost");
assertEquals(parameters.get("proxyPort"), "80"); assertEquals(parameters.get("proxyPort"), "80");
} }
} }