UrlMatcherImpl implements UrlMatcher { public Request match(String urlStr) { URL url; Map<String, String> queryMap; Request request = new Request(); String host; String urlStr4match = urlStr; try { url = new URL(urlStr4match); host = url.getPort() < 0 || url.getPort() == 80 || url.getPort() == 443 ? url.getHost() : url.getAuthority(); urlStr4match = host + url.getPath(); queryMap = splitQuery(url.getQuery()); } catch (MalformedURLException e) { Log.e("router", e.getLocalizedMessage()); return request; } request.setUrl(urlStr) .setHost(url.getHost()) .setScheme(url.getProtocol()) .setAnchor(url.getRef()) .setHost(host) .setQueryVariables(queryMap); searchCertainRoute(url, urlStr4match, request); if (request.getCompiledRoute() != null) { return request; } searchRegexRoute(url, urlStr4match, request); if (request.getCompiledRoute() != null) { return request; } return request; }  @Override void addCompiledRoute(CompiledRoute route); Request match(String urlStr); static Map<String, String> splitQuery(String query);  }
@Test public void testMatch() { UrlMatcherImpl urlMatcher = new UrlMatcherImpl(); CompiledRoute compiledRoute = new CompiledRoute() .setRegex(Pattern.compile("github\\.com/(?<vendor>[\\w\\d-]+)/(?<repository>[\\w\\d-]+)")) .setVariables(Arrays.asList("vendor", "repository")); urlMatcher.addCompiledRoute(compiledRoute); CompiledRoute compiledRoute1 = new CompiledRoute() .setConstantUrl("github.com/mr5"); urlMatcher.addCompiledRoute(compiledRoute1); CompiledRoute compiledRoute3 = new CompiledRoute() .setRegex(Pattern.compile("github\\.com/(\\w+)")); urlMatcher.addCompiledRoute(compiledRoute3); CompiledRoute compiledRoute4 = new CompiledRoute() .setRegex(Pattern.compile("github\\.com/(\\w+)")).setAnchor("comments"); urlMatcher.addCompiledRoute(compiledRoute4); CompiledRoute compiledRoute5 = new CompiledRoute(); compiledRoute5.setConstantUrl("github.com/mr5"); urlMatcher.addCompiledRoute(compiledRoute5); Request request = urlMatcher.match("https: assertEquals("-> assert specific route matched", compiledRoute, request.getCompiledRoute()); assertNull("-> assert no query", request.getQueryVariables()); assertNotNull("-> assert has path variables", request.getPathVariables()); assertEquals("-> assert path variable `vendor`=mr5", "mr5", request.getPathVariables().get("vendor")); assertEquals("-> assert path variable `repository`=android-router", "android-router", request.getPathVariables().get("repository")); Request request1 = urlMatcher.match("https: assertNotSame("-> assert specific route overrided", compiledRoute1, request1.getCompiledRoute()); assertNull("-> assert has no path variables", request1.getPathVariables()); Request request2 = urlMatcher.match("https: assertEquals("-> assert query `username`=xxxx", "xxxx", request2.getQueryVariables().get("username")); assertEquals("-> assert query `foo`=bar", "bar", request2.getQueryVariables().get("foo")); assertEquals("-> assert specific route with anchor matched", compiledRoute3, urlMatcher.match("https: assertEquals("-> assert specific route with anchor matched", compiledRoute4, urlMatcher.match("https: assertEquals("-> assert specific route with anchor and query matched", compiledRoute4, urlMatcher.match("https: CompiledRoute compiledRoute6 = new CompiledRoute() .setConstantUrl("github.com/mr5#wiki"); urlMatcher.addCompiledRoute(compiledRoute6); assertEquals("-> test override", compiledRoute6, urlMatcher.match("https: assertNull("-> test mismatched", urlMatcher.match("https: }