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