Gridarta Editor
DefaultMapControlTest.java
Go to the documentation of this file.
1 /*
2  * Gridarta MMORPG map editor for Crossfire, Daimonin and similar games.
3  * Copyright (C) 2000-2023 The Gridarta Developers.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 
20 package net.sf.gridarta.model.mapcontrol;
21 
22 import java.awt.Point;
23 import java.io.File;
24 import java.io.IOException;
30 import net.sf.gridarta.utils.Size2D;
31 import org.junit.Assert;
32 import org.junit.Test;
33 
38 public class DefaultMapControlTest {
39 
45  @Test
46  public void testSave1() throws IOException {
47  final TestMapControlCreator mapControlCreator = new TestMapControlCreator();
48  final File mapsDirectory = mapControlCreator.createMapsDirectory();
49  try {
50  final MapFile mapFile = mapControlCreator.getPathManager().getMapFile(new File(mapControlCreator.getProjectSettings().getMapsDirectory(), "path1/path2/mapfile"));
51  final MapControl<TestGameObject, TestMapArchObject, TestArchetype> mapControl = mapControlCreator.newMapControl(mapFile, "name", new Size2D(1, 1));
52  mapControl.save(); // save map must not fail even if the destination dir does not yet exist
53  Assert.assertTrue(mapFile.getFile().exists());
54  } finally {
55  TestMapControlCreator.deleteTempDir(mapsDirectory);
56  }
57  }
58 
64  @Test
65  public void testSaveAs1() throws IOException {
66  final TestMapControlCreator mapControlCreator = new TestMapControlCreator();
67  final File mapsDirectory = mapControlCreator.createMapsDirectory();
68  try {
69  final MapFile mapFile = mapControlCreator.getPathManager().getMapFile(new File(mapControlCreator.getProjectSettings().getMapsDirectory(), "path1/path2/mapfile"));
70  final MapControl<TestGameObject, TestMapArchObject, TestArchetype> mapControl = mapControlCreator.newMapControl(null, "name", new Size2D(1, 1));
71 
73  mapModel.beginTransaction("TEST");
74  try {
75  mapModel.addGameObjectToMap(mapControlCreator.getMapModelCreator().newGameObject("arch", "name"), new Point(0, 0), mapControlCreator.getMapModelCreator().getTopmostInsertionMode());
76  } finally {
77  mapModel.endTransaction();
78  }
79 
80  Assert.assertTrue(mapControl.getMapModel().isModified());
81  Assert.assertNull(mapControl.getMapModel().getMapFile());
82  mapControl.saveAs(mapFile); // save map must not fail even if the destination dir does not yet exist
83  Assert.assertTrue(mapFile.getFile().exists());
84  Assert.assertFalse(mapControl.getMapModel().isModified());
85  Assert.assertEquals(mapFile, mapControl.getMapModel().getMapFile());
86  } finally {
87  TestMapControlCreator.deleteTempDir(mapsDirectory);
88  }
89  }
90 
91 }
net.sf.gridarta.model.archetype.TestArchetype
An Archetype implementation for testing purposes.
Definition: TestArchetype.java:30
net.sf.gridarta.model.mapcontrol.TestMapControlCreator.createMapsDirectory
File createMapsDirectory(@NotNull final String... specs)
Creates a maps directory consisting of a set of maps.
Definition: TestMapControlCreator.java:312
net.sf.gridarta.model.mapmodel.MapModel
A MapModel reflects the data of a map.
Definition: MapModel.java:75
net.sf.gridarta.model.mapcontrol.MapControl.saveAs
void saveAs(@NotNull MapFile mapFile)
Saves the file with the given map file.
net.sf.gridarta
Base package of all Gridarta classes.
net.sf.gridarta.model.mapmodel.MapModel.endTransaction
void endTransaction()
End a transaction.
net.sf.gridarta.model.gameobject.TestGameObject
A GameObject implementation for testing purposes.
Definition: TestGameObject.java:34
net.sf
net.sf.gridarta.model.mapmodel.MapModel.beginTransaction
void beginTransaction(@NotNull String name)
Starts a new transaction.
net.sf.gridarta.model.mapmodel
Definition: AboveFloorInsertionMode.java:20
net.sf.gridarta.model.mapcontrol.DefaultMapControlTest
Regression tests for DefaultMapControl.
Definition: DefaultMapControlTest.java:38
net.sf.gridarta.model.mapcontrol.TestMapControlCreator.deleteTempDir
static void deleteTempDir(@NotNull final File dir)
Deletes a temporary directory.
Definition: TestMapControlCreator.java:389
net.sf.gridarta.model.archetype
Definition: AbstractArchetype.java:20
net.sf.gridarta.model.mapmodel.MapModel.addGameObjectToMap
void addGameObjectToMap(@NotNull G gameObject, @NotNull Point pos, @NotNull InsertionMode insertionMode)
Add a gameObject to the map.
net.sf.gridarta.model.mapmodel.MapModel.isModified
boolean isModified()
Return whether the map has been modified from the on-disk state.
net.sf.gridarta.model.mapcontrol.TestMapControlCreator.getPathManager
PathManager getPathManager()
Returns the PathManager.
Definition: TestMapControlCreator.java:188
net.sf.gridarta.model.mapcontrol.TestMapControlCreator
Helper class for creating MapControl instances for regression tests.
Definition: TestMapControlCreator.java:68
net.sf.gridarta.model.gameobject
GameObjects are the objects based on Archetypes found on maps.
Definition: AbstractGameObject.java:20
net
net.sf.gridarta.model.mapcontrol.DefaultMapControlTest.testSaveAs1
void testSaveAs1()
Checks that MapControl#saveAs(MapFile) does update the map's file and that it does reset the map's mo...
Definition: DefaultMapControlTest.java:65
net.sf.gridarta.model.mapcontrol.DefaultMapControlTest.testSave1
void testSave1()
Checks that MapControl#save() does not fail if the path to the map file does not yet exist.
Definition: DefaultMapControlTest.java:46
net.sf.gridarta.model.io.PathManager.getMapFile
MapFile getMapFile(@NotNull final AbsoluteMapPath mapPath)
Returns a MapFile instance from an AbsoluteMapPath.
Definition: PathManager.java:72
net.sf.gridarta.model.mapmodel.MapFile.getFile
File getFile()
Returns a File for this map file.
Definition: MapFile.java:102
net.sf.gridarta.model.mapcontrol.MapControl.save
void save()
Saves the map to a file.
net.sf.gridarta.model.mapmodel.MapFile
The location of a map file with a map directory.
Definition: MapFile.java:31
net.sf.gridarta.model.mapmodel.TestMapModelCreator.newGameObject
TestGameObject newGameObject(@NotNull final String archetypeName, @NotNull final String objectName)
Creates a new game object.
Definition: TestMapModelCreator.java:181
net.sf.gridarta.model.mapmodel.TestMapModelCreator.getTopmostInsertionMode
InsertionMode getTopmostInsertionMode()
Returns the "topmost" insertion mode.
Definition: TestMapModelCreator.java:239
net.sf.gridarta.model
net.sf.gridarta.model.mapcontrol.TestMapControlCreator.getProjectSettings
ProjectSettings getProjectSettings()
Returns the ProjectSettings.
Definition: TestMapControlCreator.java:197
net.sf.gridarta.model.mapcontrol.TestMapControlCreator.newMapControl
MapControl< TestGameObject, TestMapArchObject, TestArchetype > newMapControl(@Nullable final MapFile mapFile, @NotNull final String mapName, @NotNull final Size2D mapSize)
Creates a new map control.
Definition: TestMapControlCreator.java:176
net.sf.gridarta.model.mapcontrol.MapControl
Currently nothing more than a marker interface for unification.
Definition: MapControl.java:35
net.sf.gridarta.model.mapcontrol.TestMapControlCreator.getMapModelCreator
TestMapModelCreator getMapModelCreator()
Returns the TestMapModelCreator instance.
Definition: TestMapControlCreator.java:251
net.sf.gridarta.model.mapcontrol.MapControl.getMapModel
MapModel< G, A, R > getMapModel()
Returns the map model.
net.sf.gridarta.model.maparchobject.TestMapArchObject
A MapArchObject implementation for testing purposes.
Definition: TestMapArchObject.java:28
net.sf.gridarta.model.maparchobject
Definition: AbstractMapArchObject.java:20
net.sf.gridarta.model.settings.ProjectSettings.getMapsDirectory
File getMapsDirectory()
Returns the default maps directory.
net.sf.gridarta.model.mapmodel.MapModel.getMapFile
MapFile getMapFile()
Returns the map file.
net.sf.gridarta.utils.Size2D
The class Size2D represents a 2d rectangular area.
Definition: Size2D.java:30
net.sf.gridarta.utils
Definition: ActionBuilderUtils.java:20