statistics/src/test/java/cz/xelfi/quoridor/statistics/resources/StatisticsTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Thu, 07 Jan 2010 22:34:17 +0100
branchstatistics-and-elo
changeset 178 4b78d4f028b3
child 264 d60370059c3c
permissions -rw-r--r--
Initial version of statistics and ELO rating. Donated by Martin Rexa
jaroslav@178
     1
/*
jaroslav@178
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
jaroslav@178
     3
 *
jaroslav@178
     4
 * The contents of this file are subject to the terms of either the GNU
jaroslav@178
     5
 * General Public License Version 2 only ("GPL") or the Common
jaroslav@178
     6
 * Development and Distribution License("CDDL") (collectively, the
jaroslav@178
     7
 * "License"). You may not use this file except in compliance with the
jaroslav@178
     8
 * License. You can obtain a copy of the License at
jaroslav@178
     9
 * http://www.netbeans.org/cddl-gplv2.html
jaroslav@178
    10
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
jaroslav@178
    11
 * specific language governing permissions and limitations under the
jaroslav@178
    12
 * License.  When distributing the software, include this License Header
jaroslav@178
    13
 * Notice in each file and include the License file at
jaroslav@178
    14
 * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
jaroslav@178
    15
 * particular file as subject to the "Classpath" exception as provided
jaroslav@178
    16
 * by Sun in the GPL Version 2 section of the License file that
jaroslav@178
    17
 * accompanied this code. If applicable, add the following below the
jaroslav@178
    18
 * License Header, with the fields enclosed by brackets [] replaced by
jaroslav@178
    19
 * your own identifying information:
jaroslav@178
    20
 * "Portions Copyrighted [year] [name of copyright owner]"
jaroslav@178
    21
 *
jaroslav@178
    22
 * Contributor(s):
jaroslav@178
    23
 *
jaroslav@178
    24
 * Portions Copyrighted 2010 Martin Rexa
jaroslav@178
    25
 */
jaroslav@178
    26
jaroslav@178
    27
package cz.xelfi.quoridor.statistics.resources;
jaroslav@178
    28
jaroslav@178
    29
import com.sun.jersey.api.client.Client;
jaroslav@178
    30
import com.sun.jersey.api.client.WebResource;
jaroslav@178
    31
import com.sun.jersey.core.header.MediaTypes;
jaroslav@178
    32
import com.sun.net.httpserver.HttpServer;
jaroslav@178
    33
import java.io.File;
jaroslav@178
    34
import java.io.FileOutputStream;
jaroslav@178
    35
import java.io.IOException;
jaroslav@178
    36
import java.net.URI;
jaroslav@178
    37
import java.util.Locale;
jaroslav@178
    38
import javax.ws.rs.core.MediaType;
jaroslav@178
    39
import org.junit.AfterClass;
jaroslav@178
    40
import org.junit.Before;
jaroslav@178
    41
import org.junit.BeforeClass;
jaroslav@178
    42
import org.junit.Test;
jaroslav@178
    43
import static org.junit.Assert.*;
jaroslav@178
    44
import cz.xelfi.quoridor.statistics.EloList;
jaroslav@178
    45
/**
jaroslav@178
    46
 *
jaroslav@178
    47
 * @author Martin Rexa
jaroslav@178
    48
 */
jaroslav@178
    49
