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.io;
021
022import java.io.IOException;
023import net.sf.gridarta.model.anim.AnimationObjects;
024import net.sf.gridarta.model.anim.TestAnimationObjects;
025import net.sf.gridarta.model.archetype.AbstractArchetypeBuilder;
026import net.sf.gridarta.model.archetype.TestArchetype;
027import net.sf.gridarta.model.archetype.TestArchetypeBuilder;
028import net.sf.gridarta.model.archetype.TestArchetypeFactory;
029import net.sf.gridarta.model.archetype.UndefinedArchetypeException;
030import net.sf.gridarta.model.archetypeset.ArchetypeSet;
031import net.sf.gridarta.model.archetypeset.DefaultArchetypeSet;
032import net.sf.gridarta.model.face.FaceObjectProviders;
033import net.sf.gridarta.model.face.FaceObjects;
034import net.sf.gridarta.model.face.TestFaceObjects;
035import net.sf.gridarta.model.gameobject.TestGameObject;
036import net.sf.gridarta.model.gameobject.TestGameObjectFactory;
037import net.sf.gridarta.model.maparchobject.TestMapArchObject;
038import net.sf.gridarta.utils.ResourceIcons;
039import org.jetbrains.annotations.NotNull;
040import org.jetbrains.annotations.Nullable;
041import org.junit.Assert;
042import org.junit.Test;
043
044/**
045 * Regression tests for {@link AbstractArchetypeParser}.
046 * @author Andreas Kirschbaum
047 */
048public class ArchetypeParserTest extends AbstractArchetypeParserTest<TestGameObject, TestMapArchObject, TestArchetype> {
049
050    /**
051     * The loaded archetypes.
052     */
053    @Nullable
054    private ArchetypeSet<TestGameObject, TestMapArchObject, TestArchetype> archetypeSet;
055
056    /**
057     * Checks that a missing "object" line is detected.
058     * @throws IOException if the test fails
059     */
060    @Test
061    public void testMissingObject() throws IOException {
062        check("end\n", false, true, 0);
063    }
064
065    /**
066     * Checks that an empty archetype can be parsed.
067     * @throws IOException if the test fails
068     */
069    @Test
070    public void testEmpty() throws IOException {
071        final StringBuilder input = new StringBuilder();
072        input.append("Object test\n");
073        input.append("end\n");
074        check(input.toString(), false, false, 1);
075    }
076
077    /**
078     * Checks that a missing "end" line is reported.
079     * @throws IOException if the test fails
080     */
081    @Test
082    public void testMissingEnd() throws IOException {
083        check("Object test\n", true, false, 0);
084    }
085
086    /**
087     * Checks that msg...endmsg fields are parsed correctly.
088     * @throws IOException if the test fails
089     * @throws UndefinedArchetypeException if the test fails
090     */
091    @Test
092    public void testMsgTextEmpty() throws IOException, UndefinedArchetypeException {
093        final StringBuilder input = new StringBuilder();
094        input.append("Object test\n");
095        input.append("msg\n");
096        input.append("endmsg\n");
097        input.append("end\n");
098        check(input.toString(), false, false, 1);
099        Assert.assertNull(getArchetypeSet().getArchetype("test").getMsgText());
100    }
101
102    /**
103     * Checks that msg...endmsg fields are parsed correctly.
104     * @throws IOException if the test fails
105     * @throws UndefinedArchetypeException if the test fails
106     */
107    @Test
108    public void testMsgTextLines() throws IOException, UndefinedArchetypeException {
109        final StringBuilder input = new StringBuilder();
110        input.append("Object test\n");
111        input.append("msg\n");
112        input.append("abc\n");
113        input.append("def\n");
114        input.append("ghi\n");
115        input.append("endmsg\n");
116        input.append("end\n");
117        check(input.toString(), false, false, 1);
118        final StringBuilder msgText = new StringBuilder();
119        msgText.append("abc\n");
120        msgText.append("def\n");
121        msgText.append("ghi\n");
122        Assert.assertEquals(msgText.toString(), getArchetypeSet().getArchetype("test").getMsgText());
123    }
124
125    /**
126     * Checks that msg...endmsg fields are parsed correctly.
127     * @throws IOException if the test fails
128     * @throws UndefinedArchetypeException if the test fails
129     */
130    @Test
131    public void testMsgTextTrailingWhitespace() throws IOException, UndefinedArchetypeException {
132        final StringBuilder input = new StringBuilder();
133        input.append("Object test\n");
134        input.append("msg\n");
135        input.append("abc   \n");
136        input.append("def\n");
137        input.append("ghi   \n");
138        input.append("\n");
139        input.append("endmsg\n");
140        input.append("end\n");
141        check(input.toString(), false, false, 1);
142        final StringBuilder msgText = new StringBuilder();
143        msgText.append("abc\n");
144        msgText.append("def\n");
145        msgText.append("ghi\n");
146        msgText.append("\n");
147        Assert.assertEquals(msgText.toString(), getArchetypeSet().getArchetype("test").getMsgText());
148    }
149
150    /**
151     * Checks that msg...endmsg fields are parsed correctly.
152     * @throws IOException if the test fails
153     * @throws UndefinedArchetypeException if the test fails
154     */
155    @Test
156    public void testMsgTextLeadingWhitespace() throws IOException, UndefinedArchetypeException {
157        final StringBuilder input = new StringBuilder();
158        input.append("Object test\n");
159        input.append("msg\n");
160        input.append("\n");
161        input.append("   abc\n");
162        input.append("def\n");
163        input.append("   ghi\n");
164        input.append("endmsg\n");
165        input.append("end\n");
166        check(input.toString(), false, false, 1);
167        final StringBuilder msgText = new StringBuilder();
168        msgText.append("\n");
169        msgText.append("   abc\n");
170        msgText.append("def\n");
171        msgText.append("   ghi\n");
172        Assert.assertEquals(msgText.toString(), getArchetypeSet().getArchetype("test").getMsgText());
173    }
174
175    /**
176     * Checks that msg...endmsg fields are parsed correctly.
177     * @throws IOException if the test fails
178     */
179    @Test
180    public void testMsgTextTruncated() throws IOException {
181        final StringBuilder input = new StringBuilder();
182        input.append("Object test\n");
183        input.append("msg\n");
184        input.append("abc\n");
185        input.append("def\n");
186        check(input.toString(), true, false, 0);
187    }
188
189    /**
190     * {@inheritDoc}
191     */
192    @NotNull
193    @Override
194    protected AbstractArchetypeParser<TestGameObject, TestMapArchObject, TestArchetype, ? extends AbstractArchetypeBuilder<TestGameObject, TestMapArchObject, TestArchetype>> newArchetypeParser() {
195        final FaceObjects faceObjects = new TestFaceObjects();
196        final ResourceIcons resourceIcons = new ResourceIcons();
197        final FaceObjectProviders faceObjectProviders = new FaceObjectProviders(0, faceObjects, resourceIcons);
198        final AnimationObjects animationObjects = new TestAnimationObjects();
199        final TestGameObjectFactory gameObjectFactory = new TestGameObjectFactory(faceObjectProviders, animationObjects);
200        final TestArchetypeBuilder archetypeBuilder = new TestArchetypeBuilder(gameObjectFactory);
201        final TestArchetypeFactory archetypeFactory = new TestArchetypeFactory(faceObjectProviders, animationObjects);
202        archetypeSet = new DefaultArchetypeSet<TestGameObject, TestMapArchObject, TestArchetype>(archetypeFactory, null);
203        archetypeSet.setLoadedFromArchive(true);
204        assert archetypeSet != null;
205        return new TestArchetypeParser(archetypeBuilder, animationObjects, archetypeSet);
206    }
207
208    /**
209     * {@inheritDoc}
210     */
211    @NotNull
212    @Override
213    @SuppressWarnings("NullableProblems")
214    protected ArchetypeSet<TestGameObject, TestMapArchObject, TestArchetype> getArchetypeSet() {
215        final ArchetypeSet<TestGameObject, TestMapArchObject, TestArchetype> result = archetypeSet;
216        if (result == null) {
217            throw new IllegalStateException();
218        }
219        return result;
220    }
221
222}