Gridarta Editor
PropagateElevationTest.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.var.crossfire.model.gameobject;
21 
22 import java.util.Iterator;
26 import org.jetbrains.annotations.NotNull;
27 import org.junit.Assert;
28 import org.junit.Test;
29 
35 public class PropagateElevationTest {
36 
41  @Test
42  public void testAddFirst() {
43  final GameObjectCreator gameObjectCreator = new GameObjectCreator();
44  final GameObjectContainer<GameObject, MapArchObject, Archetype> gameObjectContainer = gameObjectCreator.newGameObject(0);
45 
46  // first game object => keep
47  gameObjectContainer.addFirst(gameObjectCreator.newGameObject(123));
48  check(gameObjectContainer, 123);
49 
50  // without elevation => propagate
51  gameObjectContainer.addFirst(gameObjectCreator.newGameObject(0));
52  check(gameObjectContainer, 123);
53 
54  // with elevation => ignore but propagate
55  gameObjectContainer.addFirst(gameObjectCreator.newGameObject(321));
56  check(gameObjectContainer, 123);
57  }
58 
63  @Test
64  public void testAddLast() {
65  final GameObjectCreator gameObjectCreator = new GameObjectCreator();
66  final GameObjectContainer<GameObject, MapArchObject, Archetype> gameObjectContainer = gameObjectCreator.newGameObject(0);
67 
68  // first game object => keep
69  gameObjectContainer.addLast(gameObjectCreator.newGameObject(123));
70  check(gameObjectContainer, 123);
71 
72  // without elevation => keep
73  gameObjectContainer.addLast(gameObjectCreator.newGameObject(0));
74  check(gameObjectContainer, 123, 0);
75 
76  // with elevation => keep
77  gameObjectContainer.addLast(gameObjectCreator.newGameObject(321));
78  check(gameObjectContainer, 123, 0, 321);
79  }
80 
85  @Test
86  public void testInsertBefore() {
87  final GameObjectCreator gameObjectCreator = new GameObjectCreator();
88  final GameObjectContainer<GameObject, MapArchObject, Archetype> gameObjectContainer = gameObjectCreator.newGameObject(0);
89 
90  // first game object => keep
91  gameObjectContainer.insertBefore(gameObjectCreator.newGameObject(1), null);
92  check(gameObjectContainer, 1);
93 
94  // without elevation => propagate
95  gameObjectContainer.insertBefore(gameObjectCreator.newGameObject(0), null);
96  check(gameObjectContainer, 1, 0);
97 
98  // with elevation => ignore but propagate
99  final GameObject ob1 = gameObjectCreator.newGameObject(2);
100  gameObjectContainer.insertBefore(ob1, null);
101  check(gameObjectContainer, 1, 0, 0);
102 
103  // with elevation => keep
104  gameObjectContainer.insertBefore(gameObjectCreator.newGameObject(3), ob1);
105  check(gameObjectContainer, 1, 3, 0, 0);
106 
107  // with elevation => ignore but propagate
108  gameObjectContainer.insertBefore(gameObjectCreator.newGameObject(4), null);
109  check(gameObjectContainer, 1, 0, 3, 0, 0);
110  }
111 
116  @Test
117  public void testInsertAfter() {
118  final GameObjectCreator gameObjectCreator = new GameObjectCreator();
119  final GameObjectContainer<GameObject, MapArchObject, Archetype> gameObjectContainer = gameObjectCreator.newGameObject(0);
120 
121  // first game object => keep
122  gameObjectContainer.insertAfter(null, gameObjectCreator.newGameObject(1));
123  check(gameObjectContainer, 1);
124 
125  // at end => keep
126  final GameObject ob1 = gameObjectCreator.newGameObject(2);
127  gameObjectContainer.insertAfter(null, ob1);
128  check(gameObjectContainer, 1, 2);
129 
130  // at front, without elevation => propagate
131  gameObjectContainer.insertAfter(ob1, gameObjectCreator.newGameObject(0));
132  check(gameObjectContainer, 1, 0, 2);
133 
134  // middle => keep
135  gameObjectContainer.insertAfter(ob1, gameObjectCreator.newGameObject(3));
136  check(gameObjectContainer, 1, 0, 3, 2);
137 
138  // at front, with elevation => ignore but propagate
139  gameObjectContainer.insertAfter(gameObjectContainer.getFirst(), gameObjectCreator.newGameObject(4));
140  check(gameObjectContainer, 1, 0, 0, 3, 2);
141  }
142 
147  @Test
148  public void testMoveBottom() {
149  final GameObjectCreator gameObjectCreator = new GameObjectCreator();
150  final GameObjectContainer<GameObject, MapArchObject, Archetype> container1 = newContainer(gameObjectCreator, 1, 0, 0, 0);
151  container1.moveBottom(get(container1, 0));
152  check(container1, 1, 0, 0, 0);
153  container1.moveBottom(get(container1, 1));
154  check(container1, 1, 0, 0, 0);
155  container1.moveBottom(get(container1, 3));
156  check(container1, 1, 0, 0, 0);
157  }
158 
163  @Test
164  public void testMoveDown() {
165  final GameObjectCreator gameObjectCreator = new GameObjectCreator();
166  final GameObjectContainer<GameObject, MapArchObject, Archetype> container1 = newContainer(gameObjectCreator, 1, 0, 0, 0);
167  container1.moveDown(get(container1, 0));
168  check(container1, 1, 0, 0, 0);
169  container1.moveDown(get(container1, 1));
170  check(container1, 1, 0, 0, 0);
171  container1.moveDown(get(container1, 3));
172  check(container1, 1, 0, 0, 0);
173  }
174 
179  @Test
180  public void testMoveUp() {
181  final GameObjectCreator gameObjectCreator = new GameObjectCreator();
182  final GameObjectContainer<GameObject, MapArchObject, Archetype> container1 = newContainer(gameObjectCreator, 1, 0, 0, 0);
183  container1.moveUp(get(container1, 0));
184  check(container1, 1, 0, 0, 0);
185  container1.moveUp(get(container1, 1));
186  check(container1, 1, 0, 0, 0);
187  container1.moveUp(get(container1, 3));
188  check(container1, 1, 0, 0, 0);
189  }
190 
195  @Test
196  public void testMoveTop() {
197  final GameObjectCreator gameObjectCreator = new GameObjectCreator();
198  final GameObjectContainer<GameObject, MapArchObject, Archetype> container1 = newContainer(gameObjectCreator, 1, 0, 0, 0);
199  container1.moveTop(get(container1, 0));
200  check(container1, 1, 0, 0, 0);
201  container1.moveTop(get(container1, 1));
202  check(container1, 1, 0, 0, 0);
203  container1.moveTop(get(container1, 3));
204  check(container1, 1, 0, 0, 0);
205  }
206 
211  @Test
212  public void testRemove() {
213  final GameObjectCreator gameObjectCreator = new GameObjectCreator();
214  final GameObjectContainer<GameObject, MapArchObject, Archetype> container1 = newContainer(gameObjectCreator, 1, 0, 0, 0);
215  container1.remove(get(container1, 0));
216  check(container1, 1, 0, 0);
217  container1.remove(get(container1, 2));
218  check(container1, 1, 0);
219  container1.remove(get(container1, 0));
220  check(container1, 1);
221  container1.remove(get(container1, 0));
222  check(container1);
223  }
224 
229  @Test
230  public void testReplace() {
231  final GameObjectCreator gameObjectCreator = new GameObjectCreator();
232  final GameObjectContainer<GameObject, MapArchObject, Archetype> container1 = newContainer(gameObjectCreator, 1, 0, 0, 0);
233  container1.replace(get(container1, 0), gameObjectCreator.newGameObject(2));
234  check(container1, 1, 0, 0, 0);
235  container1.replace(get(container1, 1), gameObjectCreator.newGameObject(3));
236  check(container1, 1, 3, 0, 0);
237  container1.replace(get(container1, 0), gameObjectCreator.newGameObject(4));
238  check(container1, 1, 3, 0, 0);
239  }
240 
245  @Test
246  public void testIterator() {
247  final GameObjectCreator gameObjectCreator = new GameObjectCreator();
248  final GameObjectContainer<GameObject, MapArchObject, Archetype> container1 = newContainer(gameObjectCreator, 1, 0, 0, 0, 0, 0);
249  final Iterator<GameObject> it = container1.iterator();
250  it.next();
251  it.remove();
252  check(container1, 1, 0, 0, 0, 0);
253  it.next();
254  it.remove();
255  check(container1, 1, 0, 0, 0);
256  it.next();
257  it.next();
258  it.remove();
259  check(container1, 1, 0, 0);
260  }
261 
266  @Test
267  public void testReverse() {
268  final GameObjectCreator gameObjectCreator = new GameObjectCreator();
269  final GameObjectContainer<GameObject, MapArchObject, Archetype> container1 = newContainer(gameObjectCreator, 1, 0, 0, 0, 0, 0);
270  final Iterator<GameObject> it = container1.reverse().iterator();
271  it.next();
272  it.remove();
273  check(container1, 1, 0, 0, 0, 0);
274  it.next();
275  it.remove();
276  check(container1, 1, 0, 0, 0);
277  it.next();
278  it.next();
279  it.remove();
280  check(container1, 1, 0, 0);
281  it.next();
282  it.remove();
283  check(container1, 1, 0);
284  it.next();
285  it.remove();
286  check(container1, 1);
287  Assert.assertFalse(it.hasNext());
288  }
289 
297  @NotNull
298  private static GameObjectContainer<GameObject, MapArchObject, Archetype> newContainer(@NotNull final GameObjectCreator gameObjectCreator, final int... elevations) {
299  final GameObjectContainer<GameObject, MapArchObject, Archetype> gameObject = gameObjectCreator.newGameObject(0);
300  for (final int elevation : elevations) {
301  gameObject.addLast(gameObjectCreator.newGameObject(elevation));
302  }
303  return gameObject;
304  }
305 
312  @NotNull
313  private static GameObject get(@NotNull final Iterable<GameObject> gameObjects, final int index) {
314  int left = index;
315  for (final GameObject gameObject : gameObjects) {
316  if (left == 0) {
317  return gameObject;
318  }
319  left--;
320  }
321 
322  Assert.fail("index " + index + " not found");
323  throw new AssertionError();
324  }
325 
331  private static void check(@NotNull final Iterable<GameObject> gameObjects, final int... elevation) {
332  int i = 0;
333  for (final GameObject gameObject : gameObjects) {
334  final int thisElevation = gameObject.getAttributeInt(GameObject.ELEVATION);
335  final int expectedElevation = i < elevation.length ? elevation[i] : 0;
336  Assert.assertEquals(gameObject.getBestName(), expectedElevation, thisElevation);
337  i++;
338  }
339  }
340 
341 }
void testRemove()
Checks that GameObjectContainer#remove(net.sf.gridarta.model.gameobject.GameObject) does work correct...
void addLast(@NotNull final G gameObject)
Add the given GameObject at the end of this Container.
void testReplace()
Checks that net.sf.gridarta.model.gameobject.GameObject) does work correctly.
void insertBefore(@NotNull final G gameObject, @Nullable final G nextGameObject)
Add a GameObject before another.
MapArchObject contains the specific meta data about a map that is stored in the map-arch, at the very beginning of the map file.
void testAddFirst()
Checks that GameObjectContainer#addFirst(net.sf.gridarta.model.gameobject.GameObject) does work corre...
void addFirst(@NotNull final G gameObject)
Add the given GameObject at the end of this Container.
Base class for classes that contain GameObjects as children in the sense of containment.
void moveTop(@NotNull final G gameObject)
Move an item to top.
void testMoveTop()
Checks that GameObjectContainer#moveTop(net.sf.gridarta.model.gameobject.GameObject) does work correc...
void testReverse()
Checks that GameObjectContainer#reverse()&#39;s Iterator#remove() does work correctly.
void testInsertAfter()
Checks that net.sf.gridarta.model.gameobject.GameObject) does work correctly.
Base package of all Gridarta classes.
void testMoveUp()
Checks that GameObjectContainer#moveUp(net.sf.gridarta.model.gameobject.GameObject) does work correct...
G getFirst()
Return the first GameObject contained in this container.
void testInsertBefore()
Checks that net.sf.gridarta.model.gameobject.GameObject) does work correctly.
void testIterator()
Checks that GameObjectContainer#iterator()&#39;s Iterator#remove() does work correctly.
void testAddLast()
Checks that GameObjectContainer#addLast(net.sf.gridarta.model.gameobject.GameObject) does work correc...
static void check(@NotNull final Iterable< GameObject > gameObjects, final int... elevation)
Checks some game objects for expected elevation values.
Checks that GameObjectContainers correctly propagate elevation information when being modified...
transient Iterable< G > reverse
Iterable implementation for reverse traversal.
void insertAfter(@Nullable final G previousGameObject, @NotNull final G gameObject)
Add a GameObject after another.
void moveUp(@NotNull final G gameObject)
Move an item up.
Main package of Gridarta4Crossfire, contains all classes specific to the Crossfire version of the Gri...
Iterator< G > iterator()
The Iterator returned does not recurse, it only contains objects on the first level.
void testMoveBottom()
Checks that GameObjectContainer#moveBottom(net.sf.gridarta.model.gameobject.GameObject) does work cor...
void moveDown(@NotNull final G gameObject)
Move an item down.
GameObject newGameObject(final int elevation)
Creates a new GameObject.
static GameObjectContainer< GameObject, MapArchObject, Archetype > newContainer(@NotNull final GameObjectCreator gameObjectCreator, final int... elevations)
Creates a new GameObjectContainer that contains game objects with the given elevation values...
static final String ELEVATION
The name of the "elevation" attribute.
Definition: GameObject.java:52
void remove(@NotNull final G gameObject)
Remove a GameObject from this container.
void testMoveDown()
Checks that GameObjectContainer#moveDown(net.sf.gridarta.model.gameobject.GameObject) does work corre...
void replace(@NotNull final G oldGameObject, @NotNull final G newGameObject)
Replace an GameObject with another one.
void moveBottom(@NotNull final G gameObject)
Move an item to bottom.