From 75aa8475e120f55078810d76feff2d9e291f5e7e Mon Sep 17 00:00:00 2001 From: Leonardo Freitas Gomes Date: Sun, 14 Dec 2014 14:35:43 +0100 Subject: [PATCH] Motivation: Sonar points out an equals comparison, where the types compared are different and don't share any common parent http://clinker.netty.io/sonar/drilldown/issues/io.netty:netty-parent:master?severity=CRITICAL# Modifications: Converted AsciiString into a String by calling toString() method before comparing with equals(). Also added a unit-test to show that it works. Result: Major violation is gone. Code is correct. --- .../io/netty/handler/codec/http/HttpResponseStatus.java | 2 +- .../test/java/io/netty/handler/codec/AsciiStringTest.java | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/codec-http/src/main/java/io/netty/handler/codec/http/HttpResponseStatus.java b/codec-http/src/main/java/io/netty/handler/codec/http/HttpResponseStatus.java index 5455f57ebf..559c061067 100644 --- a/codec-http/src/main/java/io/netty/handler/codec/http/HttpResponseStatus.java +++ b/codec-http/src/main/java/io/netty/handler/codec/http/HttpResponseStatus.java @@ -453,7 +453,7 @@ public class HttpResponseStatus implements Comparable { int code = Integer.parseInt(status.substring(0, space)); String reasonPhrase = status.substring(space + 1); HttpResponseStatus responseStatus = valueOf(code); - if (responseStatus.reasonPhrase().equals(reasonPhrase)) { + if (responseStatus.reasonPhrase().toString().equals(reasonPhrase)) { return responseStatus; } else { return new HttpResponseStatus(code, reasonPhrase); diff --git a/codec/src/test/java/io/netty/handler/codec/AsciiStringTest.java b/codec/src/test/java/io/netty/handler/codec/AsciiStringTest.java index 52fbcd999a..0d459bc1fc 100644 --- a/codec/src/test/java/io/netty/handler/codec/AsciiStringTest.java +++ b/codec/src/test/java/io/netty/handler/codec/AsciiStringTest.java @@ -23,6 +23,7 @@ import java.nio.CharBuffer; import java.nio.charset.Charset; import java.nio.charset.CharsetEncoder; +import org.junit.Assert; import org.junit.Test; /** @@ -79,6 +80,13 @@ public class AsciiStringTest { } } + @Test + public void testComparisonWithString() { + String string = "shouldn't fail"; + AsciiString ascii = new AsciiString(string.toCharArray()); + Assert.assertEquals(string, ascii.toString()); + } + private static byte[] getBytesWithEncoder(CharSequence value, Charset charset) { final CharsetEncoder encoder = CharsetUtil.getEncoder(charset); final ByteBuffer nativeBuffer = ByteBuffer.allocate((int) (encoder.maxBytesPerChar() * value.length()));