[LIB-9] Add original chesshog source files
[chesshog.git] / src / test / java / org / hedgecode / chess / ascii / ASCIIBuilderTest.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.ascii;
18
19 import java.util.LinkedList;
20 import java.util.List;
21
22 import org.apache.commons.configuration.HierarchicalConfiguration;
23 import org.apache.commons.configuration.XMLConfiguration;
24
25 import org.junit.Test;
26 import org.junit.runner.RunWith;
27 import org.junit.runners.JUnit4;
28
29 import org.hedgecode.chess.AbstractPositionTest;
30 import org.hedgecode.chess.position.Builder;
31 import org.hedgecode.chess.position.Position;
32
33 /**
34  * Tests for {@link ASCIIBuilder}.
35  *
36  * @author Dmitry Samoshin aka gotty
37  */
38 @RunWith(JUnit4.class)
39 public class ASCIIBuilderTest extends AbstractPositionTest implements ASCIIPositionTest {
40
41     private static final String LINE_REGEX = "\\r?\\n";
42
43     @Test
44     public void testBuild() throws Exception {
45         Builder asciiBuilder = ASCIIBuilder.getInstance();
46         for (HierarchicalConfiguration testConfig : getXMLConfig().configurationsAt(TESTS)) {
47             setPositionName(
48                     String.format(
49                             MSG_ASCII_POSITION, testConfig.getString(NAME)
50                     )
51             );
52             String ascii = testConfig.getString(ASCII_TAG);
53             Position position = assignPosition(
54                     testConfig.configurationAt(POSITION)
55             );
56             if (testConfig.containsKey(ASCII_TYPE)) {
57                 ASCII.setType(
58                         ASCIIBoardType.valueOf(
59                                 testConfig.getString(ASCII_TYPE)
60                         )
61                 );
62             }
63             boolean withNotation = false;
64             if (testConfig.containsKey(ASCII_NOTATION)) {
65                 withNotation = testConfig.getBoolean(ASCII_NOTATION);
66             }
67             ASCII.setNotation(withNotation);
68             assertStringEquals(
69                     ascii,
70                     asciiBuilder.build(position)
71             );
72         }
73     }
74
75     @Override
76     protected void assertStringEquals(String expected, String actual) {
77         assertNotNull(expected);
78         assertNotNull(actual);
79         String[] expectedLines = expected.split(LINE_REGEX);
80         String[] actualLines = actual.split(LINE_REGEX);
81         assertEquals(
82                 expectedLines.length,
83                 actualLines.length
84         );
85         for (int i = 0; i < expectedLines.length; ++i) {
86             super.assertStringEquals(
87                     expectedLines[i].trim(),
88                     actualLines[i].trim()
89             );
90         }
91     }
92
93     @Override
94     public List<XMLConfiguration> getXMLPositions() throws Exception {
95         List<XMLConfiguration> xmlPositions = new LinkedList<>();
96         for (HierarchicalConfiguration testConfig : getXMLConfig().configurationsAt(TESTS)) {
97             xmlPositions.add(
98                     new XMLConfiguration(
99                             testConfig.configurationAt(POSITION)
100                     )
101             );
102         }
103         return xmlPositions;
104     }
105
106 }