ParseUtils { public static Map<String, String> parseQuery(String keyPrefix, String query) { if (query == null) return new HashMap<String, String>(); if (keyPrefix == null) keyPrefix = ""; Matcher matcher = QUERY_PATTERN.matcher(query); Map<String, String> routeQuery = new HashMap<String, String>(); String key = null; while (matcher.find()) { String separator = matcher.group(1); String content = matcher.group(2); if (separator == null || separator.length() == 0 || "&".equals(separator)) { if (key != null) throw new IllegalStateException("Illegal query string \"" + query + "\", The error char '" + separator + "' at index " + matcher.start() + " before \"" + content + "\"."); key = content; } else if ("=".equals(separator)) { if (key == null) throw new IllegalStateException("Illegal query string \"" + query + "\", The error char '" + separator + "' at index " + matcher.start() + " before \"" + content + "\"."); routeQuery.put(keyPrefix + key, content); key = null; } else { if (key == null) throw new IllegalStateException("Illegal query string \"" + query + "\", The error char '" + separator + "' at index " + matcher.start() + " before \"" + content + "\"."); } } return routeQuery; } private  ParseUtils(); static String interpolate(String expression, Map<String, String> params); static List<String> interpolate(List<String> expressions, Map<String, String> params); static boolean isMatchGlobPattern(String pattern, String value); static boolean isMatchGlobPatternsNeedInterpolate(
            Collection<String> patternsNeedInterpolate,
            Map<String, String> interpolateParams, String value); static Set<String> filterByGlobPattern(String pattern, Collection<String> values); static Set<String> filterByGlobPattern(Collection<String> patterns, Collection<String> values); static boolean hasIntersection(String glob1, String glob2); static Map<String, String> parseQuery(String keyPrefix, String query); static Map<String, String> parseQuery(String query); static String replaceParameter(String query, String key, String value); static String appendParamToUri(String uri, String name, String value); static String appendParamsToUri(String uri, Map<String, String> params); static boolean matchEndStarPattern(String value, String pattern); static String METHOD_SPLIT; }
@Test public void testParseQueryNull() throws Exception { assertEquals(0, ParseUtils.parseQuery(null, null).size()); assertEquals(0, ParseUtils.parseQuery(null, "").size()); assertEquals(0, ParseUtils.parseQuery("", null).size()); }
@Test public void testParseQueryEmpty() throws Exception { assertEquals(0, ParseUtils.parseQuery("", "").size()); }
@Test public void testParseQuerySingleKeyValue() throws Exception { assertEquals("1.0.0", ParseUtils.parseQuery("", "verion=1.0.0").get("verion")); }
@Test public void testParseQueryPrefixSingleKeyValue() throws Exception { assertEquals("1.0.0", ParseUtils.parseQuery("consumer.", "verion=1.0.0").get("consumer.verion")); }
@Test public void testParseQueryMultiKeyValue() throws Exception { Map<String, String> result = new HashMap<String, String>(); result.put("verion", "1.0.0"); result.put("cluster", "china"); assertEquals(result, ParseUtils.parseQuery("", "verion=1.0.0&cluster=china")); }
@Test public void testParseQueryPrefixMultiKeyValue() throws Exception { Map<String, String> result = new HashMap<String, String>(); result.put("consumer.verion", "1.0.0"); result.put("consumer.cluster", "china"); assertEquals(result, ParseUtils.parseQuery("consumer.", "verion=1.0.0&cluster=china")); }