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.match;
021
022import net.sf.gridarta.model.anim.AnimationObjects;
023import net.sf.gridarta.model.anim.TestAnimationObjects;
024import net.sf.gridarta.model.archetype.TestArchetype;
025import net.sf.gridarta.model.archetype.TestDefaultArchetype;
026import net.sf.gridarta.model.baseobject.BaseObject;
027import net.sf.gridarta.model.face.FaceObjectProviders;
028import net.sf.gridarta.model.face.FaceObjects;
029import net.sf.gridarta.model.face.TestFaceObjects;
030import net.sf.gridarta.model.gameobject.GameObject;
031import net.sf.gridarta.model.gameobject.TestGameObject;
032import net.sf.gridarta.model.gameobject.TestGameObjectFactory;
033import net.sf.gridarta.utils.ResourceIcons;
034import org.jetbrains.annotations.NotNull;
035import org.jetbrains.annotations.Nullable;
036import org.junit.Assert;
037import org.junit.Test;
038
039/**
040 * Regression tests for {@link NamedGameObjectMatcher}.
041 * @author Andreas Kirschbaum
042 */
043public class NamedGameObjectMatcherTest {
044
045    /**
046     * The {@link ResourceIcons} instance.
047     */
048    @Nullable
049    private final ResourceIcons resourceIcons = new ResourceIcons();
050
051    /**
052     * Checks that a {@link NamedGameObjectMatcher} works correctly when not
053     * using an environment check.
054     */
055    @Test
056    public void testTypeNrsGameObjectMatcher1() {
057        final GameObjectMatcher invGameObjectMatcher = new TypeNrsGameObjectMatcher(1);
058        final GameObjectMatcher namedGameObjectMatcher = new NamedGameObjectMatcher(0, "namedGameObjectMatcher", "namedGameObjectMatcher", false, null, invGameObjectMatcher);
059        final FaceObjectProviders faceObjectProviders = newFaceObjectProviders();
060        final AnimationObjects animationObjects = new TestAnimationObjects();
061        final TestGameObjectFactory gameObjectFactory = new TestGameObjectFactory(faceObjectProviders, animationObjects);
062        Assert.assertTrue(namedGameObjectMatcher.isMatching(newGameObjects(gameObjectFactory, faceObjectProviders, animationObjects, 1)));
063        Assert.assertFalse(namedGameObjectMatcher.isMatching(newGameObjects(gameObjectFactory, faceObjectProviders, animationObjects, 2)));
064        Assert.assertTrue(namedGameObjectMatcher.isMatching(newGameObjects(gameObjectFactory, faceObjectProviders, animationObjects, 1, 1)));
065        Assert.assertTrue(namedGameObjectMatcher.isMatching(newGameObjects(gameObjectFactory, faceObjectProviders, animationObjects, 2, 1)));
066        Assert.assertFalse(namedGameObjectMatcher.isMatching(newGameObjects(gameObjectFactory, faceObjectProviders, animationObjects, 1, 2)));
067        Assert.assertFalse(namedGameObjectMatcher.isMatching(newGameObjects(gameObjectFactory, faceObjectProviders, animationObjects, 2, 2)));
068        Assert.assertTrue(namedGameObjectMatcher.isMatching(newGameObjects(gameObjectFactory, faceObjectProviders, animationObjects, 1, 1, 1)));
069        Assert.assertFalse(namedGameObjectMatcher.isMatching(newGameObjects(gameObjectFactory, faceObjectProviders, animationObjects, 1, 2, 2)));
070        Assert.assertFalse(namedGameObjectMatcher.isMatching(newGameObjects(gameObjectFactory, faceObjectProviders, animationObjects, 2, 1, 2)));
071        Assert.assertTrue(namedGameObjectMatcher.isMatching(newGameObjects(gameObjectFactory, faceObjectProviders, animationObjects, 2, 2, 1)));
072        Assert.assertFalse(namedGameObjectMatcher.isMatching(newGameObjects(gameObjectFactory, faceObjectProviders, animationObjects, 2, 2, 2)));
073    }
074
075    /**
076     * Checks that a {@link NamedGameObjectMatcher} works correctly when using
077     * an environment check.
078     */
079    @Test
080    public void testTypeNrsGameObjectMatcher2() {
081        final GameObjectMatcher invGameObjectMatcher = new TypeNrsGameObjectMatcher(1);
082        final GameObjectMatcher envGameObjectMatcher = new TypeNrsGameObjectMatcher(2);
083        final GameObjectMatcher namedGameObjectMatcher = new NamedGameObjectMatcher(0, "namedGameObjectMatcher", "namedGameObjectMatcher", false, envGameObjectMatcher, invGameObjectMatcher);
084        final FaceObjectProviders faceObjectProviders = newFaceObjectProviders();
085        final AnimationObjects animationObjects = new TestAnimationObjects();
086        final TestGameObjectFactory gameObjectFactory = new TestGameObjectFactory(faceObjectProviders, animationObjects);
087        Assert.assertFalse(namedGameObjectMatcher.isMatching(newGameObjects(gameObjectFactory, faceObjectProviders, animationObjects, 1)));
088        Assert.assertFalse(namedGameObjectMatcher.isMatching(newGameObjects(gameObjectFactory, faceObjectProviders, animationObjects, 2)));
089        Assert.assertFalse(namedGameObjectMatcher.isMatching(newGameObjects(gameObjectFactory, faceObjectProviders, animationObjects, 1, 1)));
090        Assert.assertFalse(namedGameObjectMatcher.isMatching(newGameObjects(gameObjectFactory, faceObjectProviders, animationObjects, 2, 1)));
091        Assert.assertTrue(namedGameObjectMatcher.isMatching(newGameObjects(gameObjectFactory, faceObjectProviders, animationObjects, 1, 2)));
092        Assert.assertFalse(namedGameObjectMatcher.isMatching(newGameObjects(gameObjectFactory, faceObjectProviders, animationObjects, 2, 2)));
093        Assert.assertFalse(namedGameObjectMatcher.isMatching(newGameObjects(gameObjectFactory, faceObjectProviders, animationObjects, 1, 1, 1)));
094        Assert.assertTrue(namedGameObjectMatcher.isMatching(newGameObjects(gameObjectFactory, faceObjectProviders, animationObjects, 1, 2, 2)));
095        Assert.assertTrue(namedGameObjectMatcher.isMatching(newGameObjects(gameObjectFactory, faceObjectProviders, animationObjects, 2, 1, 2)));
096        Assert.assertFalse(namedGameObjectMatcher.isMatching(newGameObjects(gameObjectFactory, faceObjectProviders, animationObjects, 2, 2, 1)));
097        Assert.assertFalse(namedGameObjectMatcher.isMatching(newGameObjects(gameObjectFactory, faceObjectProviders, animationObjects, 2, 2, 2)));
098    }
099
100    /**
101     * Creates a new {@link TestArchetype} instance.
102     * @param archetypeName the archetype's name
103     * @param typeNo the archetype's type number
104     * @param faceObjectProviders the face object providers for looking up
105     * faces
106     * @param animationObjects the animation objects for looking up animations
107     * @return the new instance
108     */
109    @NotNull
110    private static TestArchetype newArchetype(@NotNull final String archetypeName, final int typeNo, @NotNull final FaceObjectProviders faceObjectProviders, @NotNull final AnimationObjects animationObjects) {
111        final TestArchetype archetype = new TestDefaultArchetype(archetypeName, faceObjectProviders, animationObjects);
112        archetype.setAttributeInt(BaseObject.TYPE, typeNo);
113        return archetype;
114    }
115
116    /**
117     * Creates a new {@link TestGameObject} instance.
118     * @param gameObjectFactory the game object factory to use
119     * @param archetype the game object's archetype
120     * @param gameObjectName the game object's name
121     * @return the new instance
122     */
123    @NotNull
124    private static TestGameObject newGameObject(@NotNull final TestGameObjectFactory gameObjectFactory, @NotNull final TestArchetype archetype, @NotNull final String gameObjectName) {
125        final TestGameObject gameObject = gameObjectFactory.createGameObject(archetype);
126        gameObject.setAttributeString(BaseObject.NAME, gameObjectName);
127        return gameObject;
128    }
129
130    /**
131     * Creates a chain of {@link TestGameObject} instances having the given type
132     * numbers. The first type number is used for the game object; following
133     * type numbers are used for the parent objects.
134     * @param gameObjectFactory the game object factory to use
135     * @param faceObjectProviders the face object providers for looking up
136     * faces
137     * @param animationObjects the animation objects for looking up animations
138     * @param typeNumbers the type numbers
139     * @return the child game object
140     */
141    @NotNull
142    private static GameObject<?, ?, ?> newGameObjects(@NotNull final TestGameObjectFactory gameObjectFactory, @NotNull final FaceObjectProviders faceObjectProviders, @NotNull final AnimationObjects animationObjects, @NotNull final int... typeNumbers) {
143        TestGameObject gameObject = null;
144        for (final int typeNumber : typeNumbers) {
145            final String name = Integer.toString(typeNumber);
146            final TestArchetype archetype = newArchetype("arch" + name, typeNumber, faceObjectProviders, animationObjects);
147            final TestGameObject tmp = newGameObject(gameObjectFactory, archetype, name);
148            if (gameObject != null) {
149                tmp.addLast(gameObject);
150            }
151            gameObject = tmp;
152        }
153        if (gameObject == null) {
154            throw new IllegalArgumentException();
155        }
156        return gameObject;
157    }
158
159    /**
160     * Creates a new {@link FaceObjectProviders} instance.
161     * @return the face object providers instance
162     */
163    private FaceObjectProviders newFaceObjectProviders() {
164        final FaceObjects faceObjects = new TestFaceObjects();
165        assert resourceIcons != null;
166        return new FaceObjectProviders(0, faceObjects, resourceIcons);
167    }
168
169}