Gridarta Editor
ExitConnectorActionsTest.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.actions;
21 
22 import java.awt.Point;
23 import java.io.File;
39 import net.sf.gridarta.utils.Size2D;
40 import org.jetbrains.annotations.NotNull;
41 import org.jetbrains.annotations.Nullable;
42 import org.junit.Assert;
43 import org.junit.Before;
44 import org.junit.Test;
45 
51 
55  @NotNull
56  private static final MapFile BASE = new MapFile(new File("/tmp"));
57 
61  private static final MapFile MAP_FILE1 = new MapFile(BASE, "a");
62 
66  private static final String MAP_NAME1 = "name1";
67 
71  private static final MapFile MAP_FILE2 = new MapFile(BASE, "b");
72 
76  private static final String MAP_NAME2 = "name2";
77 
81  @Nullable
83 
87  @Nullable
89 
94  @Test
95  public void testExitCopy1() {
96  final ExitConnectorModel model = new TestExitConnectorModel();
98  final Size2D mapSize = new Size2D(5, 5);
99  assert testMapControlCreator != null;
100  final MapControl<TestGameObject, TestMapArchObject, TestArchetype> mapControl = testMapControlCreator.newMapControl(MAP_FILE1, MAP_NAME1, mapSize);
101 
102  // active ==> enabled
103  final Point point1 = new Point(3, 4);
104  Assert.assertTrue(actions.doExitCopy(false, mapControl, point1));
105  Assert.assertNull(model.getExitLocation());
106 
107  // perform copy
108  Assert.assertTrue(actions.doExitCopy(true, mapControl, point1));
109  assert testMapControlCreator != null;
110  Assert.assertEquals(new ExitLocation(MAP_FILE1, point1, MAP_NAME1), model.getExitLocation());
111 
112  // unsaved map ==> disabled
113  mapControl.getMapModel().setMapFile(null);
114  Assert.assertFalse(actions.doExitCopy(false, mapControl, point1));
115  assert testMapControlCreator != null;
116  Assert.assertEquals(new ExitLocation(MAP_FILE1, point1, MAP_NAME1), model.getExitLocation()); // disabled -> unchanged model
117  }
118 
123  @Test
124  public void testExitPaste1() {
125  final ExitConnectorModel model = new TestExitConnectorModel();
127  final Size2D mapSize = new Size2D(5, 5);
128  assert testMapControlCreator != null;
129  final MapControl<TestGameObject, TestMapArchObject, TestArchetype> mapControl = testMapControlCreator.newMapControl(MAP_FILE1, MAP_NAME1, mapSize);
131 
132  final Point point1 = new Point(3, 4);
133  final Point point2 = new Point(1, 2);
134 
135  assert testMapControlCreator != null;
136  model.setExitLocation(new ExitLocation(MAP_FILE2, point2, MAP_NAME2));
137 
138  // no exit at cursor and no auto-create ==> disabled
139  Assert.assertFalse(actions.doExitPaste(false, mapControl, point1));
140 
141  mapModel.beginTransaction("TEST");
142  try {
143  assert mapModelHelper != null;
144  mapModelHelper.insertFloor(mapModel, point1);
145  } finally {
146  mapModel.endTransaction();
147  }
148 
149  // no exit at cursor and no auto-create ==> disabled
150  Assert.assertFalse(actions.doExitPaste(false, mapControl, point1));
151 
152  mapModel.beginTransaction("TEST");
153  try {
154  assert mapModelHelper != null;
155  mapModelHelper.insertExit(mapModel, point1);
156  } finally {
157  mapModel.endTransaction();
158  }
159 
160  // exit at cursor ==> enabled
161  Assert.assertTrue(actions.doExitPaste(false, mapControl, point1));
162  checkExit(mapModel, point1, 1, "", new Point(0, 0)); // "check only" ==> exit is unchanged
163 
164  // perform paste
165  Assert.assertTrue(actions.doExitPaste(true, mapControl, point1));
166  checkExit(mapModel, point1, 1, "b", point2);
167  }
168 
173  @Test
174  public void testExitPaste2() {
175  final ExitConnectorModel model = new TestExitConnectorModel();
177  final Size2D mapSize = new Size2D(5, 5);
178 
179  assert testMapControlCreator != null;
180  final MapControl<TestGameObject, TestMapArchObject, TestArchetype> mapControl1 = testMapControlCreator.newMapControl(MAP_FILE1, MAP_NAME1, mapSize);
182 
183  final Point point1 = new Point(3, 4);
184  final Point point2 = new Point(1, 2);
185 
186  assert testMapControlCreator != null;
187  model.setExitLocation(new ExitLocation(MAP_FILE2, point2, MAP_NAME2));
188 
189  model.setAutoCreateExit(true);
190 
191  // copy must fail for unsaved map
192  mapModel1.setMapFile(null); // pretend unsaved map
193  Assert.assertFalse(actions.doExitCopy(true, mapControl1, point1));
194 
195  // paste must succeed for unsaved map
196  Assert.assertTrue(actions.doExitPaste(true, mapControl1, point1));
197  }
198 
203  @Test
204  public void testExitConnect1() {
205  final ExitConnectorModel model = new TestExitConnectorModel();
207 
208  final Size2D mapSize = new Size2D(5, 5);
209  assert testMapControlCreator != null;
210  final MapControl<TestGameObject, TestMapArchObject, TestArchetype> mapControl1 = testMapControlCreator.newMapControl(MAP_FILE1, MAP_NAME1, mapSize);
212 
213  assert testMapControlCreator != null;
214  final MapControl<TestGameObject, TestMapArchObject, TestArchetype> mapControl2 = testMapControlCreator.newMapControl(MAP_FILE2, MAP_NAME2, mapSize);
216 
217  final Point point1 = new Point(3, 4);
218  final Point point2 = new Point(1, 2);
219 
220  assert testMapControlCreator != null;
221  model.setExitLocation(new ExitLocation(MAP_FILE2, point2, MAP_NAME2));
222 
223  // no exit at cursor and no auto-create ==> disabled
224  Assert.assertFalse(actions.doExitConnect(false, mapControl1, point1));
225 
226  mapModel1.beginTransaction("TEST");
227  try {
228  assert mapModelHelper != null;
229  mapModelHelper.insertFloor(mapModel1, point1);
230  } finally {
231  mapModel1.endTransaction();
232  }
233 
234  // no exit at cursor and no auto-create ==> disabled
235  Assert.assertFalse(actions.doExitConnect(false, mapControl1, point1));
236 
237  mapModel1.beginTransaction("TEST");
238  try {
239  assert mapModelHelper != null;
240  mapModelHelper.insertExit(mapModel1, point1);
241  } finally {
242  mapModel1.endTransaction();
243  }
244 
245  // no exit at source and no auto-create ==> disabled
246  Assert.assertFalse(actions.doExitConnect(false, mapControl1, point1));
247 
248  mapModel2.beginTransaction("TEST");
249  try {
250  assert mapModelHelper != null;
251  mapModelHelper.insertExit(mapModel2, point2);
252  } finally {
253  mapModel2.endTransaction();
254  }
255 
256  // exit at source and cursor ==> enabled
257  Assert.assertTrue(actions.doExitConnect(false, mapControl1, point1));
258  checkExit(mapModel1, point1, 1, "", new Point(0, 0)); // "check only" ==> exit is unchanged
259  checkExit(mapModel2, point2, 0, "", new Point(0, 0));
260 
261  // perform connect
262  Assert.assertTrue(actions.doExitConnect(true, mapControl1, point1));
263  checkExit(mapModel1, point1, 1, "b", point2);
264  checkExit(mapModel2, point2, 0, "a", point1);
265  }
266 
271  @Test
272  public void testExitConnectAuto1() {
273  final ExitConnectorModel model = new TestExitConnectorModel();
275 
276  final Size2D mapSize = new Size2D(5, 5);
277  assert testMapControlCreator != null;
278  final MapControl<TestGameObject, TestMapArchObject, TestArchetype> mapControl1 = testMapControlCreator.newMapControl(MAP_FILE1, MAP_NAME1, mapSize);
280 
281  assert testMapControlCreator != null;
282  final MapControl<TestGameObject, TestMapArchObject, TestArchetype> mapControl2 = testMapControlCreator.newMapControl(MAP_FILE2, MAP_NAME2, mapSize);
284 
285  final Point point1 = new Point(3, 4);
286  final Point point2 = new Point(1, 2);
287 
288  assert testMapControlCreator != null;
289  model.setExitLocation(new ExitLocation(MAP_FILE2, point2, MAP_NAME2));
290  model.setAutoCreateExit(true);
291 
292  model.setExitArchetypeName("undefined");
293 
294  // fails due to undefined archetype
295  Assert.assertFalse(actions.doExitConnect(true, mapControl1, point1));
296  checkExit(mapModel1, point1, 0, null, null);
297  checkExit(mapModel2, point2, 0, null, null);
298 
299  model.setExitArchetypeName("exit");
300 
301  // perform connect
302  Assert.assertTrue(actions.doExitConnect(true, mapControl1, point1));
303  checkExit(mapModel1, point1, 0, "b", point2);
304  checkExit(mapModel2, point2, 0, "a", point1);
305 
306  // perform connect; auto-create will re-use existing objects
307  Assert.assertTrue(actions.doExitConnect(true, mapControl1, point1));
308  checkExit(mapModel1, point1, 0, "b", point2);
309  checkExit(mapModel1, point1, 1, null, null);
310  checkExit(mapModel2, point2, 0, "a", point1);
311  checkExit(mapModel2, point2, 1, null, null);
312  }
313 
317  @Test
318  public void testPath1() {
319  // both root ==> relative
320  testPath("/a", "/b", "b"); // both root ==> relative
321 
322  // to or from root ==> absolute
323  testPath("/HallOfSelection", "/world/world_104_115", "/world/world_104_115");
324  testPath("/world/world_104_115", "/HallOfSelection", "/HallOfSelection");
325 
326  // same top-level component ==> relative
327  testPath("/a/b/c/d", "/a/d/e", "../../d/e");
328  testPath("/a/b/c/d", "/a/b/d", "../d");
329  testPath("/a/b/c", "/a/b/d", "d");
330  testPath("/a/b/c", "/a/b/c/d", "c/d");
331 
332  // else ==> absolute
333  testPath("/a/b/c", "/b/c/d", "/b/c/d");
334  testPath("/a/b", "/b", "/b");
335  testPath("/a", "/b/c", "/b/c");
336  }
337 
345  private void testPath(@NotNull final String mapPathFrom, @NotNull final String mapPathTo, @NotNull final String expectedExitPath) {
346  final ExitConnectorModel model = new TestExitConnectorModel();
348 
349  assert testMapControlCreator != null;
350  final MapFile mapDir = new MapFile(testMapControlCreator.getProjectSettings().getMapsDirectory());
351  final MapFile mapFileFrom = new MapFile(mapDir, MapPathUtils.newMapPath(mapPathFrom));
352  final MapFile mapFileTo = new MapFile(mapDir, MapPathUtils.newMapPath(mapPathTo));
353 
354  final Size2D mapSize = new Size2D(5, 5);
355  assert testMapControlCreator != null;
356  final MapControl<TestGameObject, TestMapArchObject, TestArchetype> mapControl = testMapControlCreator.newMapControl(mapFileFrom, MAP_NAME1, mapSize);
358 
359  final Point pointFrom = new Point(3, 4);
360  final Point pointTo = new Point(1, 2);
361 
362  assert testMapControlCreator != null;
363  model.setExitLocation(new ExitLocation(mapFileTo, pointTo, MAP_NAME2));
364  mapModel.beginTransaction("TEST");
365  try {
366  assert mapModelHelper != null;
367  mapModelHelper.insertExit(mapModel, pointFrom);
368  } finally {
369  mapModel.endTransaction();
370  }
371 
372  // perform connect
373  Assert.assertTrue(actions.doExitPaste(true, mapControl, pointFrom));
374  checkExit(mapModel, pointFrom, 0, expectedExitPath, pointTo);
375  }
376 
381  @Before
382  public void setUp() throws DuplicateArchetypeException {
383  testMapControlCreator = new TestMapControlCreator();
384  mapModelHelper = testMapControlCreator.newMapModelCreator();
385  }
386 
393  final TestMapControlCreator tmpTestMapControlCreator = testMapControlCreator;
394  assert tmpTestMapControlCreator != null;
395  return new ExitConnectorActions<>(model, tmpTestMapControlCreator.getExitMatcher(), tmpTestMapControlCreator.getArchetypeSet(), tmpTestMapControlCreator.getMapManager(), new TestFileControl(), tmpTestMapControlCreator.getInsertionModeSet());
396  }
397 
408  private static void checkExit(@NotNull final MapModel<TestGameObject, TestMapArchObject, TestArchetype> mapModel, @NotNull final Point point, final int index, @Nullable final String exitPath, @Nullable final Point exitPoint) {
409  int thisIndex = 0;
410  for (final BaseObject<?, ?, ?, ?> gameObject : mapModel.getMapSquare(point)) {
411  if (thisIndex == index) {
412  if (gameObject.getTypeNo() != TestMapModelHelper.EXIT_TYPE) {
413  break;
414  }
415 
416  if (exitPath == null || exitPoint == null) {
417  Assert.fail("exit found but none expected");
418  throw new AssertionError();
419  }
420 
421  Assert.assertEquals(exitPath, gameObject.getAttributeString(BaseObject.SLAYING));
422  Assert.assertEquals(exitPoint.x, gameObject.getAttributeInt(BaseObject.HP));
423  Assert.assertEquals(exitPoint.y, gameObject.getAttributeInt(BaseObject.SP));
424  return;
425  }
426 
427  thisIndex++;
428  }
429 
430  if (exitPath != null || exitPoint != null) {
431  Assert.fail("no exit found");
432  }
433  }
434 
435 }
void testPath(@NotNull final String mapPathFrom, @NotNull final String mapPathTo, @NotNull final String expectedExitPath)
Checks that an exit path is correctly generated.
ExitMatcher< TestGameObject, TestMapArchObject, TestArchetype > getExitMatcher()
Returns the ExitMatcher.
void testExitConnect1()
Checks that MapControl, Point) does work.
void setExitLocation(@Nullable ExitLocation exitLocation)
Sets the remembered exit location.
static final int EXIT_TYPE
The archetype type used for "exit" game objects.
static MapPath newMapPath(@NotNull final String string)
Creates a MapPath instance from string representation.
void testPath1()
Checks that exit paths are correctly generated.
A MapModel reflects the data of a map.
Definition: MapModel.java:75
static final MapFile MAP_FILE1
The first map file.
void endTransaction()
End a transaction.
Helper class for creating MapModel instances for regression tests.
static final String MAP_NAME1
The first map name.
ExitConnectorActions< TestGameObject, TestMapArchObject, TestArchetype > createActions(@NotNull final ExitConnectorModel model)
Creates a new ExitConnectorActions instance.
Helper class for creating MapControl instances for regression tests.
Regression tests for ExitConnectorActions.
void testExitPaste1()
Checks that MapControl, Point) does work.
A MapArchObject implementation for testing purposes.
void setExitArchetypeName(@NotNull String exitArchetypeName)
Sets the archetype name for creating exit game objects.
String SLAYING
The name of the "slaying" attribute.
MapModel< G, A, R > getMapModel()
Returns the map model.
TestGameObject insertFloor(@NotNull final MapModel< TestGameObject, TestMapArchObject, TestArchetype > mapModel, @NotNull final Point point)
Inserts a floorArchetype game object into a map model.
Base package of all Gridarta classes.
ExitConnectorModel implementation for regression tests.
A FileControl implementation for testing purposes.
boolean doExitConnect(final boolean performAction, @NotNull final MapControl< G, A, R > mapControl, @NotNull final Point targetLocation)
Executes the "exit connect" action.
An Exception indicating that an Archetype name is not unique.
String HP
The attribute name of the "hp" attribute.
Definition: BaseObject.java:90
GameObjects are the objects based on Archetypes found on maps.
static final String MAP_NAME2
The second map name.
Utility class for MapPath related functions.
ArchetypeSet< TestGameObject, TestMapArchObject, TestArchetype > getArchetypeSet()
Returns the ArchetypeSet.
InsertionModeSet< TestGameObject, TestMapArchObject, TestArchetype > getInsertionModeSet()
Returns the InsertionModeSet.
ProjectSettings getProjectSettings()
Returns the ProjectSettings.
TestMapModelHelper newMapModelCreator()
Creates a new TestMapModelHelper instance.
void testExitConnectAuto1()
Checks that MapControl, Point) does work when auto-creating exit objects.
ExitLocation getExitLocation()
Returns the remembered exit location.
TestGameObject insertExit(@NotNull final MapModel< TestGameObject, TestMapArchObject, TestArchetype > mapModel, @NotNull final Point point)
Inserts an exitArchetype game object into a map model.
static final MapFile BASE
The base MapFile for the test.
void setAutoCreateExit(boolean autoCreateExit)
Sets whether exit game objects should be auto-created when needed.
Utility class implementing actions that operate on ExitConnectorModels.
Stores information needed by the exit connector.
Stores information about a remembered exit location.
void testExitCopy1()
Checks that MapControl, Point) does work.
void testExitPaste2()
Checks that MapControl, Point) does not crash when connecting to an unsaved map.
boolean doExitCopy(final boolean performAction, @NotNull final MapControl< G, A, R > mapControl, @NotNull final Point location)
Executes the "exit copy" action.
Currently nothing more than a marker interface for unification.
Definition: MapControl.java:35
String SP
The attribute name of the "sp" attribute.
Definition: BaseObject.java:96
static final MapFile MAP_FILE2
The second map file.
An Archetype implementation for testing purposes.
A GameObject implementation for testing purposes.
File getMapsDirectory()
Returns the default maps directory.
void beginTransaction(@NotNull String name)
Starts a new transaction.
void setMapFile(@Nullable MapFile mapFile)
Sets the map file.
TestMapControlCreator testMapControlCreator
The TestMapControlCreator for creating maps.
boolean doExitPaste(final boolean performAction, @NotNull final MapControl< G, A, R > mapControl, @NotNull final Point targetLocation)
Executes the "exit paste" action.
TestMapModelHelper mapModelHelper
The TestMapModelHelper instance.
The location of a map file with a map directory.
Definition: MapFile.java:31
MapManager< TestGameObject, TestMapArchObject, TestArchetype > getMapManager()
Returns the MapManager.
static void checkExit(@NotNull final MapModel< TestGameObject, TestMapArchObject, TestArchetype > mapModel, @NotNull final Point point, final int index, @Nullable final String exitPath, @Nullable final Point exitPoint)
Checks that a map model contains an exit game object.
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.