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 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.baseobject.BaseObject;
026import net.sf.gridarta.model.face.FaceObjectProviders;
027import net.sf.gridarta.model.face.FaceObjects;
028import net.sf.gridarta.model.face.TestFaceObjects;
029import net.sf.gridarta.model.gameobject.TestGameObjectFactory;
030import net.sf.gridarta.utils.ResourceIcons;
031import org.jetbrains.annotations.NotNull;
032import org.junit.Assert;
033import org.junit.Test;
034
035/**
036 * Test for {@link AttributeListUtils}.
037 * @author Andreas Kirschbaum
038 */
039public class AttributeListUtilsTest {
040
041    /**
042     * Test case for {@link AttributeListUtils#removeAttribute(String,
043     * String)}.
044     */
045    @Test
046    public void testRemoveAttribute() {
047        checkRemoveAttribute("", "abc", "");
048
049        checkRemoveAttribute("abc def\n", "abc", "");
050        checkRemoveAttribute("abc def\n", "ab", "abc def\n");
051        checkRemoveAttribute("abc def\n", "abcd", "abc def\n");
052
053        checkRemoveAttribute("abc def\n" + "ghi jkl\n" + "mno pqr\n", "abc", "ghi jkl\n" + "mno pqr\n");
054        checkRemoveAttribute("abc def\n" + "ghi jkl\n" + "mno pqr\n", "def", "abc def\n" + "ghi jkl\n" + "mno pqr\n");
055        checkRemoveAttribute("abc def\n" + "ghi jkl\n" + "mno pqr\n", "ghi", "abc def\n" + "mno pqr\n");
056        checkRemoveAttribute("abc def\n" + "ghi jkl\n" + "mno pqr\n", "mno", "abc def\n" + "ghi jkl\n");
057
058        checkRemoveAttribute("abc def\n" + "ghi jkl\n" + "mno pqr\n", "Abc", "abc def\n" + "ghi jkl\n" + "mno pqr\n");
059
060        checkRemoveAttribute("abc def\n\n" + "ghi jkl\n\n", "xyz", "abc def\n" + "ghi jkl\n");
061        checkRemoveAttribute("\n\n" + "abc def\n\n", "ghi", "abc def\n");
062        checkRemoveAttribute("\n\n" + "abc def\n\n", "abc", "");
063    }
064
065    /**
066     * Calls {@link AttributeListUtils#removeAttribute(String, String)} and
067     * checks for expected results.
068     * @param attributeList the attribute list parameter to pass
069     * @param key the key parameter to pass
070     * @param expectedResult the expected result value
071     */
072    private static void checkRemoveAttribute(@NotNull final String attributeList, @NotNull final String key, @NotNull final String expectedResult) {
073        Assert.assertEquals(expectedResult, AttributeListUtils.removeAttribute(attributeList, key));
074    }
075
076    /**
077     * Checks that {@link AttributeListUtils#diffArchTextValues(BaseObject,
078     * CharSequence)} returns correct values.
079     */
080    @Test
081    public void checkDiffArchTextValues1() {
082        Assert.assertEquals("b 3\nd 4\n", AttributeListUtils.diffArchTextValues(newArchetype("a 1\nb 2\nc 3\n"), "b 3\na 1\nd 4\n"));
083        Assert.assertEquals("d 4\nb 3\n", AttributeListUtils.diffArchTextValues(newArchetype("a 1\nb 2\nc 3\n"), "d 4\nb 3\na 1\n"));
084        Assert.assertEquals("b 3\nd 4\n", AttributeListUtils.diffArchTextValues(newArchetype("c 3\nb 2\na 1\n"), "b 3\na 1\nd 4\n"));
085        Assert.assertEquals("d 4\nb 3\n", AttributeListUtils.diffArchTextValues(newArchetype("c 3\nb 2\na 1\n"), "d 4\nb 3\na 1\n"));
086        Assert.assertEquals("e 5\nb 2\nf 6\n", AttributeListUtils.diffArchTextValues(newArchetype("a 1\nc 3\n"), "e 5\nb 2\nf 6\n"));
087        Assert.assertEquals("f 6\nb 2\ne 5\n", AttributeListUtils.diffArchTextValues(newArchetype("a 1\nc 3\n"), "f 6\nb 2\ne 5\n"));
088    }
089
090    /**
091     * Creates a new archetype.
092     * @param objectText the object text of the new archetype
093     * @return the new archetype
094     */
095    @NotNull
096    private static BaseObject<?, ?, ?, ?> newArchetype(@NotNull final String objectText) {
097        final FaceObjects faceObjects = new TestFaceObjects();
098        final ResourceIcons resourceIcons = new ResourceIcons();
099        final FaceObjectProviders faceObjectProviders = new FaceObjectProviders(0, faceObjects, resourceIcons);
100        final AnimationObjects animationObjects = new TestAnimationObjects();
101        final TestGameObjectFactory gameObjectFactory = new TestGameObjectFactory(faceObjectProviders, animationObjects);
102        final TestArchetype archetype = gameObjectFactory.newArchetype("arch");
103        archetype.setObjectText(objectText);
104        return archetype;
105    }
106
107}