2 * Copyright (c) 2018. Developed by Hedgecode.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.hedgecode.chess;
20 import java.util.Iterator;
21 import java.util.LinkedList;
22 import java.util.List;
25 import org.apache.commons.configuration.XMLConfiguration;
26 import org.apache.commons.configuration.HierarchicalConfiguration;
27 import org.apache.commons.configuration.SubnodeConfiguration;
29 import org.junit.Test;
31 import org.hedgecode.chess.fen.FENBuilderTest;
32 import org.hedgecode.chess.position.Castle;
33 import org.hedgecode.chess.position.Color;
34 import org.hedgecode.chess.position.ColorPiece;
35 import org.hedgecode.chess.position.Position;
36 import org.hedgecode.chess.position.Positions;
37 import org.hedgecode.chess.position.Square;
40 * Abstract Common Chess Position Test class.
42 * @author Dmitry Samoshin aka gotty
44 public abstract class AbstractPositionTest extends AbstractXMLConfigTest {
46 protected static final String NAME = "name";
47 protected static final String POSITION = "position";
49 private static final String INITIAL = "initial";
50 private static final String EMPTY = "empty";
51 private static final String SQUARES = "squares";
52 private static final String MOVE = "move";
53 private static final String CASTLE = "castle";
54 private static final String EN_PASSANT = "enPassant";
55 private static final String HALFMOVE = "halfmove";
56 private static final String FULLMOVE = "fullmove";
58 private static final String MSG_POSITION = "[%s]:";
59 private static final String MSG_POSITION_SQUARE = "[%s -> Square %s]:";
60 private static final String MSG_POSITION_MOVE = "[%s -> Move]:";
61 private static final String MSG_POSITION_CASTLE = "[%s -> Castle (%s)]:";
62 private static final String MSG_POSITION_EN_PASSANT = "[%s -> En Passant]:";
63 private static final String MSG_POSITION_HALFMOVE = "[%s -> Halfmove]:";
64 private static final String MSG_POSITION_FULLMOVE = "[%s -> Fullmove]:";
66 private String positionName;
68 public abstract List<XMLConfiguration> getXMLPositions() throws Exception;
71 public void testPositions() throws Exception {
72 for (XMLConfiguration xmlConfig : getXMLPositions())
73 testPosition(xmlConfig);
76 public void testPosition(HierarchicalConfiguration xmlPosition) throws Exception {
77 boolean isInitial = false, isEmpty = false;
78 boolean isSquares = !xmlPosition.subset(SQUARES).isEmpty();
79 if (xmlPosition.containsKey(INITIAL))
80 isInitial = xmlPosition.getBoolean(INITIAL);
81 if (xmlPosition.containsKey(EMPTY))
82 isEmpty = xmlPosition.getBoolean(EMPTY);
84 isInitial || isEmpty || isSquares
86 if (isSquares && !isEmpty) {
87 SubnodeConfiguration squaresNode = xmlPosition.configurationAt(SQUARES);
88 Iterator<String> keys = squaresNode.getKeys();
89 while (keys.hasNext()) {
90 String key = keys.next();
91 String value = squaresNode.getString(key);
96 ColorPiece.valueOf(value)
100 if (xmlPosition.containsKey(MOVE)) {
103 xmlPosition.getString(MOVE)
107 if (!xmlPosition.subset(CASTLE).isEmpty()) {
108 SubnodeConfiguration castleNode = xmlPosition.configurationAt(CASTLE);
109 Iterator<String> keys = castleNode.getKeys();
110 while (keys.hasNext()) {
111 String key = keys.next();
112 String value = castleNode.getString(key);
114 Color.valueOf(key.toUpperCase())
117 Castle.valueOf(value)
121 if (xmlPosition.containsKey(EN_PASSANT)) {
124 xmlPosition.getString(EN_PASSANT)
128 if (xmlPosition.containsKey(HALFMOVE)) {
130 xmlPosition.getInt(HALFMOVE, -1) >= 0
133 if (xmlPosition.containsKey(FULLMOVE)) {
135 xmlPosition.getInt(FULLMOVE, 0) > 0
141 protected String getConfigName() {
142 return getClass().getSimpleName();
145 protected Position assignPosition(HierarchicalConfiguration xmlPosition) {
146 if (xmlPosition.containsKey(INITIAL) && xmlPosition.getBoolean(INITIAL)) {
147 return Positions.INITIAL.getPosition();
149 Position position = Positions.EMPTY.getPosition();
150 if (!xmlPosition.subset(SQUARES).isEmpty()) {
151 SubnodeConfiguration squaresNode = xmlPosition.configurationAt(SQUARES);
152 Iterator<String> keys = squaresNode.getKeys();
153 while (keys.hasNext()) {
154 String key = keys.next();
155 String value = squaresNode.getString(key);
157 ColorPiece.valueOf(value),
158 Square.getSquare(key)
162 if (xmlPosition.containsKey(MOVE))
165 xmlPosition.getString(MOVE)
168 if (!xmlPosition.subset(CASTLE).isEmpty()) {
169 SubnodeConfiguration castleNode = xmlPosition.configurationAt(CASTLE);
170 Iterator<String> keys = castleNode.getKeys();
171 while (keys.hasNext()) {
172 String key = keys.next();
173 String value = castleNode.getString(key);
175 Color.valueOf(key.toUpperCase()),
176 Castle.valueOf(value)
180 if (xmlPosition.containsKey(EN_PASSANT))
181 position.setEnPassant(
183 xmlPosition.getString(EN_PASSANT)
186 if (xmlPosition.containsKey(HALFMOVE))
187 position.setHalfMove(
188 xmlPosition.getInt(HALFMOVE)
190 if (xmlPosition.containsKey(FULLMOVE))
191 position.setFullMove(
192 xmlPosition.getInt(FULLMOVE)
197 protected void assertPositionEquals(Position expected, Position actual) {
216 expected.getCastle(Color.WHITE),
217 actual.getCastle(Color.WHITE)
225 expected.getCastle(Color.BLACK),
226 actual.getCastle(Color.BLACK)
230 MSG_POSITION_EN_PASSANT,
233 expected.getEnPassant(),
234 actual.getEnPassant()
238 MSG_POSITION_HALFMOVE,
241 expected.getHalfMove(),
246 MSG_POSITION_FULLMOVE,
249 expected.getFullMove(),
254 protected void assertDiagramEquals(Position expected, Position actual) {
262 expected.getSquares(),
267 private void assertSquaresEquals(Map<Square, ColorPiece> expected, Map<Square, ColorPiece> actual) {
268 for (Map.Entry<Square, ColorPiece> entry : expected.entrySet()) {
273 entry.getKey().name()
276 actual.get(entry.getKey())
281 protected void assertStringEquals(String expected, String actual) {
292 public String getPositionName() {
296 protected void setPositionName(String name) {
302 public static void main(String[] args) throws Exception {
303 XMLConfiguration xmlConfig = new XMLConfiguration(
305 FENBuilderTest.class.getResource("FENBuilderTest.xml").toURI()
308 for (HierarchicalConfiguration testConfig : xmlConfig.configurationsAt(TESTS)) {
309 AbstractPositionTest.testPosition(
310 testConfig.configurationAt(POSITION)