Search in sources :

Example 26 with JsTest

use of js.JsTest in project TriggerReactor by wysohn.

the class AbstractTestExecutors method testPlayer_SetFlyMode.

@Test
public void testPlayer_SetFlyMode() throws Exception {
    Player mockPlayer = mock(Player.class);
    JsTest test = new ExecutorTest(engine, "SETFLYMODE").addVariable("player", mockPlayer);
    for (boolean b : new boolean[] { true, false }) {
        test.withArgs(b).test();
        verify(mockPlayer).setAllowFlight(eq(b));
        verify(mockPlayer).setFlying(eq(b));
    }
    assertJSError(() -> test.withArgs(true, true).test(), "Incorrect number of arguments for executor SETFLYMODE");
    assertJSError(() -> test.withArgs("merp").test(), "Invalid argument for executor SETFLYMODE: merp");
}
Also used : Player(org.bukkit.entity.Player) IPlayer(io.github.wysohn.triggerreactor.core.bridge.entity.IPlayer) JsTest(js.JsTest) ExecutorTest(js.ExecutorTest) ExecutorTest(js.ExecutorTest) Test(org.junit.Test) JsTest(js.JsTest)

Example 27 with JsTest

use of js.JsTest 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 28 with JsTest

use of js.JsTest 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 29 with JsTest

use of js.JsTest 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 30 with JsTest

use of js.JsTest 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)

Aggregations

JsTest (js.JsTest)31 Test (org.junit.Test)30 ExecutorTest (js.ExecutorTest)27 Player (org.bukkit.entity.Player)19 IPlayer (io.github.wysohn.triggerreactor.core.bridge.entity.IPlayer)18 Block (org.bukkit.block.Block)6 PlaceholderTest (js.PlaceholderTest)4 Entity (org.bukkit.entity.Entity)4 BlockState (org.bukkit.block.BlockState)3 Lever (org.bukkit.material.Lever)3 ArrayList (java.util.ArrayList)2 Callable (java.util.concurrent.Callable)2 ExecutorService (java.util.concurrent.ExecutorService)2 BukkitTriggerReactorCore (io.github.wysohn.triggerreactor.bukkit.main.BukkitTriggerReactorCore)1 VaultSupport (io.github.wysohn.triggerreactor.bukkit.manager.trigger.share.api.vault.VaultSupport)1 IInventory (io.github.wysohn.triggerreactor.core.bridge.IInventory)1 TriggerReactorCore (io.github.wysohn.triggerreactor.core.main.TriggerReactorCore)1 AbstractInventoryTriggerManager (io.github.wysohn.triggerreactor.core.manager.trigger.inventory.AbstractInventoryTriggerManager)1 World (org.bukkit.World)1 ItemFrame (org.bukkit.entity.ItemFrame)1