Gridarta Editor
MapsIndexerTest.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.index;
21 
22 import java.io.File;
23 import java.io.IOException;
24 import java.util.Collection;
25 import java.util.Iterator;
26 import java.util.concurrent.atomic.AtomicInteger;
34 import net.sf.gridarta.utils.Size2D;
35 import org.jetbrains.annotations.NotNull;
36 import org.junit.Assert;
37 import org.junit.Test;
38 
43 public class MapsIndexerTest {
44 
50  @Test
51  public void test1() throws InterruptedException, IOException {
52  final TestMapControlCreator mapControlCreator = new TestMapControlCreator();
53  final ProjectSettings projectSettings = mapControlCreator.getProjectSettings();
54  final File mapsDirectory = mapControlCreator.createMapsDirectory("map1:Map1", "path/map2:Map Name2");
55  try {
56  final MapsIndex index = new MapsIndex();
57  final MapsIndexer<TestGameObject, TestMapArchObject, TestArchetype> indexer = new MapsIndexer<>(index, mapControlCreator.getMapManager(), projectSettings);
58  indexer.start();
59  indexer.waitForIdle();
60  indexer.stop();
61  Assert.assertEquals(2, index.findPartialName("").size());
62  assertEquals(index.findPartialName("Map1"), mapsDirectory, "map1");
63  assertEquals(index.findPartialName("Map Name2"), mapsDirectory, "path/map2");
64  } finally {
65  TestMapControlCreator.deleteTempDir(mapsDirectory);
66  }
67  }
68 
75  @Test
76  public void test2() throws InterruptedException, IOException {
77  final TestMapControlCreator mapControlCreator = new TestMapControlCreator();
78  final File mapsDirectory = mapControlCreator.createMapsDirectory("map1:Map1", "path/map2:Map Name2");
79  try {
80  final MapsIndex index = new MapsIndex();
81  final AtomicInteger indexingFinished = new AtomicInteger();
82  final IndexListener<MapFile> indexListener = new IndexListener<MapFile>() {
83 
84  @Override
85  public void valueAdded(@NotNull final MapFile value) {
86  }
87 
88  @Override
89  public void valueRemoved(@NotNull final MapFile value) {
90  }
91 
92  @Override
93  public void nameChanged() {
94  }
95 
96  @Override
97  public void pendingChanged() {
98  }
99 
100  @Override
101  public void indexingFinished() {
102  indexingFinished.incrementAndGet();
103  }
104 
105  };
106  index.addIndexListener(indexListener);
107  final MapsIndexer<TestGameObject, TestMapArchObject, TestArchetype> indexer = new MapsIndexer<>(index, mapControlCreator.getMapManager(), mapControlCreator.getProjectSettings());
108  indexer.start();
109  indexer.waitForIdle();
110  Assert.assertEquals(1, indexingFinished.getAndSet(0));
111 
112  // create new map => indexer does not yet index it
113  final MapFile mapFile = mapControlCreator.getPathManager().getMapFile(new File(mapControlCreator.getProjectSettings().getMapsDirectory(), "mapfile"));
114  final MapControl<TestGameObject, TestMapArchObject, TestArchetype> mapControl = mapControlCreator.newMapControl(mapFile, "name3", new Size2D(1, 1));
115  indexer.waitForIdle();
116  Assert.assertEquals(0, indexingFinished.getAndSet(0));
117 
118  // save new map => indexer must index it
119  mapControl.save();
120  indexer.waitForIdle();
121  Assert.assertEquals(1, indexingFinished.getAndSet(0));
122  Assert.assertEquals(3, index.findPartialName("").size());
123  assertEquals(index.findPartialName("Map1"), mapsDirectory, "map1");
124  assertEquals(index.findPartialName("Map Name2"), mapsDirectory, "path/map2");
125  assertEquals(index.findPartialName("name3"), mapsDirectory, "mapfile");
126  } finally {
127  TestMapControlCreator.deleteTempDir(mapsDirectory);
128  }
129  }
130 
139  public static void assertEquals(@NotNull final Collection<MapFile> mapPaths, @NotNull final File mapsDirectory, @NotNull final String mapPath) {
140  Assert.assertEquals(1, mapPaths.size());
141  final Iterator<MapFile> it = mapPaths.iterator();
142  final MapFile mapFile = it.next();
143  Assert.assertEquals(mapsDirectory, mapFile.getMapsDir());
144  Assert.assertEquals(mapPath, mapFile.getMapPath().getPath());
145  }
146 
147 }
Indexes maps by map name.
Definition: MapsIndex.java:29
void save()
Saves the map to a file.
String getPath()
Returns the map path information.
static void deleteTempDir(@NotNull final File dir)
Deletes a temporary directory.
Regression tests for MapsIndexer.
Settings that apply to a project.
Maintains a MapsIndex for the maps directory.
Interface for listeners interested in Index related events.
AbsoluteMapPath getMapPath()
Returns the map path within getMapsDir().
Definition: MapFile.java:93
Helper class for creating MapControl instances for regression tests.
MapFile getMapFile(@NotNull final AbsoluteMapPath mapPath)
Returns a MapFile instance from an AbsoluteMapPath.
void test1()
Checks that basic indexing works as expected.
void test2()
Checks that IndexListener#finalize() is called once after indexing has completed. ...
A MapArchObject implementation for testing purposes.
void addIndexListener(@NotNull final IndexListener< V > listener)
Adds an IndexListener to be notified of changes.
Base package of all Gridarta classes.
File createMapsDirectory(@NotNull final String... specs)
Creates a maps directory consisting of a set of maps.
GameObjects are the objects based on Archetypes found on maps.
void waitForIdle()
Blocks the calling thread until all pending maps have been indexed.
ProjectSettings getProjectSettings()
Returns the ProjectSettings.
File getMapsDir()
Returns the base directory this map file is part of.
Definition: MapFile.java:84
Currently nothing more than a marker interface for unification.
Definition: MapControl.java:35
An Archetype implementation for testing purposes.
A GameObject implementation for testing purposes.
File getMapsDirectory()
Returns the default maps directory.
static void assertEquals(@NotNull final Collection< MapFile > mapPaths, @NotNull final File mapsDirectory, @NotNull final String mapPath)
Checks that a Collection of Files contains exactly one expected value.
The location of a map file with a map directory.
Definition: MapFile.java:31
MapManager< TestGameObject, TestMapArchObject, TestArchetype > getMapManager()
Returns the MapManager.
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.