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.var.crossfire.model.io;
021
022import java.io.IOException;
023import net.sf.gridarta.model.anim.AnimationObjects;
024import net.sf.gridarta.model.anim.DefaultAnimationObjects;
025import net.sf.gridarta.model.archetype.AbstractArchetypeBuilder;
026import net.sf.gridarta.model.archetype.UndefinedArchetypeException;
027import net.sf.gridarta.model.archetypeset.ArchetypeSet;
028import net.sf.gridarta.model.archetypeset.DefaultArchetypeSet;
029import net.sf.gridarta.model.archetypetype.ArchetypeTypeSet;
030import net.sf.gridarta.model.baseobject.BaseObject;
031import net.sf.gridarta.model.face.DefaultFaceObjects;
032import net.sf.gridarta.model.face.FaceObjectProviders;
033import net.sf.gridarta.model.face.FaceObjects;
034import net.sf.gridarta.model.io.AbstractArchetypeParser;
035import net.sf.gridarta.model.io.AbstractArchetypeParserTest;
036import net.sf.gridarta.model.io.GameObjectParser;
037import net.sf.gridarta.model.smoothface.SmoothFaces;
038import net.sf.gridarta.utils.ResourceIcons;
039import net.sf.gridarta.var.crossfire.model.archetype.Archetype;
040import net.sf.gridarta.var.crossfire.model.archetype.DefaultArchetypeFactory;
041import net.sf.gridarta.var.crossfire.model.gameobject.DefaultGameObjectFactory;
042import net.sf.gridarta.var.crossfire.model.gameobject.GameObject;
043import net.sf.gridarta.var.crossfire.model.maparchobject.MapArchObject;
044import net.sf.japi.swing.action.ActionBuilder;
045import net.sf.japi.swing.action.ActionBuilderFactory;
046import org.jetbrains.annotations.NotNull;
047import org.jetbrains.annotations.Nullable;
048import org.junit.Assert;
049import org.junit.BeforeClass;
050import org.junit.Test;
051
052/**
053 * Regression tests for {@link ArchetypeParser}.
054 * @author Andreas Kirschbaum
055 */
056public class ArchetypeParserTest extends AbstractArchetypeParserTest<GameObject, MapArchObject, Archetype> {
057
058    /**
059     * The loaded archetypes.
060     */
061    @Nullable
062    private ArchetypeSet<GameObject, MapArchObject, Archetype> archetypeSet;
063
064    /**
065     * Checks that inventory game objects are recognized.
066     * @throws IOException if the test fails
067     * @throws UndefinedArchetypeException if the test fails
068     */
069    @Test
070    public void testInventoryGameObjects() throws IOException, UndefinedArchetypeException {
071        final StringBuilder input = new StringBuilder();
072        input.append("Object arch1\n");
073        input.append("end\n");
074        input.append("Object arch2\n");
075        input.append("arch arch1\n");
076        input.append("name name1\n");
077        input.append("end\n");
078        input.append("end\n");
079        check(input.toString(), false, false, 2);
080        final Archetype arch2 = getArchetypeSet().getArchetype("arch2");
081        Assert.assertEquals(1, arch2.countInvObjects());
082        final GameObject inv = arch2.iterator().next();
083        Assert.assertEquals("arch1", inv.getArchetype().getArchetypeName());
084        Assert.assertEquals("name1", inv.getAttributeString(BaseObject.NAME));
085    }
086
087    /**
088     * Initializes the test.
089     */
090    @BeforeClass
091    public static void setUp() {
092        final ActionBuilder actionBuilder = ActionBuilderFactory.getInstance().getActionBuilder("net.sf.gridarta");
093        actionBuilder.addParent(ActionBuilderFactory.getInstance().getActionBuilder("net.sf.gridarta.var.crossfire"));
094    }
095
096    /**
097     * {@inheritDoc}
098     */
099    @NotNull
100    @Override
101    protected AbstractArchetypeParser<GameObject, MapArchObject, Archetype, ? extends AbstractArchetypeBuilder<GameObject, MapArchObject, Archetype>> newArchetypeParser() {
102        final FaceObjects faceObjects = new DefaultFaceObjects(false);
103        final ResourceIcons resourceIcons = new ResourceIcons();
104        final FaceObjectProviders faceObjectProviders = new FaceObjectProviders(0, faceObjects, resourceIcons);
105        final AnimationObjects animationObjects = new DefaultAnimationObjects();
106        final DefaultGameObjectFactory gameObjectFactory = new DefaultGameObjectFactory(faceObjectProviders, animationObjects);
107        final DefaultArchetypeFactory archetypeFactory = new DefaultArchetypeFactory(faceObjectProviders, animationObjects);
108        archetypeSet = new DefaultArchetypeSet<GameObject, MapArchObject, Archetype>(archetypeFactory, "images");
109        archetypeSet.setLoadedFromArchive(true);
110        final ArchetypeTypeSet archetypeTypeSet = new ArchetypeTypeSet();
111        assert archetypeSet != null;
112        final GameObjectParser<GameObject, MapArchObject, Archetype> gameObjectParser = new DefaultGameObjectParser(gameObjectFactory, archetypeSet, archetypeTypeSet);
113        assert archetypeSet != null;
114        final SmoothFaces smoothFaces = new SmoothFaces(faceObjects);
115        assert archetypeSet != null;
116        return new ArchetypeParser(gameObjectParser, animationObjects, archetypeSet, gameObjectFactory, smoothFaces);
117    }
118
119    /**
120     * {@inheritDoc}
121     */
122    @NotNull
123    @Override
124    @SuppressWarnings("NullableProblems")
125    protected ArchetypeSet<GameObject, MapArchObject, Archetype> getArchetypeSet() {
126        if (archetypeSet == null) {
127            throw new IllegalStateException();
128        }
129        return archetypeSet;
130    }
131
132}