webidor/src/test/java/cz/xelfi/quoridor/webidor/AllGamesTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sun, 12 Sep 2010 00:06:44 +0200
changeset 258 935118a5831a
parent 171 524c7f359c4e
child 264 d60370059c3c
permissions -rw-r--r--
Upgrading to jersey 1.3
     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 2009 Jaroslav Tulach
    25  */
    26 
    27 package cz.xelfi.quoridor.webidor;
    28 
    29 import com.sun.jersey.test.framework.WebAppDescriptor;
    30 import com.sun.jersey.test.framework.AppDescriptor;
    31 import com.sun.jersey.api.client.GenericType;
    32 import java.util.List;
    33 import com.sun.jersey.test.framework.JerseyTest;
    34 import java.io.File;
    35 import java.io.FileOutputStream;
    36 import java.io.IOException;
    37 import java.util.Properties;
    38 import javax.ws.rs.core.MediaType;
    39 import org.junit.Test;
    40 import static org.junit.Assert.*;
    41 
    42 /**
    43  *
    44  * @author Jaroslav Tulach <jtulach@netbeans.org>
    45  */
    46 public class AllGamesTest extends JerseyTest {
    47     static {
    48         System.setProperty("JERSEY_HTTP_PORT", "45420");
    49     }
    50 
    51     private File dir;
    52 
    53     @Override
    54     protected AppDescriptor configure() {
    55         try {
    56             dir = File.createTempFile("quoridor", ".dir");
    57             dir.delete();
    58             System.setProperty("quoridor.dir", dir.getPath());
    59             dir.mkdirs();
    60             File passwd = new File(dir, "passwd");
    61             FileOutputStream os = new FileOutputStream(passwd);
    62             os.write("Jarda=heslo\nJirka=pesko\nJan=pan\n".getBytes("UTF-8"));
    63             os.close();
    64         } catch (Exception ex) {
    65             throw new IllegalStateException(ex);
    66         }
    67         return new WebAppDescriptor.Builder("cz.xelfi.quoridor.webidor.resources").build();
    68     }
    69 
    70     @Override
    71     public void tearDown() throws Exception {
    72         deleteRec(dir);
    73     }
    74 
    75     static void deleteRec(File dir) throws IOException {
    76         if (dir == null) {
    77             return;
    78         }
    79         File[] arr = dir.listFiles();
    80         if (arr != null) {
    81             for (File f : arr) {
    82                 deleteRec(f);
    83             }
    84         }
    85         dir.delete();
    86     }
    87     @Test public void testListGames() throws Exception {
    88         File usersDir = new File(dir, "users");
    89         usersDir.mkdirs();
    90         File fJarda = new File(usersDir, "Jarda");
    91         {
    92             Properties p = new Properties();
    93             p.setProperty("email", "jar@da.cz");
    94             p.setProperty("permission.games", "true");
    95             p.store(new FileOutputStream(fJarda), "");
    96         }
    97         File fJirka = new File(usersDir, "Jirka");
    98         {
    99             Properties p = new Properties();
   100             p.setProperty("email", "jir@ka.cz");
   101             p.store(new FileOutputStream(fJirka), "");
   102         }
   103         File fJan = new File(usersDir, "Jan");
   104         {
   105             Properties p = new Properties();
   106             p.setProperty("email", "j@an.cz");
   107             p.store(new FileOutputStream(fJan), "");
   108         }
   109 
   110         String logRoot = resource().path("login").
   111             queryParam("name", "Jarda").
   112             queryParam("password", "heslo").
   113             accept(MediaType.TEXT_PLAIN).
   114             put(String.class);
   115         String logJirka = resource().path("login").
   116             queryParam("name", "Jirka").
   117             queryParam("password", "pesko").
   118             accept(MediaType.TEXT_PLAIN).
   119             put(String.class);
   120         String logJan = resource().path("login").
   121             queryParam("name", "Jan").
   122             queryParam("password", "pan").
   123             accept(MediaType.TEXT_PLAIN).
   124             put(String.class);
   125 
   126         GameId s = resource().path("games").queryParam("white", "Jan")
   127                 .queryParam("loginID", logJan)
   128                 .queryParam("black", "Jirka").post(GameId.class);
   129             resource().path("games/" + s.getId())
   130                 .queryParam("loginID", logJan)
   131                 .queryParam("player", "Jan").queryParam("move", "RESIGN").put(GameId.class);
   132 
   133         GenericType<List<GameId>> gType = new GenericType<List<GameId>>() {};
   134         List<GameId> all = resource().path("games/").queryParam("loginID", logRoot).accept(MediaType.TEXT_XML).get(gType);
   135         boolean found = false;
   136         for (GameId id : all) {
   137             if (id.getId().equals(s.getId())) {
   138                 found = true;
   139                 break;
   140             }
   141         }
   142         assertTrue("List of games shall contai all games: " + all, found);
   143 
   144         Game end = resource().path("games/" + s.getId()).queryParam("loginID", logRoot).accept(MediaType.TEXT_XML).get(Game.class);
   145         assertEquals("One can see status of games with priviledges", GameStatus.blackWon, end.getId().getStatus());
   146     }
   147 }