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 rc
@ -234,16 +265,19 @@ public class URISupport {
}
int p;
int intialParen = ssp.indexOf("(");
if (intialParen == 0) {
rc.host = ssp.substring(0, intialParen);
int initialParen = ssp.indexOf("(");
if (initialParen == 0) {
rc.host = ssp.substring(0, initialParen);
p = rc.host.indexOf("/");
if (p >= 0) {
rc.path = rc.host.substring(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();
} else {
@ -364,6 +398,7 @@ public class URISupport {
.getPath(), bindAddr.getQuery(), bindAddr.getFragment());
}
public static boolean checkParenthesis(String str) {
boolean result = true;
if (str != null) {
@ -384,11 +419,4 @@ public class URISupport {
}
return result;
}
public int indexOfParenthesisMatch(String str) {
int result = -1;
return result;
}
}

View File

@ -22,12 +22,9 @@ import java.util.HashMap;
import java.util.Map;
import junit.framework.TestCase;
import org.apache.activemq.util.URISupport.CompositeData;
/**
*
*
*/
public class URISupportTest extends TestCase {
public void testEmptyCompositePath() throws Exception {
@ -48,16 +45,34 @@ public class URISupportTest extends TestCase {
}
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);
}
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 {
CompositeData data = URISupport.parseComposite(new URI("test:(part1://host?part1=true)?outside=true"));
assertEquals(1, data.getComponents().length);
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());
assertTrue(part1Params.containsKey("part1"));
}
@ -65,7 +80,7 @@ public class URISupportTest extends TestCase {
public void testParsingURI() throws Exception {
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());
assertMapKey(map, "cheese", "Edam");
@ -76,7 +91,7 @@ public class URISupportTest extends TestCase {
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);
}
@ -115,25 +130,25 @@ public class URISupportTest extends TestCase {
}
public void testCompositeCreateURIWithQuery() throws Exception {
String queryString = "query=value";
URI originalURI = new URI("outerscheme:(innerscheme:innerssp)");
URI querylessURI = originalURI;
assertEquals(querylessURI, URISupport.createURIWithQuery(originalURI, null));
assertEquals(querylessURI, URISupport.createURIWithQuery(originalURI, ""));
assertEquals(new URI(querylessURI + "?" + queryString), URISupport.createURIWithQuery(originalURI, queryString));
originalURI = new URI("outerscheme:(innerscheme:innerssp)?outerquery=0");
assertEquals(querylessURI, URISupport.createURIWithQuery(originalURI, null));
assertEquals(querylessURI, URISupport.createURIWithQuery(originalURI, ""));
assertEquals(new URI(querylessURI + "?" + queryString), URISupport.createURIWithQuery(originalURI, queryString));
originalURI = new URI("outerscheme:(innerscheme:innerssp?innerquery=0)");
querylessURI = originalURI;
assertEquals(querylessURI, URISupport.createURIWithQuery(originalURI, null));
assertEquals(querylessURI, URISupport.createURIWithQuery(originalURI, ""));
assertEquals(new URI(querylessURI + "?" + queryString), URISupport.createURIWithQuery(originalURI, queryString));
originalURI = new URI("outerscheme:(innerscheme:innerssp?innerquery=0)?outerquery=0");
assertEquals(querylessURI, URISupport.createURIWithQuery(originalURI, null));
assertEquals(querylessURI, URISupport.createURIWithQuery(originalURI, ""));
assertEquals(new URI(querylessURI + "?" + queryString), URISupport.createURIWithQuery(originalURI, queryString));
String queryString = "query=value";
URI originalURI = new URI("outerscheme:(innerscheme:innerssp)");
URI querylessURI = originalURI;
assertEquals(querylessURI, URISupport.createURIWithQuery(originalURI, null));
assertEquals(querylessURI, URISupport.createURIWithQuery(originalURI, ""));
assertEquals(new URI(querylessURI + "?" + queryString), URISupport.createURIWithQuery(originalURI, queryString));
originalURI = new URI("outerscheme:(innerscheme:innerssp)?outerquery=0");
assertEquals(querylessURI, URISupport.createURIWithQuery(originalURI, null));
assertEquals(querylessURI, URISupport.createURIWithQuery(originalURI, ""));
assertEquals(new URI(querylessURI + "?" + queryString), URISupport.createURIWithQuery(originalURI, queryString));
originalURI = new URI("outerscheme:(innerscheme:innerssp?innerquery=0)");
querylessURI = originalURI;
assertEquals(querylessURI, URISupport.createURIWithQuery(originalURI, null));
assertEquals(querylessURI, URISupport.createURIWithQuery(originalURI, ""));
assertEquals(new URI(querylessURI + "?" + queryString), URISupport.createURIWithQuery(originalURI, queryString));
originalURI = new URI("outerscheme:(innerscheme:innerssp?innerquery=0)?outerquery=0");
assertEquals(querylessURI, URISupport.createURIWithQuery(originalURI, null));
assertEquals(querylessURI, URISupport.createURIWithQuery(originalURI, ""));
assertEquals(new URI(querylessURI + "?" + queryString), URISupport.createURIWithQuery(originalURI, queryString));
}
public void testApplyParameters() throws Exception {