### Question:
Languages { public static Language fromString(final String language) { requireNonNull(language,"Language cannot be null"); if(WILDCARD.equals(language)) { return wildcard(); } else { return of(parseLocale(language)); } } private  Languages(); static Language wildcard(); static Language of(final Locale locale); static Language fromString(final String language); static boolean includes(final Language language1, final Language language2);  }### Answer:
@Test public void parsesLanguagesWithJustLanguageTag() throws Exception { final Locale expected=Locale.ENGLISH; final Language result = Languages.fromString("en"); assertThat(result,not(nullValue())); assertThat(result.locale(),equalTo(expected)); assertThat(result.isWildcard(),equalTo(false)); assertThat(result.primaryTag(),equalTo(expected.getLanguage())); assertThat(result.subTag(),equalTo(expected.getCountry())); }
@Test public void parsesLanguagesWithLanguageAndCountryTags() throws Exception { final Locale expected=Locale.US; final Language result = Languages.fromString("en-US"); assertThat(result,not(nullValue())); assertThat(result.locale(),equalTo(expected)); assertThat(result.isWildcard(),equalTo(false)); assertThat(result.primaryTag(),equalTo(expected.getLanguage())); assertThat(result.subTag(),equalTo(expected.getCountry())); }
@Test public void parsesWildcardLanguage() throws Exception { Language result = Languages.fromString("*"); assertThat(result,not(nullValue())); assertThat(result.locale(),nullValue()); assertThat(result.isWildcard(),equalTo(true)); assertThat(result.primaryTag(),equalTo("*")); assertThat(result.subTag(),nullValue()); }
@Test public void doesNotParseLanguagesWithoutTags() throws Exception { try { Languages.fromString(""); fail("Should fail when there are no tags"); } catch (final IllegalArgumentException e) { assertThat(e.getMessage(),startsWith("Invalid language-range: ")); } }
@Test public void doesNotParseLanguagesWithTooManyTags() throws Exception { try { Languages.fromString("to-many-tags"); fail("Should fail when there are more than 2 tags"); } catch (final IllegalArgumentException e) { assertThat(e.getMessage(),startsWith("Invalid language-range: ")); } }
@Test public void doesNotParseLanguagesWithBadPrimaryTagLength() throws Exception { try { Languages.fromString("badPrimaryTagLength"); fail("Should fail when primary tag is too long"); } catch (final IllegalArgumentException e) { assertThat(e.getMessage(),startsWith("Invalid language-range: ")); } }
@Test public void doesNotParseLanguagesWithBadPrimaryTagComposition() throws Exception { try { Languages.fromString("bad 1st tag Composition"); fail("Should fail when primary tag is not alphabetic"); } catch (final IllegalArgumentException e) { assertThat(e.getMessage(),startsWith("Invalid language-range: ")); } }
@Test public void doesNotParseLanguagesWithHyphenPrimaryTag() throws Exception { try { Languages.fromString("-"); fail("Should fail when cannot find sensible media range"); } catch (final IllegalArgumentException e) { assertThat(e.getMessage(),startsWith("Invalid language-range: ")); } }
@Test public void doesNotParseLanguagesWithBadSubTagLength() throws Exception { try { Languages.fromString("en-badSubTagLength"); fail("Should fail when sub tag is too long"); } catch (final IllegalArgumentException e) { assertThat(e.getMessage(),startsWith("Invalid language-range: ")); } }
@Test public void doesNotParseLanguagesWithBadSubTagComposition() throws Exception { try { Languages.fromString("en-bad sub tag composition"); fail("Should fail when sub tag is not alphanumeric"); } catch (final IllegalArgumentException e) { assertThat(e.getMessage(),startsWith("Invalid language-range: ")); } }