6
6
import java .util .Arrays ;
7
7
import java .util .LinkedHashMap ;
8
8
import java .util .Map ;
9
+
10
+ import static org .junit .Assert .assertArrayEquals ;
9
11
import static org .junit .Assert .assertNotEquals ;
10
12
11
13
public class CompoundTagTest extends NBTTestCase {
@@ -37,16 +39,16 @@ public void testEquals() {
37
39
ct2 .putString ("str" , "foo" );
38
40
ct2 .put ("list" , new ListTag <>(ByteTag .class ));
39
41
ct2 .getListTag ("list" ).addByte ((byte ) 123 );
40
- assertTrue (ct . equals ( ct2 ) );
42
+ assertEquals (ct , ct2 );
41
43
42
44
ct2 .getListTag ("list" ).asByteTagList ().get (0 ).setValue ((byte ) 124 );
43
- assertFalse (ct . equals ( ct2 ) );
45
+ assertNotEquals (ct , ct2 );
44
46
45
47
ct2 .remove ("str" );
46
- assertFalse (ct . equals ( ct2 ) );
48
+ assertNotEquals (ct , ct2 );
47
49
48
50
assertThrowsNoRuntimeException (() -> ct .equals ("blah" ));
49
- assertFalse ( ct . equals ( "blah" ) );
51
+ assertNotEquals ( "blah" , ct );
50
52
51
53
assertEquals (ct , ct );
52
54
}
@@ -137,10 +139,10 @@ public void testHashCode() {
137
139
public void testClone () {
138
140
CompoundTag ct = createCompoundTag ();
139
141
CompoundTag cl = ct .clone ();
140
- assertTrue (ct . equals ( cl ) );
141
- assertFalse (ct == cl );
142
- assertFalse (ct .get ("list" ) == cl .get ("list" ));
143
- assertFalse (invokeGetValue (ct ) == invokeGetValue (cl ));
142
+ assertEquals (ct , cl );
143
+ assertNotSame (ct , cl );
144
+ assertNotSame (ct .get ("list" ), cl .get ("list" ));
145
+ assertNotSame (invokeGetValue (ct ), invokeGetValue (cl ));
144
146
}
145
147
146
148
public void testClear () {
@@ -154,9 +156,12 @@ public void testClear() {
154
156
public void testSerializeDeserialize () {
155
157
CompoundTag ct = createCompoundTag ();
156
158
byte [] data = serialize (ct );
157
- assertTrue (Arrays .equals (new byte []{10 , 0 , 0 , 1 , 0 , 1 , 98 , 127 , 8 , 0 , 3 , 115 , 116 , 114 , 0 , 3 , 102 , 111 , 111 , 9 , 0 , 4 , 108 , 105 , 115 , 116 , 1 , 0 , 0 , 0 , 1 , 123 , 0 }, data ));
159
+ assertArrayEquals (
160
+ new byte [] { 10 , 0 , 0 , 1 , 0 , 1 , 98 , 127 , 8 , 0 , 3 , 115 , 116 , 114 , 0 , 3 , 102 , 111 , 111 , 9 , 0 , 4 , 108 , 105 , 115 , 116 ,
161
+ 1 , 0 , 0 , 0 , 1 , 123 , 0
162
+ }, data );
158
163
CompoundTag tt = (CompoundTag ) deserialize (data );
159
- assertTrue (ct . equals ( tt ) );
164
+ assertEquals (ct , tt );
160
165
}
161
166
162
167
public void testCasting () {
@@ -169,9 +174,9 @@ public void testCasting() {
169
174
assertEquals (Byte .MAX_VALUE , cc .get ("b" , ByteTag .class ).asByte ());
170
175
assertThrowsRuntimeException (() -> cc .getShort ("b" ), ClassCastException .class );
171
176
assertEquals (0 , cc .getByte ("bb" ));
172
- assertEquals ( true , cc .getBoolean ("b" ));
177
+ assertTrue ( cc .getBoolean ("b" ));
173
178
cc .putByte ("b2" , (byte ) 0 );
174
- assertEquals ( false , cc .getBoolean ("b2" ));
179
+ assertFalse ( cc .getBoolean ("b2" ));
175
180
cc .putBoolean ("b3" , false );
176
181
assertEquals (0 , cc .getByte ("b3" ));
177
182
cc .putBoolean ("b4" , true );
@@ -222,23 +227,23 @@ public void testCasting() {
222
227
cc .putByteArray ("ba" , new byte []{Byte .MIN_VALUE , 0 , Byte .MAX_VALUE });
223
228
assertEquals (new ByteArrayTag (new byte []{Byte .MIN_VALUE , 0 , Byte .MAX_VALUE }), cc .getByteArrayTag ("ba" ));
224
229
assertNull (cc .getByteArrayTag ("baba" ));
225
- assertTrue ( Arrays . equals ( new byte []{ Byte .MIN_VALUE , 0 , Byte .MAX_VALUE }, cc .get ("ba" , ByteArrayTag .class ).getValue () ));
230
+ assertArrayEquals ( new byte [] { Byte .MIN_VALUE , 0 , Byte .MAX_VALUE }, cc .get ("ba" , ByteArrayTag .class ).getValue ());
226
231
assertThrowsRuntimeException (() -> cc .getIntArray ("ba" ), ClassCastException .class );
227
- assertTrue ( Arrays . equals ( new byte [0 ], cc .getByteArray ("baba" ) ));
232
+ assertArrayEquals ( new byte [0 ], cc .getByteArray ("baba" ));
228
233
229
234
cc .putIntArray ("ia" , new int []{Integer .MIN_VALUE , 0 , Integer .MAX_VALUE });
230
235
assertEquals (new IntArrayTag (new int []{Integer .MIN_VALUE , 0 , Integer .MAX_VALUE }), cc .getIntArrayTag ("ia" ));
231
236
assertNull (cc .getIntArrayTag ("iaia" ));
232
- assertTrue ( Arrays . equals ( new int []{ Integer .MIN_VALUE , 0 , Integer .MAX_VALUE }, cc .get ("ia" , IntArrayTag .class ).getValue () ));
237
+ assertArrayEquals ( new int [] { Integer .MIN_VALUE , 0 , Integer .MAX_VALUE }, cc .get ("ia" , IntArrayTag .class ).getValue ());
233
238
assertThrowsRuntimeException (() -> cc .getLongArray ("ia" ), ClassCastException .class );
234
- assertTrue ( Arrays . equals ( new int [0 ], cc .getIntArray ("iaia" ) ));
239
+ assertArrayEquals ( new int [0 ], cc .getIntArray ("iaia" ));
235
240
236
241
cc .putLongArray ("la" , new long []{Long .MIN_VALUE , 0 , Long .MAX_VALUE });
237
242
assertEquals (new LongArrayTag (new long []{Long .MIN_VALUE , 0 , Long .MAX_VALUE }), cc .getLongArrayTag ("la" ));
238
243
assertNull (cc .getLongArrayTag ("lala" ));
239
- assertTrue ( Arrays . equals ( new long []{ Long .MIN_VALUE , 0 , Long .MAX_VALUE }, cc .get ("la" , LongArrayTag .class ).getValue () ));
244
+ assertArrayEquals ( new long [] { Long .MIN_VALUE , 0 , Long .MAX_VALUE }, cc .get ("la" , LongArrayTag .class ).getValue ());
240
245
assertThrowsRuntimeException (() -> cc .getListTag ("la" ), ClassCastException .class );
241
- assertTrue ( Arrays . equals ( new long [0 ], cc .getLongArray ("lala" ) ));
246
+ assertArrayEquals ( new long [0 ], cc .getLongArray ("lala" ));
242
247
243
248
cc .put ("li" , new ListTag <>(IntTag .class ));
244
249
assertEquals (new ListTag <>(IntTag .class ), cc .getListTag ("li" ));
@@ -302,7 +307,7 @@ public void testEntrySet() {
302
307
303
308
public void testContains () {
304
309
CompoundTag ct = createCompoundTag ();
305
- assertTrue ( 3 == ct .size ());
310
+ assertEquals ( 3 , ct .size ());
306
311
assertTrue (ct .containsKey ("b" ));
307
312
assertTrue (ct .containsKey ("str" ));
308
313
assertTrue (ct .containsKey ("list" ));
@@ -359,4 +364,13 @@ public void testIterator() {
359
364
});
360
365
assertEquals (3 , ct .size ());
361
366
}
367
+
368
+ public void testPutIfNotNull () {
369
+ CompoundTag ct = new CompoundTag ();
370
+ assertEquals (0 , ct .size ());
371
+ ct .putIfNotNull ("foo" , new StringTag ("bar" ));
372
+ ct .putIfNotNull ("bar" , null );
373
+ assertEquals (1 , ct .size ());
374
+ assertEquals ("bar" , ct .getString ("foo" ));
375
+ }
362
376
}
0 commit comments