Search in sources :

Example 51 with ExecutorTest

use of js.ExecutorTest in project TriggerReactor by wysohn.

the class AbstractTestExecutors method testSetBlock.

@Test
public void testSetBlock() throws Exception {
    // {block id} and is used in Block related event
    World mockWorld = mock(World.class);
    Block mockBlock = mock(Block.class);
    JsTest test = new ExecutorTest(engine, "SETBLOCK");
    test.addVariable("block", mockBlock);
    when(server.getWorld("world")).thenReturn(mockWorld);
    test.withArgs("STONE").test();
    verify(mockBlock).setType(eq(Material.STONE));
}
Also used : Block(org.bukkit.block.Block) JsTest(js.JsTest) ExecutorTest(js.ExecutorTest) ExecutorTest(js.ExecutorTest) Test(org.junit.Test) JsTest(js.JsTest)

Example 52 with ExecutorTest

use of js.ExecutorTest in project TriggerReactor by wysohn.

the class AbstractTestExecutors method testItemFrameRotate.

@Test
public void testItemFrameRotate() throws Exception {
    Location vLoc = mock(Location.class);
    Location vLoc2 = mock(Location.class);
    Block vBlock = mock(Block.class);
    World vWorld = mock(World.class);
    List<Entity> vEntities = new ArrayList<>();
    for (int i = 0; i < 4; i++) {
        vEntities.add(mock(ItemFrame.class));
    }
    JsTest test = new ExecutorTest(engine, "ITEMFRAMEROTATE");
    when(vLoc.getBlock()).thenReturn(vBlock);
    when(vBlock.getWorld()).thenReturn(vWorld);
    when(vBlock.getLocation()).thenReturn(vLoc2);
    when(vWorld.getNearbyEntities(vLoc2, 2.0, 2.0, 2.0)).thenReturn(vEntities);
    test.withArgs("NOne", vLoc).test();
    for (Entity entity : vEntities) {
        verify((ItemFrame) entity).setRotation(Rotation.valueOf("NOne".toUpperCase()));
    }
    assertJSError(() -> test.withArgs().test(), "Invalid parameters. Need [Rotation<string>, Location<location or number number number>]");
// TODO - need test for the situation of args.length == 4
}
Also used : Entity(org.bukkit.entity.Entity) ArrayList(java.util.ArrayList) Block(org.bukkit.block.Block) JsTest(js.JsTest) ItemFrame(org.bukkit.entity.ItemFrame) ExecutorTest(js.ExecutorTest) ExecutorTest(js.ExecutorTest) Test(org.junit.Test) JsTest(js.JsTest)

Example 53 with ExecutorTest

use of js.ExecutorTest in project TriggerReactor by wysohn.

the class AbstractTestExecutors method testMessageMultiThreaded.

