From 1784d5c314409a78f264ffb9f567ce6d106b7da9 Mon Sep 17 00:00:00 2001 From: Trever Fischer Date: Mon, 27 Feb 2012 11:34:55 -0500 Subject: [PATCH] Add basic tests --- pom.xml | 7 ++- src/test/java/us/camin/APIServer.java | 66 +++++++++++++++++++++++++++ src/test/java/us/camin/JoinTest.java | 57 +++++++++++++++++++++++ 3 files changed, 129 insertions(+), 1 deletion(-) create mode 100644 src/test/java/us/camin/APIServer.java create mode 100644 src/test/java/us/camin/JoinTest.java diff --git a/pom.xml b/pom.xml index 6c8c579..49b5933 100644 --- a/pom.xml +++ b/pom.xml @@ -23,11 +23,16 @@ json compile 20090211 + + + org.mortbay.jetty + jetty + 6.1H.22 junit junit - 3.8.1 + 4.10 test diff --git a/src/test/java/us/camin/APIServer.java b/src/test/java/us/camin/APIServer.java new file mode 100644 index 0000000..6fbd086 --- /dev/null +++ b/src/test/java/us/camin/APIServer.java @@ -0,0 +1,66 @@ +package us.camin.tests; + +/* + This file is part of Caminus + + Caminus is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + Caminus is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with Caminus. If not, see . + */ + +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import javax.servlet.ServletOutputStream; +import javax.servlet.ServletException; +import org.mortbay.jetty.servlet.ServletHolder; +import org.mortbay.jetty.servlet.Context; +import org.mortbay.jetty.Server; + +import java.io.IOException; + +public class APIServer { + private Server m_server; + + public APIServer() { + m_server = new Server(8001); + Context api = new Context(m_server, "/api"); + api.addServlet(new ServletHolder(new ValidateServlet()), "/validate/*"); + api.addServlet(new ServletHolder(new MOTDServlet()), "/motd/*"); + } + + public void start() throws Exception { + m_server.start(); + } + + public void stop() throws Exception { + m_server.stop(); + } + + private class ValidateServlet extends HttpServlet { + public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException { + resp.setContentType("application/json"); + if (req.getPathInfo().equals("/TestUser")) + resp.sendError(200); + else + resp.sendError(404); + } + } + + private class MOTDServlet extends HttpServlet { + public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException { + resp.setContentType("application/json"); + ServletOutputStream out = resp.getOutputStream(); + out.println("{\"motd\": [\"Test MOTD\"]}"); + } + } +} diff --git a/src/test/java/us/camin/JoinTest.java b/src/test/java/us/camin/JoinTest.java new file mode 100644 index 0000000..754b1d2 --- /dev/null +++ b/src/test/java/us/camin/JoinTest.java @@ -0,0 +1,57 @@ +package us.camin.tests; + +/* + This file is part of Caminus + + Caminus is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + Caminus is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with Caminus. If not, see . + */ + +import org.json.JSONException; +import org.junit.Before; +import org.junit.After; +import org.junit.Test; +import static org.junit.Assert.*; + +import java.io.IOException; + +import us.camin.JoinListener; + +public class JoinTest { + private JoinListener listener; + private APIServer server; + @Before public void setup() throws Exception { + server = new APIServer(); + server.start(); + listener = new JoinListener(); + listener.setURL("http://localhost:8001/api/"); + } + + @After public void teardown() throws Exception { + server.stop(); + } + + @Test public void validUser() throws IOException { + assertTrue(listener.isUserAuthed("TestUser")); + } + + @Test public void invaliduser() throws IOException { + assertFalse(listener.isUserAuthed("InvalidUser")); + } + + @Test public void motd() throws IOException, JSONException { + String[] goodMOTD = {"Test MOTD"}; + String[] motd = listener.fetchMOTD("TestUser"); + assertArrayEquals(null, goodMOTD, motd); + } +}