@Test public void testInviteThenLeave() throws IOException, InterruptedException { ChatServer server = new ChatServer(4445); PipedOutputStream pipe = new PipedOutputStream(); PipedInputStream in = new PipedInputStream(pipe); ByteArrayOutputStream out = new ByteArrayOutputStream(); PipedOutputStream pipe2 = new PipedOutputStream(); PipedInputStream in2 = new PipedInputStream(pipe2); ByteArrayOutputStream out2 = new ByteArrayOutputStream(); PipedOutputStream pipe3 = new PipedOutputStream(); PipedInputStream in3 = new PipedInputStream(pipe3); ByteArrayOutputStream out3 = new ByteArrayOutputStream(); Thread t = new Thread(new User(server, in, out, true, false)); Thread t2 = new Thread(new User(server, in2, out2, true, false)); Thread t3 = new Thread(new User(server, in3, out3, true, false)); t.start(); t2.start(); t3.start(); send(pipe, t, "login cliu 123\n"); send(pipe, t, "create\n"); send(pipe2, t2, "login user2 456\n"); send(pipe, t, "invite user2 0\n"); send(pipe3, t3, "register bleh pass\n"); send(pipe3, t3, "login bleh pass\n"); send(pipe2, t2, "accept 0\n"); send(pipe, t, "invite bleh 0\n"); send(pipe, t, "leave 0\n"); send(pipe3, t3, "accept 0\n"); server.kill(); assertEquals(true, out2.toString().contains("leave cliu 0")); assertEquals(false, out.toString().contains("leave cliu 0")); assertEquals(true, out3.toString().contains("enter bleh 0")); assertEquals(false, out3.toString().contains("leave cliu 0")); }
/** * @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest, * javax.servlet.http.HttpServletResponse) */ @Override protected void doPost(HttpServletRequest p_request, HttpServletResponse p_response) throws ServletException, IOException { boolean usernameExists = true; ChatServer chatServer = new ChatServer(); try { usernameExists = chatServer.isExistingUsername(p_request.getParameter("username")); } catch (Exception exception) { } if (usernameExists) { p_response.setStatus(Constants.USERNAME_ALREADY_EXISTS_HTTP_STATUS_CODE); } else { p_response.setStatus(HttpServletResponse.SC_OK); } }
@Test public void testSinglePersonLeaveRoomDies() throws IOException, InterruptedException { PrintWriter clear = new PrintWriter(new FileOutputStream(new File("src/server/userfile"))); clear.close(); ChatServer server = new ChatServer(4445); PipedOutputStream pipe = new PipedOutputStream(); PipedInputStream in = new PipedInputStream(pipe); ByteArrayOutputStream out = new ByteArrayOutputStream(); Thread t = new Thread(new User(server, in, out, true, false)); t.start(); send(pipe, t, "register cliu 123\n"); send(pipe, t, "login cliu 123\n"); send(pipe, t, "create\n"); send(pipe, t, "leave 0\n"); send(pipe, t, "create\n"); server.kill(); String user1expected = "connection successful\nregisterSuccess cliu\nwelcome cliu\nroomcreated 0\nroomcreated 1\n"; assertEquals(user1expected, out.toString()); }