1+ package me .ialistannen .mininbt ;
2+
3+ import static org .junit .jupiter .api .Assertions .assertEquals ;
4+
5+ import java .util .logging .Logger ;
6+ import me .ialistannen .mininbt .NBTWrappers .NBTTagCompound ;
7+ import me .ialistannen .mininbt .reflection .FluentReflection .FluentType ;
8+ import org .bukkit .Bukkit ;
9+ import org .bukkit .Material ;
10+ import org .bukkit .Server ;
11+ import org .bukkit .craftbukkit .v1_14_R1 .inventory .CraftItemFactory ;
12+ import org .bukkit .inventory .ItemStack ;
13+ import org .junit .jupiter .api .Disabled ;
14+ import org .junit .jupiter .api .Test ;
15+ import org .mockito .Mockito ;
16+
17+ class ItemNBTUtilTest {
18+
19+ @ Disabled // Pegs all cores at 100% and needs ~15 seconds and quite a lot of RAM
20+ @ Test
21+ public void tagRoundTrip () {
22+ NamedTimer timer = new NamedTimer ();
23+ timer .step ("Starting" );
24+ Server serverMock = Mockito .mock (Server .class );
25+ timer .step ("Mock created" );
26+ Mockito .when (serverMock .getItemFactory ()).thenReturn (
27+ CraftItemFactory .instance ()
28+ );
29+ Mockito .when (serverMock .getLogger ()).thenReturn (Logger .getLogger ("Test" ));
30+ timer .step ("Registered" );
31+
32+ new FluentType <>(Bukkit .class ).findField ()
33+ .withName ("server" )
34+ .findSingle ().getOrThrow ()
35+ .setValue (null , serverMock );
36+
37+ timer .step ("Set" );
38+
39+ NBTTagCompound baseTag = new NBTTagCompound ();
40+ baseTag .setString ("Hello world" , "!!" );
41+
42+ timer .step ("Tag created" );
43+ ItemStack item = new ItemStack (Material .GOLDEN_AXE );
44+ timer .step ("Item created" );
45+ ItemStack changed = ItemNBTUtil .setNBTTag (baseTag , item );
46+ timer .step ("Item tag set" );
47+
48+ assertEquals (
49+ new NBTTagCompound (),
50+ ItemNBTUtil .getTag (item )
51+ );
52+
53+ assertEquals (
54+ baseTag ,
55+ ItemNBTUtil .getTag (changed )
56+ );
57+ }
58+
59+ private static class NamedTimer {
60+
61+ private long current = System .currentTimeMillis ();
62+
63+ void step (String message ) {
64+ long duration = System .currentTimeMillis () - current ;
65+ System .out .println (message + " (" + duration + "ms)" );
66+ current = System .currentTimeMillis ();
67+ }
68+ }
69+ }
0 commit comments