From 69663da5fe00a99fa2ebe40198554a2d7fdf7bf1 Mon Sep 17 00:00:00 2001 From: Rossen Stoyanchev Date: Thu, 14 Nov 2019 14:16:26 +0000 Subject: [PATCH] Polishing contribution See gh-1885 --- .../AbstractWireFeedHttpMessageConverter.java | 2 +- .../AtomFeedHttpMessageConverterTests.java | 39 +++++++++--------- .../RssChannelHttpMessageConverterTests.java | 41 +++++++++---------- 3 files changed, 39 insertions(+), 43 deletions(-) diff --git a/spring-web/src/main/java/org/springframework/http/converter/feed/AbstractWireFeedHttpMessageConverter.java b/spring-web/src/main/java/org/springframework/http/converter/feed/AbstractWireFeedHttpMessageConverter.java index 5494fe8a2929..5a62697950cd 100644 --- a/spring-web/src/main/java/org/springframework/http/converter/feed/AbstractWireFeedHttpMessageConverter.java +++ b/spring-web/src/main/java/org/springframework/http/converter/feed/AbstractWireFeedHttpMessageConverter.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/spring-web/src/test/java/org/springframework/http/converter/feed/AtomFeedHttpMessageConverterTests.java b/spring-web/src/test/java/org/springframework/http/converter/feed/AtomFeedHttpMessageConverterTests.java index 0c6da02d2cd9..1b057e584aec 100644 --- a/spring-web/src/test/java/org/springframework/http/converter/feed/AtomFeedHttpMessageConverterTests.java +++ b/spring-web/src/test/java/org/springframework/http/converter/feed/AtomFeedHttpMessageConverterTests.java @@ -21,15 +21,12 @@ import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; import java.util.ArrayList; -import java.util.HashMap; import java.util.List; -import java.util.Map; import com.rometools.rome.feed.atom.Entry; import com.rometools.rome.feed.atom.Feed; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import org.xml.sax.SAXException; import org.xmlunit.diff.DefaultNodeMatcher; import org.xmlunit.diff.ElementSelectors; import org.xmlunit.diff.NodeMatcher; @@ -47,6 +44,10 @@ */ public class AtomFeedHttpMessageConverterTests { + private static final MediaType ATOM_XML_UTF8 = + new MediaType(MediaType.APPLICATION_ATOM_XML, StandardCharsets.UTF_8); + + private AtomFeedHttpMessageConverter converter; @@ -58,21 +59,21 @@ public void setUp() { @Test public void canRead() { - assertThat(converter.canRead(Feed.class, new MediaType("application", "atom+xml"))).isTrue(); - assertThat(converter.canRead(Feed.class, new MediaType("application", "atom+xml", StandardCharsets.UTF_8))).isTrue(); + assertThat(converter.canRead(Feed.class, MediaType.APPLICATION_ATOM_XML)).isTrue(); + assertThat(converter.canRead(Feed.class, ATOM_XML_UTF8)).isTrue(); } @Test public void canWrite() { - assertThat(converter.canWrite(Feed.class, new MediaType("application", "atom+xml"))).isTrue(); - assertThat(converter.canWrite(Feed.class, new MediaType("application", "atom+xml", StandardCharsets.UTF_8))).isTrue(); + assertThat(converter.canWrite(Feed.class, MediaType.APPLICATION_ATOM_XML)).isTrue(); + assertThat(converter.canWrite(Feed.class, ATOM_XML_UTF8)).isTrue(); } @Test public void read() throws IOException { InputStream is = getClass().getResourceAsStream("atom.xml"); MockHttpInputMessage inputMessage = new MockHttpInputMessage(is); - inputMessage.getHeaders().setContentType(new MediaType("application", "atom+xml", StandardCharsets.UTF_8)); + inputMessage.getHeaders().setContentType(ATOM_XML_UTF8); Feed result = converter.read(Feed.class, inputMessage); assertThat(result.getTitle()).isEqualTo("title"); assertThat(result.getSubtitle().getValue()).isEqualTo("subtitle"); @@ -89,7 +90,7 @@ public void read() throws IOException { } @Test - public void write() throws IOException, SAXException { + public void write() throws IOException { Feed feed = new Feed("atom_1.0"); feed.setTitle("title"); @@ -111,7 +112,7 @@ public void write() throws IOException, SAXException { assertThat(outputMessage.getHeaders().getContentType()) .as("Invalid content-type") - .isEqualTo(new MediaType("application", "atom+xml", StandardCharsets.UTF_8)); + .isEqualTo(ATOM_XML_UTF8); String expected = "" + "title" + "id1title1" + "id2title2"; @@ -121,7 +122,7 @@ public void write() throws IOException, SAXException { } @Test - public void writeOtherCharset() throws IOException, SAXException { + public void writeOtherCharset() throws IOException { Feed feed = new Feed("atom_1.0"); feed.setTitle("title"); String encoding = "ISO-8859-1"; @@ -137,17 +138,15 @@ public void writeOtherCharset() throws IOException, SAXException { @Test public void writeOtherContentTypeParameters() throws IOException { - Feed feed = new Feed("atom_1.0"); + MockHttpOutputMessage message = new MockHttpOutputMessage(); + MediaType contentType = new MediaType("application", "atom+xml", singletonMap("type", "feed")); + converter.write(new Feed("atom_1.0"), contentType, message); - MockHttpOutputMessage outputMessage = new MockHttpOutputMessage(); - converter.write(feed, new MediaType("application", "atom+xml", singletonMap("type", "feed")), outputMessage); - - Map expectedParameters = new HashMap<>(); - expectedParameters.put("charset", "UTF-8"); - expectedParameters.put("type", "feed"); - assertThat(outputMessage.getHeaders().getContentType()) + assertThat(message.getHeaders().getContentType().getParameters()) .as("Invalid content-type") - .isEqualTo(new MediaType("application", "atom+xml", expectedParameters)); + .hasSize(2) + .containsEntry("type", "feed") + .containsEntry("charset", "UTF-8"); } } diff --git a/spring-web/src/test/java/org/springframework/http/converter/feed/RssChannelHttpMessageConverterTests.java b/spring-web/src/test/java/org/springframework/http/converter/feed/RssChannelHttpMessageConverterTests.java index 3d8c5e7d508c..a720f16d3b53 100644 --- a/spring-web/src/test/java/org/springframework/http/converter/feed/RssChannelHttpMessageConverterTests.java +++ b/spring-web/src/test/java/org/springframework/http/converter/feed/RssChannelHttpMessageConverterTests.java @@ -21,15 +21,12 @@ import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; import java.util.ArrayList; -import java.util.HashMap; import java.util.List; -import java.util.Map; import com.rometools.rome.feed.rss.Channel; import com.rometools.rome.feed.rss.Item; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import org.xml.sax.SAXException; import org.springframework.http.MediaType; import org.springframework.http.MockHttpInputMessage; @@ -44,6 +41,10 @@ */ public class RssChannelHttpMessageConverterTests { + private static final MediaType RSS_XML_UTF8 = + new MediaType(MediaType.APPLICATION_RSS_XML, StandardCharsets.UTF_8); + + private RssChannelHttpMessageConverter converter; @@ -54,22 +55,19 @@ public void setUp() { @Test - public void canRead() { - assertThat(converter.canRead(Channel.class, new MediaType("application", "rss+xml"))).isTrue(); - assertThat(converter.canRead(Channel.class, new MediaType("application", "rss+xml", StandardCharsets.UTF_8))).isTrue(); - } + public void canReadAndWrite() { + assertThat(converter.canRead(Channel.class, MediaType.APPLICATION_RSS_XML)).isTrue(); + assertThat(converter.canRead(Channel.class, RSS_XML_UTF8)).isTrue(); - @Test - public void canWrite() { - assertThat(converter.canWrite(Channel.class, new MediaType("application", "rss+xml"))).isTrue(); - assertThat(converter.canWrite(Channel.class, new MediaType("application", "rss+xml", StandardCharsets.UTF_8))).isTrue(); + assertThat(converter.canWrite(Channel.class, MediaType.APPLICATION_RSS_XML)).isTrue(); + assertThat(converter.canWrite(Channel.class, RSS_XML_UTF8)).isTrue(); } @Test public void read() throws IOException { InputStream is = getClass().getResourceAsStream("rss.xml"); MockHttpInputMessage inputMessage = new MockHttpInputMessage(is); - inputMessage.getHeaders().setContentType(new MediaType("application", "rss+xml", StandardCharsets.UTF_8)); + inputMessage.getHeaders().setContentType(RSS_XML_UTF8); Channel result = converter.read(Channel.class, inputMessage); assertThat(result.getTitle()).isEqualTo("title"); assertThat(result.getLink()).isEqualTo("https://example.com"); @@ -86,7 +84,7 @@ public void read() throws IOException { } @Test - public void write() throws IOException, SAXException { + public void write() throws IOException { Channel channel = new Channel("rss_2.0"); channel.setTitle("title"); channel.setLink("https://example.com"); @@ -108,7 +106,7 @@ public void write() throws IOException, SAXException { assertThat(outputMessage.getHeaders().getContentType()) .as("Invalid content-type") - .isEqualTo(new MediaType("application", "rss+xml", StandardCharsets.UTF_8)); + .isEqualTo(RSS_XML_UTF8); String expected = "" + "titlehttps://example.comdescription" + "title1" + @@ -119,7 +117,7 @@ public void write() throws IOException, SAXException { } @Test - public void writeOtherCharset() throws IOException, SAXException { + public void writeOtherCharset() throws IOException { Channel channel = new Channel("rss_2.0"); channel.setTitle("title"); channel.setLink("https://example.com"); @@ -146,15 +144,14 @@ public void writeOtherContentTypeParameters() throws IOException { channel.setLink("http://example.com"); channel.setDescription("description"); - MockHttpOutputMessage outputMessage = new MockHttpOutputMessage(); - converter.write(channel, new MediaType("application", "rss+xml", singletonMap("x", "y")), outputMessage); + MockHttpOutputMessage message = new MockHttpOutputMessage(); + converter.write(channel, new MediaType("application", "rss+xml", singletonMap("x", "y")), message); - Map expectedParameters = new HashMap<>(); - expectedParameters.put("charset", "UTF-8"); - expectedParameters.put("x", "y"); - assertThat(outputMessage.getHeaders().getContentType()) + assertThat(message.getHeaders().getContentType().getParameters()) .as("Invalid content-type") - .isEqualTo(new MediaType("application", "rss+xml", expectedParameters)); + .hasSize(2) + .containsEntry("x", "y") + .containsEntry("charset", "UTF-8"); } }