Gridarta Editor
ArchetypeTypeSetParserTest.java
Go to the documentation of this file.
1 /*
2  * Gridarta MMORPG map editor for Crossfire, Daimonin and similar games.
3  * Copyright (C) 2000-2015 The Gridarta Developers.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 
20 package net.sf.gridarta.model.archetypetype;
21 
22 import java.io.ByteArrayOutputStream;
23 import java.io.File;
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.net.URISyntaxException;
27 import java.net.URL;
28 import java.util.ArrayList;
29 import java.util.Arrays;
30 import java.util.List;
31 import java.util.regex.Pattern;
36 import nu.xom.ParsingException;
37 import nu.xom.xinclude.XIncludeException;
38 import org.jetbrains.annotations.NotNull;
39 import org.junit.Assert;
40 import org.junit.Test;
41 import org.xml.sax.SAXException;
42 
48 
52  @NotNull
53  private static final Pattern CR = Pattern.compile("\r");
54 
58  @NotNull
59  private final ErrorView errorView = new TestErrorView();
60 
64  @NotNull
65  private final ErrorViewCollector errorViewCollector = new ErrorViewCollector(errorView, new File("*input*"));
66 
75  @Test
76  public void test1() throws IOException, ParsingException, SAXException, URISyntaxException, XIncludeException {
77  check("test1");
78  }
79 
88  @Test
89  public void testImport1() throws IOException, ParsingException, SAXException, URISyntaxException, XIncludeException {
90  check("import1");
91  }
92 
101  @Test
102  public void testImport2() throws IOException, ParsingException, SAXException, URISyntaxException, XIncludeException {
103  check("import2");
104  }
105 
114  @Test
115  public void testIgnoreDefaultAttribute1() throws IOException, ParsingException, SAXException, URISyntaxException, XIncludeException {
116  check("ignoreDefaultAttribute1");
117  }
118 
127  @Test
128  public void testIgnoreImportAttribute1() throws IOException, ParsingException, SAXException, URISyntaxException, XIncludeException {
129  check("ignoreImportAttribute1");
130  }
131 
140  @Test
141  public void testIgnoreDefinedAttribute1() throws IOException, ParsingException, SAXException, URISyntaxException, XIncludeException {
142  check("ignoreDefinedAttribute1");
143  }
144 
153  @Test
154  public void testMsgDefault1() throws IOException, ParsingException, SAXException, URISyntaxException, XIncludeException {
155  check("msgDefault1");
156  }
157 
166  @Test
167  public void testMsgDefault2() throws IOException, ParsingException, SAXException, URISyntaxException, XIncludeException {
168  check("msgDefault2");
169  }
170 
179  @Test
180  public void testMsgDefault3() throws IOException, ParsingException, SAXException, URISyntaxException, XIncludeException {
181  check("msgDefault3");
182  }
183 
193  @Test
194  public void testArchetypeOrder1() throws IOException, ParsingException, SAXException, URISyntaxException, XIncludeException {
195  final ArchetypeTypeSet typeSet = loadArchetypeTypeSet("attributeOrder1");
196  Assert.assertFalse(errorView.hasErrors());
197  final List<String> keys = new ArrayList<>(Arrays.asList("a", "b", "c", "d"));
198  keys.sort(typeSet.getAttributeOrderComparator());
199  Assert.assertEquals("[a, b, c, d]", keys.toString());
200  }
201 
210  @Test
211  public void testArchetypeOrder2() throws IOException, ParsingException, SAXException, URISyntaxException, XIncludeException {
212  final ArchetypeTypeSet typeSet = loadArchetypeTypeSet("attributeOrder2");
213  Assert.assertFalse(errorView.hasErrors());
214  final List<String> keys = new ArrayList<>(Arrays.asList("a", "b", "c", "d"));
215  keys.sort(typeSet.getAttributeOrderComparator());
216  Assert.assertEquals("[a, b, c, d]", keys.toString());
217  }
218 
228  @Test
229  public void testArchetypeOrder3() throws IOException, ParsingException, SAXException, URISyntaxException, XIncludeException {
230  final ArchetypeTypeSet typeSet = loadArchetypeTypeSet("attributeOrder3");
231  Assert.assertFalse(errorView.hasErrors());
232  final List<String> keys = new ArrayList<>(Arrays.asList("a", "b", "c", "d"));
233  keys.sort(typeSet.getAttributeOrderComparator());
234  Assert.assertEquals("[c, d, a, b]", keys.toString());
235  }
236 
246  @Test
247  public void testArchetypeOrder4() throws IOException, ParsingException, SAXException, URISyntaxException, XIncludeException {
248  final ArchetypeTypeSet typeSet = loadArchetypeTypeSet("attributeOrder4");
249  Assert.assertFalse(errorView.hasErrors());
250  final List<String> keys = new ArrayList<>(Arrays.asList("a", "b", "c", "d"));
251  keys.sort(typeSet.getAttributeOrderComparator());
252  Assert.assertEquals("[c, a, b, d]", keys.toString());
253  }
254 
264  private void check(@NotNull final String name) throws IOException, ParsingException, SAXException, URISyntaxException, XIncludeException {
265  final ArchetypeTypeSet archetypeTypeSet = loadArchetypeTypeSet(name);
266  final InputStream is = getClass().getResourceAsStream("ArchetypeTypeSetParserTest-" + name + "-result.txt");
267  Assert.assertNotNull(is);
268  final ByteArrayOutputStream baos = new ByteArrayOutputStream();
269  final byte[] tmp = new byte[1024];
270  while (true) {
271  final int len = is.read(tmp);
272  if (len == -1) {
273  break;
274  }
275  baos.write(tmp, 0, len);
276  }
277  final String expectedResult = CR.matcher(baos.toString("UTF-8")).replaceAll("");
278  Assert.assertEquals(expectedResult, "errors=" + errorView.hasErrors() + "\n" + archetypeTypeSet);
279  }
280 
291  @NotNull
292  private ArchetypeTypeSet loadArchetypeTypeSet(@NotNull final String name) throws IOException, ParsingException, SAXException, URISyntaxException, XIncludeException {
294 
295  final URL typesXml = getClass().getResource("ArchetypeTypeSetParserTest-" + name + "-types.xml");
296  Assert.assertNotNull(typesXml);
297  return parser.loadTypesFromXML(errorViewCollector, ParseUtils.readXmlUrl(typesXml, "types", "types.dtd", "/system/dtd/types.dtd", null));
298  }
299 
304  @NotNull
306  final ArchetypeAttributeFactory archetypeAttributeFactory = new TestArchetypeAttributeFactory();
307  final ArchetypeAttributeParser archetypeAttributeParser = new ArchetypeAttributeParser(archetypeAttributeFactory);
308  final ArchetypeTypeParser archetypeTypeParser = new ArchetypeTypeParser(archetypeAttributeParser);
309  return new ArchetypeTypeSetParser(archetypeTypeParser);
310  }
311 
312 }
static final Pattern CR
A Pattern that matches characters.
Comparator< String > getAttributeOrderComparator()
Returns a Comparator that sorts attributes keys by the order defined by addAttributeOrder(String).
Convenience class for adding messages to a ErrorView instance using a fixed category name...
void testIgnoreDefaultAttribute1()
Checks that ignoring default attributes do work.
Interface for classes implementing ArchetypeAttribute factories.
Manages ArchetypeType instances, list, and bitmask definitions.
Utility class for parsing XML files.
Definition: ParseUtils.java:43
static ArchetypeTypeSetParser createArchetypeTypeSetParser()
Creates a new ArchetypeTypeSetParser instance.
Utility class for parsing ArchetypeType instances.
ArchetypeTypeSet loadTypesFromXML(@NotNull final ErrorViewCollector errorViewCollector, @NotNull final Document document)
Loads a types.xml file.
Interface for classes displaying error messages.
Definition: ErrorView.java:28
Base package of all Gridarta classes.
ArchetypeTypeSet loadArchetypeTypeSet(@NotNull final String name)
Loads a types.xml file from resources.
static Document readXmlUrl(@NotNull final URL url, @NotNull final String rootElement, @NotNull final String systemId, @NotNull final String resourceName, @Nullable final ErrorHandler errorHandler)
Reads an XML file from an URL.
Definition: ParseUtils.java:68
void testMsgDefault2()
Checks that a "msg" field in default_type does work.
boolean hasErrors()
Whether at least one error message has been added.
void check(@NotNull final String name)
Checks that a types.xml file can be read.
void testIgnoreImportAttribute1()
Checks that ignoring import attributes do work.
void testArchetypeOrder4()
Checks that a "archetype_order" is parsed correctly: some attributes missing.
void testMsgDefault1()
Checks that a "msg" field in default_type does work.
Parser for ArchetypeTypeSets ("types.xml" files).
void testArchetypeOrder1()
Checks that a "archetype_order" is parsed correctly: missing attribute_order element.
void testArchetypeOrder3()
Checks that a "archetype_order" is parsed correctly: all attributes specified.
void testMsgDefault3()
Checks that a "msg" field in default_type CAN BE "ignored".
final ErrorViewCollector errorViewCollector
The ErrorViewCollector used for parsing.
void testIgnoreDefinedAttribute1()
Checks that ignoring defined attributes do work.
void testArchetypeOrder2()
Checks that a "archetype_order" is parsed correctly: empty element.
An ErrorView suitable for unit tests.