### Question:
RegexUtil { public static String globToRegex(String line) { line = line.trim(); int strLen = line.length(); StringBuilder sb = new StringBuilder(strLen); boolean escaping = false; int inCurlies = 0; for (char currentChar : line.toCharArray()) { switch (currentChar) { case '*': if (escaping) sb.append("\\*"); else sb.append(".*"); escaping = false; break; case '?': if (escaping) sb.append("\\?"); else sb.append('.'); escaping = false; break; case '.': case '(': case ')': case '+': case '|': case '^': case '$': case '@': case '%': sb.append('\\'); sb.append(currentChar); escaping = false; break; case '\\': if (escaping) { sb.append("\\\\"); escaping = false; } else escaping = true; break; case '{': if (escaping) { sb.append("\\{"); } else { sb.append('('); inCurlies++; } escaping = false; break; case '}': if (inCurlies > 0 && !escaping) { sb.append(')'); inCurlies--; } else if (escaping) sb.append("\\}"); else sb.append("}"); escaping = false; break; case ',': if (inCurlies > 0 && !escaping) sb.append('|'); else if (escaping) sb.append("\\,"); else sb.append(","); break; default: escaping = false; sb.append(currentChar); } } return sb.toString(); }  static String globToRegex(String line);  }### Answer:
@Test public void testStarWithExtensionDot() { String[] files = new String[]{"a.txt", "b.txt", "cc.txt"}; String glob = "*.txt"; String regex = RegexUtil.globToRegex(glob); Pattern p = Pattern.compile(regex); for (String file : files) { Matcher m = p.matcher(file); assertTrue(file + " matches glob: " + glob, m.matches()); } }
@Test public void testStarWithoutExtensionDot() { String[] files = new String[]{"a.txt", "b.txt", "cc.txt"}; String glob = "*txt"; String regex = RegexUtil.globToRegex(glob); Pattern p = Pattern.compile(regex); for (String file : files) { Matcher m = p.matcher(file); assertTrue(file + " matches glob: " + glob, m.matches()); } }
@Test public void testStarDotStar() { String glob = "*.*"; String regex = RegexUtil.globToRegex(glob); Pattern p = Pattern.compile(regex); String str = "a.txt"; Matcher m = p.matcher(str); assertTrue(str + " matches glob: " + glob, m.matches()); str = "noext"; m = p.matcher(str); assertFalse(str + " matches glob: " + glob, m.matches()); }
@Test @Ignore public void cornerCases() { String glob = "*"; String regex = RegexUtil.globToRegex(glob); String str = ".hidden"; assertFalse(str + " matches glob: " + glob, Pattern.compile(regex).matcher(str).matches()); }