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 java.awt.Point;
023import java.io.File;
024import java.io.IOException;
025import java.util.List;
026import net.sf.gridarta.model.archetype.DuplicateArchetypeException;
027import net.sf.gridarta.model.archetype.TestArchetype;
028import net.sf.gridarta.model.gameobject.TestGameObject;
029import net.sf.gridarta.model.maparchobject.TestMapArchObject;
030import net.sf.gridarta.model.mapcontrol.MapControl;
031import net.sf.gridarta.model.mapcontrol.TestMapControlCreator;
032import net.sf.gridarta.model.mapmodel.InsertionMode;
033import net.sf.gridarta.model.mapmodel.MapModel;
034import net.sf.gridarta.model.mapmodel.TestMapModelCreator;
035import net.sf.gridarta.utils.Size2D;
036import org.junit.Assert;
037import org.junit.Test;
038
039/**
040 * Regression tests for {@link DefaultMapReader}.
041 * @author Andreas Kirschbaum
042 */
043public class DefaultMapReaderTest {
044
045    /**
046     * Checks that map loading doesn't reorder multi-square game objects.
047     * @throws DuplicateArchetypeException if the test fails
048     * @throws IOException if the test fails
049     */
050    @Test
051    public void testReorderMultiSquares() throws DuplicateArchetypeException, IOException {
052        final TestMapControlCreator mapControlCreator = new TestMapControlCreator();
053        final TestMapModelCreator mapModelCreator = mapControlCreator.getMapModelCreator();
054        final File mapFile = File.createTempFile("gridarta", null);
055        final MapControl<TestGameObject, TestMapArchObject, TestArchetype> mapControl = mapControlCreator.newMapControl(mapFile, "test", new Size2D(2, 1));
056        final MapModel<TestGameObject, TestMapArchObject, TestArchetype> mapModel = mapControl.getMapModel();
057        final InsertionMode<TestGameObject, TestMapArchObject, TestArchetype> insertionMode = mapModelCreator.getTopmostInsertionMode();
058        final TestArchetype arch1x1 = mapModelCreator.getArchetype("arch1x1");
059        final TestArchetype arch2x1 = mapModelCreator.newArchetype("arch2x1");
060        final TestArchetype arch2x1b = mapModelCreator.newArchetype("arch2x1b");
061        arch2x1b.setMultiX(1);
062        arch2x1.addTailPart(arch2x1b);
063        mapModelCreator.getArchetypeSet().addArchetype(arch2x1);
064        mapModel.insertArchToMap(arch1x1, null, new Point(0, 0), false);
065        mapModel.insertArchToMap(arch1x1, null, new Point(1, 0), false);
066        mapModel.insertArchToMap(arch2x1, null, new Point(0, 0), false);
067        mapModel.insertArchToMap(arch1x1, null, new Point(0, 0), false);
068        mapModel.insertArchToMap(arch1x1, null, new Point(1, 0), false);
069        mapControl.save();
070        final DefaultMapReader<TestGameObject, TestMapArchObject, TestArchetype> reader = mapControlCreator.newMapReader(mapFile);
071        final List<TestGameObject> objects = reader.getGameObjects();
072        Assert.assertEquals(6, objects.size());
073        Assert.assertEquals("arch1x1", objects.get(0).getArchetype().getArchetypeName());
074        Assert.assertEquals("arch2x1", objects.get(1).getArchetype().getArchetypeName());
075        Assert.assertEquals("arch1x1", objects.get(2).getArchetype().getArchetypeName());
076        Assert.assertEquals("arch1x1", objects.get(3).getArchetype().getArchetypeName());
077        Assert.assertEquals("arch1x1", objects.get(4).getArchetype().getArchetypeName());
078        Assert.assertEquals("arch2x1b", objects.get(5).getArchetype().getArchetypeName());
079    }
080
081}