From 7e03728b2c27b2fa2332acd57de060b860a05d54 Mon Sep 17 00:00:00 2001 From: Rossen Stoyanchev Date: Wed, 4 Mar 2020 19:26:25 +0000 Subject: [PATCH] Polishing contribution See gh-24632 --- .../mvc/method/annotation/SseEmitter.java | 4 +-- .../method/annotation/SseEmitterTests.java | 34 +++++++++++-------- 2 files changed, 21 insertions(+), 17 deletions(-) diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/SseEmitter.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/SseEmitter.java index 6cc638de0d..7700b0199f 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/SseEmitter.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/SseEmitter.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2020 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. @@ -39,7 +39,7 @@ import org.springframework.util.StringUtils; */ public class SseEmitter extends ResponseBodyEmitter { - static final MediaType TEXT_PLAIN = new MediaType("text", "plain", StandardCharsets.UTF_8); + private static final MediaType TEXT_PLAIN = new MediaType("text", "plain", StandardCharsets.UTF_8); /** * Create a new SseEmitter instance. diff --git a/spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/SseEmitterTests.java b/spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/SseEmitterTests.java index 03509ab70b..6b6e35d4f3 100644 --- a/spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/SseEmitterTests.java +++ b/spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/SseEmitterTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2020 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. @@ -16,6 +16,7 @@ package org.springframework.web.servlet.mvc.method.annotation; import java.io.IOException; +import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.List; import java.util.function.Consumer; @@ -35,6 +36,9 @@ import static org.springframework.web.servlet.mvc.method.annotation.SseEmitter.e */ public class SseEmitterTests { + private static final MediaType TEXT_PLAIN_UTF8 = new MediaType("text", "plain", StandardCharsets.UTF_8); + + private SseEmitter emitter; private TestHandler handler; @@ -52,18 +56,18 @@ public class SseEmitterTests { public void send() throws Exception { this.emitter.send("foo"); this.handler.assertSentObjectCount(3); - this.handler.assertObject(0, "data:", SseEmitter.TEXT_PLAIN); + this.handler.assertObject(0, "data:", TEXT_PLAIN_UTF8); this.handler.assertObject(1, "foo"); - this.handler.assertObject(2, "\n\n", SseEmitter.TEXT_PLAIN); + this.handler.assertObject(2, "\n\n", TEXT_PLAIN_UTF8); } @Test public void sendWithMediaType() throws Exception { this.emitter.send("foo", MediaType.TEXT_PLAIN); this.handler.assertSentObjectCount(3); - this.handler.assertObject(0, "data:", SseEmitter.TEXT_PLAIN); + this.handler.assertObject(0, "data:", TEXT_PLAIN_UTF8); this.handler.assertObject(1, "foo", MediaType.TEXT_PLAIN); - this.handler.assertObject(2, "\n\n", SseEmitter.TEXT_PLAIN); + this.handler.assertObject(2, "\n\n", TEXT_PLAIN_UTF8); } @Test @@ -76,40 +80,40 @@ public class SseEmitterTests { public void sendEventWithDataLine() throws Exception { this.emitter.send(event().data("foo")); this.handler.assertSentObjectCount(3); - this.handler.assertObject(0, "data:", SseEmitter.TEXT_PLAIN); + this.handler.assertObject(0, "data:", TEXT_PLAIN_UTF8); this.handler.assertObject(1, "foo"); - this.handler.assertObject(2, "\n\n", SseEmitter.TEXT_PLAIN); + this.handler.assertObject(2, "\n\n", TEXT_PLAIN_UTF8); } @Test public void sendEventWithTwoDataLines() throws Exception { this.emitter.send(event().data("foo").data("bar")); this.handler.assertSentObjectCount(5); - this.handler.assertObject(0, "data:", SseEmitter.TEXT_PLAIN); + this.handler.assertObject(0, "data:", TEXT_PLAIN_UTF8); this.handler.assertObject(1, "foo"); - this.handler.assertObject(2, "\ndata:", SseEmitter.TEXT_PLAIN); + this.handler.assertObject(2, "\ndata:", TEXT_PLAIN_UTF8); this.handler.assertObject(3, "bar"); - this.handler.assertObject(4, "\n\n", SseEmitter.TEXT_PLAIN); + this.handler.assertObject(4, "\n\n", TEXT_PLAIN_UTF8); } @Test public void sendEventFull() throws Exception { this.emitter.send(event().comment("blah").name("test").reconnectTime(5000L).id("1").data("foo")); this.handler.assertSentObjectCount(3); - this.handler.assertObject(0, ":blah\nevent:test\nretry:5000\nid:1\ndata:", SseEmitter.TEXT_PLAIN); + this.handler.assertObject(0, ":blah\nevent:test\nretry:5000\nid:1\ndata:", TEXT_PLAIN_UTF8); this.handler.assertObject(1, "foo"); - this.handler.assertObject(2, "\n\n", SseEmitter.TEXT_PLAIN); + this.handler.assertObject(2, "\n\n", TEXT_PLAIN_UTF8); } @Test public void sendEventFullWithTwoDataLinesInTheMiddle() throws Exception { this.emitter.send(event().comment("blah").data("foo").data("bar").name("test").reconnectTime(5000L).id("1")); this.handler.assertSentObjectCount(5); - this.handler.assertObject(0, ":blah\ndata:", SseEmitter.TEXT_PLAIN); + this.handler.assertObject(0, ":blah\ndata:", TEXT_PLAIN_UTF8); this.handler.assertObject(1, "foo"); - this.handler.assertObject(2, "\ndata:", SseEmitter.TEXT_PLAIN); + this.handler.assertObject(2, "\ndata:", TEXT_PLAIN_UTF8); this.handler.assertObject(3, "bar"); - this.handler.assertObject(4, "\nevent:test\nretry:5000\nid:1\n\n", SseEmitter.TEXT_PLAIN); + this.handler.assertObject(4, "\nevent:test\nretry:5000\nid:1\n\n", TEXT_PLAIN_UTF8); }