quoridor/src/test/java/cz/xelfi/quoridor/SerializeTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sun, 30 Aug 2009 14:37:47 +0200
changeset 48 69e897fe8140
parent 15 8c8eafa8b3ae
child 178 4b78d4f028b3
permissions -rw-r--r--
Spliting Game into GameId and Game with full info about the state of the game
jtulach@9
     1
/*
jtulach@9
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
jtulach@9
     3
 *
jtulach@9
     4
 * The contents of this file are subject to the terms of either the GNU
jtulach@9
     5
 * General Public License Version 2 only ("GPL") or the Common
jtulach@9
     6
 * Development and Distribution License("CDDL") (collectively, the
jtulach@9
     7
 * "License"). You may not use this file except in compliance with the
jtulach@9
     8
 * License. You can obtain a copy of the License at
jtulach@9
     9
 * http://www.netbeans.org/cddl-gplv2.html
jtulach@9
    10
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
jtulach@9
    11
 * specific language governing permissions and limitations under the
jtulach@9
    12
 * License.  When distributing the software, include this License Header
jtulach@9
    13
 * Notice in each file and include the License file at
jtulach@9
    14
 * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
jtulach@9
    15
 * particular file as subject to the "Classpath" exception as provided
jtulach@9
    16
 * by Sun in the GPL Version 2 section of the License file that
jtulach@9
    17
 * accompanied this code. If applicable, add the following below the
jtulach@9
    18
 * License Header, with the fields enclosed by brackets [] replaced by
jtulach@9
    19
 * your own identifying information:
jtulach@9
    20
 * "Portions Copyrighted [year] [name of copyright owner]"
jtulach@9
    21
 *
jtulach@9
    22
 * Contributor(s):
jtulach@9
    23
 *
jtulach@9
    24
 * Portions Copyrighted 2009 Jaroslav Tulach
jtulach@9
    25
 */
jtulach@9
    26
package cz.xelfi.quoridor;
jtulach@9
    27
jtulach@15
    28
import cz.xelfi.quoridor.Player.Direction;
jtulach@9
    29
import java.io.IOException;
jtulach@9
    30
import java.io.StringReader;
jtulach@9
    31
import java.io.StringWriter;
jtulach@9
    32
jtulach@9
    33
/** Basic tests in simple configuration.
jtulach@9
    34
 *
jtulach@9
    35
 * @author Jaroslav Tulach
jtulach@9
    36
 */
jtulach@9
    37
public class SerializeTest extends BoardCase {
jtulach@9
    38
    public SerializeTest (String testName) {
jtulach@9
    39
        super (testName);
jtulach@9
    40
    }
jtulach@9
    41
jtulach@9
    42
    @Override
jtulach@9
    43
    protected Board move(Board b, int player, Direction... where) throws IllegalPositionException {
jtulach@9
    44
        try {
jtulach@9
    45
            Board read = reread(b);
jtulach@10
    46
            return read.move(read.getPlayers().get(player), where);
jtulach@9
    47
        } catch (IOException ex) {
jtulach@9
    48
            throw new IllegalStateException(ex);
jtulach@9
    49
        }
jtulach@9
    50
    }
jtulach@15
    51
    protected Board fence(Board b, int player, char x, int y, Fence.Orientation orie)
jtulach@9
    52
    throws IllegalPositionException {
jtulach@9
    53
        try {
jtulach@9
    54
            Board read = reread(b);
jtulach@10
    55
            return read.fence(read.getPlayers().get(player), x, y, orie);
jtulach@9
    56
        } catch (IOException ex) {
jtulach@9
    57
            throw new IllegalStateException(ex);
jtulach@9
    58
        }
jtulach@9
    59
    }
jtulach@9
    60
jtulach@13
    61
    @Override
jtulach@13
    62
    protected Board apply(Board b, Move move) throws IllegalPositionException {
jtulach@13
    63
        try {
jtulach@13
    64
            Board read = reread(b);
jtulach@13
    65
            return read.apply(move);
jtulach@13
    66
        } catch (IOException ex) {
jtulach@13
    67
            throw new IllegalStateException(ex);
jtulach@13
    68
        }
jtulach@13
    69
    }
jtulach@13
    70
jtulach@10
    71
    private Board reread(Board b) throws IOException, IllegalPositionException {
jtulach@9
    72
        StringWriter w = new StringWriter();
jtulach@9
    73
        b.write(w);
jtulach@9
    74
        w.close();
jaroslav@48
    75
        return Board.valueOf(w.toString());
jtulach@9
    76
    }
jtulach@13
    77
jtulach@9
    78
}