Gridarta Editor
AbstractFilesResourcesReaderTest.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.resource;
21 
22 import java.io.BufferedWriter;
23 import java.io.File;
24 import java.io.FileOutputStream;
25 import java.io.IOException;
26 import java.io.OutputStream;
27 import java.io.OutputStreamWriter;
28 import java.io.Writer;
29 import java.nio.charset.StandardCharsets;
30 import java.util.ArrayList;
31 import java.util.Collections;
32 import java.util.Enumeration;
33 import java.util.HashMap;
34 import java.util.List;
35 import java.util.Map;
36 import java.util.ResourceBundle;
37 import java.util.regex.Pattern;
60 import net.sf.japi.swing.action.ActionBuilder;
61 import net.sf.japi.swing.action.ActionBuilderFactory;
62 import net.sf.japi.swing.action.DefaultActionBuilder;
63 import net.sf.japi.util.IteratorEnumeration;
64 import org.jetbrains.annotations.NotNull;
65 import org.jetbrains.annotations.Nullable;
66 import org.junit.Assert;
67 import org.junit.Test;
68 
74 
78  @NotNull
79  private static final Pattern PATTERN_DEFAULT_KEY = Pattern.compile("fileDialog\\.filter\\..*");
80 
85  @Test
86  public void testAnimationPaths() throws IOException {
87  final File tmpDirectory = File.createTempFile("gridarta", null);
88  try {
89  Assert.assertTrue(tmpDirectory.delete());
90  Assert.assertTrue(tmpDirectory.mkdir());
91  final File archDirectory = new File(tmpDirectory, "archetypes");
92  Assert.assertTrue(archDirectory.mkdir());
93  final File collectedDirectory = new File(tmpDirectory, "collected");
94  Assert.assertTrue(collectedDirectory.mkdir());
95  createAnimationFile(archDirectory, "anim1");
96  createAnimationFile(new File(archDirectory, "p"), "anim2");
97  createAnimationFile(new File(new File(new File(archDirectory, "p"), "q"), "r"), "anim3");
98 
99  final ActionBuilder actionBuilder = new DefaultActionBuilder("net.sf.gridarta");
100  ActionBuilderFactory.getInstance().putActionBuilder("net.sf.gridarta", actionBuilder);
101  final ResourceBundle resourceBundle = new ResourceBundle() {
102 
106  @NotNull
107  private final Map<String, Object> objects = new HashMap<>();
108 
109  @Nullable
110  @Override
111  protected Object handleGetObject(@NotNull final String key) {
112  final Object existingObject = objects.get(key);
113  if (existingObject != null) {
114  return existingObject;
115  }
116 
117  final Object object;
118  if (PATTERN_DEFAULT_KEY.matcher(key).matches()) {
119  object = "Description";
120  } else {
121  return null;
122  }
123  objects.put(key, object);
124  return object;
125  }
126 
127  @NotNull
128  @Override
129  public Enumeration<String> getKeys() {
130  return new IteratorEnumeration<>(Collections.unmodifiableSet(objects.keySet()).iterator());
131  }
132 
133  };
134  actionBuilder.addBundle(resourceBundle);
135 
136  final AnimationObjects animationObjects = readAnimations(archDirectory, collectedDirectory);
137 
138  Assert.assertEquals(3, animationObjects.size());
139  final AnimationObject anim1 = animationObjects.get("anim1");
140  final AnimationObject anim2 = animationObjects.get("anim2");
141  final AnimationObject anim3 = animationObjects.get("anim3");
142  Assert.assertNotNull(anim1);
143  Assert.assertNotNull(anim2);
144  Assert.assertNotNull(anim3);
145  Assert.assertEquals("anim1", anim1.getAnimName());
146  Assert.assertEquals("anim2", anim2.getAnimName());
147  Assert.assertEquals("anim3", anim3.getAnimName());
148  Assert.assertEquals("/anim1", anim1.getPath());
149  Assert.assertEquals("/p/anim2", anim2.getPath());
150  Assert.assertEquals("/p/q/r/anim3", anim3.getPath());
151  } finally {
152  delete(tmpDirectory);
153  }
154  }
155 
162  @NotNull
163  private static AnimationObjects readAnimations(@NotNull final File archDirectory, @NotNull final File collectedDirectory) {
164  final ArchFaceProvider archFaceProvider = new ArchFaceProvider();
165  final FaceObjects faceObjects = new TestFaceObjects();
166  final ResourceIcons resourceIcons = new ResourceIcons();
167  final FaceObjectProviders faceObjectProviders = new FaceObjectProviders(1, faceObjects, resourceIcons);
168  final AnimationObjects animationObjects = new TestAnimationObjects();
169  final ArchetypeFactory<TestGameObject, TestMapArchObject, TestArchetype> archetypeFactory = new TestArchetypeFactory(faceObjectProviders, animationObjects);
170  final ArchetypeSet<TestGameObject, TestMapArchObject, TestArchetype> archetypeSet = new DefaultArchetypeSet<>(archetypeFactory, null);
171  final GameObjectFactory<TestGameObject, TestMapArchObject, TestArchetype> gameObjectFactory = new TestGameObjectFactory(faceObjectProviders, animationObjects);
172  final TestArchetypeBuilder archetypeBuilder = new TestArchetypeBuilder(gameObjectFactory);
173  final AbstractArchetypeParser<TestGameObject, TestMapArchObject, TestArchetype, ?> archetypeParser = new TestArchetypeParser(archetypeBuilder, animationObjects, archetypeSet);
174  final AbstractResourcesReader<TestGameObject> filesResourcesReader = new TestFilesResourcesReader(archDirectory, archetypeSet, archetypeParser, archFaceProvider, collectedDirectory, null, animationObjects, faceObjects);
175  final List<TestGameObject> invObjects = new ArrayList<>();
176  final ErrorView errorView = new TestErrorView();
177  filesResourcesReader.read(errorView, invObjects);
178  return animationObjects;
179  }
180 
188  private static void createAnimationFile(@NotNull final File directory, @NotNull final String animName) throws IOException {
189  if (!directory.isDirectory()) {
190  Assert.assertTrue(directory.mkdirs());
191  }
192 
193  final File file = new File(directory, animName + ".anim");
194  Assert.assertFalse(file.exists());
195  try (OutputStream fileOutputStream = new FileOutputStream(file)) {
196  try (Writer outputStreamWriter = new OutputStreamWriter(fileOutputStream, StandardCharsets.UTF_8)) {
197  try (Writer bufferedWriter = new BufferedWriter(outputStreamWriter)) {
198  bufferedWriter.append("anim ").append(animName).append("\n");
199  bufferedWriter.append("face1\n");
200  bufferedWriter.append("face2\n");
201  bufferedWriter.append("mina\n");
202  }
203  }
204  }
205  }
206 
211  private static void delete(@NotNull final File file) {
212  //noinspection ResultOfMethodCallIgnored
213  file.delete();
214 
215  if (file.isDirectory()) {
216  final File[] files = file.listFiles();
217  if (files != null) {
218  for (final File subFile : files) {
219  delete(subFile);
220  }
221  }
222  }
223 
224  if (file.exists()) {
225  Assert.assertTrue(file.delete());
226  }
227  }
228 
229 }
An AbstractFilesResourcesReader for regression tests.
A FaceObjects for regression tests.
An ArchetypeParser for regression tests.
Reading and writing of maps, handling of paths.
static void createAnimationFile(@NotNull final File directory, @NotNull final String animName)
Creates a new .anim file.
int size()
Get the number of objects.
Gridarta can handle frame information of animations and allow the selection of an animation using a t...
An AnimationObjects for regression tests.
A MapArchObject implementation for testing purposes.
Interface for classes displaying error messages.
Definition: ErrorView.java:28
A factory for creating Archetype instances.
Base package of all Gridarta classes.
Abstract factory for creating GameObject instances.
E get(@NotNull String objectName)
Gets a AbstractNamedObject.
AnimationObjects is a container for AnimationObjects.
GameObjects are the objects based on Archetypes found on maps.
An AnimationObject reflects the animation ("@code anim\n @endcode " ...
FaceObjects is a container for FaceObjects.
String getPath()
Get the path of this AbstractNamedObject.
An ArchetypeFactory implementation for testing purposes.
Provider for faces of GameObjects and Archetypes.
The face is the appearance of an object.
Creates ImageIcon instances from resources.
An Archetype implementation for testing purposes.
A GameObject implementation for testing purposes.
Interface that captures similarities between different ArchetypeSet implementations.
Implementation of FaceProvider which reads images from the arch directory.
void testAnimationPaths()
checks that animations read from .anim files have correct path names.
Abstract base class for archetype set loader classes.
Abstract base implementation of ArchetypeParser.
static AnimationObjects readAnimations(@NotNull final File archDirectory, @NotNull final File collectedDirectory)
Reads resources from individual files.
abstract FaceProvider read(@NotNull ErrorView errorView, @NotNull List< G > invObjects)
Reads the resources.
An ErrorView suitable for unit tests.
static final Pattern PATTERN_DEFAULT_KEY
The Pattern for matching validator keys for default values.
An AbstractArchetypeBuilder for regression tests.
String getAnimName()
Get the animName, which is the name of the animation as usable by the "animations" attribute...