statistics/src/test/java/cz/xelfi/quoridor/statistics/resources/StatisticsTest.java
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/statistics/src/test/java/cz/xelfi/quoridor/statistics/resources/StatisticsTest.java Thu Jan 07 22:34:17 2010 +0100
1.3 @@ -0,0 +1,131 @@
1.4 +/*
1.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
1.6 + *
1.7 + * The contents of this file are subject to the terms of either the GNU
1.8 + * General Public License Version 2 only ("GPL") or the Common
1.9 + * Development and Distribution License("CDDL") (collectively, the
1.10 + * "License"). You may not use this file except in compliance with the
1.11 + * License. You can obtain a copy of the License at
1.12 + * http://www.netbeans.org/cddl-gplv2.html
1.13 + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
1.14 + * specific language governing permissions and limitations under the
1.15 + * License. When distributing the software, include this License Header
1.16 + * Notice in each file and include the License file at
1.17 + * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
1.18 + * particular file as subject to the "Classpath" exception as provided
1.19 + * by Sun in the GPL Version 2 section of the License file that
1.20 + * accompanied this code. If applicable, add the following below the
1.21 + * License Header, with the fields enclosed by brackets [] replaced by
1.22 + * your own identifying information:
1.23 + * "Portions Copyrighted [year] [name of copyright owner]"
1.24 + *
1.25 + * Contributor(s):
1.26 + *
1.27 + * Portions Copyrighted 2010 Martin Rexa
1.28 + */
1.29 +
1.30 +package cz.xelfi.quoridor.statistics.resources;
1.31 +
1.32 +import com.sun.jersey.api.client.Client;
1.33 +import com.sun.jersey.api.client.WebResource;
1.34 +import com.sun.jersey.core.header.MediaTypes;
1.35 +import com.sun.net.httpserver.HttpServer;
1.36 +import java.io.File;
1.37 +import java.io.FileOutputStream;
1.38 +import java.io.IOException;
1.39 +import java.net.URI;
1.40 +import java.util.Locale;
1.41 +import javax.ws.rs.core.MediaType;
1.42 +import org.junit.AfterClass;
1.43 +import org.junit.Before;
1.44 +import org.junit.BeforeClass;
1.45 +import org.junit.Test;
1.46 +import static org.junit.Assert.*;
1.47 +import cz.xelfi.quoridor.statistics.EloList;
1.48 +/**
1.49 + *
1.50 + * @author Martin Rexa
1.51 + */
1.52 +public class StatisticsTest {
1.53 + private static File dir;
1.54 + private static File fGames;
1.55 + private static HttpServer stopAPI;
1.56 + private WebResource apiResource;
1.57 +
1.58 + public StatisticsTest() throws Exception {
1.59 + }
1.60 +
1.61 + @BeforeClass
1.62 + public static void localeEnglish() throws Exception {
1.63 + Locale.setDefault(Locale.ENGLISH);
1.64 + dir = File.createTempFile("quoridor", ".dir");
1.65 + dir.delete();
1.66 + dir.mkdirs();
1.67 + fGames = new File(dir, "games");
1.68 + fGames.mkdirs();
1.69 +
1.70 + System.setProperty("quoridor.dir", dir.getPath());
1.71 + stopAPI = Statistics.start(9990);
1.72 +
1.73 + File passwd = new File(dir, "passwd");
1.74 + FileOutputStream os = new FileOutputStream(passwd);
1.75 + os.write("test=pes\nJarda=darda\n".getBytes("UTF-8"));
1.76 + os.close();
1.77 + }
1.78 +
1.79 + @Before
1.80 + public void setUp() throws Exception {
1.81 + Client client = new Client();
1.82 + apiResource = client.resource(new URI("http://localhost:9990/"));
1.83 + }
1.84 +
1.85 + @AfterClass
1.86 + public static void cleanUpAll() throws Exception {
1.87 + deleteRec(dir);
1.88 + if (stopAPI != null) {
1.89 + stopAPI.stop(0);
1.90 + }
1.91 + }
1.92 +
1.93 + static void deleteRec(File dir) throws IOException {
1.94 + if (dir == null) {
1.95 + return;
1.96 + }
1.97 + File[] arr = dir.listFiles();
1.98 + if (arr != null) {
1.99 + for (File f : arr) {
1.100 + deleteRec(f);
1.101 + }
1.102 + }
1.103 + dir.delete();
1.104 + }
1.105 +
1.106 + @Test public void testApplicationWadl() {
1.107 + String serviceWadl = apiResource.path("application.wadl").
1.108 + accept(MediaTypes.WADL).get(String.class);
1.109 + assertTrue(serviceWadl.length() > 0);
1.110 + }
1.111 +
1.112 + @Test public void testEloList() throws Exception {
1.113 + createFile(fGames, "g1", "# white: W\n# black: B\n# status: IN_PROGRESS\nN S\n\n");
1.114 + createFile(fGames, "g2", "# white: W\n# black: B\n# status: blackWon\nRESIGN\n\n");
1.115 + createFile(fGames, "g3", "# white: W\n# black: B\n# status: blackWon\nRESIGN\n\n");
1.116 + createFile(fGames, "g4", "# white: W\n# black: B\n# status: blackWon\nRESIGN\n\n");
1.117 + Statistics.processGames();
1.118 +
1.119 + EloList eloList = apiResource.path("elo").path("list").accept(MediaType.TEXT_XML).get(EloList.class);
1.120 +// assertFalse(eloList.getFinalList().isEmpty());
1.121 +// System.out.println("ELO LIST");
1.122 +// for (EloEntry entry : eloList.getFinalList()){
1.123 +// System.out.println(entry);
1.124 +// }
1.125 + }
1.126 +
1.127 + private void createFile(File dir, String filename, String content) throws Exception{
1.128 + File f = new File(dir, filename);
1.129 + FileOutputStream os = new FileOutputStream(f);
1.130 + os.write(content.getBytes("UTF-8"));
1.131 + os.close();
1.132 + }
1.133 +
1.134 +}