Gridarta Editor
ReplaceUtilsTest.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.gui.dialog.replace;
21 
22 import java.awt.Point;
23 import java.util.Collections;
24 import java.util.List;
37 import net.sf.gridarta.utils.Size2D;
38 import org.junit.Assert;
39 import org.junit.Test;
40 
44 @SuppressWarnings("JavaDoc")
45 public class ReplaceUtilsTest {
46 
47  @Test
49  final TestMapControlCreator mapControlCreator = new TestMapControlCreator();
50  final MapControl<TestGameObject, TestMapArchObject, TestArchetype> mapControl = mapControlCreator.newMapControl(null, "test", new Size2D(2, 1));
52 
53  final InsertionMode insertionMode = mapControlCreator.getInsertionModeSet().getTopmostInsertionMode();
54  final TestArchetype arch1 = mapControlCreator.getMapModelCreator().getArchetype("arch1");
56  map.beginTransaction("test");
57  try {
58  final TestGameObject gameObject1 = mapControlCreator.getMapModelCreator().newGameObject(arch1.getArchetypeName(), "name", 0);
59  gameObject1.setAttributeInt(TestGameObject.ELEVATION, 1);
60  map.insertBaseObject(gameObject1, new Point(0, 0), true, false, insertionMode);
61 
62  final TestGameObject gameObject2 = mapControlCreator.getMapModelCreator().newGameObject(arch1.getArchetypeName(), "name", 0);
63  gameObject2.setAttributeInt(TestGameObject.ELEVATION, 2);
64  map.insertBaseObject(gameObject2, new Point(1, 0), true, false, insertionMode);
65  } finally {
66  map.endTransaction();
67  }
68 
69  final TestArchetype arch2 = mapControlCreator.getMapModelCreator().getArchetype("arch2");
70  final List<? extends BaseObject<TestGameObject, TestMapArchObject, TestArchetype, ?>> replaceList = Collections.singletonList(arch2);
71 
72  ReplaceUtils.replace(new ArchetypeNameMatchCriteria<>(arch1.getArchetypeName()), true, false, 100, mapView, mapControlCreator.getInsertionModeSet(), replaceList);
73 
74  final TestGameObject gameObject1 = map.getMapSquare(new Point(0, 0)).getFirst();
75  Assert.assertNotNull(gameObject1);
76  Assert.assertEquals(arch2, gameObject1.getArchetype());
77  Assert.assertEquals(1, gameObject1.getAttributeInt(TestGameObject.ELEVATION));
78 
79  final TestGameObject gameObject2 = map.getMapSquare(new Point(1, 0)).getFirst();
80  Assert.assertNotNull(gameObject2);
81  Assert.assertEquals(arch2, gameObject2.getArchetype());
82  Assert.assertEquals(2, gameObject2.getAttributeInt(TestGameObject.ELEVATION));
83  }
84 
85 }
net.sf.gridarta.model.archetype.TestArchetype
An Archetype implementation for testing purposes.
Definition: TestArchetype.java:30
net.sf.gridarta.gui.dialog.replace.ReplaceUtils.replace
static< G extends GameObject< G, A, R > A extends R extends Archetype< G, A, R > int replace(@NotNull final MatchCriteria< G, A, R > matchCriteria, final boolean entireMap, final boolean deleteOnly, final int replaceDensity, @NotNull final MapView< G, A, R > mapView, @NotNull final InsertionModeSet< G, A, R > insertionModeSet, @Nullable final List<? extends BaseObject< G, A, R, ?>> replaceList)
Definition: ReplaceUtils.java:62
net.sf.gridarta.model.mapmodel.MapModel
A MapModel reflects the data of a map.
Definition: MapModel.java:75
net.sf.gridarta.model.archetype.Archetype.getArchetypeName
String getArchetypeName()
Returns the name of this archetype.
net.sf.gridarta.model.mapmodel.MapModel.insertBaseObject
G insertBaseObject(@NotNull BaseObject< G, A, R, ?> baseObject, @NotNull Point pos, boolean allowMany, boolean join, @NotNull InsertionMode insertionMode)
Inserts a BaseObject to a map.
net.sf.gridarta
Base package of all Gridarta classes.
net.sf.gridarta.model.mapmodel.MapSquare.getFirst
G getFirst(@NotNull final GameObjectMatcher gameObjectMatcher)
Returns the first occurrence of a matching game object.
Definition: MapSquare.java:230
net.sf.gridarta.gui.dialog.replace.ReplaceUtilsTest.replace_withElevation_retainsElevation
void replace_withElevation_retainsElevation()
Definition: ReplaceUtilsTest.java:48
net.sf.gridarta.model.mapmodel.MapModel.endTransaction
void endTransaction()
End a transaction.
net.sf.gridarta.model.mapcontrol.TestMapControlCreator.getInsertionModeSet
InsertionModeSet< TestGameObject, TestMapArchObject, TestArchetype > getInsertionModeSet()
Returns the InsertionModeSet.
Definition: TestMapControlCreator.java:233
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.select.ArchetypeNameMatchCriteria
A MatchCriteria that matches by archetype name.
Definition: ArchetypeNameMatchCriteria.java:33
net.sf.gridarta.model.mapmodel
Definition: AboveFloorInsertionMode.java:20
net.sf.gridarta.model.archetype
Definition: AbstractArchetype.java:20
net.sf.gridarta.model.select
Definition: ArchetypeNameMatchCriteria.java:20
net.sf.gridarta.model.mapcontrol
Definition: DefaultMapControl.java:20
net.sf.gridarta.gui.map.test.TestMapControlCreatorUtils.newMapView
static MapView< TestGameObject, TestMapArchObject, TestArchetype > newMapView(@NotNull final MapControl< TestGameObject, TestMapArchObject, TestArchetype > mapControl)
Creates a new MapView instance.
Definition: TestMapControlCreatorUtils.java:64
net.sf.gridarta.gui.dialog.replace.ReplaceUtilsTest
Regression tests for ShrinkMapSizeUtils.
Definition: ReplaceUtilsTest.java:45
net.sf.gridarta.gui
Graphical User Interface of Gridarta.
net.sf.gridarta.gui.dialog.shrinkmapsize
Definition: ShrinkMapSizeDialog.java:20
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.gui.map.mapview
Definition: AbstractMapView.java:20
net.sf.gridarta.gui.map.mapview.MapView
A map view consists of a map grid and a map cursor, and is attached to a map control.
Definition: MapView.java:43
net.sf.gridarta.model.mapmodel.InsertionMode
Insertion modes.
Definition: InsertionMode.java:33
net.sf.gridarta.model.baseobject.BaseObject
Definition: BaseObject.java:34
net.sf.gridarta.gui.dialog
net.sf.gridarta.model.mapmodel.TestMapModelCreator.getArchetype
TestArchetype getArchetype(@NotNull final String archetypeName)
Returns an archetype.
Definition: TestMapModelCreator.java:249
net.sf.gridarta.gui.map.test.TestMapControlCreatorUtils
Helper class for creating MapControl instances for regression tests.
Definition: TestMapControlCreatorUtils.java:50
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.MapModel.getMapSquare
MapSquare< G, A, R > getMapSquare(@NotNull Point pos)
Get the square at a specified location.
net.sf.gridarta.model
net.sf.gridarta.model.gameobject.TestGameObject.ELEVATION
static final String ELEVATION
The name of the "elevation" attribute.
Definition: TestGameObject.java:45
net.sf.gridarta.model.baseobject
Definition: AbstractBaseObject.java:20
net.sf.gridarta.gui.map
Base classes for rendering maps.
Definition: AbstractPerMapDialogManager.java:20
net.sf.gridarta.gui.dialog.replace.ReplaceUtils
Utility class to replace objects in a map.
Definition: ReplaceUtils.java:44
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.utils.Size2D
The class Size2D represents a 2d rectangular area.
Definition: Size2D.java:30
net.sf.gridarta.utils
Definition: ActionBuilderUtils.java:20
net.sf.gridarta.gui.map.test
Definition: TestMapControlCreatorUtils.java:20
net.sf.gridarta.gui.dialog.shrinkmapsize.ShrinkMapSizeUtils
Utility class to remove empty squares from a map's border.
Definition: ShrinkMapSizeUtils.java:30