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.gui.copybuffer;
021
022import java.awt.Point;
023import java.awt.Rectangle;
024import java.io.File;
025import net.sf.gridarta.gui.map.mapview.MapView;
026import net.sf.gridarta.gui.map.test.TestMapControlCreatorUtils;
027import net.sf.gridarta.model.archetype.DuplicateArchetypeException;
028import net.sf.gridarta.model.archetype.TestArchetype;
029import net.sf.gridarta.model.gameobject.TestGameObject;
030import net.sf.gridarta.model.maparchobject.TestMapArchObject;
031import net.sf.gridarta.model.mapcontrol.MapControl;
032import net.sf.gridarta.model.mapcontrol.TestMapControlCreator;
033import net.sf.gridarta.model.mapgrid.SelectionMode;
034import net.sf.gridarta.model.mapmodel.CannotInsertGameObjectException;
035import net.sf.gridarta.model.mapmodel.MapModel;
036import net.sf.gridarta.model.mapmodel.TestMapModelHelper;
037import net.sf.gridarta.utils.Size2D;
038import org.junit.Test;
039
040/**
041 * Regression tests for {@link CopyBuffer}.
042 * @author Andreas Kirschbaum
043 */
044public class CopyBufferTest {
045
046    /**
047     * The first map file.
048     */
049    private static final File MAP_FILE1 = new File("a");
050
051    /**
052     * The first map name.
053     */
054    private static final String MAP_NAME1 = "name1";
055
056    /**
057     * Checks that {@link CopyBuffer#cut(MapView, Rectangle)} followed by {@link
058     * CopyBuffer#paste(MapView, Point)} does work.
059     * @throws CannotInsertGameObjectException if the test fails
060     * @throws DuplicateArchetypeException if the test fails
061     */
062    @Test
063    public void testCutPaste1() throws CannotInsertGameObjectException, DuplicateArchetypeException {
064        final TestMapControlCreator testMapControlCreator = new TestMapControlCreator();
065        final TestMapModelHelper mapModelHelper = testMapControlCreator.newMapModelCreator();
066        final MapControl<TestGameObject, TestMapArchObject, TestArchetype> mapControl = testMapControlCreator.newMapControl(MAP_FILE1, MAP_NAME1, new Size2D(3, 3));
067
068        final Point point = new Point(1, 1);
069        final MapModel<TestGameObject, TestMapArchObject, TestArchetype> mapModel = mapControl.getMapModel();
070        mapModel.beginTransaction("TEST");
071        final TestGameObject ob1 = mapModelHelper.insertFloor(mapModel, point);
072        final TestGameObject ob2 = mapModelHelper.insertMob21(ob1);
073
074        TestMapModelHelper.checkMapContents(mapModel, "||", "|floor|", "||");
075        TestMapModelHelper.checkContents(ob1, ob2);
076
077        // select + cut
078        final CopyBuffer<TestGameObject, TestMapArchObject, TestArchetype> copyBuffer = TestMapControlCreatorUtils.newCopyBuffer(testMapControlCreator);
079        final MapView<TestGameObject, TestMapArchObject, TestArchetype> mapView = TestMapControlCreatorUtils.newMapView(mapControl);
080        mapView.getMapGrid().select(new Point(1, 1), SelectionMode.ADD);
081        copyBuffer.cut(mapView, new Rectangle(1, 1, 1, 1));
082
083        TestMapModelHelper.checkMapContents(mapModel, "||", "||", "||");
084
085        // paste
086        copyBuffer.paste(mapView, new Point(1, 1));
087
088        TestMapModelHelper.checkMapContents(mapModel, "||", "|floor|", "||");
089    }
090
091}