@Test
public void testMessageMultiThreaded() throws Exception {
    Player mockPlayer = mock(Player.class);
    ExecutorService pool = Executors.newSingleThreadExecutor();
    when(mockMain.isServerThread()).thenReturn(false);
    when(mockMain.callSyncMethod(any(Callable.class))).then(invocation -> {
        Callable call = invocation.getArgument(0);
        return pool.submit(call);
    });
    Runnable run = new Runnable() {

        final JsTest test = new ExecutorTest(engine, "MESSAGE").addVariable("player", mockPlayer).withArgs("&cTest Message");

        @Override
        public void run() {
            for (int i = 0; i < 1000; i++) {
                try {
                    test.test();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    };
    Thread.UncaughtExceptionHandler handler = mock(Thread.UncaughtExceptionHandler.class);
    Thread thread1 = new Thread(run);
    thread1.setUncaughtExceptionHandler(handler);
    Thread thread2 = new Thread(run);
    thread2.setUncaughtExceptionHandler(handler);
    thread1.start();
    thread2.start();
    thread1.join();
    thread2.join();
    verify(handler, never()).uncaughtException(any(), any());
    String expected = ChatColor.translateAlternateColorCodes('&', "&cTest Message");
    verify(mockPlayer, times(2000)).sendMessage(argThat((ArgumentMatcher<String>) expected::equals));
}
Also used : Player(org.bukkit.entity.Player) IPlayer(io.github.wysohn.triggerreactor.core.bridge.entity.IPlayer) Callable(java.util.concurrent.Callable) ArgumentMatcher(org.mockito.ArgumentMatcher) ExecutorService(java.util.concurrent.ExecutorService) JsTest(js.JsTest) ExecutorTest(js.ExecutorTest) ExecutorTest(js.ExecutorTest) Test(org.junit.Test) JsTest(js.JsTest)

Example 54 with ExecutorTest

use of js.ExecutorTest in project TriggerReactor by wysohn.

the class AbstractTestExecutors method testLeverToggle.

@Test
public void testLeverToggle() throws Exception {
    Location vLoc = mock(Location.class);
    Block vBlock = mock(Block.class);
    BlockState vBS = mock(BlockState.class);
    Lever vLever = mock(Lever.class);
    JsTest test = new ExecutorTest(engine, "LEVERTOGGLE");
    when(vLoc.getBlock()).thenReturn(vBlock);
    when(vBlock.getState()).thenReturn(vBS);
    when(vBS.getData()).thenReturn(vLever);
    // case1
    when(vLever.isPowered()).thenReturn(false);
    test.withArgs(vLoc).test();
    verify(vLever).setPowered(true);
    // case2
    when(vLever.isPowered()).thenReturn(true);
    test.withArgs(vLoc).test();
    verify(vLever).setPowered(false);
    assertJSError(() -> test.withArgs().test(), "Invalid parameters. Need [Location<location or number number number>]");
// TODO - need test for the situation of args.length == 3
}
Also used : BlockState(org.bukkit.block.BlockState) Lever(org.bukkit.material.Lever) Block(org.bukkit.block.Block) JsTest(js.JsTest) ExecutorTest(js.ExecutorTest) ExecutorTest(js.ExecutorTest) Test(org.junit.Test) JsTest(js.JsTest)

Example 55 with ExecutorTest

use of js.ExecutorTest in project TriggerReactor by wysohn.

the class AbstractTestExecutors method testDropItem.

@Test
public void testDropItem() throws Exception {
    ItemFactory factory = mock(ItemFactory.class);
    Player player = mock(Player.class);
    World world = mock(World.class);
    when(server.getItemFactory()).thenReturn(factory);
    when(player.getWorld()).thenReturn(world);
    when(factory.equals(any(), any())).thenReturn(true);
    new ExecutorTest(engine, "DROPITEM").addVariable("player", player).withArgs("STONE", 1, "None", -204, 82, 266).test();
    verify(world).dropItem(new Location(world, -204, 82, 266), new ItemStack(Material.STONE));
}
Also used : Player(org.bukkit.entity.Player) IPlayer(io.github.wysohn.triggerreactor.core.bridge.entity.IPlayer) ItemFactory(org.bukkit.inventory.ItemFactory) ItemStack(org.bukkit.inventory.ItemStack) ExecutorTest(js.ExecutorTest) ExecutorTest(js.ExecutorTest) Test(org.junit.Test) JsTest(js.JsTest)

Aggregations

ExecutorTest (js.ExecutorTest)63 JsTest (js.JsTest)61 Test (org.junit.Test)60 Player (org.bukkit.entity.Player)48 IPlayer (io.github.wysohn.triggerreactor.core.bridge.entity.IPlayer)43 Block (org.bukkit.block.Block)16 ItemStack (org.bukkit.inventory.ItemStack)11 Location (org.bukkit.Location)6 World (org.bukkit.World)5 PlayerInventory (org.bukkit.inventory.PlayerInventory)5 BlockState (org.bukkit.block.BlockState)4 ArrayList (java.util.ArrayList)3 Entity (org.bukkit.entity.Entity)3 ItemMeta (org.bukkit.inventory.meta.ItemMeta)3 Lever (org.bukkit.material.Lever)3 BukkitTriggerReactorCore (io.github.wysohn.triggerreactor.bukkit.main.BukkitTriggerReactorCore)2 IInventory (io.github.wysohn.triggerreactor.core.bridge.IInventory)2 AbstractJavaPlugin (io.github.wysohn.triggerreactor.bukkit.main.AbstractJavaPlugin)1 VaultSupport (io.github.wysohn.triggerreactor.bukkit.manager.trigger.share.api.vault.VaultSupport)1 TriggerReactorCore (io.github.wysohn.triggerreactor.core.main.TriggerReactorCore)1