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-2015 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 
46  @Test
47  public void testSave1() throws IOException, InterruptedException {
48  final TestMapControlCreator mapControlCreator = new TestMapControlCreator();
49  final File mapsDirectory = mapControlCreator.createMapsDirectory();
50  try {
51  final MapFile mapFile = mapControlCreator.getPathManager().getMapFile(new File(mapControlCreator.getProjectSettings().getMapsDirectory(), "path1/path2/mapfile"));
52  final MapControl<TestGameObject, TestMapArchObject, TestArchetype> mapControl = mapControlCreator.newMapControl(mapFile, "name", new Size2D(1, 1));
53  mapControl.save(); // save map must not fail even if the destination dir does not yet exist
54  Assert.assertTrue(mapFile.getFile().exists());
55  } finally {
56  TestMapControlCreator.deleteTempDir(mapsDirectory);
57  }
58  }
59 
66  @Test
67  public void testSaveAs1() throws IOException, InterruptedException {
68  final TestMapControlCreator mapControlCreator = new TestMapControlCreator();
69  final File mapsDirectory = mapControlCreator.createMapsDirectory();
70  try {
71  final MapFile mapFile = mapControlCreator.getPathManager().getMapFile(new File(mapControlCreator.getProjectSettings().getMapsDirectory(), "path1/path2/mapfile"));
72  final MapControl<TestGameObject, TestMapArchObject, TestArchetype> mapControl = mapControlCreator.newMapControl(null, "name", new Size2D(1, 1));
73 
75  mapModel.beginTransaction("TEST");
76  try {
77  mapModel.addGameObjectToMap(mapControlCreator.getMapModelCreator().newGameObject("arch", "name"), new Point(0, 0), mapControlCreator.getMapModelCreator().getTopmostInsertionMode());
78  } finally {
79  mapModel.endTransaction();
80  }
81 
82  Assert.assertTrue(mapControl.getMapModel().isModified());
83  Assert.assertNull(mapControl.getMapModel().getMapFile());
84  mapControl.saveAs(mapFile); // save map must not fail even if the destination dir does not yet exist
85  Assert.assertTrue(mapFile.getFile().exists());
86  Assert.assertFalse(mapControl.getMapModel().isModified());
87  Assert.assertEquals(mapFile, mapControl.getMapModel().getMapFile());
88  } finally {
89  TestMapControlCreator.deleteTempDir(mapsDirectory);
90  }
91  }
92 
93 }
void save()
Saves the map to a file.
void testSaveAs1()
Checks that MapControl#saveAs(MapFile) does update the map&#39;s file and that it does reset the map&#39;s mo...
A MapModel reflects the data of a map.
Definition: MapModel.java:75
static void deleteTempDir(@NotNull final File dir)
Deletes a temporary directory.
void endTransaction()
End a transaction.
TestMapModelCreator getMapModelCreator()
Returns the TestMapModelCreator instance.
Helper class for creating MapControl instances for regression tests.
MapFile getMapFile(@NotNull final AbsoluteMapPath mapPath)
Returns a MapFile instance from an AbsoluteMapPath.
boolean isModified()
Return whether the map has been modified from the on-disk state.
A MapArchObject implementation for testing purposes.
MapModel< G, A, R > getMapModel()
Returns the map model.
Base package of all Gridarta classes.
void addGameObjectToMap(@NotNull G gameObject, @NotNull Point pos, @NotNull InsertionMode< G, A, R > insertionMode)
Add a gameObject to the map.
File createMapsDirectory(@NotNull final String... specs)
Creates a maps directory consisting of a set of maps.
TestGameObject newGameObject(@NotNull final String archetypeName, @NotNull final String objectName)
Creates a new game object.
GameObjects are the objects based on Archetypes found on maps.
void testSave1()
Checks that MapControl#save() does not fail if the path to the map file does not yet exist...
ProjectSettings getProjectSettings()
Returns the ProjectSettings.
Currently nothing more than a marker interface for unification.
Definition: MapControl.java:35
MapFile getMapFile()
Returns the map file.
File getFile()
Returns a File for this map file.
Definition: MapFile.java:102
An Archetype implementation for testing purposes.
A GameObject implementation for testing purposes.
File getMapsDirectory()
Returns the default maps directory.
InsertionMode< TestGameObject, TestMapArchObject, TestArchetype > getTopmostInsertionMode()
Returns the "topmost" insertion mode.
void saveAs(@NotNull MapFile mapFile)
Saves the file with the given map file.
void beginTransaction(@NotNull String name)
Starts a new transaction.
The location of a map file with a map directory.
Definition: MapFile.java:31
The class Size2D represents a 2d rectangular area.
Definition: Size2D.java:30
MapControl< TestGameObject, TestMapArchObject, TestArchetype > newMapControl(@Nullable final MapFile mapFile, @NotNull final String mapName, @NotNull final Size2D mapSize)
Creates a new map control.