2121import io .netty .buffer .ByteBuf ;
2222import io .netty .buffer .Unpooled ;
2323import org .jetbrains .annotations .NotNull ;
24- import org .junit .jupiter .api .Assertions ;
2524import org .junit .jupiter .api .Test ;
2625import org .junit .jupiter .params .ParameterizedTest ;
2726import org .junit .jupiter .params .provider .Arguments ;
27+ import org .junit .jupiter .params .provider .CsvSource ;
2828import org .junit .jupiter .params .provider .MethodSource ;
2929
30- import java .nio .charset .Charset ;
3130import java .nio .charset .StandardCharsets ;
3231import java .util .ArrayList ;
3332import java .util .Arrays ;
3433import java .util .List ;
3534
36- import static org .junit .Assert .*;
35+ import static org .junit .jupiter . api . Assertions .*;
3736
3837/**
3938 * @author Silvio Giebl
@@ -70,30 +69,34 @@ private static Iterable<Arguments> tooLongStringProvider() {
7069 @ MethodSource ("tooLongStringProvider" )
7170 void from_stringTooLong_throws (final @ NotNull String tooLong , final int expectedLength ) {
7271 final IllegalArgumentException exception =
73- Assertions .assertThrows (IllegalArgumentException .class , () -> MqttUtf8StringImpl .of (tooLong ));
74- assertTrue ("IllegalArgumentException must give hint that string encoded in UTF-8 exceeds binary limit." ,
75- exception .getMessage ().contains ("must not be longer than" ));
76- assertTrue ("IllegalArgumentException contains actual length." ,
77- exception .getMessage ().contains (Integer .toString (expectedLength - 2 )));
72+ assertThrows (IllegalArgumentException .class , () -> MqttUtf8StringImpl .of (tooLong ));
73+ assertTrue (
74+ exception .getMessage ().contains ("must not be longer than" ),
75+ "IllegalArgumentException must give hint that string encoded in UTF-8 exceeds binary limit." );
76+ assertTrue (
77+ exception .getMessage ().contains (Integer .toString (expectedLength - 2 )),
78+ "IllegalArgumentException contains actual length." );
7879 }
7980
8081 @ Test
8182 void from_stringWithNullCharacter_throws () {
8283 final IllegalArgumentException exception =
83- Assertions .assertThrows (IllegalArgumentException .class , () -> MqttUtf8StringImpl .of ("abc\0 def" ));
84- assertTrue ("IllegalArgumentException must give hint that string contains a forbidden null character." ,
85- exception .getMessage ().contains ("null character (U+0000)" ));
84+ assertThrows (IllegalArgumentException .class , () -> MqttUtf8StringImpl .of ("abc\0 def" ));
85+ assertTrue (
86+ exception .getMessage ().contains ("null character (U+0000)" ),
87+ "IllegalArgumentException must give hint that string contains a forbidden null character." );
8688 }
8789
88- @ Test
89- void from_stringWithUtf16Surrogates_throws () {
90+ @ ParameterizedTest
91+ @ CsvSource ({"abc, def" , "abc, ''" , "'' ,def" , "'', ''" })
92+ void from_stringWithUtf16Surrogates_throws (final @ NotNull String prefix , final @ NotNull String postfix ) {
9093 for (char c = '\uD800' ; c <= '\uDFFF' ; c ++) {
91- final String stringWithSurrogate = "abc" + c + "def" ;
92- final IllegalArgumentException exception = Assertions . assertThrows ( IllegalArgumentException . class ,
93- () -> MqttUtf8StringImpl .of (stringWithSurrogate ));
94+ final String stringWithSurrogate = prefix + c + postfix ;
95+ final IllegalArgumentException exception =
96+ assertThrows ( IllegalArgumentException . class , () -> MqttUtf8StringImpl .of (stringWithSurrogate ));
9497 assertTrue (
95- "IllegalArgumentException must give hint that string contains a forbidden UTF-16 surrogate." ,
96- exception . getMessage (). contains ( " UTF-16 surrogate" ) );
98+ exception . getMessage (). contains ( " UTF-16 surrogate" ) ,
99+ "IllegalArgumentException must give hint that string contains a forbidden UTF-16 surrogate." );
97100 }
98101 }
99102
@@ -173,6 +176,25 @@ void containsShouldNotCharacters_stringWithNonCharacters() {
173176 assertTrue (binaryString .containsShouldNotCharacters ());
174177 }
175178 }
179+ for (int c = 0xFDD0 ; c <= 0xFDEF ; c ++) {
180+ final String nonCharacterString = String .valueOf (Character .toChars (c ));
181+ final MqttUtf8StringImpl string = MqttUtf8StringImpl .of ("abc" + nonCharacterString + "def" );
182+ assertNotNull (string );
183+ assertTrue (string .containsShouldNotCharacters ());
184+
185+ final byte [] nonCharacterBinary = nonCharacterString .getBytes (StandardCharsets .UTF_8 );
186+ final byte [] binary = new byte [6 + nonCharacterBinary .length ];
187+ binary [0 ] = 'a' ;
188+ binary [1 ] = 'b' ;
189+ binary [2 ] = 'c' ;
190+ System .arraycopy (nonCharacterBinary , 0 , binary , 3 , nonCharacterBinary .length );
191+ binary [3 + nonCharacterBinary .length ] = 'd' ;
192+ binary [3 + nonCharacterBinary .length + 1 ] = 'e' ;
193+ binary [3 + nonCharacterBinary .length + 2 ] = 'f' ;
194+ final MqttUtf8StringImpl binaryString = MqttUtf8StringImpl .of (binary );
195+ assertNotNull (binaryString );
196+ assertTrue (binaryString .containsShouldNotCharacters ());
197+ }
176198 }
177199
178200 @ Test
@@ -204,7 +226,7 @@ void from_byteBuf() {
204226 final String string = "abcdef" ;
205227
206228 final ByteBuf byteBuf = Unpooled .buffer ();
207- MqttBinaryData .encode (string .getBytes (Charset . forName ( "UTF-8" ) ), byteBuf );
229+ MqttBinaryData .encode (string .getBytes (StandardCharsets . UTF_8 ), byteBuf );
208230 final MqttUtf8StringImpl mqtt5UTF8String = MqttUtf8StringImpl .decode (byteBuf );
209231 byteBuf .release ();
210232
@@ -270,8 +292,8 @@ void encodedLength_fromStringWithMaxLength(final @NotNull String string, final i
270292 }
271293
272294 @ Test
273- @ SuppressWarnings ("all" )
274- public void equals () {
295+ @ SuppressWarnings ({ "SimplifiableJUnitAssertion" , "ConstantConditions" } )
296+ void equals () {
275297 assertTrue (MqttUtf8StringImpl .of ("test" ).equals (MqttUtf8StringImpl .of ("test" )));
276298 assertTrue (MqttUtf8StringImpl .of ("test" ).equals (MqttUtf8StringImpl .of (new byte []{'t' , 'e' , 's' , 't' })));
277299 assertTrue (MqttUtf8StringImpl .of (new byte []{'t' , 'e' , 's' , 't' }).equals (MqttUtf8StringImpl .of ("test" )));
@@ -289,8 +311,31 @@ public void equals() {
289311 }
290312
291313 @ Test
292- @ SuppressWarnings ("all" )
293- public void equals_same () {
314+ @ SuppressWarnings ({"ConstantConditions" })
315+ void compareTo () {
316+ assertEquals (0 , MqttUtf8StringImpl .of ("test" ).compareTo (MqttUtf8StringImpl .of ("test" )));
317+ assertEquals (0 , MqttUtf8StringImpl .of ("test" ).compareTo (MqttUtf8StringImpl .of (new byte []{'t' , 'e' , 's' , 't' })));
318+ assertEquals (0 , MqttUtf8StringImpl .of (new byte []{'t' , 'e' , 's' , 't' }).compareTo (MqttUtf8StringImpl .of ("test" )));
319+ assertEquals (0 , MqttUtf8StringImpl .of (new byte []{'t' , 'e' , 's' , 't' })
320+ .compareTo (MqttUtf8StringImpl .of (new byte []{'t' , 'e' , 's' , 't' })));
321+
322+ assertTrue (MqttUtf8StringImpl .of ("test" ).compareTo (MqttUtf8StringImpl .of ("test2" )) < 0 );
323+ assertTrue (MqttUtf8StringImpl .of ("test2" ).compareTo (MqttUtf8StringImpl .of ("test" )) > 0 );
324+ assertTrue (MqttUtf8StringImpl .of ("test" ).compareTo (MqttUtf8StringImpl .of (new byte []{'t' , 'e' , 's' , 't' , '2' })) <
325+ 0 );
326+ assertTrue (MqttUtf8StringImpl .of ("test2" ).compareTo (MqttUtf8StringImpl .of (new byte []{'t' , 'e' , 's' , 't' })) > 0 );
327+ assertTrue (MqttUtf8StringImpl .of (new byte []{'t' , 'e' , 's' , 't' }).compareTo (MqttUtf8StringImpl .of ("test2" )) < 0 );
328+ assertTrue (MqttUtf8StringImpl .of (new byte []{'t' , 'e' , 's' , 't' , '2' }).compareTo (MqttUtf8StringImpl .of ("test" )) >
329+ 0 );
330+ assertTrue (MqttUtf8StringImpl .of (new byte []{'t' , 'e' , 's' , 't' })
331+ .compareTo (MqttUtf8StringImpl .of (new byte []{'t' , 'e' , 's' , 't' , '2' })) < 0 );
332+ assertTrue (MqttUtf8StringImpl .of (new byte []{'t' , 'e' , 's' , 't' , '2' })
333+ .compareTo (MqttUtf8StringImpl .of (new byte []{'t' , 'e' , 's' , 't' })) > 0 );
334+ }
335+
336+ @ Test
337+ @ SuppressWarnings ({"SimplifiableJUnitAssertion" , "ConstantConditions" , "EqualsWithItself" })
338+ void equals_same () {
294339 final MqttUtf8StringImpl string = MqttUtf8StringImpl .of ("test" );
295340 final MqttUtf8StringImpl binary = MqttUtf8StringImpl .of (new byte []{'t' , 'e' , 's' , 't' });
296341
@@ -299,8 +344,18 @@ public void equals_same() {
299344 }
300345
301346 @ Test
302- @ SuppressWarnings ("all" )
303- public void equals_converted () {
347+ @ SuppressWarnings ({"ConstantConditions" , "EqualsWithItself" })
348+ void compareTo_same () {
349+ final MqttUtf8StringImpl string = MqttUtf8StringImpl .of ("test" );
350+ final MqttUtf8StringImpl binary = MqttUtf8StringImpl .of (new byte []{'t' , 'e' , 's' , 't' });
351+
352+ assertEquals (0 , string .compareTo (string ));
353+ assertEquals (0 , binary .compareTo (binary ));
354+ }
355+
356+ @ Test
357+ @ SuppressWarnings ({"SimplifiableJUnitAssertion" , "ConstantConditions" })
358+ void equals_converted () {
304359 final MqttUtf8StringImpl stringAndBinary = MqttUtf8StringImpl .of ("test" );
305360 stringAndBinary .toBinary ();
306361
@@ -311,6 +366,19 @@ public void equals_converted() {
311366 assertTrue (MqttUtf8StringImpl .of (new byte []{'t' , 'e' , 's' , 't' }).equals (stringAndBinary ));
312367 }
313368
369+ @ Test
370+ @ SuppressWarnings ("ConstantConditions" )
371+ void compareTo_converted () {
372+ final MqttUtf8StringImpl stringAndBinary = MqttUtf8StringImpl .of ("test" );
373+ stringAndBinary .toBinary ();
374+
375+ assertEquals (0 , stringAndBinary .compareTo (MqttUtf8StringImpl .of ("test" )));
376+ assertEquals (0 , stringAndBinary .compareTo (MqttUtf8StringImpl .of (new byte []{'t' , 'e' , 's' , 't' })));
377+
378+ assertEquals (0 , MqttUtf8StringImpl .of ("test" ).compareTo (stringAndBinary ));
379+ assertEquals (0 , MqttUtf8StringImpl .of (new byte []{'t' , 'e' , 's' , 't' }).compareTo (stringAndBinary ));
380+ }
381+
314382 @ Test
315383 void hashCode_sameAsString () {
316384 final MqttUtf8StringImpl string = MqttUtf8StringImpl .of ("test" );
0 commit comments