public void testSerial( int handlerCount, boolean handlerSleep, int clientCount, int callerCount, int callCount) throws Exception { Server server = new TestServer(handlerCount, handlerSleep); InetSocketAddress addr = NetUtils.getConnectAddress(server); server.start(); Client[] clients = new Client[clientCount]; for (int i = 0; i < clientCount; i++) { clients[i] = new Client(LongWritable.class, conf); } SerialCaller[] callers = new SerialCaller[callerCount]; for (int i = 0; i < callerCount; i++) { callers[i] = new SerialCaller(clients[i % clientCount], addr, callCount); callers[i].start(); } for (int i = 0; i < callerCount; i++) { callers[i].join(); assertFalse(callers[i].failed); } for (int i = 0; i < clientCount; i++) { clients[i].stop(); } server.stop(); }
@Test public void shouldNotConnectWithoutCredentials() throws IOException { HttpConnectionUtil httpConnectionUtil = mockConnection(); Rules rules = new Rules(); Server server = new Server(rules, httpConnectionUtil); HttpURLConnection conn = mock(HttpURLConnection.class); when(httpConnectionUtil.getConnection(any(URL.class))).thenReturn(conn); when(conn.getContent()).thenReturn(new Object()); server.getUrl(new URL("http://exmaple.org/")); verify(conn, never()).setRequestProperty(anyString(), anyString()); }
@Test public void assertDeleteWorkspaceClosesReader() throws Exception { Reader spy = spy(new StringReader("")); when(server.execute(isA(MaskedArgumentListBuilder.class))).thenReturn(spy); new Workspaces(server).deleteWorkspace(new Workspace(server, "name")); verify(spy).close(); }
@Test public void testGetPipelineInstance() throws Exception { HttpConnectionUtil httpConnectionUtil = mockConnection(); Rules rules = new Rules(); rules.setGoServerHost("https://example.org"); Server server = new Server(rules, httpConnectionUtil); server.getPipelineInstance("pipeline-test", 42); ArgumentCaptor<URL> url = ArgumentCaptor.forClass(URL.class); verify(httpConnectionUtil).getConnection(url.capture()); assertThat( url.getValue().toString(), is("https://example.org/go/api/pipelines/pipeline-test/instance/42")); }
@Test public void testWorldImportWithNoFolder() { // Make sure the world directory do NOT exist // (it was created by the TestInstanceCreator) File worldFile = new File(TestInstanceCreator.serverDirectory, "world"); assertTrue(worldFile.exists()); assertTrue(worldFile.delete()); // Start actual testing. // Pull a core instance from the server. Plugin plugin = mockServer.getPluginManager().getPlugin("Multiverse-Core"); // Make sure Core is not null assertNotNull(plugin); // Make sure Core is enabled assertTrue(plugin.isEnabled()); // Initialize a fake command Command mockCommand = mock(Command.class); when(mockCommand.getName()).thenReturn("mv"); String[] normalArgs = new String[] {"import", "world", "normal"}; // Ensure we have a fresh copy of MV, 0 worlds. assertEquals(0, creator.getCore().getMVWorldManager().getMVWorlds().size()); // Import the first world. The world folder does not exist. plugin.onCommand(mockCommandSender, mockCommand, "", normalArgs); verify(mockCommandSender).sendMessage(ChatColor.RED + "FAILED."); verify(mockCommandSender) .sendMessage("That world folder does not exist. These look like worlds to me:"); // We should still have no worlds. assertEquals(0, creator.getCore().getMVWorldManager().getMVWorlds().size()); }
@Test public void testNullWorld() { // Pull a core instance from the server. Plugin plugin = mockServer.getPluginManager().getPlugin("Multiverse-Core"); // Make sure Core is not null assertNotNull(plugin); // Make sure Core is enabled assertTrue(plugin.isEnabled()); // Initialize a fake command Command mockCommand = mock(Command.class); when(mockCommand.getName()).thenReturn("mv"); // Ensure that there are no worlds imported. This is a fresh setup. assertEquals(0, creator.getCore().getMVWorldManager().getMVWorlds().size()); // Create the NULL world // The safe check is now BALLS SLOW. Use the -n to skip checking. String[] normalArgs = new String[] {"create", "nullworld", "normal", "-n"}; plugin.onCommand(mockCommandSender, mockCommand, "", normalArgs); // We should now have one world! assertEquals(1, creator.getCore().getMVWorldManager().getMVWorlds().size()); // Verify verify(mockCommandSender).sendMessage("Starting creation of world 'nullworld'..."); verify(mockCommandSender).sendMessage("Complete!"); WorldCreatorMatcher matcher = new WorldCreatorMatcher(new WorldCreator("nullworld")); verify(mockServer).createWorld(Matchers.argThat(matcher)); }
@Test public void shouldConnectWithCredentials() throws IOException { HttpConnectionUtil httpConnectionUtil = mockConnection(); Rules rules = new Rules(); Server server = new Server(rules, httpConnectionUtil); rules.setGoLogin("login"); rules.setGoPassword("pass"); HttpURLConnection conn = mock(HttpURLConnection.class); when(httpConnectionUtil.getConnection(any(URL.class))).thenReturn(conn); when(conn.getContent()).thenReturn(new Object()); server.getUrl(new URL("http://exmaple.org/")); verify(conn).setRequestProperty("Authorization", "Basic bG9naW46cGFzcw=="); }
@Test public void testWorldCreateInvalidGenerator() { // Pull a core instance from the server. Plugin plugin = mockServer.getPluginManager().getPlugin("Multiverse-Core"); // Make sure Core is not null assertNotNull(plugin); // Make sure Core is enabled assertTrue(plugin.isEnabled()); // Initialize a fake command Command mockCommand = mock(Command.class); when(mockCommand.getName()).thenReturn("mv"); // Ensure that there are no worlds imported. This is a fresh setup. assertEquals(0, creator.getCore().getMVWorldManager().getMVWorlds().size()); // Create the world String[] normalArgs = new String[] {"create", "newworld", "normal", "-g", "BogusGen"}; plugin.onCommand(mockCommandSender, mockCommand, "", normalArgs); // This command should halt, not creating any worlds assertEquals(0, creator.getCore().getMVWorldManager().getMVWorlds().size()); // Verify verify(mockCommandSender) .sendMessage( "Invalid generator! 'BogusGen'. " + ChatColor.RED + "Aborting world creation."); }
@Test public void assertUnknownWorkspaceDoesNotExists() throws Exception { when(server.execute(isA(MaskedArgumentListBuilder.class))).thenReturn(new StringReader("")); Workspaces workspaces = new Workspaces(server); assertFalse( "The unknown workspace was reported as existing", workspaces.exists(new Workspace(server, "name1"))); }
@Test public void assertGettingNewWorkspaceIsNotRetrievingServerList() throws Exception { when(server.execute(isA(MaskedArgumentListBuilder.class))).thenReturn(new StringReader("")); Workspaces workspaces = new Workspaces(server); workspaces.newWorkspace("name1"); assertNotNull("The get new workspace returned null", workspaces.getWorkspace("name1")); verify(server, times(1)).execute(isA(MaskedArgumentListBuilder.class)); }
@Test public void assertNewWorkspaceIsAddedToMap() throws Exception { when(server.execute(isA(MaskedArgumentListBuilder.class))).thenReturn(new StringReader("")); Workspaces workspaces = new Workspaces(server); Workspace workspace = workspaces.newWorkspace("name1"); assertNotNull("The new workspace was null", workspace); assertTrue("The workspace was reported as non existant", workspaces.exists(workspace)); }
@Test public void assertNewWorkspaceExistsIsNotRetrievingServerList() throws Exception { when(server.execute(isA(MaskedArgumentListBuilder.class))).thenReturn(new StringReader("")); Workspaces workspaces = new Workspaces(server); Workspace workspace = workspaces.newWorkspace("name1"); assertTrue("The get new workspace did not exists", workspaces.exists(workspace)); verify(server, times(1)).execute(isA(MaskedArgumentListBuilder.class)); }
public void testErrorClient() throws Exception { // start server Server server = new TestServer(1, false); InetSocketAddress addr = NetUtils.getConnectAddress(server); server.start(); // start client Client client = new Client(LongErrorWritable.class, conf); try { client.call(new LongErrorWritable(RANDOM.nextLong()), addr, null, null); fail("Expected an exception to have been thrown"); } catch (IOException e) { // check error Throwable cause = e.getCause(); assertTrue(cause instanceof IOException); assertEquals(LongErrorWritable.ERR_MSG, cause.getMessage()); } }
@Test public void assertWorkspaceExistsWithOnlyName() throws Exception { when(server.execute(isA(MaskedArgumentListBuilder.class))) .thenReturn( new StringReader( "--------- -------------- -------- ----------------------------------------------------------------------------------------------------------\n" + "\n" + "name1 SND\\redsolo_cp COMPUTER\n")); Workspaces workspaces = new Workspaces(server); assertTrue("The workspace was reported as non existant", workspaces.exists("name1")); }
@Test public void assertListFromServerIsParsedProperly() throws Exception { when(server.execute(isA(MaskedArgumentListBuilder.class))) .thenReturn( new StringReader( "--------- -------------- -------- ----------------------------------------------------------------------------------------------------------\n" + "\n" + "name1 SND\\redsolo_cp COMPUTER\n")); Workspaces workspaces = new Workspaces(server); Workspace workspace = workspaces.getWorkspace("name1"); assertNotNull("Workspace was null", workspace); }
@Test public void assertWorkspaceIsDeletedFromMap() throws Exception { when(server.execute(isA(MaskedArgumentListBuilder.class))).thenReturn(new StringReader("")); Workspaces workspaces = new Workspaces(server); // Populate the map in test object assertFalse( "The workspace was reported as existant", workspaces.exists(new Workspace(server, "name"))); Workspace workspace = workspaces.newWorkspace("name"); assertTrue( "The workspace was reported as non existant", workspaces.exists(new Workspace(server, "name"))); workspaces.deleteWorkspace(workspace); assertFalse("The workspace was reported as existant", workspaces.exists(workspace)); }
@Test public void testWorldImport() { // Pull a core instance from the server. Plugin plugin = mockServer.getPluginManager().getPlugin("Multiverse-Core"); // Make sure Core is not null assertNotNull(plugin); // Make sure Core is enabled assertTrue(plugin.isEnabled()); // Initialize a fake command Command mockCommand = mock(Command.class); when(mockCommand.getName()).thenReturn("mv"); // Ensure that there are no worlds imported. This is a fresh setup. assertEquals(0, creator.getCore().getMVWorldManager().getMVWorlds().size()); // Import the first world. String[] normalArgs = new String[] {"import", "world", "normal"}; plugin.onCommand(mockCommandSender, mockCommand, "", normalArgs); // We should now have one world imported! assertEquals(1, creator.getCore().getMVWorldManager().getMVWorlds().size()); // Import the second world. String[] netherArgs = new String[] {"import", "world_nether", "nether"}; plugin.onCommand(mockCommandSender, mockCommand, "", netherArgs); // We should now have 2 worlds imported! assertEquals(2, creator.getCore().getMVWorldManager().getMVWorlds().size()); // Import the third world. String[] skyArgs = new String[] {"import", "world_the_end", "end"}; plugin.onCommand(mockCommandSender, mockCommand, "", skyArgs); // We should now have 2 worlds imported! assertEquals(3, creator.getCore().getMVWorldManager().getMVWorlds().size()); // Verify that the commandSender has been called 3 times. verify(mockCommandSender).sendMessage("Starting import of world 'world'..."); verify(mockCommandSender).sendMessage("Starting import of world 'world_nether'..."); verify(mockCommandSender).sendMessage("Starting import of world 'world_the_end'..."); verify(mockCommandSender, VerificationModeFactory.times(3)) .sendMessage(ChatColor.GREEN + "Complete!"); }
@Test // TODO Migrate this to TestWorldProperties public void testModifyGameMode() { // Pull a core instance from the server. Plugin plugin = mockServer.getPluginManager().getPlugin("Multiverse-Core"); Command mockCommand = mock(Command.class); when(mockCommand.getName()).thenReturn("mv"); // Ensure that there are no worlds imported. This is a fresh setup. assertEquals(0, creator.getCore().getMVWorldManager().getMVWorlds().size()); this.createInitialWorlds(plugin, mockCommand); // Ensure that the default worlds have been created. assertEquals(3, creator.getCore().getMVWorldManager().getMVWorlds().size()); MultiverseWorld mainWorld = creator.getCore().getMVWorldManager().getMVWorld("world"); // Ensure that the default mode was normal. assertEquals(GameMode.SURVIVAL, mainWorld.getGameMode()); // Set the mode to creative in world. plugin.onCommand( mockCommandSender, mockCommand, "", new String[] {"modify", "set", "mode", "creative", "world"}); verify(mockCommandSender) .sendMessage( ChatColor.GREEN + "Success!" + ChatColor.WHITE + " Property " + ChatColor.AQUA + "mode" + ChatColor.WHITE + " was set to " + ChatColor.GREEN + "creative"); // Ensure the world is now a creative world assertEquals(GameMode.CREATIVE, mainWorld.getGameMode()); // More tests, with alternate syntax. plugin.onCommand( mockCommandSender, mockCommand, "", new String[] {"modify", "set", "gamemode", "0", "world"}); verify(mockCommandSender) .sendMessage( ChatColor.GREEN + "Success!" + ChatColor.WHITE + " Property " + ChatColor.AQUA + "gamemode" + ChatColor.WHITE + " was set to " + ChatColor.GREEN + "0"); assertEquals(GameMode.SURVIVAL, mainWorld.getGameMode()); // Now fail one. plugin.onCommand( mockCommandSender, mockCommand, "", new String[] {"modify", "set", "mode", "fish", "world"}); try { verify(mockCommandSender).sendMessage(ChatColor.RED + mainWorld.getPropertyHelp("mode")); } catch (PropertyDoesNotExistException e) { fail("Mode property did not exist."); } plugin.onCommand( mockCommandSender, mockCommand, "", new String[] {"modify", "set", "blah", "fish", "world"}); verify(mockCommandSender) .sendMessage( ChatColor.RED + "Sorry, You can't set: '" + ChatColor.GRAY + "blah" + ChatColor.RED + "'"); }
@Test public void assertGetUnknownWorkspaceReturnsNull() throws Exception { when(server.execute(isA(MaskedArgumentListBuilder.class))).thenReturn(new StringReader("")); Workspaces workspaces = new Workspaces(server); assertNull("The unknown workspace was not null", workspaces.getWorkspace("name1")); }