@Benchmark public Object withCharset() { return MediaType.create("text", "plain" + System.nanoTime(), UTF_8); }
@Benchmark public Object noCharset() { return MediaType.create("text", "plain" + System.nanoTime()); }
@Test public void resolvesFileMimeType() throws Exception { Map<String, MediaType> mimeTypes = new HashMap<>(); mimeTypes.put("txt", TEXT); mimeTypes.put("json", JSON); mimeTypes.put("xml", XML); mimeTypes.put("html", HTML); mimeTypes.put("csv", MediaType.create("text", "csv")); for (String extension : mimeTypes.keySet()) { doFileMimeTypeTest(extension, mimeTypes.get(extension)); } }
@Test public void parseTemplateWithOverriddenDataTypeAsExpression() throws Exception { String customEncoding = "UTF-16"; MediaType customMediaType = create("application", "lrmextension"); parseTemplateProcessor.setLocation(UNKNOWN_MEDIATYPE_LOCATION); parseTemplateProcessor.setOutputMimeType(customMediaType.toRfcString()); parseTemplateProcessor.setOutputEncoding(customEncoding); parseTemplateProcessor.initialise(); String expectedExpression = IOUtils.getResourceAsString(UNKNOWN_MEDIATYPE_LOCATION, this.getClass()); when(mockExpressionManager.parseLogTemplate(eq(expectedExpression), eq(event), any(), any())).thenReturn("Parsed"); CoreEvent response = parseTemplateProcessor.process(event); assertThat(response.getMessage().getPayload().getDataType().getMediaType().getPrimaryType(), is(equalTo(customMediaType.getPrimaryType()))); assertThat(response.getMessage().getPayload().getDataType().getMediaType().getSubType(), is(equalTo(customMediaType.getSubType()))); assertThat(response.getMessage().getPayload().getDataType().getMediaType().getCharset().get().toString(), is(equalTo(customEncoding))); }
protected CoreEvent configureEvent() throws Exception { when(message.getPayload()) .thenReturn(new TypedValue<>(TEST_PAYLOAD, DataType.builder().mediaType(MediaType.create("*", "*", defaultCharset())).build())); when(message.getAttributes()) .thenReturn(new TypedValue<>(null, DataType.builder().fromObject(null).build())); return eventBuilder(muleContext).message(message).build(); }
@Test public void parseTemplateWithOverriddenDataType() throws Exception { String customEncoding = "UTF-16"; MediaType customMediaType = create("application", "lrmextension"); parseTemplateProcessor.setLocation(UNKNOWN_MEDIATYPE_LOCATION); parseTemplateProcessor.setOutputMimeType(customMediaType.toRfcString()); parseTemplateProcessor.setOutputEncoding(customEncoding); parseTemplateProcessor.initialise(); String expectedExpression = IOUtils.getResourceAsString(UNKNOWN_MEDIATYPE_LOCATION, this.getClass()); when(mockExpressionManager.parseLogTemplate(eq(expectedExpression), eq(event), any(), any())).thenReturn("Parsed"); CoreEvent response = parseTemplateProcessor.process(event); assertThat(response.getMessage().getPayload().getDataType().getMediaType().getPrimaryType(), is(equalTo(customMediaType.getPrimaryType()))); assertThat(response.getMessage().getPayload().getDataType().getMediaType().getSubType(), is(equalTo(customMediaType.getSubType()))); assertThat(response.getMessage().getPayload().getDataType().getMediaType().getCharset().get().toString(), is(equalTo(customEncoding))); }
/** * Creates a new {@link MediaType} instance keeping the {@code type} and {@code sub-type} but removing all the * parameters (like the {@code charset}) * * @return new immutable {@link MediaType} instance. */ public MediaType withoutParameters() { return create(this.getPrimaryType(), this.getSubType()); }
@Test public void resolvesFileMimeType() throws Exception { Map<String, MediaType> mimeTypes = new HashMap<>(); mimeTypes.put("txt", TEXT); mimeTypes.put("json", JSON); mimeTypes.put("xml", XML); mimeTypes.put("html", HTML); mimeTypes.put("csv", MediaType.create("text", "csv")); for (String extension : mimeTypes.keySet()) { doFileMimeTypeTest(extension, mimeTypes.get(extension)); } }