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));
}
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
}
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));
}
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
}
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));
}
Aggregations