statistics/src/test/java/cz/xelfi/quoridor/statistics/resources/StatisticsTest.java
2 * Quoridor server and related libraries
3 * Copyright (C) 2009-2010 Martin Rexa
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.
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.
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/.
18 package cz.xelfi.quoridor.statistics.resources;
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;
25 import java.io.FileOutputStream;
26 import java.io.IOException;
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;
40 public class StatisticsTest {
41 private static File dir;
42 private static File fGames;
43 private static HttpServer stopAPI;
44 private WebResource apiResource;
46 public StatisticsTest() throws Exception {
50 public static void localeEnglish() throws Exception {
51 Locale.setDefault(Locale.ENGLISH);
52 dir = File.createTempFile("quoridor", ".dir");
55 fGames = new File(dir, "games");
58 System.setProperty("quoridor.dir", dir.getPath());
59 stopAPI = Statistics.start(9990);
61 File passwd = new File(dir, "passwd");
62 FileOutputStream os = new FileOutputStream(passwd);
63 os.write("test=pes\nJarda=darda\n".getBytes("UTF-8"));
68 public void setUp() throws Exception {
69 Client client = new Client();
70 apiResource = client.resource(new URI("http://localhost:9990/"));
74 public static void cleanUpAll() throws Exception {
76 if (stopAPI != null) {
81 static void deleteRec(File dir) throws IOException {
85 File[] arr = dir.listFiles();
94 @Test public void testApplicationWadl() {
95 String serviceWadl = apiResource.path("application.wadl").
96 accept(MediaTypes.WADL).get(String.class);
97 assertTrue(serviceWadl.length() > 0);
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();
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);
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"));