Gridarta Editor
FilterGameObjectIteratorTest.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.mapmodel;
21 
22 import java.util.Arrays;
23 import java.util.Collection;
24 import java.util.HashSet;
25 import java.util.Iterator;
26 import java.util.NoSuchElementException;
30 import org.jetbrains.annotations.NotNull;
31 import org.junit.Assert;
32 import org.junit.Test;
33 
39 
43  @Test
44  public void testEmptyIterator() {
46  }
47 
52  @Test
53  public void testAllMatcher() {
54  final TestMapModelCreator creator = new TestMapModelCreator(false);
55  final TestGameObject o1 = creator.newGameObject("arch", "o1");
56  final TestGameObject o2 = creator.newGameObject("arch", "o2");
57  final TestGameObject o3 = creator.newGameObject("arch", "o3");
58  check(new AcceptAllMatcher(), new ArrayGameObjectIterator(o1, o2, o3), o1, o2, o3);
59  }
60 
65  @Test
66  public void testAcceptMatcher() {
67  final TestMapModelCreator creator = new TestMapModelCreator(false);
68  final TestGameObject o1 = creator.newGameObject("arch", "o1");
69  final TestGameObject o2 = creator.newGameObject("arch", "o2");
70  final TestGameObject o3 = creator.newGameObject("arch", "o3");
71  check(new AcceptMatcher(o1, o2), new ArrayGameObjectIterator(o1, o2, o3, o1, o2, o3, o1, o1, o2, o3, o3), o1, o2, o1, o2, o1, o1, o2);
72  }
73 
83  private static void check(@NotNull final GameObjectMatcher matcher, @NotNull final Iterator<TestGameObject> iterator, @NotNull final TestGameObject... gameObjects) {
84  final Iterator<TestGameObject> filterGameObjectIterator = new FilterGameObjectIterator<>(iterator, matcher);
85  for (final TestGameObject gameObject : gameObjects) {
86  Assert.assertTrue(filterGameObjectIterator.hasNext());
87  Assert.assertSame(gameObject, filterGameObjectIterator.next());
88  }
89  Assert.assertFalse(filterGameObjectIterator.hasNext());
90  }
91 
95  private static class AcceptAllMatcher implements GameObjectMatcher {
96 
100  private static final long serialVersionUID = 1L;
101 
102  @Override
103  public boolean isMatching(@NotNull final GameObject<?, ?, ?> gameObject) {
104  return true;
105  }
106 
107  }
108 
112  private static class AcceptMatcher implements GameObjectMatcher {
113 
117  private static final long serialVersionUID = 1L;
118 
122  @NotNull
123  private final Collection<GameObject<?, ?, ?>> gameObjects = new HashSet<>();
124 
129  private AcceptMatcher(@NotNull final GameObject<?, ?, ?>... gameObjects) {
130  this.gameObjects.addAll(Arrays.asList(gameObjects));
131  }
132 
133  @Override
134  public boolean isMatching(@NotNull final GameObject<?, ?, ?> gameObject) {
135  return gameObjects.contains(gameObject);
136  }
137 
138  }
139 
144  private static class ArrayGameObjectIterator implements Iterator<TestGameObject> {
145 
149  @NotNull
150  private final TestGameObject[] gameObjects;
151 
155  private int index;
156 
161  private ArrayGameObjectIterator(@NotNull final TestGameObject... gameObjects) {
162  this.gameObjects = gameObjects.clone();
163  }
164 
165  @Override
166  public boolean hasNext() {
167  return index < gameObjects.length;
168  }
169 
170  @Override
171  public TestGameObject next() {
172  if (index >= gameObjects.length) {
173  throw new NoSuchElementException();
174  }
175  return gameObjects[index++];
176  }
177 
178  @Override
179  public void remove() {
180  throw new UnsupportedOperationException();
181  }
182 
183  }
184 
185 }
boolean isMatching(@NotNull final GameObject<?, ?, ?> gameObject)
Matches an GameObject.
void testAcceptMatcher()
Checks that a matcher that accepts some game objects returns only the accepted game objects...
Interface for classes that match GameObjects.
This package contains classes related to matching GameObjects, so called GameObjectMatchers.
Helper class for regression tests to create MapModel instances.
static void check(@NotNull final GameObjectMatcher matcher, @NotNull final Iterator< TestGameObject > iterator, @NotNull final TestGameObject... gameObjects)
Creates a new FilterGameObjectIterator instance and checks that it returns the expected game objects...
AcceptMatcher(@NotNull final GameObject<?, ?, ?>... gameObjects)
Creates a new instance.
void testAllMatcher()
Checks that a matcher that accepts all game objects returns all game objects.
Base package of all Gridarta classes.
Reflects a game object (object on a map).
Definition: GameObject.java:36
An Iterator that filters another iterator according to a GameObjectMatcher.
ArrayGameObjectIterator(@NotNull final TestGameObject... gameObjects)
Creates a new instance.
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 testEmptyIterator()
Checks that an empty iterator is filtered correctly.
A GameObject implementation for testing purposes.
boolean isMatching(@NotNull final GameObject<?, ?, ?> gameObject)
Matches an GameObject.