Add basic tests

This commit is contained in:
Trever Fischer 2012-02-27 11:34:55 -05:00
parent 54b2bbb92e
commit 1784d5c314
3 changed files with 129 additions and 1 deletions

View File

@ -23,11 +23,16 @@
<artifactId>json</artifactId>
<scope>compile</scope>
<version>20090211</version>
</dependency>
<dependency>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty</artifactId>
<version>6.1H.22</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<version>4.10</version>
<scope>test</scope>
</dependency>
</dependencies>

View File

@ -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 <http://www.gnu.org/licenses/>.
*/
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\"]}");
}
}
}

View File

@ -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 <http://www.gnu.org/licenses/>.
*/
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);
}
}