268 lines
12 KiB
Java
268 lines
12 KiB
Java
package games.dmg.spigotbase;
|
|
|
|
import static org.junit.jupiter.api.Assertions.*;
|
|
import static org.mockito.Mockito.*;
|
|
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
import java.util.logging.Logger;
|
|
import org.bukkit.Keyed;
|
|
import org.bukkit.NamespacedKey;
|
|
import org.bukkit.Server;
|
|
import org.bukkit.inventory.ItemStack;
|
|
import org.bukkit.inventory.Recipe;
|
|
import org.bukkit.inventory.RecipeChoice;
|
|
import org.bukkit.inventory.ShapedRecipe;
|
|
import org.junit.jupiter.api.Test;
|
|
|
|
final class PocketBaseRecipeRegistryTest {
|
|
@Test
|
|
void allThreeRegisterAsOneOwnedFailClosedGroupWithoutTouchingConflicts() {
|
|
NamespacedKey keystone = NamespacedKey.minecraft("keystone");
|
|
NamespacedKey creation = NamespacedKey.minecraft("sharing");
|
|
NamespacedKey recharge = NamespacedKey.minecraft("sharing_recharge");
|
|
Recipe first = recipe(keystone);
|
|
Recipe conflict = recipe(creation);
|
|
Recipe unattempted = recipe(recharge);
|
|
Server server = mock(Server.class);
|
|
when(server.addRecipe(first)).thenReturn(true);
|
|
when(server.addRecipe(conflict)).thenReturn(false);
|
|
when(server.getRecipe(keystone)).thenReturn(first);
|
|
when(server.removeRecipe(keystone)).thenReturn(true);
|
|
PocketBaseRecipeRegistry registry = new PocketBaseRecipeRegistry(
|
|
server, List.of(first, conflict, unattempted), Logger.getAnonymousLogger());
|
|
|
|
assertFalse(registry.register());
|
|
assertFalse(registry.isAvailable());
|
|
verify(server).removeRecipe(keystone);
|
|
verify(server, never()).removeRecipe(creation);
|
|
verify(server, never()).removeRecipe(recharge);
|
|
verify(server, never()).addRecipe(unattempted);
|
|
|
|
registry.close();
|
|
verify(server).removeRecipe(keystone);
|
|
|
|
Server successfulServer = mock(Server.class);
|
|
when(successfulServer.addRecipe(any())).thenReturn(true);
|
|
when(successfulServer.getRecipe(keystone)).thenReturn(first);
|
|
when(successfulServer.getRecipe(creation)).thenReturn(conflict);
|
|
when(successfulServer.getRecipe(recharge)).thenReturn(unattempted);
|
|
when(successfulServer.removeRecipe(any())).thenReturn(true);
|
|
PocketBaseRecipeRegistry successful = new PocketBaseRecipeRegistry(
|
|
successfulServer, List.of(first, conflict, unattempted), Logger.getAnonymousLogger());
|
|
assertTrue(successful.register());
|
|
assertTrue(successful.isAvailable());
|
|
successful.close();
|
|
verify(successfulServer).removeRecipe(keystone);
|
|
verify(successfulServer).removeRecipe(creation);
|
|
verify(successfulServer).removeRecipe(recharge);
|
|
}
|
|
|
|
@Test
|
|
void registrationDefinitionsReceiveUniqueLifecycleProvenanceGroups() {
|
|
ItemStack result = mock(ItemStack.class);
|
|
RecipeChoice choice = mock(RecipeChoice.class);
|
|
ShapedRecipe first = shapedRecipe(
|
|
NamespacedKey.minecraft("first"), result, choice);
|
|
ShapedRecipe second = shapedRecipe(
|
|
NamespacedKey.minecraft("second"), result, choice);
|
|
ShapedRecipe third = shapedRecipe(
|
|
NamespacedKey.minecraft("third"), result, choice);
|
|
|
|
new PocketBaseRecipeRegistry(
|
|
mock(Server.class), List.of(first, second, third), Logger.getAnonymousLogger());
|
|
|
|
var groups = org.mockito.ArgumentCaptor.forClass(String.class);
|
|
verify(first).setGroup(groups.capture());
|
|
verify(second).setGroup(groups.capture());
|
|
verify(third).setGroup(groups.capture());
|
|
assertEquals(3, groups.getAllValues().stream().distinct().count());
|
|
assertTrue(groups.getAllValues().stream()
|
|
.allMatch(group -> group.startsWith("spigotbase-owned-")));
|
|
}
|
|
|
|
@Test
|
|
void cleanupPreservesALaterForeignReplacementUnderAnOwnedKey() {
|
|
NamespacedKey replacedKey = NamespacedKey.minecraft("replaced");
|
|
NamespacedKey retainedKey = NamespacedKey.minecraft("retained");
|
|
NamespacedKey thirdKey = NamespacedKey.minecraft("third");
|
|
Recipe replaced = recipe(replacedKey);
|
|
Recipe replacement = recipe(replacedKey);
|
|
Recipe retained = recipe(retainedKey);
|
|
Recipe third = recipe(thirdKey);
|
|
Server server = mock(Server.class);
|
|
when(server.addRecipe(any())).thenReturn(true);
|
|
when(server.getRecipe(replacedKey)).thenReturn(replacement);
|
|
when(server.getRecipe(retainedKey)).thenReturn(retained);
|
|
when(server.getRecipe(thirdKey)).thenReturn(third);
|
|
when(server.removeRecipe(any())).thenReturn(true);
|
|
PocketBaseRecipeRegistry registry = new PocketBaseRecipeRegistry(
|
|
server, List.of(replaced, retained, third), Logger.getAnonymousLogger());
|
|
assertTrue(registry.register());
|
|
|
|
registry.close();
|
|
|
|
verify(server, never()).removeRecipe(replacedKey);
|
|
verify(server).removeRecipe(retainedKey);
|
|
verify(server).removeRecipe(thirdKey);
|
|
}
|
|
|
|
@Test
|
|
void cleanupRemovesAnActiveStructuralCopyOfTheOwnedDefinition() {
|
|
NamespacedKey firstKey = NamespacedKey.minecraft("structural");
|
|
ItemStack result = mock(ItemStack.class);
|
|
RecipeChoice choice = mock(RecipeChoice.class);
|
|
ShapedRecipe definition = shapedRecipe(firstKey, result, choice);
|
|
ShapedRecipe activeCopy = shapedRecipe(firstKey, result, choice);
|
|
Recipe second = recipe(NamespacedKey.minecraft("second"));
|
|
Recipe third = recipe(NamespacedKey.minecraft("third"));
|
|
Server server = mock(Server.class);
|
|
when(server.addRecipe(any())).thenReturn(true);
|
|
when(server.getRecipe(firstKey)).thenReturn(activeCopy);
|
|
when(server.getRecipe(((Keyed) second).getKey())).thenReturn(second);
|
|
when(server.getRecipe(((Keyed) third).getKey())).thenReturn(third);
|
|
when(server.removeRecipe(any())).thenReturn(true);
|
|
PocketBaseRecipeRegistry registry = new PocketBaseRecipeRegistry(
|
|
server, List.of(definition, second, third), Logger.getAnonymousLogger());
|
|
assertTrue(registry.register());
|
|
|
|
registry.close();
|
|
|
|
verify(server).removeRecipe(firstKey);
|
|
}
|
|
|
|
@Test
|
|
void runtimeSymbolRenamingStillMatchesTheOwnedIngredientGrid() {
|
|
NamespacedKey firstKey = NamespacedKey.minecraft("renamed-symbols");
|
|
ItemStack result = mock(ItemStack.class);
|
|
RecipeChoice firstChoice = mock(RecipeChoice.class);
|
|
RecipeChoice secondChoice = mock(RecipeChoice.class);
|
|
ShapedRecipe definition = shapedRecipe(firstKey, result,
|
|
new String[] {"ABA", "BAB", "ABA"},
|
|
Map.of('A', firstChoice, 'B', secondChoice));
|
|
ShapedRecipe activeCopy = shapedRecipe(firstKey, result,
|
|
new String[] {"abc", "def", "ghi"},
|
|
Map.of(
|
|
'a', firstChoice, 'b', secondChoice, 'c', firstChoice,
|
|
'd', secondChoice, 'e', firstChoice, 'f', secondChoice,
|
|
'g', firstChoice, 'h', secondChoice, 'i', firstChoice));
|
|
Recipe second = recipe(NamespacedKey.minecraft("second"));
|
|
Recipe third = recipe(NamespacedKey.minecraft("third"));
|
|
Server server = mock(Server.class);
|
|
when(server.addRecipe(any())).thenReturn(true);
|
|
when(server.getRecipe(firstKey)).thenReturn(activeCopy);
|
|
when(server.getRecipe(((Keyed) second).getKey())).thenReturn(second);
|
|
when(server.getRecipe(((Keyed) third).getKey())).thenReturn(third);
|
|
when(server.removeRecipe(any())).thenReturn(true);
|
|
PocketBaseRecipeRegistry registry = new PocketBaseRecipeRegistry(
|
|
server, List.of(definition, second, third), Logger.getAnonymousLogger());
|
|
assertTrue(registry.register());
|
|
|
|
assertTrue(registry.isAvailable());
|
|
assertTrue(registry.controls(activeCopy));
|
|
registry.close();
|
|
verify(server).removeRecipe(firstKey);
|
|
}
|
|
|
|
@Test
|
|
void failedOwnedCleanupIsRetriedOnDisableWithoutTouchingTheConflict() {
|
|
NamespacedKey owned = NamespacedKey.minecraft("owned");
|
|
NamespacedKey conflict = NamespacedKey.minecraft("conflict");
|
|
Recipe first = recipe(owned);
|
|
Recipe second = recipe(conflict);
|
|
Recipe third = recipe(NamespacedKey.minecraft("third"));
|
|
Server server = mock(Server.class);
|
|
when(server.addRecipe(first)).thenReturn(true);
|
|
when(server.addRecipe(second)).thenReturn(false);
|
|
when(server.getRecipe(owned)).thenReturn(first);
|
|
when(server.removeRecipe(owned))
|
|
.thenThrow(new IllegalStateException("temporary cleanup failure"))
|
|
.thenReturn(true);
|
|
PocketBaseRecipeRegistry registry = new PocketBaseRecipeRegistry(
|
|
server, List.of(first, second, third), Logger.getAnonymousLogger());
|
|
|
|
assertFalse(registry.register());
|
|
assertTrue(registry.controls(first),
|
|
"A failed-removal residual must remain controlled while active");
|
|
registry.close();
|
|
assertFalse(registry.controls(first));
|
|
|
|
verify(server, times(2)).removeRecipe(owned);
|
|
verify(server, never()).removeRecipe(conflict);
|
|
}
|
|
|
|
@Test
|
|
void falseRemovalResultRetainsOwnershipForDisableRetry() {
|
|
NamespacedKey owned = NamespacedKey.minecraft("false-removal");
|
|
Recipe first = recipe(owned);
|
|
Recipe conflict = recipe(NamespacedKey.minecraft("conflict"));
|
|
Recipe third = recipe(NamespacedKey.minecraft("third"));
|
|
Server server = mock(Server.class);
|
|
when(server.addRecipe(first)).thenReturn(true);
|
|
when(server.addRecipe(conflict)).thenReturn(false);
|
|
when(server.getRecipe(owned)).thenReturn(first);
|
|
when(server.removeRecipe(owned)).thenReturn(false, true);
|
|
PocketBaseRecipeRegistry registry = new PocketBaseRecipeRegistry(
|
|
server, List.of(first, conflict, third), Logger.getAnonymousLogger());
|
|
|
|
assertFalse(registry.register());
|
|
assertTrue(registry.controls(first));
|
|
registry.close();
|
|
assertFalse(registry.controls(first));
|
|
verify(server, times(2)).removeRecipe(owned);
|
|
}
|
|
|
|
@Test
|
|
void postInsertionRegistrationExceptionOwnsAndRollsBackTheInsertedRecipe() {
|
|
NamespacedKey firstKey = NamespacedKey.minecraft("first");
|
|
NamespacedKey insertedKey = NamespacedKey.minecraft("inserted-before-exception");
|
|
Recipe first = recipe(firstKey);
|
|
Recipe inserted = recipe(insertedKey);
|
|
Recipe third = recipe(NamespacedKey.minecraft("third"));
|
|
Server server = mock(Server.class);
|
|
when(server.addRecipe(first)).thenReturn(true);
|
|
when(server.addRecipe(inserted))
|
|
.thenThrow(new IllegalStateException("reload failed after insertion"));
|
|
when(server.getRecipe(firstKey)).thenReturn(first);
|
|
when(server.getRecipe(insertedKey)).thenReturn(inserted);
|
|
when(server.removeRecipe(any())).thenReturn(true);
|
|
PocketBaseRecipeRegistry registry = new PocketBaseRecipeRegistry(
|
|
server, List.of(first, inserted, third), Logger.getAnonymousLogger());
|
|
|
|
assertFalse(registry.register());
|
|
|
|
verify(server).removeRecipe(firstKey);
|
|
verify(server).removeRecipe(insertedKey);
|
|
verify(server, never()).addRecipe(third);
|
|
assertFalse(registry.controls(inserted));
|
|
}
|
|
|
|
private static ShapedRecipe shapedRecipe(
|
|
NamespacedKey key, ItemStack result, RecipeChoice choice) {
|
|
return shapedRecipe(key, result, new String[] {"A"}, Map.of('A', choice));
|
|
}
|
|
|
|
private static ShapedRecipe shapedRecipe(
|
|
NamespacedKey key,
|
|
ItemStack result,
|
|
String[] shape,
|
|
Map<Character, RecipeChoice> choices
|
|
) {
|
|
ShapedRecipe recipe = mock(ShapedRecipe.class);
|
|
when(recipe.getKey()).thenReturn(key);
|
|
when(recipe.getResult()).thenReturn(result);
|
|
when(recipe.getShape()).thenReturn(shape);
|
|
when(recipe.getGroup()).thenReturn("");
|
|
when(recipe.getCategory()).thenReturn(
|
|
org.bukkit.inventory.recipe.CraftingBookCategory.MISC);
|
|
when(recipe.getChoiceMap()).thenReturn(choices);
|
|
return recipe;
|
|
}
|
|
|
|
private static Recipe recipe(NamespacedKey key) {
|
|
Recipe recipe = mock(Recipe.class, withSettings().extraInterfaces(Keyed.class));
|
|
when(((Keyed) recipe).getKey()).thenReturn(key);
|
|
return recipe;
|
|
}
|
|
}
|