001/*
002 * Gridarta MMORPG map editor for Crossfire, Daimonin and similar games.
003 * Copyright (C) 2000-2011 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.actions;
021
022import java.awt.GraphicsEnvironment;
023import java.awt.Point;
024import net.sf.gridarta.gui.undo.UndoControl;
025import net.sf.gridarta.model.archetype.DuplicateArchetypeException;
026import net.sf.gridarta.model.archetype.TestArchetype;
027import net.sf.gridarta.model.autojoin.AutojoinListsHelper;
028import net.sf.gridarta.model.autojoin.IllegalAutojoinListException;
029import net.sf.gridarta.model.baseobject.BaseObject;
030import net.sf.gridarta.model.gameobject.TestGameObject;
031import net.sf.gridarta.model.maparchobject.TestMapArchObject;
032import net.sf.gridarta.model.mapcontrol.MapControl;
033import net.sf.gridarta.model.mapcontrol.TestMapControlCreator;
034import net.sf.gridarta.model.mapmanager.MapManager;
035import net.sf.gridarta.model.mapmodel.CannotInsertGameObjectException;
036import net.sf.gridarta.model.mapmodel.MapModel;
037import net.sf.gridarta.model.mapmodel.MapSquare;
038import net.sf.gridarta.model.mapmodel.TestMapModelCreator;
039import net.sf.gridarta.model.mapmodel.TestMapModelHelper;
040import net.sf.gridarta.utils.Size2D;
041import org.jetbrains.annotations.NotNull;
042import org.junit.Assert;
043import org.junit.Test;
044
045/**
046 * Regression tests for {@link UndoActions}.
047 * @author Andreas Kirschbaum
048 */
049public class UndoActionsTest {
050
051    /**
052     * Checks that undo correctly sets the "face" attribute.
053     * @throws IllegalAutojoinListException if the test fails
054     * @throws DuplicateArchetypeException if the test fails
055     * @throws CannotInsertGameObjectException if the test fails
056     */
057    @Test
058    public void testInsert1() throws CannotInsertGameObjectException, DuplicateArchetypeException, IllegalAutojoinListException {
059        if (GraphicsEnvironment.isHeadless()) {
060            return;
061        }
062
063        final TestMapControlCreator mapControlCreator = new TestMapControlCreator();
064        final TestMapModelCreator mapModelCreator = mapControlCreator.getMapModelCreator();
065        final AutojoinListsHelper autojoinListsHelper = mapControlCreator.newAutojoinListsHelper();
066        for (int i = 0; i < 16; i++) {
067            final TestArchetype a = mapModelCreator.getArchetype("a" + i);
068            a.setAttributeString(BaseObject.FACE, "face" + i);
069        }
070        autojoinListsHelper.newAutojoinLists("a0", "a1", "a2", "a3", "a4", "a5", "a6", "a7", "a8", "a9", "a10", "a11", "a12", "a13", "a14", "a15");
071
072        final MapManager<TestGameObject, TestMapArchObject, TestArchetype> mapManager = mapControlCreator.newMapManager();
073        final UndoControl<TestGameObject, TestMapArchObject, TestArchetype> undoControl = new UndoControl<TestGameObject, TestMapArchObject, TestArchetype>(mapManager, mapModelCreator.getGameObjectFactory(), mapModelCreator.getGameObjectMatchers());
074
075        final TestMapArchObject mapArchObject = new TestMapArchObject();
076        mapArchObject.setMapSize(new Size2D(2, 1));
077        final MapControl<TestGameObject, TestMapArchObject, TestArchetype> mapControl = mapManager.newMap(null, mapArchObject, null, true);
078        final MapModel<TestGameObject, TestMapArchObject, TestArchetype> mapModel = mapControl.getMapModel();
079        final TestMapModelHelper testMapModelHelper = mapModelCreator.newTestMapModelHelper();
080
081        final TestArchetype a0 = mapModelCreator.getArchetype("a0");
082
083        mapModelCreator.getMapViewSettings().setAutojoin(true);
084        mapModel.beginTransaction("TEST");
085        try {
086            testMapModelHelper.insertArchetype(mapModel, 0, 0, a0, false);
087        } finally {
088            mapModel.endTransaction();
089        }
090
091        TestMapModelHelper.checkMapContents(mapModel, "a0|");
092        checkFace(mapModel, 0, 0, "face0");
093
094        mapModel.beginTransaction("TEST");
095        try {
096            testMapModelHelper.insertArchetype(mapModel, 1, 0, a0, true);
097        } finally {
098            mapModel.endTransaction();
099        }
100
101        TestMapModelHelper.checkMapContents(mapModel, "a2|a8");
102        checkFace(mapModel, 0, 0, "face2");
103        checkFace(mapModel, 1, 0, "face8");
104
105        undoControl.undo();
106        TestMapModelHelper.checkMapContents(mapModel, "a0|");
107        checkFace(mapModel, 0, 0, "face0");
108    }
109
110    /**
111     * Checks the face name of the first object in a map square.
112     * @param mapModel the map model to check
113     * @param x the x coordinate of the map square to check
114     * @param y the y coordinate of the map square to check
115     * @param face the expected face name
116     */
117    private static void checkFace(@NotNull final MapModel<TestGameObject, TestMapArchObject, TestArchetype> mapModel, final int x, final int y, @NotNull final String face) {
118        final MapSquare<?, ?, ?> mapSquare = mapModel.getMapSquare(new Point(x, y));
119        final BaseObject<?, ?, ?, ?> object = mapSquare.getFirst();
120        Assert.assertNotNull(object);
121        Assert.assertEquals(face, object.getAttributeString(BaseObject.FACE));
122    }
123
124}