[LIB-9] Add original chesshog source files
[chesshog.git] / src / test / java / org / hedgecode / chess / AbstractXMLConfigTest.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
21 import org.apache.commons.configuration.XMLConfiguration;
22
23 import org.junit.Assert;
24 import org.junit.Test;
25
26 /**
27  * Abstract Test class with input params from XML file.
28  *
29  * @author Dmitry Samoshin aka gotty
30  */
31 public abstract class AbstractXMLConfigTest extends Assert {
32
33     private static final String RESOURCES_ROOT = "/";
34     private static final String XML_EXT = ".xml";
35
36     protected static final String CONFIG = "config";
37     protected static final String TESTS = "tests.test";
38
39     @Test
40     public void testXML() throws Exception {
41         XMLConfiguration xmlConfig = getXMLConfig();
42         assertTrue(
43                 CONFIG.equals(xmlConfig.getRoot().getName())
44         );
45         assertTrue(
46                 xmlConfig.configurationsAt(TESTS).size() > 0
47         );
48     }
49
50     protected abstract String getConfigName();
51
52     protected XMLConfiguration getXMLConfig() throws Exception {
53         String xmlConfigName = getConfigName() + XML_EXT;
54         return new XMLConfiguration(
55                 getResourceFile(
56                         xmlConfigName
57                 )
58         );
59     }
60
61     protected XMLConfiguration getDefXMLConfig() throws Exception {
62         return new XMLConfiguration(
63                 getResourceFile(
64                         getClass().getSimpleName() + XML_EXT
65                 )
66         );
67     }
68
69     private File getResourceFile(String name) throws Exception {
70         if (getClass().getResource(name) != null)
71             return new File(
72                     getClass().getResource(name).toURI()
73             );
74         return new File(
75                 getClass().getResource(RESOURCES_ROOT).getPath() + name
76         );
77     }
78
79 }