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/.
19 package cz.xelfi.quoridor.statistics.resources;
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;
26 import java.io.FileOutputStream;
27 import java.io.IOException;
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;
41 public class StatisticsTest {
42 private static File dir;
43 private static File fGames;
44 private static HttpServer stopAPI;
45 private WebResource apiResource;
47 public StatisticsTest() throws Exception {
51 public static void localeEnglish() throws Exception {
52 Locale.setDefault(Locale.ENGLISH);
53 dir = File.createTempFile("quoridor", ".dir");
56 fGames = new File(dir, "games");
59 System.setProperty("quoridor.dir", dir.getPath());
60 stopAPI = Statistics.start(9990);
62 File passwd = new File(dir, "passwd");
63 FileOutputStream os = new FileOutputStream(passwd);
64 os.write("test=pes\nJarda=darda\n".getBytes("UTF-8"));
69 public void setUp() throws Exception {
70 Client client = new Client();
71 apiResource = client.resource(new URI("http://localhost:9990/"));
75 public static void cleanUpAll() throws Exception {
77 if (stopAPI != null) {
82 static void deleteRec(File dir) throws IOException {
86 File[] arr = dir.listFiles();
95 @Test public void testApplicationWadl() {
96 String serviceWadl = apiResource.path("application.wadl").
97 accept(MediaTypes.WADL).get(String.class);
98 assertTrue(serviceWadl.length() > 0);
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();
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);
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"));