### Question:
LocaleUtils { public static Locale parseLocale(String string) { String[] segments = string.split("[-_]"); if (segments.length == 1) { if ("".equals(segments[0].trim())) { throw new IllegalArgumentException("Invalid locale string: " + string); } else { return new Locale(segments[0].trim()); } } else if (segments.length == 2) { if ("".equals(segments[0].trim()) || "".equals(segments[1].trim())) { throw new IllegalArgumentException("Invalid locale string: " + string); } else { return new Locale(segments[0].trim(), segments[1].trim()); } } else if (segments.length == 3) { if ("".equals(segments[0].trim()) || "".equals(segments[1].trim()) || "".equals(segments[2].trim())) { throw new IllegalArgumentException("Invalid locale string: " + string); } else { return new Locale(segments[0].trim(), segments[1].trim(), segments[2].trim()); } } else { throw new IllegalArgumentException("Invalid locale string: " + string); } }  static Locale parseLocale(String string); static Locale bestLocale(Locale[] preferredLocales, Locale[] availableLocales); static final Locale DEFAULT_LOCALE; }### Answer:
@Test(expected=IllegalArgumentException.class) public void singleUnderscore() { System.err.println(LocaleUtils.parseLocale("_").getDisplayName()); }
@Test(expected=IllegalArgumentException.class) public void doubleUnderscore() { System.err.println(LocaleUtils.parseLocale("__").getDisplayName()); }
@Test(expected=IllegalArgumentException.class) public void tripleUnderscore() { System.err.println(LocaleUtils.parseLocale("_-_").getDisplayName()); }
@Test(expected=IllegalArgumentException.class) public void spaces1() { System.err.println(LocaleUtils.parseLocale("_US").getDisplayName()); }
@Test(expected=IllegalArgumentException.class) public void spaces2() { System.err.println(LocaleUtils.parseLocale(" _US").getDisplayName()); }
@Test(expected=IllegalArgumentException.class) public void spaces3() { System.err.println(LocaleUtils.parseLocale("en - US__ -_").getDisplayName()); }
@Test public void testParseLocale() { assertEquals( new Locale(Locale.ENGLISH.getLanguage(), Locale.US.getCountry()), LocaleUtils.parseLocale("en-us") ); assertEquals( new Locale(Locale.ENGLISH.getLanguage(), Locale.US.getCountry()), LocaleUtils.parseLocale("EN-us") ); assertEquals( new Locale(Locale.ENGLISH.getLanguage(), Locale.US.getCountry()), LocaleUtils.parseLocale("en-US") ); assertEquals( new Locale(Locale.ENGLISH.getLanguage(), Locale.US.getCountry()), LocaleUtils.parseLocale("EN-US") ); assertEquals( new Locale(Locale.ENGLISH.getLanguage(), Locale.US.getCountry()), LocaleUtils.parseLocale("en_us") ); assertEquals( new Locale(Locale.ENGLISH.getLanguage(), Locale.US.getCountry()), LocaleUtils.parseLocale("en - US_") ); assertEquals( new Locale(Locale.FRENCH.getLanguage(), Locale.CANADA.getCountry(), "Quebec"), LocaleUtils.parseLocale("fr_ca_Quebec") ); assertFalse( new Locale(Locale.ENGLISH.getLanguage()).equals( LocaleUtils.parseLocale("en_us") ) ); assertFalse( new Locale(Locale.GERMAN.getLanguage(), Locale.US.getCountry()).equals( LocaleUtils.parseLocale("en_us") ) ); }