jaroslav@115: /* jaroslav@264: * Quoridor server and related libraries jaroslav@264: * Copyright (C) 2009-2010 Jaroslav Tulach jaroslav@115: * jaroslav@264: * This program is free software: you can redistribute it and/or modify jaroslav@264: * it under the terms of the GNU General Public License as published by jaroslav@264: * the Free Software Foundation, either version 3 of the License. jaroslav@115: * jaroslav@264: * This program is distributed in the hope that it will be useful, jaroslav@264: * but WITHOUT ANY WARRANTY; without even the implied warranty of jaroslav@264: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the jaroslav@264: * GNU General Public License for more details. jaroslav@115: * jaroslav@264: * You should have received a copy of the GNU General Public License jaroslav@264: * along with this program. Look for COPYING file in the top folder. jaroslav@264: * If not, see http://www.gnu.org/licenses/. jaroslav@115: */ jaroslav@115: jaroslav@115: package cz.xelfi.quoridor.webidor.resources; jaroslav@115: jaroslav@258: import com.sun.jersey.test.framework.WebAppDescriptor; jaroslav@258: import com.sun.jersey.test.framework.AppDescriptor; jaroslav@115: import com.sun.jersey.test.framework.JerseyTest; jaroslav@285: import com.sun.jersey.test.framework.WebAppDescriptor.Builder; jaroslav@115: import cz.xelfi.quoridor.Board; jaroslav@115: import cz.xelfi.quoridor.Move; jaroslav@115: import cz.xelfi.quoridor.webidor.Game; jaroslav@115: import cz.xelfi.quoridor.webidor.GameId; jaroslav@115: import cz.xelfi.quoridor.webidor.Note; jaroslav@115: import java.io.File; jaroslav@118: import java.io.FileInputStream; jaroslav@115: import java.io.FileNotFoundException; jaroslav@115: import java.io.FileOutputStream; jaroslav@115: import java.io.IOException; jaroslav@118: import java.io.InputStreamReader; jaroslav@118: import java.io.Reader; jaroslav@115: import java.util.List; jtulach@117: import java.util.ResourceBundle; jaroslav@115: import javax.ws.rs.core.MediaType; jaroslav@115: import org.junit.Test; jaroslav@115: import static org.junit.Assert.*; jaroslav@115: jaroslav@115: /** jaroslav@115: * jaroslav@115: * @author Jaroslav Tulach jaroslav@115: */ jaroslav@115: public class ChatTest extends JerseyTest { jaroslav@149: static { jaroslav@149: System.setProperty("JERSEY_HTTP_PORT", "33437"); jaroslav@149: } jaroslav@115: private File dir; jaroslav@115: jaroslav@115: @Override jaroslav@258: protected AppDescriptor configure() { jaroslav@258: try { jaroslav@258: dir = File.createTempFile("quoridor", ".dir"); jaroslav@258: dir.delete(); jaroslav@258: System.setProperty("quoridor.dir", dir.getPath()); jaroslav@258: dir.mkdirs(); jaroslav@258: File passwd = new File(dir, "passwd"); jaroslav@258: FileOutputStream os = new FileOutputStream(passwd); jaroslav@258: os.write("Jarda=heslo\nJirka=pesko\n".getBytes("UTF-8")); jaroslav@258: os.close(); jaroslav@258: } catch (Exception ex) { jaroslav@258: throw new IllegalStateException(ex); jaroslav@258: } jaroslav@285: final Builder builder = new WebAppDescriptor.Builder("cz.xelfi.quoridor.webidor.resources"); jaroslav@285: return builder.contextPath("context").build(); jaroslav@115: } jaroslav@115: jaroslav@115: @Override jaroslav@115: public void tearDown() throws Exception { jaroslav@115: super.tearDown(); jaroslav@115: deleteRec(dir); jaroslav@115: } jaroslav@115: jaroslav@115: static void deleteRec(File dir) throws IOException { jaroslav@115: if (dir == null) { jaroslav@115: return; jaroslav@115: } jaroslav@115: File[] arr = dir.listFiles(); jaroslav@115: if (arr != null) { jaroslav@115: for (File f : arr) { jaroslav@115: deleteRec(f); jaroslav@115: } jaroslav@115: } jaroslav@115: dir.delete(); jaroslav@115: } jaroslav@115: jaroslav@115: @Test public void testCreateAGame() throws Exception { jaroslav@258: String logJarda = resource().path("login"). jaroslav@115: queryParam("name", "Jarda"). jaroslav@115: queryParam("password", "heslo"). jaroslav@115: accept(MediaType.TEXT_PLAIN). jaroslav@115: put(String.class); jaroslav@258: String logJirka = resource().path("login"). jaroslav@115: queryParam("name", "Jirka"). jaroslav@115: queryParam("password", "pesko"). jaroslav@115: accept(MediaType.TEXT_PLAIN). jaroslav@115: put(String.class); jaroslav@258: GameId s = resource().path("games").queryParam("loginID", logJarda). jaroslav@115: queryParam("white", "Jarda") jaroslav@115: .queryParam("black", "Jirka").post(GameId.class); jaroslav@115: jaroslav@115: Thread.sleep(100); jaroslav@115: final long now = System.currentTimeMillis(); jaroslav@115: if (s.getModified() >= now) { jaroslav@115: fail("The game is supposed to be modified in past"); jaroslav@115: } jaroslav@115: Thread.sleep(100); jaroslav@115: jtulach@117: ResourceBundle b = ResourceBundle.getBundle("cz/xelfi/quoridor/webidor/TestBundle"); jtulach@117: String comment = b.getString("COMMENT"); jtulach@117: jaroslav@258: GameId s1 = resource().path("games/" + s.getId()). jaroslav@115: queryParam("loginID", logJarda). jaroslav@115: queryParam("player", "Jarda").queryParam("move", "N").put(GameId.class); jaroslav@115: jaroslav@258: GameId comment1 = resource().path("games/" + s.getId()). jaroslav@115: queryParam("loginID", logJarda). jaroslav@115: queryParam("player", "Jarda").queryParam("comment", "I like this game!").put(GameId.class); jaroslav@130: assertEquals("One comment in the game", 1, comment1.getComments()); jaroslav@115: jaroslav@258: GameId comment2 = resource().path("games/" + s.getId()). jaroslav@115: queryParam("loginID", logJirka). jtulach@117: queryParam("player", "Jirka").queryParam("comment", comment).put(GameId.class); jaroslav@130: assertEquals("Snd comment in the game", 2, comment2.getComments()); jaroslav@130: jaroslav@258: GameId s2 = resource().path("games/" + s.getId()). jaroslav@115: queryParam("loginID", logJirka). jaroslav@115: queryParam("player", "Jirka").queryParam("move", "S").put(GameId.class); jaroslav@115: assertNotNull("Successful move", s2); jaroslav@130: assertEquals("Still two comments in the game", 2, s2.getComments()); jaroslav@115: jaroslav@115: File game = new File(new File(dir, "games"), s1.getId()); jaroslav@115: assertTrue("File for game exists", game.exists()); jaroslav@115: String content = readFile(game); jaroslav@115: jaroslav@115: if (!content.contains("# white: Jarda")) { jaroslav@115: fail(content); jaroslav@115: } jaroslav@115: if (!content.contains("# black: Jirka")) { jaroslav@115: fail(content); jaroslav@115: } jaroslav@115: if (!content.contains("N")) { jaroslav@115: fail(content); jaroslav@115: } jaroslav@115: if (!content.contains("... S")) { jaroslav@115: fail(content); jaroslav@115: } jaroslav@115: if (!content.contains("I like")) { jaroslav@115: fail(content); jaroslav@115: } jtulach@117: if (!content.contains(comment)) { jaroslav@115: fail(content); jaroslav@115: } jaroslav@115: jaroslav@115: Games read = new Games(new File(dir, "games"), new Quoridor()); jaroslav@115: List readGames = read.getGames(); jaroslav@115: assertEquals("One game read", 1, readGames.size()); jaroslav@115: Board board = readGames.get(0).getBoard(); jaroslav@115: assertEquals(1, board.getPlayers().get(0).getRow()); jaroslav@115: assertEquals(7, board.getPlayers().get(1).getRow()); jaroslav@115: assertEquals(Move.NORTH, readGames.get(0).getMoves().get(0).getMove()); jaroslav@115: assertEquals(Move.SOUTH, readGames.get(0).getMoves().get(1).getMove()); jaroslav@115: jaroslav@115: Game rg = readGames.get(0); jaroslav@115: assertNull("No comments on second move", rg.getMoves().get(1).getComments()); jaroslav@115: List cmnts = rg.getMoves().get(0).getComments(); jaroslav@115: assertNotNull("Some comments on first move", cmnts); jaroslav@115: assertEquals("Two comments: " + cmnts, 2, cmnts.size()); jaroslav@115: jaroslav@115: if (!cmnts.get(0).getComment().contains("I like")) { jaroslav@115: fail(); jaroslav@115: } jtulach@117: if (!cmnts.get(1).getComment().contains(comment)) { jaroslav@115: fail(); jaroslav@115: } jaroslav@115: jaroslav@121: Thread.sleep(1500); jaroslav@121: jaroslav@115: File tmp = File.createTempFile("test-board", "game"); jaroslav@115: read.storeGame(rg, tmp); jaroslav@115: jaroslav@130: String sss1 = readFile(game).replaceAll("@.*:", "date"); jaroslav@121: Thread.sleep(1500); jaroslav@121: jaroslav@130: assertEquals("Newly written file remains the same", sss1, readFile(tmp).replaceAll("@.*:", "date")); jaroslav@115: jaroslav@258: String sGame = resource().path("games").path(s.getId()).accept(MediaType.TEXT_XML).get(String.class); jaroslav@258: Game readGame = resource().path("games").path(s.getId()).accept(MediaType.TEXT_XML).get(Game.class); jaroslav@115: assertNotNull("Game really returned", readGame); jaroslav@115: jaroslav@115: assertEquals(Move.NORTH, readGame.getMoves().get(0).getMove()); jaroslav@115: assertEquals(Move.SOUTH, readGame.getMoves().get(1).getMove()); jaroslav@115: jaroslav@115: assertNull("No comments on second move", readGame.getMoves().get(1).getComments()); jaroslav@115: cmnts = readGame.getMoves().get(0).getComments(); jaroslav@115: assertNotNull("Some comments on first move", cmnts); jaroslav@115: assertEquals("Two comments: " + cmnts, 2, cmnts.size()); jaroslav@115: if (!cmnts.get(0).getComment().contains("I like")) { jaroslav@115: fail(); jaroslav@115: } jtulach@117: if (!cmnts.get(1).getComment().contains(comment)) { jaroslav@115: fail(); jaroslav@115: } jaroslav@115: } jaroslav@115: jaroslav@115: private String readFile(File game) throws IOException, FileNotFoundException { jaroslav@115: char[] arr = new char[4096]; jaroslav@118: Reader gameContent = new InputStreamReader(new FileInputStream(game), "UTF-8"); jaroslav@115: int len = gameContent.read(arr); jaroslav@115: String content = new String(arr, 0, len); jaroslav@115: return content; jaroslav@115: } jaroslav@115: jaroslav@115: }