### Question:
URLParser { public static Map<DomainParts, String> splitDomain(String domain) { Map<DomainParts, String> domainParts = null; String[] dottedParts = domain.trim().toLowerCase(Locale.ENGLISH).split("\\."); if(dottedParts.length==2) { domainParts = new HashMap<>(); domainParts.put(DomainParts.SUBDOMAIN, null); domainParts.put(DomainParts.DOMAINNAME, dottedParts[0]); domainParts.put(DomainParts.TLD, dottedParts[1]); } else if(dottedParts.length>2) { int n = dottedParts.length; if( COMMON_TLD.contains(dottedParts[n-2]+"."+dottedParts[n-1]) || (COMMON_TLD.contains(dottedParts[n-1]) && COMMON_SLD.contains(dottedParts[n-2])) || (COMMON_TLD.contains(dottedParts[n-2]) && COMMON_SLD.contains(dottedParts[n-1]))) { domainParts = new HashMap<>(); domainParts.put(DomainParts.TLD, dottedParts[n-2]+"."+dottedParts[n-1]); domainParts.put(DomainParts.DOMAINNAME, dottedParts[n-3]); StringBuilder sb = new StringBuilder(dottedParts[0]); for(int i=1;i<n-3;++i) { sb.append(".").append(dottedParts[i]); } domainParts.put(DomainParts.SUBDOMAIN, sb.toString()); } else if(COMMON_TLD.contains(dottedParts[n-1])) { domainParts = new HashMap<>(); domainParts.put(DomainParts.TLD, dottedParts[n-1]); domainParts.put(DomainParts.DOMAINNAME, dottedParts[n-2]); StringBuilder sb = new StringBuilder(dottedParts[0]); for(int i=1;i<n-2;++i) { sb.append(".").append(dottedParts[i]); } domainParts.put(DomainParts.SUBDOMAIN, sb.toString()); } } return domainParts; }  static URL toAbsolute(URL baseURL, String relative); static String toAbsolute(String base, String relative); static Map<URLParts, String> splitURL(String URLString); static Map<URLParts, String> splitURL(URL url); static String joinURL(Map<URLParts, String> urlParts); static Map<DomainParts, String> splitDomain(String domain);  }### Answer:
@Test public void testSplitDomain1() { logger.info("splitDomain1"); String domain = "www.cars.example.co.uk"; Map<URLParser.DomainParts, String> expResult = new HashMap<>(); expResult.put(URLParser.DomainParts.TLD, "co.uk"); expResult.put(URLParser.DomainParts.DOMAINNAME, "example"); expResult.put(URLParser.DomainParts.SUBDOMAIN, "www.cars"); Map<URLParser.DomainParts, String> result = URLParser.splitDomain(domain); assertEquals(expResult, result); }
@Test public void testSplitDomain2() { logger.info("splitDomain2"); String domain = "example.com"; Map<URLParser.DomainParts, String> expResult = new HashMap<>(); expResult.put(URLParser.DomainParts.TLD, "com"); expResult.put(URLParser.DomainParts.DOMAINNAME, "example"); expResult.put(URLParser.DomainParts.SUBDOMAIN, null); Map<URLParser.DomainParts, String> result = URLParser.splitDomain(domain); assertEquals(expResult, result); }
@Test public void testSplitDomain3() { logger.info("splitDomain3"); String domain = "www.example.com"; Map<URLParser.DomainParts, String> expResult = new HashMap<>(); expResult.put(URLParser.DomainParts.TLD, "com"); expResult.put(URLParser.DomainParts.DOMAINNAME, "example"); expResult.put(URLParser.DomainParts.SUBDOMAIN, "www"); Map<URLParser.DomainParts, String> result = URLParser.splitDomain(domain); assertEquals(expResult, result); }