[LIB-9] Separate chesshog-format module
[chesshog.git] / chesshog-core / 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.util.Iterator;
20 import java.util.List;
21 import java.util.Map;
22
23 import org.apache.commons.configuration.XMLConfiguration;
24 import org.apache.commons.configuration.HierarchicalConfiguration;
25 import org.apache.commons.configuration.SubnodeConfiguration;
26
27 import org.junit.Test;
28
29 import org.hedgecode.chess.position.Castle;
30 import org.hedgecode.chess.position.Color;
31 import org.hedgecode.chess.position.ColorPiece;
32 import org.hedgecode.chess.position.Position;
33 import org.hedgecode.chess.position.Positions;
34 import org.hedgecode.chess.position.Square;
35
36 /**
37  * Abstract Common Chess Position Test class.
38  *
39  * @author Dmitry Samoshin aka gotty
40  */
41 public abstract class AbstractPositionTest extends AbstractXMLConfigTest {
42
43     protected static final String NAME = "name";
44     protected static final String POSITION = "position";
45
46     private static final String INITIAL = "initial";
47     private static final String EMPTY = "empty";
48     private static final String SQUARES = "squares";
49     private static final String MOVE = "move";
50     private static final String CASTLE = "castle";
51     private static final String EN_PASSANT = "enPassant";
52     private static final String HALFMOVE = "halfmove";
53     private static final String FULLMOVE = "fullmove";
54
55     private static final String MSG_POSITION = "[%s]:";
56     private static final String MSG_POSITION_SQUARE = "[%s -> Square %s]:";
57     private static final String MSG_POSITION_MOVE = "[%s -> Move]:";
58     private static final String MSG_POSITION_CASTLE = "[%s -> Castle (%s)]:";
59     private static final String MSG_POSITION_EN_PASSANT = "[%s -> En Passant]:";
60     private static final String MSG_POSITION_HALFMOVE = "[%s -> Halfmove]:";
61     private static final String MSG_POSITION_FULLMOVE = "[%s -> Fullmove]:";
62
63     private String positionName;
64
65     public abstract List<XMLConfiguration> getXMLPositions() throws Exception;
66
67     @Test
68     public void testPositions() throws Exception {
69         for (XMLConfiguration xmlConfig : getXMLPositions())
70             testPosition(xmlConfig);
71     }
72
73     public void testPosition(HierarchicalConfiguration xmlPosition) throws Exception {
74         boolean isInitial = false, isEmpty = false;
75         boolean isSquares = !xmlPosition.subset(SQUARES).isEmpty();
76         if (xmlPosition.containsKey(INITIAL))
77             isInitial = xmlPosition.getBoolean(INITIAL);
78         if (xmlPosition.containsKey(EMPTY))
79             isEmpty = xmlPosition.getBoolean(EMPTY);
80         assertTrue(
81                 isInitial || isEmpty || isSquares
82         );
83         if (isSquares && !isEmpty) {
84             SubnodeConfiguration squaresNode = xmlPosition.configurationAt(SQUARES);
85             Iterator<String> keys = squaresNode.getKeys();
86             while (keys.hasNext()) {
87                 String key = keys.next();
88                 String value = squaresNode.getString(key);
89                 assertNotNull(
90                         Square.getSquare(key)
91                 );
92                 assertNotNull(
93                         ColorPiece.valueOf(value)
94                 );
95             }
96         }
97         if (xmlPosition.containsKey(MOVE)) {
98             assertNotNull(
99                     Color.valueOf(
100                             xmlPosition.getString(MOVE)
101                     )
102             );
103         }
104         if (!xmlPosition.subset(CASTLE).isEmpty()) {
105             SubnodeConfiguration castleNode = xmlPosition.configurationAt(CASTLE);
106             Iterator<String> keys = castleNode.getKeys();
107             while (keys.hasNext()) {
108                 String key = keys.next();
109                 String value = castleNode.getString(key);
110                 assertNotNull(
111                         Color.valueOf(key.toUpperCase())
112                 );
113                 assertNotNull(
114                         Castle.valueOf(value)
115                 );
116             }
117         }
118         if (xmlPosition.containsKey(EN_PASSANT)) {
119             assertNotNull(
120                     Square.getSquare(
121                             xmlPosition.getString(EN_PASSANT)
122                     )
123             );
124         }
125         if (xmlPosition.containsKey(HALFMOVE)) {
126             assertTrue(
127                     xmlPosition.getInt(HALFMOVE, -1) >= 0
128             );
129         }
130         if (xmlPosition.containsKey(FULLMOVE)) {
131             assertTrue(
132                     xmlPosition.getInt(FULLMOVE, 0) > 0
133             );
134         }
135     }
136
137     @Override
138     protected String getConfigName() {
139         return getClass().getSimpleName();
140     }
141
142     protected Position assignPosition(HierarchicalConfiguration xmlPosition) {
143         if (xmlPosition.containsKey(INITIAL) && xmlPosition.getBoolean(INITIAL)) {
144             return Positions.INITIAL.getPosition();
145         }
146         Position position = Positions.EMPTY.getPosition();
147         if (!xmlPosition.subset(SQUARES).isEmpty()) {
148             SubnodeConfiguration squaresNode = xmlPosition.configurationAt(SQUARES);
149             Iterator<String> keys = squaresNode.getKeys();
150             while (keys.hasNext()) {
151                 String key = keys.next();
152                 String value = squaresNode.getString(key);
153                 position.setPiece(
154                         ColorPiece.valueOf(value),
155                         Square.getSquare(key)
156                 );
157             }
158         }
159         if (xmlPosition.containsKey(MOVE))
160             position.setMove(
161                     Color.valueOf(
162                             xmlPosition.getString(MOVE)
163                     )
164             );
165         if (!xmlPosition.subset(CASTLE).isEmpty()) {
166             SubnodeConfiguration castleNode = xmlPosition.configurationAt(CASTLE);
167             Iterator<String> keys = castleNode.getKeys();
168             while (keys.hasNext()) {
169                 String key = keys.next();
170                 String value = castleNode.getString(key);
171                 position.setCastle(
172                         Color.valueOf(key.toUpperCase()),
173                         Castle.valueOf(value)
174                 );
175             }
176         }
177         if (xmlPosition.containsKey(EN_PASSANT))
178             position.setEnPassant(
179                     Square.getSquare(
180                             xmlPosition.getString(EN_PASSANT)
181                     )
182             );
183         if (xmlPosition.containsKey(HALFMOVE))
184             position.setHalfMove(
185                     xmlPosition.getInt(HALFMOVE)
186             );
187         if (xmlPosition.containsKey(FULLMOVE))
188             position.setFullMove(
189                     xmlPosition.getInt(FULLMOVE)
190             );
191         return position;
192     }
193
194     protected void assertPositionEquals(Position expected, Position actual) {
195         assertDiagramEquals(
196                 expected,
197                 actual
198         );
199         assertEquals(
200                 String.format(
201                         MSG_POSITION_MOVE,
202                         positionName
203                 ),
204                 expected.getMove(),
205                 actual.getMove()
206         );
207         assertEquals(
208                 String.format(
209                         MSG_POSITION_CASTLE,
210                         positionName,
211                         Color.WHITE
212                 ),
213                 expected.getCastle(Color.WHITE),
214                 actual.getCastle(Color.WHITE)
215         );
216         assertEquals(
217                 String.format(
218                         MSG_POSITION_CASTLE,
219                         positionName,
220                         Color.BLACK
221                 ),
222                 expected.getCastle(Color.BLACK),
223                 actual.getCastle(Color.BLACK)
224         );
225         assertEquals(
226                 String.format(
227                         MSG_POSITION_EN_PASSANT,
228                         positionName
229                 ),
230                 expected.getEnPassant(),
231                 actual.getEnPassant()
232         );
233         assertEquals(
234                 String.format(
235                         MSG_POSITION_HALFMOVE,
236                         positionName
237                 ),
238                 expected.getHalfMove(),
239                 actual.getHalfMove()
240         );
241         assertEquals(
242                 String.format(
243                         MSG_POSITION_FULLMOVE,
244                         positionName
245                 ),
246                 expected.getFullMove(),
247                 actual.getFullMove()
248         );
249     }
250
251     protected void assertDiagramEquals(Position expected, Position actual) {
252         assertNotNull(
253                 expected
254         );
255         assertNotNull(
256                 actual
257         );
258         assertSquaresEquals(
259                 expected.getSquares(),
260                 actual.getSquares()
261         );
262     }
263
264     private void assertSquaresEquals(Map<Square, ColorPiece> expected, Map<Square, ColorPiece> actual) {
265         for (Map.Entry<Square, ColorPiece> entry : expected.entrySet()) {
266             assertEquals(
267                     String.format(
268                             MSG_POSITION_SQUARE,
269                             positionName,
270                             entry.getKey().name()
271                     ),
272                     entry.getValue(),
273                     actual.get(entry.getKey())
274             );
275         }
276     }
277
278     protected void assertStringEquals(String expected, String actual) {
279         assertEquals(
280                 String.format(
281                         MSG_POSITION,
282                         positionName
283                 ),
284                 expected,
285                 actual
286         );
287     }
288
289     public String getPositionName() {
290         return positionName;
291     }
292
293     protected void setPositionName(String name) {
294         positionName = name;
295     }
296
297
298 /*
299     public static void main(String[] args) throws Exception {
300         XMLConfiguration xmlConfig = new XMLConfiguration(
301                 new File(
302                         FENBuilderTest.class.getResource("FENBuilderTest.xml").toURI()
303                 )
304         );
305         for (HierarchicalConfiguration testConfig : xmlConfig.configurationsAt(TESTS)) {
306             AbstractPositionTest.testPosition(
307                     testConfig.configurationAt(POSITION)
308             );
309         }
310     }
311 */
312
313 }