webidor/src/test/java/cz/xelfi/quoridor/webidor/AllGamesTest.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 com.sun.jersey.api.client.GenericType;
    24 import java.util.List;
    25 import com.sun.jersey.test.framework.JerseyTest;
    26 import java.io.File;
    27 import java.io.FileOutputStream;
    28 import java.io.IOException;
    29 import java.util.Properties;
    30 import javax.ws.rs.core.MediaType;
    31 import org.junit.Test;
    32 import static org.junit.Assert.*;
    33 
    34 /**
    35  *
    36  * @author Jaroslav Tulach <jtulach@netbeans.org>
    37  */
    38 public class AllGamesTest extends JerseyTest {
    39     static {
    40         System.setProperty("JERSEY_HTTP_PORT", "45420");
    41     }
    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\nJan=pan\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("allgamess").build();
    60     }
    61 
    62     @Override
    63     public void tearDown() throws Exception {
    64         deleteRec(dir);
    65     }
    66 
    67     static void deleteRec(File dir) throws IOException {
    68         if (dir == null) {
    69             return;
    70         }
    71         File[] arr = dir.listFiles();
    72         if (arr != null) {
    73             for (File f : arr) {
    74                 deleteRec(f);
    75             }
    76         }
    77         dir.delete();
    78     }
    79     @Test public void testListGames() throws Exception {
    80         File usersDir = new File(dir, "users");
    81         usersDir.mkdirs();
    82         File fJarda = new File(usersDir, "Jarda");
    83         {
    84             Properties p = new Properties();
    85             p.setProperty("email", "jar@da.cz");
    86             p.setProperty("permission.games", "true");
    87             p.store(new FileOutputStream(fJarda), "");
    88         }
    89         File fJirka = new File(usersDir, "Jirka");
    90         {
    91             Properties p = new Properties();
    92             p.setProperty("email", "jir@ka.cz");
    93             p.store(new FileOutputStream(fJirka), "");
    94         }
    95         File fJan = new File(usersDir, "Jan");
    96         {
    97             Properties p = new Properties();
    98             p.setProperty("email", "j@an.cz");
    99             p.store(new FileOutputStream(fJan), "");
   100         }
   101 
   102         String logRoot = resource().path("login").
   103             queryParam("name", "Jarda").
   104             queryParam("password", "heslo").
   105             accept(MediaType.TEXT_PLAIN).
   106             put(String.class);
   107         String logJirka = resource().path("login").
   108             queryParam("name", "Jirka").
   109             queryParam("password", "pesko").
   110             accept(MediaType.TEXT_PLAIN).
   111             put(String.class);
   112         String logJan = resource().path("login").
   113             queryParam("name", "Jan").
   114             queryParam("password", "pan").
   115             accept(MediaType.TEXT_PLAIN).
   116             put(String.class);
   117 
   118         GameId s = resource().path("games").queryParam("white", "Jan")
   119                 .queryParam("loginID", logJan)
   120                 .queryParam("black", "Jirka").post(GameId.class);
   121             resource().path("games/" + s.getId())
   122                 .queryParam("loginID", logJan)
   123                 .queryParam("player", "Jan").queryParam("move", "RESIGN").put(GameId.class);
   124 
   125         GenericType<List<GameId>> gType = new GenericType<List<GameId>>() {};
   126         List<GameId> all = resource().path("games/").queryParam("loginID", logRoot).accept(MediaType.TEXT_XML).get(gType);
   127         boolean found = false;
   128         for (GameId id : all) {
   129             if (id.getId().equals(s.getId())) {
   130                 found = true;
   131                 break;
   132             }
   133         }
   134         assertTrue("List of games shall contai all games: " + all, found);
   135 
   136         Game end = resource().path("games/" + s.getId()).queryParam("loginID", logRoot).accept(MediaType.TEXT_XML).get(Game.class);
   137         assertEquals("One can see status of games with priviledges", GameStatus.blackWon, end.getId().getStatus());
   138     }
   139 }