--- /dev/null
+/*
+ * Copyright (c) 2018. Developed by Hedgecode.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.hedgecode.chess;
+
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+
+import org.apache.commons.configuration.XMLConfiguration;
+import org.apache.commons.configuration.HierarchicalConfiguration;
+import org.apache.commons.configuration.SubnodeConfiguration;
+
+import org.junit.Test;
+
+import org.hedgecode.chess.position.Castle;
+import org.hedgecode.chess.position.Color;
+import org.hedgecode.chess.position.ColorPiece;
+import org.hedgecode.chess.position.Position;
+import org.hedgecode.chess.position.Positions;
+import org.hedgecode.chess.position.Square;
+
+/**
+ * Abstract Common Chess Position Test class.
+ *
+ * @author Dmitry Samoshin aka gotty
+ */
+public abstract class AbstractPositionTest extends AbstractXMLConfigTest {
+
+ protected static final String NAME = "name";
+ protected static final String POSITION = "position";
+
+ private static final String INITIAL = "initial";
+ private static final String EMPTY = "empty";
+ private static final String SQUARES = "squares";
+ private static final String MOVE = "move";
+ private static final String CASTLE = "castle";
+ private static final String EN_PASSANT = "enPassant";
+ private static final String HALFMOVE = "halfmove";
+ private static final String FULLMOVE = "fullmove";
+
+ private static final String MSG_POSITION = "[%s]:";
+ private static final String MSG_POSITION_SQUARE = "[%s -> Square %s]:";
+ private static final String MSG_POSITION_MOVE = "[%s -> Move]:";
+ private static final String MSG_POSITION_CASTLE = "[%s -> Castle (%s)]:";
+ private static final String MSG_POSITION_EN_PASSANT = "[%s -> En Passant]:";
+ private static final String MSG_POSITION_HALFMOVE = "[%s -> Halfmove]:";
+ private static final String MSG_POSITION_FULLMOVE = "[%s -> Fullmove]:";
+
+ private String positionName;
+
+ public abstract List<XMLConfiguration> getXMLPositions() throws Exception;
+
+ @Test
+ public void testPositions() throws Exception {
+ for (XMLConfiguration xmlConfig : getXMLPositions())
+ testPosition(xmlConfig);
+ }
+
+ public void testPosition(HierarchicalConfiguration xmlPosition) throws Exception {
+ boolean isInitial = false, isEmpty = false;
+ boolean isSquares = !xmlPosition.subset(SQUARES).isEmpty();
+ if (xmlPosition.containsKey(INITIAL))
+ isInitial = xmlPosition.getBoolean(INITIAL);
+ if (xmlPosition.containsKey(EMPTY))
+ isEmpty = xmlPosition.getBoolean(EMPTY);
+ assertTrue(
+ isInitial || isEmpty || isSquares
+ );
+ if (isSquares && !isEmpty) {
+ SubnodeConfiguration squaresNode = xmlPosition.configurationAt(SQUARES);
+ Iterator<String> keys = squaresNode.getKeys();
+ while (keys.hasNext()) {
+ String key = keys.next();
+ String value = squaresNode.getString(key);
+ assertNotNull(
+ Square.getSquare(key)
+ );
+ assertNotNull(
+ ColorPiece.valueOf(value)
+ );
+ }
+ }
+ if (xmlPosition.containsKey(MOVE)) {
+ assertNotNull(
+ Color.valueOf(
+ xmlPosition.getString(MOVE)
+ )
+ );
+ }
+ if (!xmlPosition.subset(CASTLE).isEmpty()) {
+ SubnodeConfiguration castleNode = xmlPosition.configurationAt(CASTLE);
+ Iterator<String> keys = castleNode.getKeys();
+ while (keys.hasNext()) {
+ String key = keys.next();
+ String value = castleNode.getString(key);
+ assertNotNull(
+ Color.valueOf(key.toUpperCase())
+ );
+ assertNotNull(
+ Castle.valueOf(value)
+ );
+ }
+ }
+ if (xmlPosition.containsKey(EN_PASSANT)) {
+ assertNotNull(
+ Square.getSquare(
+ xmlPosition.getString(EN_PASSANT)
+ )
+ );
+ }
+ if (xmlPosition.containsKey(HALFMOVE)) {
+ assertTrue(
+ xmlPosition.getInt(HALFMOVE, -1) >= 0
+ );
+ }
+ if (xmlPosition.containsKey(FULLMOVE)) {
+ assertTrue(
+ xmlPosition.getInt(FULLMOVE, 0) > 0
+ );
+ }
+ }
+
+ @Override
+ protected String getConfigName() {
+ return getClass().getSimpleName();
+ }
+
+ protected Position assignPosition(HierarchicalConfiguration xmlPosition) {
+ if (xmlPosition.containsKey(INITIAL) && xmlPosition.getBoolean(INITIAL)) {
+ return Positions.INITIAL.getPosition();
+ }
+ Position position = Positions.EMPTY.getPosition();
+ if (!xmlPosition.subset(SQUARES).isEmpty()) {
+ SubnodeConfiguration squaresNode = xmlPosition.configurationAt(SQUARES);
+ Iterator<String> keys = squaresNode.getKeys();
+ while (keys.hasNext()) {
+ String key = keys.next();
+ String value = squaresNode.getString(key);
+ position.setPiece(
+ ColorPiece.valueOf(value),
+ Square.getSquare(key)
+ );
+ }
+ }
+ if (xmlPosition.containsKey(MOVE))
+ position.setMove(
+ Color.valueOf(
+ xmlPosition.getString(MOVE)
+ )
+ );
+ if (!xmlPosition.subset(CASTLE).isEmpty()) {
+ SubnodeConfiguration castleNode = xmlPosition.configurationAt(CASTLE);
+ Iterator<String> keys = castleNode.getKeys();
+ while (keys.hasNext()) {
+ String key = keys.next();
+ String value = castleNode.getString(key);
+ position.setCastle(
+ Color.valueOf(key.toUpperCase()),
+ Castle.valueOf(value)
+ );
+ }
+ }
+ if (xmlPosition.containsKey(EN_PASSANT))
+ position.setEnPassant(
+ Square.getSquare(
+ xmlPosition.getString(EN_PASSANT)
+ )
+ );
+ if (xmlPosition.containsKey(HALFMOVE))
+ position.setHalfMove(
+ xmlPosition.getInt(HALFMOVE)
+ );
+ if (xmlPosition.containsKey(FULLMOVE))
+ position.setFullMove(
+ xmlPosition.getInt(FULLMOVE)
+ );
+ return position;
+ }
+
+ protected void assertPositionEquals(Position expected, Position actual) {
+ assertDiagramEquals(
+ expected,
+ actual
+ );
+ assertEquals(
+ String.format(
+ MSG_POSITION_MOVE,
+ positionName
+ ),
+ expected.getMove(),
+ actual.getMove()
+ );
+ assertEquals(
+ String.format(
+ MSG_POSITION_CASTLE,
+ positionName,
+ Color.WHITE
+ ),
+ expected.getCastle(Color.WHITE),
+ actual.getCastle(Color.WHITE)
+ );
+ assertEquals(
+ String.format(
+ MSG_POSITION_CASTLE,
+ positionName,
+ Color.BLACK
+ ),
+ expected.getCastle(Color.BLACK),
+ actual.getCastle(Color.BLACK)
+ );
+ assertEquals(
+ String.format(
+ MSG_POSITION_EN_PASSANT,
+ positionName
+ ),
+ expected.getEnPassant(),
+ actual.getEnPassant()
+ );
+ assertEquals(
+ String.format(
+ MSG_POSITION_HALFMOVE,
+ positionName
+ ),
+ expected.getHalfMove(),
+ actual.getHalfMove()
+ );
+ assertEquals(
+ String.format(
+ MSG_POSITION_FULLMOVE,
+ positionName
+ ),
+ expected.getFullMove(),
+ actual.getFullMove()
+ );
+ }
+
+ protected void assertDiagramEquals(Position expected, Position actual) {
+ assertNotNull(
+ expected
+ );
+ assertNotNull(
+ actual
+ );
+ assertSquaresEquals(
+ expected.getSquares(),
+ actual.getSquares()
+ );
+ }
+
+ private void assertSquaresEquals(Map<Square, ColorPiece> expected, Map<Square, ColorPiece> actual) {
+ for (Map.Entry<Square, ColorPiece> entry : expected.entrySet()) {
+ assertEquals(
+ String.format(
+ MSG_POSITION_SQUARE,
+ positionName,
+ entry.getKey().name()
+ ),
+ entry.getValue(),
+ actual.get(entry.getKey())
+ );
+ }
+ }
+
+ protected void assertStringEquals(String expected, String actual) {
+ assertEquals(
+ String.format(
+ MSG_POSITION,
+ positionName
+ ),
+ expected,
+ actual
+ );
+ }
+
+ public String getPositionName() {
+ return positionName;
+ }
+
+ protected void setPositionName(String name) {
+ positionName = name;
+ }
+
+
+/*
+ public static void main(String[] args) throws Exception {
+ XMLConfiguration xmlConfig = new XMLConfiguration(
+ new File(
+ FENBuilderTest.class.getResource("FENBuilderTest.xml").toURI()
+ )
+ );
+ for (HierarchicalConfiguration testConfig : xmlConfig.configurationsAt(TESTS)) {
+ AbstractPositionTest.testPosition(
+ testConfig.configurationAt(POSITION)
+ );
+ }
+ }
+*/
+
+}