webidor/src/test/java/cz/xelfi/quoridor/webidor/QuoridorTest.java
changeset 37 782d925cb5a1
parent 35 2e85dd878f04
child 39 6b889f2717e9
     1.1 --- a/webidor/src/test/java/cz/xelfi/quoridor/webidor/QuoridorTest.java	Sun Jul 12 13:59:07 2009 +0200
     1.2 +++ b/webidor/src/test/java/cz/xelfi/quoridor/webidor/QuoridorTest.java	Wed Jul 29 08:19:35 2009 +0200
     1.3 @@ -30,6 +30,10 @@
     1.4  import com.sun.jersey.api.client.UniformInterfaceException;
     1.5  import com.sun.jersey.core.header.MediaTypes;
     1.6  import com.sun.jersey.test.framework.JerseyTest;
     1.7 +import java.io.File;
     1.8 +import java.io.FileReader;
     1.9 +import java.io.IOException;
    1.10 +import java.util.Arrays;
    1.11  import java.util.List;
    1.12  import org.junit.Test;
    1.13  import static org.junit.Assert.*;
    1.14 @@ -39,11 +43,39 @@
    1.15   * @author Jaroslav Tulach <jtulach@netbeans.org>
    1.16   */
    1.17  public class QuoridorTest extends JerseyTest {
    1.18 +    private File dir;
    1.19  
    1.20      public QuoridorTest() throws Exception {
    1.21          super("cz.xelfi.quoridor.webidor.resources");
    1.22      }
    1.23  
    1.24 +    @Override
    1.25 +    public void setUp() throws Exception {
    1.26 +        dir = File.createTempFile("quoridor", ".dir");
    1.27 +        dir.delete();
    1.28 +        System.setProperty("quoridor.dir", dir.getPath());
    1.29 +        super.setUp();
    1.30 +    }
    1.31 +
    1.32 +    @Override
    1.33 +    public void tearDown() throws Exception {
    1.34 +        super.tearDown();
    1.35 +        deleteRec(dir);
    1.36 +    }
    1.37 +
    1.38 +    static void deleteRec(File dir) throws IOException {
    1.39 +        if (dir == null) {
    1.40 +            return;
    1.41 +        }
    1.42 +        File[] arr = dir.listFiles();
    1.43 +        if (arr != null) {
    1.44 +            for (File f : arr) {
    1.45 +                deleteRec(f);
    1.46 +            }
    1.47 +        }
    1.48 +        dir.delete();
    1.49 +    }
    1.50 +
    1.51      /**
    1.52       * Test if a WADL document is available at the relative path
    1.53       * "application.wadl".
    1.54 @@ -54,7 +86,7 @@
    1.55          assertTrue(serviceWadl.length() > 0);
    1.56      }
    1.57  
    1.58 -    @Test public void testCreateAGame() {
    1.59 +    @Test public void testCreateAGame() throws Exception {
    1.60          Game s = webResource.path("games").queryParam("white", "Jarda")
    1.61                  .queryParam("black", "Jirka").post(Game.class);
    1.62  
    1.63 @@ -83,6 +115,24 @@
    1.64          }
    1.65          Game s2 = webResource.path("games/" + s.getId()).queryParam("player", "Jirka").queryParam("move", "S").put(Game.class);
    1.66          assertNotNull("Successful move", s2);
    1.67 +
    1.68 +        File game = new File(new File(dir, "games"), s2.getId());
    1.69 +        assertTrue("File for game exists", game.exists());
    1.70 +
    1.71 +        char[] arr = new char[4096];
    1.72 +        FileReader gameContent = new FileReader(game);
    1.73 +        int len = gameContent.read(arr);
    1.74 +        String content = new String(arr, 0, len);
    1.75 +
    1.76 +        if (!content.contains("# white: Jarda")) {
    1.77 +            fail(content);
    1.78 +        }
    1.79 +        if (!content.contains("# black: Jirka")) {
    1.80 +            fail(content);
    1.81 +        }
    1.82 +        if (!content.contains("N S")) {
    1.83 +            fail(content);
    1.84 +        }
    1.85      }
    1.86  
    1.87  }