001/*
002 * Gridarta MMORPG map editor for Crossfire, Daimonin and similar games.
003 * Copyright (C) 2000-2010 The Gridarta Developers.
004 *
005 * This program is free software; you can redistribute it and/or modify
006 * it under the terms of the GNU General Public License as published by
007 * the Free Software Foundation; either version 2 of the License, or
008 * (at your option) any later version.
009 *
010 * This program is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
013 * GNU General Public License for more details.
014 *
015 * You should have received a copy of the GNU General Public License along
016 * with this program; if not, write to the Free Software Foundation, Inc.,
017 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
018 */
019
020package net.sf.gridarta.model.archetypetype;
021
022import java.io.ByteArrayOutputStream;
023import java.io.File;
024import java.io.IOException;
025import java.io.InputStream;
026import java.util.ArrayList;
027import java.util.Arrays;
028import java.util.Collections;
029import java.util.List;
030import javax.xml.parsers.ParserConfigurationException;
031import net.sf.gridarta.model.errorview.ErrorView;
032import net.sf.gridarta.model.errorview.ErrorViewCollector;
033import net.sf.gridarta.model.errorview.TestErrorView;
034import net.sf.gridarta.utils.XmlHelper;
035import org.jetbrains.annotations.NotNull;
036import org.junit.Assert;
037import org.junit.Test;
038import org.xml.sax.InputSource;
039
040/**
041 * Regression tests for {@link ArchetypeTypeSetParser}.
042 * @author Andreas Kirschbaum
043 */
044public class ArchetypeTypeSetParserTest {
045
046    /**
047     * The {@link ErrorView} used for parsing.
048     */
049    @NotNull
050    private final ErrorView errorView = new TestErrorView();
051
052    /**
053     * The {@link ErrorViewCollector} used for parsing.
054     */
055    @NotNull
056    private final ErrorViewCollector errorViewCollector = new ErrorViewCollector(errorView, new File("*input*"));
057
058    /**
059     * Read a simple types.xml file.
060     * @throws ParserConfigurationException if the test fails
061     * @throws IOException if the test fails
062     */
063    @Test
064    public void test1() throws ParserConfigurationException, IOException {
065        check("test1");
066    }
067
068    /**
069     * Checks that imports do work.
070     * @throws ParserConfigurationException if the test fails
071     * @throws IOException if the test fails
072     */
073    @Test
074    public void testImport1() throws ParserConfigurationException, IOException {
075        check("import1");
076    }
077
078    /**
079     * Checks that multi-imports do work.
080     * @throws ParserConfigurationException if the test fails
081     * @throws IOException if the test fails
082     */
083    @Test
084    public void testImport2() throws ParserConfigurationException, IOException {
085        check("import2");
086    }
087
088    /**
089     * Checks that ignoring default attributes do work.
090     * @throws ParserConfigurationException if the test fails
091     * @throws IOException if the test fails
092     */
093    @Test
094    public void testIgnoreDefaultAttribute1() throws ParserConfigurationException, IOException {
095        check("ignoreDefaultAttribute1");
096    }
097
098    /**
099     * Checks that ignoring import attributes do work.
100     * @throws ParserConfigurationException if the test fails
101     * @throws IOException if the test fails
102     */
103    @Test
104    public void testIgnoreImportAttribute1() throws ParserConfigurationException, IOException {
105        check("ignoreImportAttribute1");
106    }
107
108    /**
109     * Checks that ignoring defined attributes do work.
110     * @throws ParserConfigurationException if the test fails
111     * @throws IOException if the test fails
112     */
113    @Test
114    public void testIgnoreDefinedAttribute1() throws ParserConfigurationException, IOException {
115        check("ignoreDefinedAttribute1");
116    }
117
118    /**
119     * Checks that a "msg" field in default_type does work.
120     * @throws ParserConfigurationException if the test fails
121     * @throws IOException if the test fails
122     */
123    @Test
124    public void testMsgDefault1() throws ParserConfigurationException, IOException {
125        check("msgDefault1");
126    }
127
128    /**
129     * Checks that a "msg" field in default_type does work.
130     * @throws ParserConfigurationException if the test fails
131     * @throws IOException if the test fails
132     */
133    @Test
134    public void testMsgDefault2() throws ParserConfigurationException, IOException {
135        check("msgDefault2");
136    }
137
138    /**
139     * Checks that a "msg" field in default_type CAN BE "ignored".
140     * @throws ParserConfigurationException if the test fails
141     * @throws IOException if the test fails
142     */
143    @Test
144    public void testMsgDefault3() throws ParserConfigurationException, IOException {
145        check("msgDefault3");
146    }
147
148    /**
149     * Checks that a "archetype_order" is parsed correctly: missing
150     * attribute_order element.
151     * @throws ParserConfigurationException if the test fails
152     */
153    @Test
154    public void testArchetypeOrder1() throws ParserConfigurationException {
155        final ArchetypeTypeSet typeSet = loadArchetypeTypeSet("attributeOrder1");
156        Assert.assertFalse(errorView.hasErrors());
157        final List<String> keys = new ArrayList<String>(Arrays.asList("a", "b", "c", "d"));
158        Collections.sort(keys, typeSet.getAttributeOrderComparator());
159        Assert.assertEquals("[a, b, c, d]", keys.toString());
160    }
161
162    /**
163     * Checks that a "archetype_order" is parsed correctly: empty element.
164     * @throws ParserConfigurationException if the test fails
165     */
166    @Test
167    public void testArchetypeOrder2() throws ParserConfigurationException {
168        final ArchetypeTypeSet typeSet = loadArchetypeTypeSet("attributeOrder2");
169        Assert.assertFalse(errorView.hasErrors());
170        final List<String> keys = new ArrayList<String>(Arrays.asList("a", "b", "c", "d"));
171        Collections.sort(keys, typeSet.getAttributeOrderComparator());
172        Assert.assertEquals("[a, b, c, d]", keys.toString());
173    }
174
175    /**
176     * Checks that a "archetype_order" is parsed correctly: all attributes
177     * specified.
178     * @throws ParserConfigurationException if the test fails
179     */
180    @Test
181    public void testArchetypeOrder3() throws ParserConfigurationException {
182        final ArchetypeTypeSet typeSet = loadArchetypeTypeSet("attributeOrder3");
183        Assert.assertFalse(errorView.hasErrors());
184        final List<String> keys = new ArrayList<String>(Arrays.asList("a", "b", "c", "d"));
185        Collections.sort(keys, typeSet.getAttributeOrderComparator());
186        Assert.assertEquals("[c, d, a, b]", keys.toString());
187    }
188
189    /**
190     * Checks that a "archetype_order" is parsed correctly: some attributes
191     * missing.
192     * @throws ParserConfigurationException if the test fails
193     */
194    @Test
195    public void testArchetypeOrder4() throws ParserConfigurationException {
196        final ArchetypeTypeSet typeSet = loadArchetypeTypeSet("attributeOrder4");
197        Assert.assertFalse(errorView.hasErrors());
198        final List<String> keys = new ArrayList<String>(Arrays.asList("a", "b", "c", "d"));
199        Collections.sort(keys, typeSet.getAttributeOrderComparator());
200        Assert.assertEquals("[c, a, b, d]", keys.toString());
201    }
202
203    /**
204     * Checks that a types.xml file can be read.
205     * @param name the base name of the resources to load
206     * @throws ParserConfigurationException if the test fails
207     * @throws IOException if the test fails
208     */
209    private void check(@NotNull final String name) throws ParserConfigurationException, IOException {
210        final ArchetypeTypeSet archetypeTypeSet = loadArchetypeTypeSet(name);
211        final InputStream is = getClass().getResourceAsStream("ArchetypeTypeSetParserTest-" + name + "-result.txt");
212        Assert.assertNotNull(is);
213        final ByteArrayOutputStream baos = new ByteArrayOutputStream();
214        final byte[] tmp = new byte[1024];
215        while (true) {
216            final int len = is.read(tmp);
217            if (len == -1) {
218                break;
219            }
220            baos.write(tmp, 0, len);
221        }
222        final String expectedResult = baos.toString("UTF-8");
223        Assert.assertEquals(expectedResult, "errors=" + errorView.hasErrors() + "\n" + archetypeTypeSet);
224    }
225
226    /**
227     * Loads a types.xml file from resources.
228     * @param name the base name of the resources to load
229     * @return the archetype type set
230     * @throws ParserConfigurationException if the file cannot be read
231     */
232    @NotNull
233    private ArchetypeTypeSet loadArchetypeTypeSet(@NotNull final String name) throws ParserConfigurationException {
234        final ArchetypeTypeSetParser parser = createArchetypeTypeSetParser();
235
236        final InputStream typesXml = getClass().getResourceAsStream("ArchetypeTypeSetParserTest-" + name + "-types.xml");
237        System.out.println("ArchetypeTypeSetParserTest-" + name + "-types.xml");
238        Assert.assertNotNull(typesXml);
239        final InputSource inputSource = new InputSource(typesXml);
240        inputSource.setSystemId("types.dtd");
241
242        return parser.loadTypesFromXML(errorViewCollector, inputSource);
243    }
244
245    /**
246     * Creates a new {@link ArchetypeTypeSetParser} instance.
247     * @return the archetype type set parser instance
248     * @throws ParserConfigurationException if the parser cannot be created
249     */
250    @NotNull
251    private static ArchetypeTypeSetParser createArchetypeTypeSetParser() throws ParserConfigurationException {
252        final XmlHelper xmlHelper = new XmlHelper();
253        final ArchetypeAttributeFactory archetypeAttributeFactory = new TestArchetypeAttributeFactory();
254        final ArchetypeAttributeParser archetypeAttributeParser = new ArchetypeAttributeParser(archetypeAttributeFactory);
255        final ArchetypeTypeParser archetypeTypeParser = new ArchetypeTypeParser(archetypeAttributeParser);
256        return new ArchetypeTypeSetParser(xmlHelper.getDocumentBuilder(), archetypeTypeParser);
257    }
258
259}