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
jaroslav@171
     1
/*
jaroslav@264
     2
 * Quoridor server and related libraries
jaroslav@264
     3
 * Copyright (C) 2009-2010 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
jaroslav@171
     4
 *
jaroslav@264
     5
 * This program is free software: you can redistribute it and/or modify
jaroslav@264
     6
 * it under the terms of the GNU General Public License as published by
jaroslav@264
     7
 * the Free Software Foundation, either version 3 of the License.
jaroslav@171
     8
 *
jaroslav@264
     9
 * This program is distributed in the hope that it will be useful,
jaroslav@264
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
jaroslav@264
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
jaroslav@264
    12
 * GNU General Public License for more details.
jaroslav@171
    13
 *
jaroslav@264
    14
 * You should have received a copy of the GNU General Public License
jaroslav@264
    15
 * along with this program. Look for COPYING file in the top folder.
jaroslav@264
    16
 * If not, see http://www.gnu.org/licenses/.
jaroslav@171
    17
 */
jaroslav@171
    18
jaroslav@171
    19
package cz.xelfi.quoridor.webidor;
jaroslav@171
    20
jaroslav@258
    21
import com.sun.jersey.test.framework.WebAppDescriptor;
jaroslav@258
    22
import com.sun.jersey.test.framework.AppDescriptor;
jaroslav@171
    23
import com.sun.jersey.api.client.GenericType;
jaroslav@171
    24
import java.util.List;
jaroslav@171
    25
import com.sun.jersey.test.framework.JerseyTest;
jaroslav@171
    26
import java.io.File;
jaroslav@171
    27
import java.io.FileOutputStream;
jaroslav@171
    28
import java.io.IOException;
jaroslav@171
    29
import java.util.Properties;
jaroslav@171
    30
import javax.ws.rs.core.MediaType;
jaroslav@171
    31
import org.junit.Test;
jaroslav@171
    32
import static org.junit.Assert.*;
jaroslav@171
    33
jaroslav@171
    34
/**
jaroslav@171
    35
 *
jaroslav@171
    36
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jaroslav@171
    37
 */
jaroslav@171
    38
