statistics/src/test/java/cz/xelfi/quoridor/statistics/resources/StatisticsTest.java
Initial version of statistics and ELO rating. Donated by Martin Rexa
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
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]"
24 * Portions Copyrighted 2010 Martin Rexa
27 package cz.xelfi.quoridor.statistics.resources;
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;
34 import java.io.FileOutputStream;
35 import java.io.IOException;
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;
49 public class StatisticsTest {
50 private static File dir;
51 private static File fGames;
52 private static HttpServer stopAPI;
53 private WebResource apiResource;
55 public StatisticsTest() throws Exception {
59 public static void localeEnglish() throws Exception {
60 Locale.setDefault(Locale.ENGLISH);
61 dir = File.createTempFile("quoridor", ".dir");
64 fGames = new File(dir, "games");
67 System.setProperty("quoridor.dir", dir.getPath());
68 stopAPI = Statistics.start(9990);
70 File passwd = new File(dir, "passwd");
71 FileOutputStream os = new FileOutputStream(passwd);
72 os.write("test=pes\nJarda=darda\n".getBytes("UTF-8"));
77 public void setUp() throws Exception {
78 Client client = new Client();
79 apiResource = client.resource(new URI("http://localhost:9990/"));
83 public static void cleanUpAll() throws Exception {
85 if (stopAPI != null) {
90 static void deleteRec(File dir) throws IOException {
94 File[] arr = dir.listFiles();
103 @Test public void testApplicationWadl() {
104 String serviceWadl = apiResource.path("application.wadl").
105 accept(MediaTypes.WADL).get(String.class);
106 assertTrue(serviceWadl.length() > 0);
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();
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);
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"));