### Question:
ContentNegotiationUtils { static Weighted<MediaType> accept(final String header) { return Weighted.fromString(header, new MediaTypeParser()); } private  ContentNegotiationUtils();   }### Answer:
@Test public void acceptDoesNotAllowsBadContentAfterQualityDefinition() { try { accept("text/turtle;q=0.000;bad data"); fail("accept should fail when bad content is found after the quality definition"); } catch (final IllegalArgumentException e) { assertThat(e.getMessage(),endsWith(" (;bad data)")); } }
@Test public void acceptDoesNotAllowsBadContentBeforeExtensionParameter() { try { accept("text/turtle;q=0.000;;param=value dangling data"); fail("accept should fail when fails to find first candidate extension parameter"); } catch (final IllegalArgumentException e) { assertThat(e.getMessage(),startsWith("Invalid content before extension parameter: ")); assertThat(e.getMessage(),endsWith(" (;;param=value dangling data)")); } }
@Test public void acceptDoesNotAllowsBadContentAfterExtensionParameter() { try { accept("text/turtle;q=0.000;param=value dangling data"); fail("accept should fail when bad content is defined after extension parameter"); } catch (final IllegalArgumentException e) { assertThat(e.getMessage(),equalTo("Invalid content after extension parameter [ dangling data] (;param=value dangling data)")); } }
@Test public void dependsOnMediaTypesClassForCreatingWeightedMediaTypes() { final String value = "text/turtle"; new MockUp<MediaTypes>() { @Mock public MediaType fromString(Invocation context, String aValue) { assertThat(aValue,equalTo(value)); return context.proceed(aValue); } }; final Weighted<MediaType> result=ContentNegotiationUtils.accept(value); assertThat(result.entity().type(),equalTo("text")); assertThat(result.entity().subType(),equalTo("turtle")); assertThat(result.hasWeight(),equalTo(false)); assertThat(result.weight(),equalTo(1.0D)); }
@Test public void acceptAllowsAnExtensionParameterAfterQualityDefinition() { final Weighted<MediaType> weighted = accept("text/turtle;q=0.000;param=value"); assertThat(weighted.hasWeight(),equalTo(true)); assertThat(weighted.weight(),equalTo(0.0D)); final MediaType result = weighted.entity(); assertThat(result.parameters().isEmpty(),equalTo(true)); assertThat(result.type(),equalTo("text")); assertThat(result.subType(),equalTo("turtle")); }
@Test public void acceptDoesNotRequireAnExtensionParameterAfterQualityDefinition() { final Weighted<MediaType> weighted = accept("text/turtle;q=0.000"); assertThat(weighted.hasWeight(),equalTo(true)); assertThat(weighted.weight(),equalTo(0.0D)); final MediaType result = weighted.entity(); assertThat(result.parameters().isEmpty(),equalTo(true)); assertThat(result.type(),equalTo("text")); assertThat(result.subType(),equalTo("turtle")); }
@Test public void acceptDoesNotAllowsMultipleParametersAfterQualityDefinition() { try { accept("text/turtle;q=0.000;param=value;ext=value"); fail("accept should fail when multiple parameters are found after quality definition"); } catch (final IllegalArgumentException e) { assertThat(e.getMessage(),equalTo("Invalid content after extension parameter [;ext=value] (;param=value;ext=value)")); } }