Gridarta Editor
TestMapControlCreator.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.mapcontrol;
21 
22 import java.io.File;
23 import java.io.IOException;
58 import net.sf.gridarta.utils.Size2D;
60 import org.jetbrains.annotations.NotNull;
61 import org.jetbrains.annotations.Nullable;
62 import org.junit.Assert;
63 
68 public class TestMapControlCreator {
69 
73  @NotNull
75 
79  @NotNull
81 
85  @NotNull
86  private final PathManager pathManager;
87 
91  @NotNull
93 
97  @NotNull
99 
103  @NotNull
105 
109  @NotNull
111 
115  @NotNull
117 
121  @NotNull
123 
127  @NotNull
129 
133  @NotNull
135 
140  final GameObjectMatcher exitGameObjectMatcher = new TypeNrsGameObjectMatcher(TestMapModelHelper.EXIT_TYPE);
141  exitMatcher = new ExitMatcher<>(exitGameObjectMatcher);
142  mapArchObjectParserFactory = new TestMapArchObjectParserFactory();
143  gameObjectParser = mapModelCreator.newGameObjectParser();
144  final MapReaderFactory<TestGameObject, TestMapArchObject> mapReaderFactory = new TestMapReaderFactory(mapArchObjectParserFactory, mapArchObjectFactory, gameObjectParser);
145  projectSettings = new TestProjectSettings();
148  pathManager = new PathManager(projectSettings);
149  mapModelFactory = new MapModelFactory<>(archetypeChooserModel, mapModelCreator.getAutojoinLists(), mapModelCreator.getGameObjectFactory(), mapModelCreator.getGameObjectMatchers(), mapModelCreator.getTopmostInsertionMode());
150  final MapControlFactory<TestGameObject, TestMapArchObject, TestArchetype> mapControlFactory = new TestMapControlFactory(mapWriter, projectSettings, mapModelFactory);
151  final AbstractMapManager<TestGameObject, TestMapArchObject, TestArchetype> tmpMapManager = new DefaultMapManager<>(mapReaderFactory, mapControlFactory, projectSettings, mapModelCreator.getFaceObjectProviders(), pathManager);
153  tmpMapManager.setFileControl(fileControl);
154  mapManager = tmpMapManager;
155  final AbstractMapManager<TestGameObject, TestMapArchObject, TestArchetype> tmpPickmapManager = new DefaultMapManager<>(mapReaderFactory, mapControlFactory, projectSettings, mapModelCreator.getFaceObjectProviders(), pathManager);
156  tmpPickmapManager.setFileControl(fileControl);
157  pickmapManager = tmpPickmapManager;
158  }
159 
166  return mapModelCreator.newTestMapModelHelper();
167  }
168 
176  public MapControl<TestGameObject, TestMapArchObject, TestArchetype> newMapControl(@Nullable final MapFile mapFile, @NotNull final String mapName, @NotNull final Size2D mapSize) {
177  final TestMapArchObject mapArchObject = mapArchObjectFactory.newMapArchObject(false);
178  mapArchObject.setMapSize(mapSize);
179  mapArchObject.setMapName(mapName);
180  return mapManager.newMap(null, mapArchObject, mapFile, true);
181  }
182 
187  @NotNull
189  return pathManager;
190  }
191 
196  @NotNull
198  return projectSettings;
199  }
200 
205  @NotNull
207  return exitMatcher;
208  }
209 
214  @NotNull
216  return mapModelCreator.getArchetypeSet();
217  }
218 
223  @NotNull
225  return mapManager;
226  }
227 
232  @NotNull
234  return mapModelCreator.getInsertionModeSet();
235  }
236 
241  @NotNull
243  return new AutojoinListsHelper(mapModelCreator);
244  }
245 
250  @NotNull
252  return mapModelCreator;
253  }
254 
259  @NotNull
261  return mapArchObjectFactory;
262  }
263 
268  @NotNull
270  return mapModelFactory;
271  }
272 
277  @NotNull
279  return pickmapManager;
280  }
281 
286  @NotNull
288  return mapWriter;
289  }
290 
297  @NotNull
298  public DefaultMapReader<TestGameObject, TestMapArchObject, TestArchetype> newMapReader(@NotNull final File mapFile) throws IOException {
300  }
301 
311  @NotNull
312  public File createMapsDirectory(@NotNull final String... specs) throws IOException {
313  final File mapsDirectory = createTempDir("gridarta");
314  projectSettings.setMapsDirectory(mapsDirectory);
315  @Nullable File directoryToDelete = mapsDirectory;
316  try {
317  createMaps(specs);
318  directoryToDelete = null;
319  } finally {
320  if (directoryToDelete != null) {
321  deleteTempDir(directoryToDelete);
322  }
323  }
324  return mapsDirectory;
325  }
326 
332  private void createMaps(@NotNull final String[] specs) throws IOException {
333  for (final String spec : specs) {
334  final String[] tmp = StringUtils.PATTERN_COLON.split(spec, 2);
335  if (tmp.length != 2) {
336  throw new IllegalArgumentException();
337  }
338 
339  final MapFile mapFile = new PathManager(projectSettings).getMapFile(new File(projectSettings.getMapsDirectory(), tmp[0]));
340  final String mapName = tmp[1];
341 
342  final File file = mapFile.getFile();
343  if (!file.getParentFile().exists() && !file.getParentFile().mkdirs()) {
344  throw new IOException("cannot create directory: " + file.getParentFile());
345  }
346 
347  final MapModel<TestGameObject, TestMapArchObject, TestArchetype> mapModel = mapModelCreator.newMapModel(5, 5);
349  mapControl.getMapModel().getMapArchObject().setMapName(mapName);
350  mapControl.saveAs(mapFile);
351  }
352  }
353 
362  @NotNull
363  private static File createTempDir(@NotNull final String prefix) throws IOException {
364  final String tmpDir = System.getProperty("java.io.tmpdir");
365  if (tmpDir == null) {
366  throw new IOException("the system property 'java.io.tmpdir' does not exist");
367  }
368 
369  final File tmpFile = new File(tmpDir);
370  if (!tmpFile.exists() && !tmpFile.mkdirs()) {
371  throw new IOException("cannot create temporary directory " + tmpFile);
372  }
373 
374  final File result = File.createTempFile(prefix, null);
375  if (!result.delete()) {
376  throw new IOException("cannot delete temporary file " + result);
377  }
378  if (!result.mkdir()) {
379  throw new IOException("cannot create temporary directory " + result);
380  }
381 
382  return result;
383  }
384 
389  public static void deleteTempDir(@NotNull final File dir) {
390  final File[] files = dir.listFiles();
391  if (files != null) {
392  for (final File file : files) {
393  if (file.isDirectory()) {
394  deleteTempDir(file);
395  } else if (!file.delete()) {
396  Assert.fail("cannot delete file " + file);
397  }
398  }
399  }
400  if (!dir.delete()) {
401  Assert.fail("cannot delete directory " + dir);
402  }
403  }
404 
405 }
A factory for creating MapModel instances.
void createMaps(@NotNull final String[] specs)
Creates a set of map files in the maps directory.
Default implementation of MapWriter.
ExitMatcher< TestGameObject, TestMapArchObject, TestArchetype > getExitMatcher()
Returns the ExitMatcher.
final MapArchObjectFactory< TestMapArchObject > mapArchObjectFactory
The MapArchObjectFactory instance.
Utility class for string manipulation.
MapModel< TestGameObject, TestMapArchObject, TestArchetype > newMapModel(final int w, final int h)
Creates a new MapModel instance.
static final int EXIT_TYPE
The archetype type used for "exit" game objects.
This class contains methods for converting relative map paths to absolute map paths and vice versa...
A MapModel reflects the data of a map.
Definition: MapModel.java:75
A MapManager manages all opened maps.
Definition: MapManager.java:37
static void deleteTempDir(@NotNull final File dir)
Deletes a temporary directory.
Reading and writing of maps, handling of paths.
Interface for classes that match GameObjects.
This package contains classes related to matching GameObjects, so called GameObjectMatchers.
MapControl< G, A, R > newMap(@Nullable List< G > objects, @NotNull A mapArchObject, @Nullable MapFile mapFile, boolean interactive)
Creates a new map control without view.
ArchetypeSet< TestGameObject, TestMapArchObject, TestArchetype > getArchetypeSet()
Returns the ArchetypeSet.
Helper class for regression tests to create MapModel instances.
Settings that apply to a project.
Implements AutojoinList related functions.
Interface for classes that write map files.
Definition: MapWriter.java:34
Helper class for creating MapModel instances for regression tests.
A factory for creating MapReader instances.
void setMapName(@NotNull final String name)
Sets the map name.
TestMapModelCreator getMapModelCreator()
Returns the TestMapModelCreator instance.
An MapControlFactory implementation for testing purposes.
Helper class for creating MapControl instances for regression tests.
MapWriter< TestGameObject, TestMapArchObject, TestArchetype > getMapWriter()
Returns the MapWriter instance.
Default implementation of MapReader.
MapFile getMapFile(@NotNull final AbsoluteMapPath mapPath)
Returns a MapFile instance from an AbsoluteMapPath.
Factory for creating MapArchObject instances.
MapArchObjectFactory< TestMapArchObject > getMapArchObjectFactory()
Returns the MapArchObjectFactory instance.
Interface for classes that read or write GameObject instances.
An MapReaderFactory implementation for testing purposes.
MapModelFactory< TestGameObject, TestMapArchObject, TestArchetype > getMapModelFactory()
Returns the MapModelFactory instance.
FaceObjectProviders getFaceObjectProviders()
Returns the FaceObjectProviders instance.
static File createTempDir(@NotNull final String prefix)
Creates an empty directory in the default temporary directory using the given prefix.
final GameObjectParser< TestGameObject, TestMapArchObject, TestArchetype > gameObjectParser
The GameObjectParser instance.
A MapArchObject implementation for testing purposes.
Factory for creating MapArchObjectParser instances.
InsertionModeSet< TestGameObject, TestMapArchObject, TestArchetype > getInsertionModeSet()
Returns the InsertionModeSet instance.
final ProjectSettings projectSettings
The ProjectSettings instance.
A newMapArchObject(boolean addDefaultAttributes)
Creates a new MapArchObject instance.
MapModel< G, A, R > getMapModel()
Returns the map model.
Abstract base class for MapManager implementations.
Base package of all Gridarta classes.
final PathManager pathManager
The PathManager instance.
File createMapsDirectory(@NotNull final String... specs)
Creates a maps directory consisting of a set of maps.
AutojoinListsHelper newAutojoinListsHelper()
Returns a new AutojoinListsHelper instance.
A FileControl implementation for testing purposes.
final MapManager< TestGameObject, TestMapArchObject, TestArchetype > pickmapManager
The pickmap MapManager instance.
Selects valid exit game objects from maps.
An Exception indicating that an Archetype name is not unique.
void setFileControl(@NotNull final FileControl< G, A, R > fileControl)
GameObjects are the objects based on Archetypes found on maps.
void setMapsDirectory(@NotNull File mapsDirectory)
Sets the default maps directory.
final ExitMatcher< TestGameObject, TestMapArchObject, TestArchetype > exitMatcher
The ExitMatcher instance.
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 setMapSize(@NotNull final Size2D mapSize)
Sets the map size.
final MapArchObjectParserFactory< TestMapArchObject > mapArchObjectParserFactory
The MapArchObjectParserFactory instance.
GameObjectParser< TestGameObject, TestMapArchObject, TestArchetype > newGameObjectParser()
Creates a new GameObjectParser instance.
TestMapModelHelper newTestMapModelHelper()
Creates a new TestMapModelHelper instance.
A getMapArchObject()
Returns the Map Arch Object with the meta information about the map.
Currently nothing more than a marker interface for unification.
Definition: MapControl.java:35
File getFile()
Returns a File for this map file.
Definition: MapFile.java:102
AutojoinLists< TestGameObject, TestMapArchObject, TestArchetype > getAutojoinLists()
Returns the AutojoinLists instance.
An Archetype implementation for testing purposes.
An GameObjectMatcher matching certain archetype types.
A GameObject implementation for testing purposes.
File getMapsDirectory()
Returns the default maps directory.
Interface that captures similarities between different ArchetypeSet implementations.
static final Pattern PATTERN_COLON
The pattern that matches a single colon (":").
DefaultMapReader< TestGameObject, TestMapArchObject, TestArchetype > newMapReader(@NotNull final File mapFile)
Creates a new instance.
final MapWriter< TestGameObject, TestMapArchObject, TestArchetype > mapWriter
The MapWriter instance.
An ProjectSettings implementation for testing purposes.
GameObjectMatchers getGameObjectMatchers()
Returns the GameObjectMatchers instance.
InsertionMode< TestGameObject, TestMapArchObject, TestArchetype > getTopmostInsertionMode()
Returns the "topmost" insertion mode.
void saveAs(@NotNull MapFile mapFile)
Saves the file with the given map file.
final MapManager< TestGameObject, TestMapArchObject, TestArchetype > mapManager
The MapManager instance.
final TestMapModelCreator mapModelCreator
The TestMapModelCreator instance.
GameObjectFactory< TestGameObject, TestMapArchObject, TestArchetype > getGameObjectFactory()
Returns the GameObjectFactory instance.
final MapModelFactory< TestGameObject, TestMapArchObject, TestArchetype > mapModelFactory
The MapModelFactory instance.
The location of a map file with a map directory.
Definition: MapFile.java:31
A MapArchObjectParserFactory for regression tests.
MapManager< TestGameObject, TestMapArchObject, TestArchetype > getMapManager()
Returns the MapManager.
MapManager< TestGameObject, TestMapArchObject, TestArchetype > getPickmapManager()
Returns the pickmap manager instance.
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.