33import static org .hamcrest .MatcherAssert .assertThat ;
44import static org .hamcrest .Matchers .is ;
55import static org .mockito .ArgumentMatchers .any ;
6+ import static org .mockito .ArgumentMatchers .argThat ;
7+ import static org .mockito .ArgumentMatchers .eq ;
8+ import static org .mockito .Mockito .doAnswer ;
69import static org .mockito .Mockito .doReturn ;
710import static org .mockito .Mockito .mock ;
811import static org .mockito .Mockito .mockStatic ;
1114
1215import org .bukkit .Bukkit ;
1316import org .bukkit .Material ;
17+ import org .bukkit .NamespacedKey ;
1418import org .bukkit .Registry ;
19+ import org .bukkit .Tag ;
1520import org .bukkit .enchantments .Enchantment ;
1621import org .bukkit .inventory .ItemStack ;
22+ import org .bukkit .inventory .ItemType ;
1723import org .junit .jupiter .api .AfterAll ;
1824import org .junit .jupiter .api .BeforeAll ;
1925import org .junit .jupiter .api .BeforeEach ;
2228import org .junit .jupiter .params .ParameterizedTest ;
2329import org .junit .jupiter .params .provider .CsvSource ;
2430import org .junit .jupiter .params .provider .ValueSource ;
31+ import org .mockito .ArgumentMatcher ;
2532import org .mockito .MockedStatic ;
2633
2734@ TestInstance (TestInstance .Lifecycle .PER_CLASS )
2835class MetaVanillaBehaviorTest {
2936
3037 private MockedStatic <Bukkit > bukkit ;
31- private MockedStatic <RepairMaterial > repairMaterial ;
3238 private MetaVanillaBehavior behavior ;
3339
3440 @ BeforeAll
3541 void setUp () {
3642 bukkit = mockStatic ();
43+ // Set up for Material tag for all diamond stuff
44+ ArgumentMatcher <NamespacedKey > isDiamondTag = key ->
45+ key != null && key .getKey ().contains ("diamond" );
46+ bukkit .when (() -> Bukkit .getTag (eq ("items" ), argThat (isDiamondTag ), eq (Material .class )))
47+ .thenAnswer (invocation -> {
48+ Tag <Material > tag = mock ();
49+ doAnswer (invIsTagged -> {
50+ Material argument = invIsTagged .getArgument (0 );
51+ return argument .getKey ().getKey ().contains ("diamond" );
52+ }).when (tag ).isTagged (any ());
53+ return tag ;
54+ });
55+ // Set up for fallthrough to ItemType tag for all gold stuff
56+ ArgumentMatcher <NamespacedKey > isGoldTag = key ->
57+ key != null && key .getKey ().contains ("gold" );
58+ bukkit .when (() -> Bukkit .getTag (eq ("items" ), argThat (isGoldTag ), eq (ItemType .class )))
59+ .thenAnswer (invocation -> {
60+ Tag <ItemType > tag = mock ();
61+ doAnswer (invIsTagged -> {
62+ ItemType argument = invIsTagged .getArgument (0 );
63+ return argument .getKey ().getKey ().contains ("gold" );
64+ }).when (tag ).isTagged (any ());
65+ return tag ;
66+ });
67+ // All other tags will be nonexistent
68+ // Set up other registries (Registry.MATERIAL is backed by the enum and doesn't need mocking)
3769 bukkit .when (() -> Bukkit .getRegistry (any ())).thenAnswer (invocation -> mock (Registry .class ));
38- repairMaterial = mockStatic ();
39- repairMaterial .when (() -> RepairMaterial .repairs (any (), any ())).thenReturn (true );
4070 }
4171
4272 @ AfterAll
4373 void tearDownAll () {
44- repairMaterial .close ();
4574 bukkit .close ();
4675 }
4776
@@ -91,15 +120,65 @@ void itemsCombineEnchants(Material baseMat, Material additionMat, boolean result
91120
92121 @ Test
93122 void itemRepairedBy () {
123+ ItemStack baseStack = mock ();
124+ doReturn (Material .DIAMOND_PICKAXE ).when (baseStack ).getType ();
94125 MetaCachedStack base = mock ();
126+ doReturn (baseStack ).when (base ).getItem ();
127+ ItemStack additionStack = mock ();
128+ doReturn (Material .DIAMOND ).when (additionStack ).getType ();
95129 MetaCachedStack addition = mock ();
130+ doReturn (additionStack ).when (addition ).getItem ();
96131
97- assertThat (
98- "Delegates to RepairMaterial" ,
99- behavior .itemRepairedBy (base , addition ),
100- is (true )
101- );
102- repairMaterial .verify (() -> RepairMaterial .repairs (any (), any ()));
132+ assertThat ("Item is repairable" , behavior .itemRepairedBy (base , addition ), is (true ));
133+ }
134+
135+ @ Test
136+ void repairsNotRepairMat () {
137+ ItemStack baseStack = mock ();
138+ doReturn (Material .DIAMOND_PICKAXE ).when (baseStack ).getType ();
139+ MetaCachedStack base = mock ();
140+ doReturn (baseStack ).when (base ).getItem ();
141+ ItemStack additionStack = mock ();
142+ doReturn (Material .DIRT ).when (additionStack ).getType ();
143+ MetaCachedStack addition = mock ();
144+ doReturn (additionStack ).when (addition ).getItem ();
145+
146+ assertThat ("Item is not repairable" , behavior .itemRepairedBy (base , addition ), is (false ));
147+ }
148+
149+ @ Test
150+ void repairsItemTypeFallthrough () {
151+ ItemStack baseStack = mock ();
152+ doReturn (Material .GOLDEN_PICKAXE ).when (baseStack ).getType ();
153+ MetaCachedStack base = mock ();
154+ doReturn (baseStack ).when (base ).getItem ();
155+
156+ NamespacedKey key = mock ();
157+ doReturn ("gold" ).when (key ).getKey ();
158+ ItemType type = mock ();
159+ doReturn (key ).when (type ).getKey ();
160+ Material material = mock ();
161+ doReturn (type ).when (material ).asItemType ();
162+ ItemStack additionStack = mock ();
163+ doReturn (material ).when (additionStack ).getType ();
164+ MetaCachedStack addition = mock ();
165+ doReturn (additionStack ).when (addition ).getItem ();
166+
167+ assertThat ("Item is repairable" , behavior .itemRepairedBy (base , addition ), is (true ));
168+ }
169+
170+ @ Test
171+ void repairsNotRepairable () {
172+ ItemStack baseStack = mock ();
173+ doReturn (Material .WOODEN_PICKAXE ).when (baseStack ).getType ();
174+ MetaCachedStack base = mock ();
175+ doReturn (baseStack ).when (base ).getItem ();
176+ ItemStack additionStack = mock ();
177+ doReturn (Material .DIRT ).when (additionStack ).getType ();
178+ MetaCachedStack addition = mock ();
179+ doReturn (baseStack ).when (addition ).getItem ();
180+
181+ assertThat ("Item is not repairable" , behavior .itemRepairedBy (base , addition ), is (false ));
103182 }
104183
105- }
184+ }
0 commit comments