public class AllGamesTest extends JerseyTest {
jaroslav@171
    39
    static {
jaroslav@171
    40
        System.setProperty("JERSEY_HTTP_PORT", "45420");
jaroslav@171
    41
    }
jaroslav@171
    42
jaroslav@171
    43
    private File dir;
jaroslav@171
    44
jaroslav@171
    45
    @Override
jaroslav@258
    46
    protected AppDescriptor configure() {
jaroslav@258
    47
        try {
jaroslav@258
    48
            dir = File.createTempFile("quoridor", ".dir");
jaroslav@258
    49
            dir.delete();
jaroslav@258
    50
            System.setProperty("quoridor.dir", dir.getPath());
jaroslav@258
    51
            dir.mkdirs();
jaroslav@258
    52
            File passwd = new File(dir, "passwd");
jaroslav@258
    53
            FileOutputStream os = new FileOutputStream(passwd);
jaroslav@258
    54
            os.write("Jarda=heslo\nJirka=pesko\nJan=pan\n".getBytes("UTF-8"));
jaroslav@258
    55
            os.close();
jaroslav@258
    56
        } catch (Exception ex) {
jaroslav@258
    57
            throw new IllegalStateException(ex);
jaroslav@258
    58
        }
jaroslav@285
    59
        return new WebAppDescriptor.Builder("cz.xelfi.quoridor.webidor.resources").contextPath("allgamess").build();
jaroslav@171
    60
    }
jaroslav@171
    61
jaroslav@171
    62
    @Override
jaroslav@171
    63
    public void tearDown() throws Exception {
jaroslav@171
    64
        deleteRec(dir);
jaroslav@171
    65
    }
jaroslav@171
    66
jaroslav@171
    67
    static void deleteRec(File dir) throws IOException {
jaroslav@171
    68
        if (dir == null) {
jaroslav@171
    69
            return;
jaroslav@171
    70
        }
jaroslav@171
    71
        File[] arr = dir.listFiles();
jaroslav@171
    72
        if (arr != null) {
jaroslav@171
    73
            for (File f : arr) {
jaroslav@171
    74
                deleteRec(f);
jaroslav@171
    75
            }
jaroslav@171
    76
        }
jaroslav@171
    77
        dir.delete();
jaroslav@171
    78
    }
jaroslav@171
    79
    @Test public void testListGames() throws Exception {
jaroslav@171
    80
        File usersDir = new File(dir, "users");
jaroslav@171
    81
        usersDir.mkdirs();
jaroslav@171
    82
        File fJarda = new File(usersDir, "Jarda");
jaroslav@171
    83
        {
jaroslav@171
    84
            Properties p = new Properties();
jaroslav@171
    85
            p.setProperty("email", "jar@da.cz");
jaroslav@171
    86
            p.setProperty("permission.games", "true");
jaroslav@171
    87
            p.store(new FileOutputStream(fJarda), "");
jaroslav@171
    88
        }
jaroslav@171
    89
        File fJirka = new File(usersDir, "Jirka");
jaroslav@171
    90
        {
jaroslav@171
    91
            Properties p = new Properties();
jaroslav@171
    92
            p.setProperty("email", "jir@ka.cz");
jaroslav@171
    93
            p.store(new FileOutputStream(fJirka), "");
jaroslav@171
    94
        }
jaroslav@171
    95
        File fJan = new File(usersDir, "Jan");
jaroslav@171
    96
        {
jaroslav@171
    97
            Properties p = new Properties();
jaroslav@171
    98
            p.setProperty("email", "j@an.cz");
jaroslav@171
    99
            p.store(new FileOutputStream(fJan), "");
jaroslav@171
   100
        }
jaroslav@171
   101
jaroslav@258
   102
        String logRoot = resource().path("login").
jaroslav@171
   103
            queryParam("name", "Jarda").
jaroslav@171
   104
            queryParam("password", "heslo").
jaroslav@171
   105
            accept(MediaType.TEXT_PLAIN).
jaroslav@171
   106
            put(String.class);
jaroslav@258
   107
        String logJirka = resource().path("login").
jaroslav@171
   108
            queryParam("name", "Jirka").
jaroslav@171
   109
            queryParam("password", "pesko").
jaroslav@171
   110
            accept(MediaType.TEXT_PLAIN).
jaroslav@171
   111
            put(String.class);
jaroslav@258
   112
        String logJan = resource().path("login").
jaroslav@171
   113
            queryParam("name", "Jan").
jaroslav@171
   114
            queryParam("password", "pan").
jaroslav@171
   115
            accept(MediaType.TEXT_PLAIN).
jaroslav@171
   116
            put(String.class);
jaroslav@171
   117
jaroslav@258
   118
        GameId s = resource().path("games").queryParam("white", "Jan")
jaroslav@171
   119
                .queryParam("loginID", logJan)
jaroslav@171
   120
                .queryParam("black", "Jirka").post(GameId.class);
jaroslav@258
   121
            resource().path("games/" + s.getId())
jaroslav@171
   122
                .queryParam("loginID", logJan)
jaroslav@171
   123
                .queryParam("player", "Jan").queryParam("move", "RESIGN").put(GameId.class);
jaroslav@171
   124
jaroslav@171
   125
        GenericType<List<GameId>> gType = new GenericType<List<GameId>>() {};
jaroslav@258
   126
        List<GameId> all = resource().path("games/").queryParam("loginID", logRoot).accept(MediaType.TEXT_XML).get(gType);
jaroslav@171
   127
        boolean found = false;
jaroslav@171
   128
        for (GameId id : all) {
jaroslav@171
   129
            if (id.getId().equals(s.getId())) {
jaroslav@171
   130
                found = true;
jaroslav@171
   131
                break;
jaroslav@171
   132
            }
jaroslav@171
   133
        }
jaroslav@171
   134
        assertTrue("List of games shall contai all games: " + all, found);
jaroslav@171
   135
jaroslav@258
   136
        Game end = resource().path("games/" + s.getId()).queryParam("loginID", logRoot).accept(MediaType.TEXT_XML).get(Game.class);
jaroslav@171
   137
        assertEquals("One can see status of games with priviledges", GameStatus.blackWon, end.getId().getStatus());
jaroslav@171
   138
    }
jaroslav@171
   139
}