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