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