statistics/src/test/java/cz/xelfi/quoridor/statistics/resources/StatisticsTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 14 Sep 2010 08:56:13 +0200
changeset 264 d60370059c3c
parent 178 4b78d4f028b3
child 266 15fcdfc4cd4a
permissions -rw-r--r--
Changing headers to GPLv3
jaroslav@178
     1
/*
jaroslav@264
     2
 * Quoridor server and related libraries
jaroslav@264
     3
 * Copyright (C) 2009-2010 Martin Rexa
jaroslav@178
     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@178
     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@178
    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@178
    17
 */
jaroslav@178
    18
package cz.xelfi.quoridor.statistics.resources;
jaroslav@178
    19
jaroslav@178
    20
import com.sun.jersey.api.client.Client;
jaroslav@178
    21
import com.sun.jersey.api.client.WebResource;
jaroslav@178
    22
import com.sun.jersey.core.header.MediaTypes;
jaroslav@178
    23
import com.sun.net.httpserver.HttpServer;
jaroslav@178
    24
import java.io.File;
jaroslav@178
    25
import java.io.FileOutputStream;
jaroslav@178
    26
import java.io.IOException;
jaroslav@178
    27
import java.net.URI;
jaroslav@178
    28
import java.util.Locale;
jaroslav@178
    29
import javax.ws.rs.core.MediaType;
jaroslav@178
    30
import org.junit.AfterClass;
jaroslav@178
    31
import org.junit.Before;
jaroslav@178
    32
import org.junit.BeforeClass;
jaroslav@178
    33
import org.junit.Test;
jaroslav@178
    34
import static org.junit.Assert.*;
jaroslav@178
    35
import cz.xelfi.quoridor.statistics.EloList;
jaroslav@178
    36
/**
jaroslav@178
    37
 *
jaroslav@178
    38
 * @author Martin Rexa
jaroslav@178
    39
 */
jaroslav@178
    40
public class StatisticsTest {
jaroslav@178
    41
    private static File dir;
jaroslav@178
    42
    private static File fGames;
jaroslav@178
    43
    private static HttpServer stopAPI;
jaroslav@178
    44
    private WebResource apiResource;
jaroslav@178
    45
jaroslav@178
    46
    public StatisticsTest() throws Exception {
jaroslav@178
    47
    }
jaroslav@178
    48
jaroslav@178
    49
    @BeforeClass
jaroslav@178
    50
    public static void localeEnglish() throws Exception {
jaroslav@178
    51
        Locale.setDefault(Locale.ENGLISH);
jaroslav@178
    52
        dir = File.createTempFile("quoridor", ".dir");
jaroslav@178
    53
        dir.delete();
jaroslav@178
    54
        dir.mkdirs();
jaroslav@178
    55
        fGames = new File(dir, "games");
jaroslav@178
    56
        fGames.mkdirs();
jaroslav@178
    57
jaroslav@178
    58
        System.setProperty("quoridor.dir", dir.getPath());
jaroslav@178
    59
        stopAPI = Statistics.start(9990);
jaroslav@178
    60
jaroslav@178
    61
        File passwd = new File(dir, "passwd");
jaroslav@178
    62
        FileOutputStream os = new FileOutputStream(passwd);
jaroslav@178
    63
        os.write("test=pes\nJarda=darda\n".getBytes("UTF-8"));
jaroslav@178
    64
        os.close();
jaroslav@178
    65
    }
jaroslav@178
    66
jaroslav@178
    67
    @Before
jaroslav@178
    68
    public void setUp() throws Exception {
jaroslav@178
    69
        Client client = new Client();
jaroslav@178
    70
        apiResource = client.resource(new URI("http://localhost:9990/"));
jaroslav@178
    71
    }
jaroslav@178
    72
jaroslav@178
    73
    @AfterClass
jaroslav@178
    74
    public static void cleanUpAll() throws Exception {
jaroslav@178
    75
        deleteRec(dir);
jaroslav@178
    76
        if (stopAPI != null) {
jaroslav@178
    77
            stopAPI.stop(0);
jaroslav@178
    78
        }
jaroslav@178
    79
    }
jaroslav@178
    80
jaroslav@178
    81
    static void deleteRec(File dir) throws IOException {
jaroslav@178
    82
        if (dir == null) {
jaroslav@178
    83
            return;
jaroslav@178
    84
        }
jaroslav@178
    85
        File[] arr = dir.listFiles();
jaroslav@178
    86
        if (arr != null) {
jaroslav@178
    87
            for (File f : arr) {
jaroslav@178
    88
                deleteRec(f);
jaroslav@178
    89
            }
jaroslav@178
    90
        }
jaroslav@178
    91
        dir.delete();
jaroslav@178
    92
    }
jaroslav@178
    93
jaroslav@178
    94
    @Test public void testApplicationWadl() {
jaroslav@178
    95
        String serviceWadl = apiResource.path("application.wadl").
jaroslav@178
    96
                accept(MediaTypes.WADL).get(String.class);
jaroslav@178
    97
        assertTrue(serviceWadl.length() > 0);
jaroslav@178
    98
    }
jaroslav@178
    99
jaroslav@178
   100
    @Test public void testEloList() throws Exception {
jaroslav@178
   101
        createFile(fGames, "g1", "# white: W\n# black: B\n# status: IN_PROGRESS\nN S\n\n");
jaroslav@178
   102
        createFile(fGames, "g2", "# white: W\n# black: B\n# status: blackWon\nRESIGN\n\n");
jaroslav@178
   103
        createFile(fGames, "g3", "# white: W\n# black: B\n# status: blackWon\nRESIGN\n\n");
jaroslav@178
   104
        createFile(fGames, "g4", "# white: W\n# black: B\n# status: blackWon\nRESIGN\n\n");
jaroslav@178
   105
        Statistics.processGames();
jaroslav@178
   106
jaroslav@178
   107
        EloList eloList = apiResource.path("elo").path("list").accept(MediaType.TEXT_XML).get(EloList.class);
jaroslav@178
   108
//        assertFalse(eloList.getFinalList().isEmpty());
jaroslav@178
   109
//        System.out.println("ELO LIST");
jaroslav@178
   110
//        for (EloEntry entry : eloList.getFinalList()){
jaroslav@178
   111
//            System.out.println(entry);
jaroslav@178
   112
//        }
jaroslav@178
   113
    }
jaroslav@178
   114
jaroslav@178
   115
    private void createFile(File dir, String filename, String content) throws Exception{
jaroslav@178
   116
        File f = new File(dir, filename);
jaroslav@178
   117
        FileOutputStream os = new FileOutputStream(f);
jaroslav@178
   118
        os.write(content.getBytes("UTF-8"));
jaroslav@178
   119
        os.close();
jaroslav@178
   120
    }
jaroslav@178
   121
jaroslav@178
   122
}