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.artifact;
021
022import java.io.IOException;
023import net.sf.gridarta.model.archetype.Archetype;
024import net.sf.gridarta.model.archetype.DuplicateArchetypeException;
025import net.sf.gridarta.model.archetype.TestArchetype;
026import net.sf.gridarta.model.archetype.UndefinedArchetypeException;
027import net.sf.gridarta.model.errorview.TestErrorView;
028import net.sf.gridarta.model.gameobject.TestGameObject;
029import net.sf.gridarta.model.maparchobject.TestMapArchObject;
030import org.junit.Assert;
031import org.junit.Test;
032
033/**
034 * Regression tests for {@link ArtifactParser}.
035 * @author Andreas Kirschbaum
036 */
037public class ArtifactParserTest {
038
039    /**
040     * Checks that a missing "object" line is detected.
041     * @throws DuplicateArchetypeException if the test fails
042     * @throws IOException if the test fails
043     * @throws UndefinedArchetypeException if the test fails
044     */
045    @Test
046    public void testMissingObject() throws DuplicateArchetypeException, IOException, UndefinedArchetypeException {
047        final TestErrorView errorView = new TestErrorView();
048        final TestParser parser = new TestParser(errorView);
049        parser.addArchetype("base", "sp 2");
050        final StringBuilder artifacts = new StringBuilder();
051        artifacts.append("artifact art\n");
052        artifacts.append("def_arch base\n");
053        artifacts.append("Object obj\n");
054        artifacts.append("sp 3\n");
055        artifacts.append("end\n");
056        parser.parseArtifacts(artifacts.toString());
057        Assert.assertFalse(errorView.hasWarnings());
058        Assert.assertFalse(errorView.hasErrors());
059        Assert.assertEquals(2, parser.getArchetypeCount());
060        final Archetype<TestGameObject, TestMapArchObject, TestArchetype> archetype = parser.getArchetype("art");
061        Assert.assertEquals("sp 3\nname base\n", archetype.getObjectText());
062    }
063
064    /**
065     * Checks that an artifact definition inherits the def_arch archetype's name
066     * by default.
067     * @throws DuplicateArchetypeException if the test fails
068     * @throws IOException if the test fails
069     * @throws UndefinedArchetypeException if the test fails
070     */
071    @Test
072    public void testDefaultName1() throws DuplicateArchetypeException, IOException, UndefinedArchetypeException {
073        final TestParser parser = new TestParser();
074        parser.addArchetype("horn");
075        final StringBuilder artifacts = new StringBuilder();
076        artifacts.append("artifact horn_fools\n");
077        artifacts.append("def_arch horn\n");
078        artifacts.append("Object obj\n");
079        artifacts.append("title of fools\n");
080        artifacts.append("end\n");
081        parser.parseArtifacts(artifacts.toString());
082        Assert.assertEquals("horn of fools", parser.getArchetype("horn_fools").getBestName());
083    }
084
085    /**
086     * Checks that an artifact definition inherits the def_arch archetype's name
087     * by default.
088     * @throws DuplicateArchetypeException if the test fails
089     * @throws IOException if the test fails
090     * @throws UndefinedArchetypeException if the test fails
091     */
092    @Test
093    public void testDefaultName2() throws DuplicateArchetypeException, IOException, UndefinedArchetypeException {
094        final TestParser parser = new TestParser();
095        parser.addArchetype("horn", "name horn2");
096        final StringBuilder artifacts = new StringBuilder();
097        artifacts.append("artifact horn_fools\n");
098        artifacts.append("def_arch horn\n");
099        artifacts.append("Object obj\n");
100        artifacts.append("title of fools\n");
101        artifacts.append("end\n");
102        parser.parseArtifacts(artifacts.toString());
103        Assert.assertEquals("horn2 of fools", parser.getArchetype("horn_fools").getBestName());
104    }
105
106    /**
107     * Checks that an artifact definition inherits the def_arch archetype's name
108     * by default.
109     * @throws DuplicateArchetypeException if the test fails
110     * @throws IOException if the test fails
111     * @throws UndefinedArchetypeException if the test fails
112     */
113    @Test
114    public void testDefaultName3() throws DuplicateArchetypeException, IOException, UndefinedArchetypeException {
115        final TestParser parser = new TestParser();
116        parser.addArchetype("horn", "name base_horn");
117        final StringBuilder artifacts = new StringBuilder();
118        artifacts.append("artifact horn_fools\n");
119        artifacts.append("def_arch horn\n");
120        artifacts.append("Object obj\n");
121        artifacts.append("title of fools\n");
122        artifacts.append("end\n");
123        parser.parseArtifacts(artifacts.toString());
124        Assert.assertEquals("base_horn of fools", parser.getArchetype("horn_fools").getBestName());
125    }
126
127    /**
128     * Checks that an artifact definition inherits the def_arch archetype's name
129     * by default.
130     * @throws DuplicateArchetypeException if the test fails
131     * @throws IOException if the test fails
132     * @throws UndefinedArchetypeException if the test fails
133     */
134    @Test
135    public void testDefaultName4() throws DuplicateArchetypeException, IOException, UndefinedArchetypeException {
136        final TestParser parser = new TestParser();
137        parser.addArchetype("horn", "name base_horn");
138        final StringBuilder artifacts = new StringBuilder();
139        artifacts.append("artifact horn_fools\n");
140        artifacts.append("def_arch horn\n");
141        artifacts.append("Object obj\n");
142        artifacts.append("name special_horn\n");
143        artifacts.append("title of fools\n");
144        artifacts.append("end\n");
145        parser.parseArtifacts(artifacts.toString());
146        Assert.assertEquals("special_horn of fools", parser.getArchetype("horn_fools").getBestName());
147    }
148
149    /**
150     * Checks for spurious error messages when setting an artifact's name when
151     * the base archetype doesn't has an explicit name.
152     * @throws DuplicateArchetypeException if the test fails
153     * @throws IOException if the test fails
154     * @throws UndefinedArchetypeException if the test fails
155     */
156    @Test
157    public void testDefaultName5a() throws DuplicateArchetypeException, IOException, UndefinedArchetypeException {
158        final TestParser parser = new TestParser();
159        parser.addArchetype("note", "face note.101", "layer 3", "identified 1", "type 8", "material 1", "value 8", "weight 75", "level 1", "exp 10");
160        final StringBuilder artifacts = new StringBuilder();
161        artifacts.append("Allowed none\n");
162        artifacts.append("chance 1\n");
163        artifacts.append("artifact my_notebook_item\n");
164        artifacts.append("def_arch note\n");
165        artifacts.append("Object\n");
166        artifacts.append("name A notebook\n");
167        artifacts.append("msg\n");
168        artifacts.append("This should be giving a false positive for duplicate attributes.\n");
169        artifacts.append("endmsg\n");
170        artifacts.append("end\n");
171        parser.parseArtifacts(artifacts.toString());
172        Assert.assertEquals("A notebook", parser.getArchetype("my_notebook_item").getBestName());
173    }
174
175    /**
176     * Checks for spurious error messages when setting an artifact's name when
177     * the base archetype doesn't has an explicit name.
178     * @throws DuplicateArchetypeException if the test fails
179     * @throws IOException if the test fails
180     * @throws UndefinedArchetypeException if the test fails
181     */
182    @Test
183    public void testDefaultName5b() throws DuplicateArchetypeException, IOException, UndefinedArchetypeException {
184        final TestParser parser = new TestParser();
185        parser.addArchetype("note", "face note.101", "name note", "layer 3", "identified 1", "type 8", "material 1", "value 8", "weight 75", "level 1", "exp 10");
186        final StringBuilder artifacts = new StringBuilder();
187        artifacts.append("Allowed none\n");
188        artifacts.append("chance 1\n");
189        artifacts.append("artifact my_notebook_item\n");
190        artifacts.append("def_arch note\n");
191        artifacts.append("Object\n");
192        artifacts.append("name A notebook\n");
193        artifacts.append("msg\n");
194        artifacts.append("This should be giving a false positive for duplicate attributes.\n");
195        artifacts.append("endmsg\n");
196        artifacts.append("end\n");
197        parser.parseArtifacts(artifacts.toString());
198        Assert.assertEquals("A notebook", parser.getArchetype("my_notebook_item").getBestName());
199    }
200
201}