[LIB-9] Add original chesshog source files
[chesshog.git] / src / test / java / org / hedgecode / chess / AbstractPositionTest.java
1 /*
2  * Copyright (c) 2018. Developed by Hedgecode.
3  *
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
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package org.hedgecode.chess;
18
19 import java.io.File;
20 import java.util.Iterator;
21 import java.util.LinkedList;
22 import java.util.List;
23 import java.util.Map;
24
25 import org.apache.commons.configuration.XMLConfiguration;
26 import org.apache.commons.configuration.HierarchicalConfiguration;
27 import org.apache.commons.configuration.SubnodeConfiguration;
28
29 import org.junit.Test;
30
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;
38
39 /**
40  * Abstract Common Chess Position Test class.
41  *
42  * @author Dmitry Samoshin aka gotty
43  */
44 public abstract class AbstractPositionTest extends AbstractXMLConfigTest {
45
46     protected static final String NAME = "name";
47     protected static final String POSITION = "position";
48
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";
57
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]:";
65
66     private String positionName;
67
68     public abstract List<XMLConfiguration> getXMLPositions() throws Exception;
69
70     @Test
71     public void testPositions() throws Exception {
72         for (XMLConfiguration xmlConfig : getXMLPositions())
73             testPosition(xmlConfig);
74     }
75
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);
83         assertTrue(
84                 isInitial || isEmpty || isSquares
85         );
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);
92                 assertNotNull(
93                         Square.getSquare(key)
94                 );
95                 assertNotNull(
96                         ColorPiece.valueOf(value)
97                 );
98             }
99         }
100         if (xmlPosition.containsKey(MOVE)) {
101             assertNotNull(
102                     Color.valueOf(
103                             xmlPosition.getString(MOVE)
104                     )
105             );
106         }
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);
113                 assertNotNull(
114                         Color.valueOf(key.toUpperCase())
115                 );
116                 assertNotNull(
117                         Castle.valueOf(value)
118                 );
119             }
120         }
121         if (xmlPosition.containsKey(EN_PASSANT)) {
122             assertNotNull(
123                     Square.getSquare(
124                             xmlPosition.getString(EN_PASSANT)
125                     )
126             );
127         }
128         if (xmlPosition.containsKey(HALFMOVE)) {
129             assertTrue(
130                     xmlPosition.getInt(HALFMOVE, -1) >= 0
131             );
132         }
133         if (xmlPosition.containsKey(FULLMOVE)) {
134             assertTrue(
135                     xmlPosition.getInt(FULLMOVE, 0) > 0
136             );
137         }
138     }
139
140     @Override
141     protected String getConfigName() {
142         return getClass().getSimpleName();
143     }
144
145     protected Position assignPosition(HierarchicalConfiguration xmlPosition) {
146         if (xmlPosition.containsKey(INITIAL) && xmlPosition.getBoolean(INITIAL)) {
147             return Positions.INITIAL.getPosition();
148         }
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);
156                 position.setPiece(
157                         ColorPiece.valueOf(value),
158                         Square.getSquare(key)
159                 );
160             }
161         }
162         if (xmlPosition.containsKey(MOVE))
163             position.setMove(
164                     Color.valueOf(
165                             xmlPosition.getString(MOVE)
166                     )
167             );
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);
174                 position.setCastle(
175                         Color.valueOf(key.toUpperCase()),
176                         Castle.valueOf(value)
177                 );
178             }
179         }
180         if (xmlPosition.containsKey(EN_PASSANT))
181             position.setEnPassant(
182                     Square.getSquare(
183                             xmlPosition.getString(EN_PASSANT)
184                     )
185             );
186         if (xmlPosition.containsKey(HALFMOVE))
187             position.setHalfMove(
188                     xmlPosition.getInt(HALFMOVE)
189             );
190         if (xmlPosition.containsKey(FULLMOVE))
191             position.setFullMove(
192                     xmlPosition.getInt(FULLMOVE)
193             );
194         return position;
195     }
196
197     protected void assertPositionEquals(Position expected, Position actual) {
198         assertDiagramEquals(
199                 expected,
200                 actual
201         );
202         assertEquals(
203                 String.format(
204                         MSG_POSITION_MOVE,
205                         positionName
206                 ),
207                 expected.getMove(),
208                 actual.getMove()
209         );
210         assertEquals(
211                 String.format(
212                         MSG_POSITION_CASTLE,
213                         positionName,
214                         Color.WHITE
215                 ),
216                 expected.getCastle(Color.WHITE),
217                 actual.getCastle(Color.WHITE)
218         );
219         assertEquals(
220                 String.format(
221                         MSG_POSITION_CASTLE,
222                         positionName,
223                         Color.BLACK
224                 ),
225                 expected.getCastle(Color.BLACK),
226                 actual.getCastle(Color.BLACK)
227         );
228         assertEquals(
229                 String.format(
230                         MSG_POSITION_EN_PASSANT,
231                         positionName
232                 ),
233                 expected.getEnPassant(),
234                 actual.getEnPassant()
235         );
236         assertEquals(
237                 String.format(
238                         MSG_POSITION_HALFMOVE,
239                         positionName
240                 ),
241                 expected.getHalfMove(),
242                 actual.getHalfMove()
243         );
244         assertEquals(
245                 String.format(
246                         MSG_POSITION_FULLMOVE,
247                         positionName
248                 ),
249                 expected.getFullMove(),
250                 actual.getFullMove()
251         );
252     }
253
254     protected void assertDiagramEquals(Position expected, Position actual) {
255         assertNotNull(
256                 expected
257         );
258         assertNotNull(
259                 actual
260         );
261         assertSquaresEquals(
262                 expected.getSquares(),
263                 actual.getSquares()
264         );
265     }
266
267     private void assertSquaresEquals(Map<Square, ColorPiece> expected, Map<Square, ColorPiece> actual) {
268         for (Map.Entry<Square, ColorPiece> entry : expected.entrySet()) {
269             assertEquals(
270                     String.format(
271                             MSG_POSITION_SQUARE,
272                             positionName,
273                             entry.getKey().name()
274                     ),
275                     entry.getValue(),
276                     actual.get(entry.getKey())
277             );
278         }
279     }
280
281     protected void assertStringEquals(String expected, String actual) {
282         assertEquals(
283                 String.format(
284                         MSG_POSITION,
285                         positionName
286                 ),
287                 expected,
288                 actual
289         );
290     }
291
292     public String getPositionName() {
293         return positionName;
294     }
295
296     protected void setPositionName(String name) {
297         positionName = name;
298     }
299
300
301 /*
302     public static void main(String[] args) throws Exception {
303         XMLConfiguration xmlConfig = new XMLConfiguration(
304                 new File(
305                         FENBuilderTest.class.getResource("FENBuilderTest.xml").toURI()
306                 )
307         );
308         for (HierarchicalConfiguration testConfig : xmlConfig.configurationsAt(TESTS)) {
309             AbstractPositionTest.testPosition(
310                     testConfig.configurationAt(POSITION)
311             );
312         }
313     }
314 */
315
316 }