webidor/src/test/java/cz/xelfi/quoridor/webidor/FinishedGameTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sat, 20 Aug 2011 17:14:23 +0200
branchglassfish
changeset 285 bc4ddef89763
parent 264 d60370059c3c
permissions -rw-r--r--
Adjusting tests to use different path
     1 /*
     2  * Quoridor server and related libraries
     3  * Copyright (C) 2009-2010 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     4  *
     5  * This program is free software: you can redistribute it and/or modify
     6  * it under the terms of the GNU General Public License as published by
     7  * the Free Software Foundation, either version 3 of the License.
     8  *
     9  * This program is distributed in the hope that it will be useful,
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12  * GNU General Public License for more details.
    13  *
    14  * You should have received a copy of the GNU General Public License
    15  * along with this program. Look for COPYING file in the top folder.
    16  * If not, see http://www.gnu.org/licenses/.
    17  */
    18 
    19 package cz.xelfi.quoridor.webidor;
    20 
    21 import com.sun.jersey.test.framework.WebAppDescriptor;
    22 import com.sun.jersey.test.framework.AppDescriptor;
    23 import java.util.Properties;
    24 import com.sun.jersey.api.client.GenericType;
    25 import com.sun.jersey.api.client.UniformInterfaceException;
    26 import com.sun.jersey.test.framework.JerseyTest;
    27 import java.io.File;
    28 import java.io.FileOutputStream;
    29 import java.io.IOException;
    30 import java.util.List;
    31 import javax.ws.rs.core.MediaType;
    32 import org.junit.Test;
    33 import static org.junit.Assert.*;
    34 
    35 /**
    36  *
    37  * @author Jaroslav Tulach <jtulach@netbeans.org>
    38  */
    39 public class FinishedGameTest extends JerseyTest {
    40     static {
    41         System.setProperty("JERSEY_HTTP_PORT", "33435");
    42     }
    43     private File dir;
    44     
    45     @Override
    46     protected AppDescriptor configure() {
    47         try {
    48             dir = File.createTempFile("quoridor", ".dir");
    49             dir.delete();
    50             System.setProperty("quoridor.dir", dir.getPath());
    51             dir.mkdirs();
    52             File passwd = new File(dir, "passwd");
    53             FileOutputStream os = new FileOutputStream(passwd);
    54             os.write("Jarda=heslo\nJirka=pesko\nMaster=mr\n".getBytes("UTF-8"));
    55             os.close();
    56         } catch (Exception ex) {
    57             throw new IllegalStateException(ex);
    58         }
    59         return new WebAppDescriptor.Builder("cz.xelfi.quoridor.webidor.resources").contextPath("finishedGame").build();
    60     }
    61 
    62     @Override
    63     public void tearDown() throws Exception {
    64         super.tearDown();
    65         deleteRec(dir);
    66     }
    67 
    68     static void deleteRec(File dir) throws IOException {
    69         if (dir == null) {
    70             return;
    71         }
    72         File[] arr = dir.listFiles();
    73         if (arr != null) {
    74             for (File f : arr) {
    75                 deleteRec(f);
    76             }
    77         }
    78         dir.delete();
    79     }
    80     @Test public void testNotLoggedIn() {
    81         String status  = resource().path("login").queryParam("id", "not-logged-in").
    82                 accept(MediaType.TEXT_PLAIN).get(String.class);
    83         assertEquals("Nobody is logged in", "", status);
    84     }
    85 
    86 
    87     @Test public void testCreateAGame() throws Exception {
    88         String logJarda = resource().path("login").
    89             queryParam("name", "Jarda").
    90             queryParam("password", "heslo").
    91             accept(MediaType.TEXT_PLAIN).
    92             put(String.class);
    93         String logJirka = resource().path("login").
    94             queryParam("name", "Jirka").
    95             queryParam("password", "pesko").
    96             accept(MediaType.TEXT_PLAIN).
    97             put(String.class);
    98         assertNotNull("Logged in ok", logJarda);
    99         GameId s = resource().path("games").queryParam("white", "Jarda")
   100                 .queryParam("loginID", logJarda)
   101                 .queryParam("black", "Jirka").post(GameId.class);
   102 
   103         for (int i = 0; i < 3; i++) {
   104             resource().path("games/" + s.getId())
   105                 .queryParam("loginID", logJarda)
   106                 .queryParam("player", "Jarda").queryParam("move", "N").put(GameId.class);
   107             resource().path("games/" + s.getId())
   108                 .queryParam("loginID", logJirka)
   109                 .queryParam("player", "Jirka").queryParam("move", "S").put(GameId.class);
   110         }
   111 
   112         resource().path("games/" + s.getId())
   113             .queryParam("loginID", logJarda)
   114             .queryParam("player", "Jarda").queryParam("move", "N").put(GameId.class);
   115         resource().path("games/" + s.getId())
   116             .queryParam("loginID", logJirka)
   117             .queryParam("player", "Jirka").queryParam("move", "SS").put(GameId.class);
   118 
   119         
   120         GenericType<List<GameId>> gType = new GenericType<List<GameId>>() {};
   121         List<GameId> nothing = resource().path("games/").queryParam("status", "blackWon").accept(MediaType.TEXT_XML).get(gType);
   122         assertTrue("Nothing has been finished yet: " + nothing, nothing.isEmpty());
   123 
   124         for (int i = 0; i < 3; i++) {
   125             resource().path("games/" + s.getId())
   126                 .queryParam("loginID", logJarda)
   127                 .queryParam("player", "Jarda").queryParam("move", "N").put(GameId.class);
   128             resource().path("games/" + s.getId())
   129                 .queryParam("loginID", logJirka)
   130                 .queryParam("player", "Jirka").queryParam("move", "S").put(GameId.class);
   131         }
   132 
   133 
   134         try {
   135             Game end = resource().path("games/" + s.getId()).accept(MediaType.TEXT_XML).get(Game.class);
   136             fail("If the game is finished, one cannot get its status without login");
   137         } catch (UniformInterfaceException ex) {
   138             // OK
   139         }
   140         Game end = resource().path("games/" + s.getId()).queryParam("loginID", logJirka).accept(MediaType.TEXT_XML).get(Game.class);
   141         assertEquals("BlackWins", GameStatus.blackWon, end.getId().getStatus());
   142 
   143         assertEquals("Jirka wins", "Jirka", end.getCurrentPlayer());
   144 
   145         List<GameId> none = resource().path("games/").queryParam("status", "blackWon").accept(MediaType.TEXT_XML).get(gType);
   146         assertEquals("No games, for not logged in users: " + none, 0, none.size());
   147 
   148         List<GameId> something = resource().path("games/").queryParam("loginID", logJirka).queryParam("status", "blackWon").accept(MediaType.TEXT_XML).get(gType);
   149         assertEquals("One game finished: " + something, 1, something.size());
   150         assertEquals("Id is OK", end.getId().getId(), something.get(0).getId());
   151     }
   152 
   153     @Test public void testResignAGame() throws Exception {
   154         String logJarda = resource().path("login").
   155             queryParam("name", "Jarda").
   156             queryParam("password", "heslo").
   157             accept(MediaType.TEXT_PLAIN).
   158             put(String.class);
   159         GameId s = resource().path("games").queryParam("white", "Jarda")
   160                 .queryParam("loginID", logJarda)
   161                 .queryParam("black", "Jirka").post(GameId.class);
   162 
   163         assertTrue("In progress", s.getStatus().isInProgress());
   164 
   165         resource().path("games/" + s.getId()).
   166             queryParam("loginID", logJarda).
   167             queryParam("player", "Jarda").
   168             queryParam("move", "RESIGN").put(GameId.class);
   169         try {
   170             Game end = resource().path("games/" + s.getId()).accept(MediaType.TEXT_XML).get(Game.class);
   171             fail("Should not be able to get game when finished");
   172         } catch (UniformInterfaceException ex) {
   173             // OK
   174         }
   175         Game end = resource().path("games/" + s.getId()).queryParam("loginID", logJarda).accept(MediaType.TEXT_XML).get(Game.class);
   176         assertEquals("BlackWins", GameStatus.blackWon, end.getId().getStatus());
   177         assertEquals("Jirka wins", "Jirka", end.getCurrentPlayer());
   178 
   179         assertFalse("is finished", end.getId().getStatus().isInProgress());
   180     }
   181 
   182     @Test public void testResignBGame() throws Exception {
   183         String logJarda = resource().path("login").
   184             queryParam("name", "Jarda").
   185             queryParam("password", "heslo").
   186             accept(MediaType.TEXT_PLAIN).
   187             put(String.class);
   188         String logJirka = resource().path("login").
   189             queryParam("name", "Jirka").
   190             queryParam("password", "pesko").
   191             accept(MediaType.TEXT_PLAIN).
   192             put(String.class);
   193 
   194         GameId s = resource().path("games").queryParam("white", "Jarda")
   195                 .queryParam("loginID", logJarda)
   196                 .queryParam("black", "Jirka").post(GameId.class);
   197 
   198         assertTrue("In progress", s.getStatus().isInProgress());
   199 
   200         resource().path("games/" + s.getId()).
   201             queryParam("loginID", logJarda).
   202             queryParam("player", "Jarda").
   203             queryParam("move", "N").put(GameId.class);
   204         resource().path("games/" + s.getId()).
   205             queryParam("loginID", logJirka).
   206             queryParam("player", "Jirka").
   207             queryParam("move", "RESIGN").put(GameId.class);
   208 
   209 
   210         try {
   211             Game end = resource().path("games/" + s.getId()).accept(MediaType.TEXT_XML).get(Game.class);
   212             fail("Should not be able to get game when finished");
   213         } catch (UniformInterfaceException ex) {
   214             // OK
   215         }
   216         Game end = resource().path("games/" + s.getId()).queryParam("loginID", logJarda).accept(MediaType.TEXT_XML).get(Game.class);
   217         assertEquals("WhiteWins", GameStatus.whiteWon, end.getId().getStatus());
   218         assertEquals("Jarda wins", "Jarda", end.getCurrentPlayer());
   219 
   220         assertFalse("is finished", end.getId().getStatus().isInProgress());
   221     }
   222 
   223     @Test public void testResignForeignGame() throws Exception {
   224         String logJarda = resource().path("login").
   225             queryParam("name", "Jarda").
   226             queryParam("password", "heslo").
   227             accept(MediaType.TEXT_PLAIN).
   228             put(String.class);
   229         GameId s = resource().path("games").queryParam("white", "Jarda")
   230                 .queryParam("loginID", logJarda)
   231                 .queryParam("black", "Jirka").post(GameId.class);
   232         File usersDir = new File(dir, "users");
   233         usersDir.mkdirs();
   234         File Master = new File(usersDir, "Master");
   235         {
   236             Properties p = new Properties();
   237             p.setProperty("email", "mas@ter.cz");
   238             p.setProperty("permission.resign", "true");
   239             p.store(new FileOutputStream(Master), "");
   240         }
   241 
   242         assertTrue("In progress", s.getStatus().isInProgress());
   243         String logMaster = resource().path("login").
   244                 queryParam("name", "Master").
   245                 queryParam("password", "mr").
   246                 accept(MediaType.TEXT_PLAIN).
   247                 put(String.class);
   248 
   249         resource().path("games/" + s.getId()).
   250             queryParam("loginID", logMaster).
   251             queryParam("player", "Jarda").
   252             queryParam("move", "RESIGN").put(GameId.class);
   253         try {
   254             Game end = resource().path("games/" + s.getId()).accept(MediaType.TEXT_XML).get(Game.class);
   255             fail("Should not be able to get game when finished");
   256         } catch (UniformInterfaceException ex) {
   257             // OK
   258         }
   259         Game end = resource().path("games/" + s.getId()).queryParam("loginID", logJarda).accept(MediaType.TEXT_XML).get(Game.class);
   260         assertEquals("BlackWins", GameStatus.blackWon, end.getId().getStatus());
   261         assertEquals("Jirka wins", "Jirka", end.getCurrentPlayer());
   262 
   263         assertFalse("is finished", end.getId().getStatus().isInProgress());
   264     }
   265 
   266 }