public class StatisticsTest {
jaroslav@178
    50
    private static File dir;
jaroslav@178
    51
    private static File fGames;
jaroslav@178
    52
    private static HttpServer stopAPI;
jaroslav@178
    53
    private WebResource apiResource;
jaroslav@178
    54
jaroslav@178
    55
    public StatisticsTest() throws Exception {
jaroslav@178
    56
    }
jaroslav@178
    57
jaroslav@178
    58
    @BeforeClass
jaroslav@178
    59
    public static void localeEnglish() throws Exception {
jaroslav@178
    60
        Locale.setDefault(Locale.ENGLISH);
jaroslav@178
    61
        dir = File.createTempFile("quoridor", ".dir");
jaroslav@178
    62
        dir.delete();
jaroslav@178
    63
        dir.mkdirs();
jaroslav@178
    64
        fGames = new File(dir, "games");
jaroslav@178
    65
        fGames.mkdirs();
jaroslav@178
    66
jaroslav@178
    67
        System.setProperty("quoridor.dir", dir.getPath());
jaroslav@178
    68
        stopAPI = Statistics.start(9990);
jaroslav@178
    69
jaroslav@178
    70
        File passwd = new File(dir, "passwd");
jaroslav@178
    71
        FileOutputStream os = new FileOutputStream(passwd);
jaroslav@178
    72
        os.write("test=pes\nJarda=darda\n".getBytes("UTF-8"));
jaroslav@178
    73
        os.close();
jaroslav@178
    74
    }
jaroslav@178
    75
jaroslav@178
    76
    @Before
jaroslav@178
    77
    public void setUp() throws Exception {
jaroslav@178
    78
        Client client = new Client();
jaroslav@178
    79
        apiResource = client.resource(new URI("http://localhost:9990/"));
jaroslav@178
    80
    }
jaroslav@178
    81
jaroslav@178
    82
    @AfterClass
jaroslav@178
    83
    public static void cleanUpAll() throws Exception {
jaroslav@178
    84
        deleteRec(dir);
jaroslav@178
    85
        if (stopAPI != null) {
jaroslav@178
    86
            stopAPI.stop(0);
jaroslav@178
    87
        }
jaroslav@178
    88
    }
jaroslav@178
    89
jaroslav@178
    90
    static void deleteRec(File dir) throws IOException {
jaroslav@178
    91
        if (dir == null) {
jaroslav@178
    92
            return;
jaroslav@178
    93
        }
jaroslav@178
    94
        File[] arr = dir.listFiles();
jaroslav@178
    95
        if (arr != null) {
jaroslav@178
    96
            for (File f : arr) {
jaroslav@178
    97
                deleteRec(f);
jaroslav@178
    98
            }
jaroslav@178
    99
        }
jaroslav@178
   100
        dir.delete();
jaroslav@178
   101
    }
jaroslav@178
   102
jaroslav@178
   103
    @Test public void testApplicationWadl() {
jaroslav@178
   104
        String serviceWadl = apiResource.path("application.wadl").
jaroslav@178
   105
                accept(MediaTypes.WADL).get(String.class);
jaroslav@178
   106
        assertTrue(serviceWadl.length() > 0);
jaroslav@178
   107
    }
jaroslav@178
   108
jaroslav@178
   109
    @Test public void testEloList() throws Exception {
jaroslav@178
   110
        createFile(fGames, "g1", "# white: W\n# black: B\n# status: IN_PROGRESS\nN S\n\n");
jaroslav@178
   111
        createFile(fGames, "g2", "# white: W\n# black: B\n# status: blackWon\nRESIGN\n\n");
jaroslav@178
   112
        createFile(fGames, "g3", "# white: W\n# black: B\n# status: blackWon\nRESIGN\n\n");
jaroslav@178
   113
        createFile(fGames, "g4", "# white: W\n# black: B\n# status: blackWon\nRESIGN\n\n");
jaroslav@178
   114
        Statistics.processGames();
jaroslav@178
   115
jaroslav@178
   116
        EloList eloList = apiResource.path("elo").path("list").accept(MediaType.TEXT_XML).get(EloList.class);
jaroslav@178
   117
//        assertFalse(eloList.getFinalList().isEmpty());
jaroslav@178
   118
//        System.out.println("ELO LIST");
jaroslav@178
   119
//        for (EloEntry entry : eloList.getFinalList()){
jaroslav@178
   120
//            System.out.println(entry);
jaroslav@178
   121
//        }
jaroslav@178
   122
    }
jaroslav@178
   123
jaroslav@178
   124
    private void createFile(File dir, String filename, String content) throws Exception{
jaroslav@178
   125
        File f = new File(dir, filename);
jaroslav@178
   126
        FileOutputStream os = new FileOutputStream(f);
jaroslav@178
   127
        os.write(content.getBytes("UTF-8"));
jaroslav@178
   128
        os.close();
jaroslav@178
   129
    }
jaroslav@178
   130
jaroslav@178
   131
}