Gridarta Editor
MapsIndexTest.java
Go to the documentation of this file.
1 /*
2  * Gridarta MMORPG map editor for Crossfire, Daimonin and similar games.
3  * Copyright (C) 2000-2023 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.util.Collection;
24 import java.util.HashSet;
25 import java.util.Iterator;
26 import java.util.TreeSet;
27 import org.jetbrains.annotations.NotNull;
28 import org.junit.Assert;
29 import org.junit.Test;
30 
35 public class MapsIndexTest {
36 
40  @Test
41  public void test1() {
42  final Index<File> index = newIndex();
43  index.add(new File("a"), 1L);
44  index.add(new File("b"), 1L);
45  index.add(new File("c"), 1L);
46  Assert.assertEquals("a,b,c", getPending(index));
47  Assert.assertEquals("", getPending(index));
48  index.add(new File("d"), 1L);
49  Assert.assertEquals("d", getPending(index));
50  index.add(new File("b"), 1L);
51  index.add(new File("d"), 1L);
52  Assert.assertEquals("", getPending(index));
53  index.add(new File("b"), 2L);
54  index.add(new File("d"), 2L);
55  Assert.assertEquals("b,d", getPending(index));
56  index.setPending(new File("a"));
57  Assert.assertEquals("a", getPending(index));
58  index.setPending(new File("a"));
59  index.add(new File("b"), 3L);
60  index.add(new File("c"), 3L);
61  index.remove(new File("b"));
62  Assert.assertEquals("a,c", getPending(index));
63  }
64 
69  @NotNull
70  private static Index<File> newIndex() {
71  return new AbstractIndex<File>() {
72 
73  };
74  }
75 
79  @Test
80  public void testFind1() {
81  final Index<File> index = newIndex();
82  Assert.assertEquals("", findPartialName(index, "a"));
83  index.add(new File("abc"), 1L);
84  index.setName(new File("abc"), 1L, "abC");
85  Assert.assertEquals("abc", findPartialName(index, "a"));
86  Assert.assertEquals("abc", findPartialName(index, "b"));
87  Assert.assertEquals("", findPartialName(index, "x"));
88  index.add(new File("bcd"), 1L);
89  index.setName(new File("bcd"), 1L, "BcD");
90  Assert.assertEquals("abc", findPartialName(index, "a"));
91  Assert.assertEquals("abc,bcd", findPartialName(index, "b"));
92  Assert.assertEquals("abc,bcd", findPartialName(index, "Bc"));
93  Assert.assertEquals("", findPartialName(index, "x"));
94  }
95 
100  @Test
101  public void testTransaction() {
102  final Index<File> index = newIndex();
103  index.add(new File("a"), 1L);
104  index.add(new File("b"), 1L);
105  index.add(new File("c"), 1L);
106  index.setName(new File("a"), 2L, "a");
107  index.setName(new File("b"), 2L, "a");
108  index.setName(new File("c"), 2L, "a");
109  index.beginUpdate();
110  index.add(new File("b"), 1L);
111  index.add(new File("c"), 2L);
112  index.add(new File("d"), 2L);
113  index.endUpdate();
114  index.setName(new File("b"), 2L, "a");
115  index.setName(new File("c"), 2L, "a");
116  index.setName(new File("d"), 2L, "a");
117  Assert.assertEquals("b,c,d", findPartialName(index, "a"));
118  }
119 
123  @Test
124  public void testListener() {
125  final Index<File> index = newIndex();
126  final Listener listener = new Listener();
127  index.addIndexListener(listener);
128 
129  index.add(new File("a"), 1L);
130  Assert.assertEquals("add a\n" + "pending changed\n", listener.getAndClearEvents());
131 
132  index.add(new File("a"), 1L);
133  Assert.assertEquals("", listener.getAndClearEvents());
134 
135  index.add(new File("a"), 2L);
136  Assert.assertEquals("", listener.getAndClearEvents());
137 
138  index.add(new File("b"), 2L);
139  index.add(new File("c"), 2L);
140  Assert.assertEquals("add b\n" + "add c\n", listener.getAndClearEvents());
141 
142  index.remove(new File("b"));
143  Assert.assertEquals("del b\n" + "name changed\n", listener.getAndClearEvents());
144 
145  index.setName(new File("a"), 2L, "name1");
146  index.setName(new File("b"), 2L, "name2");
147  index.setName(new File("c"), 2L, "name3");
148  Assert.assertEquals("name changed\n" + "name changed\n" + "name changed\n", listener.getAndClearEvents());
149  }
150 
157  @NotNull
158  private static String getPending(@NotNull final Index<File> index) {
159  final Collection<File> pendingFiles = new HashSet<>();
160  while (true) {
161  final File pendingFile = index.removePending();
162  if (pendingFile == null) {
163  break;
164  }
165  pendingFiles.add(pendingFile);
166  }
167  return format(pendingFiles);
168  }
169 
177  @NotNull
178  private static String findPartialName(@NotNull final Index<File> index, @NotNull final String name) {
179  return format(index.findPartialName(name));
180  }
181 
187  @NotNull
188  private static String format(@NotNull final Collection<File> files) {
189  final Iterable<File> tmp = new TreeSet<>(files);
190  final StringBuilder sb = new StringBuilder();
191  final Iterator<File> it = tmp.iterator();
192  if (it.hasNext()) {
193  sb.append(it.next());
194  while (it.hasNext()) {
195  sb.append(',');
196  sb.append(it.next());
197  }
198  }
199  return sb.toString();
200  }
201 
206  private static class Listener implements IndexListener<File> {
207 
211  @NotNull
212  private final StringBuilder stringBuilder = new StringBuilder();
213 
214  @Override
215  public void valueAdded(@NotNull final File value) {
216  stringBuilder.append("add ").append(value).append("\n");
217  }
218 
219  @Override
220  public void valueRemoved(@NotNull final File value) {
221  stringBuilder.append("del ").append(value).append("\n");
222  }
223 
224  @Override
225  public void nameChanged() {
226  stringBuilder.append("name changed\n");
227  }
228 
229  @Override
230  public void pendingChanged() {
231  stringBuilder.append("pending changed\n");
232  }
233 
234  @Override
235  public void indexingFinished() {
236  stringBuilder.append("indexing finished");
237  }
238 
243  @NotNull
244  protected String getAndClearEvents() {
245  final String result = stringBuilder.toString();
246  stringBuilder.setLength(0);
247  return result;
248  }
249 
250  }
251 
252 }
name
name
Definition: ArchetypeTypeSetParserTest-ignoreDefaultAttribute1-result.txt:2
net.sf.gridarta.model.index.MapsIndexTest.Listener.nameChanged
void nameChanged()
Definition: MapsIndexTest.java:225
net.sf.gridarta.model.index.MapsIndexTest.findPartialName
static String findPartialName(@NotNull final Index< File > index, @NotNull final String name)
Calls Index#findPartialName(String) on the given Index and name and returns a string representation o...
Definition: MapsIndexTest.java:178
net.sf.gridarta.model.index.MapsIndexTest.Listener.indexingFinished
void indexingFinished()
Definition: MapsIndexTest.java:235
net.sf.gridarta.model.index.MapsIndexTest.Listener.pendingChanged
void pendingChanged()
Definition: MapsIndexTest.java:230
files
Standard Edition Runtime Environment README Import and export control rules on cryptographic software vary from country to country The Java Cryptography Java provides two different sets of cryptographic policy files
Definition: README.txt:26
net.sf.gridarta.model.index.MapsIndexTest.newIndex
static Index< File > newIndex()
Returns a new Index instance.
Definition: MapsIndexTest.java:70
net.sf.gridarta.model.index.MapsIndexTest.Listener.stringBuilder
final StringBuilder stringBuilder
The recorded events in text representation.
Definition: MapsIndexTest.java:212
net.sf.gridarta.model.index.Index.endUpdate
void endUpdate()
Ends an update.
net.sf.gridarta.model.index.Index.remove
void remove(@NotNull V value)
Removes a value from the cache.
net.sf.gridarta.model.index.MapsIndexTest.getPending
static String getPending(@NotNull final Index< File > index)
Returns all pending values of a MapsIndex.
Definition: MapsIndexTest.java:158
net.sf.gridarta.model.index.MapsIndexTest.Listener.valueAdded
void valueAdded(@NotNull final File value)
Definition: MapsIndexTest.java:215
net.sf.gridarta.model.index.Index.setPending
void setPending(@NotNull V value)
Marks a value as pending.
net.sf.gridarta.model.index.MapsIndexTest.format
static String format(@NotNull final Collection< File > files)
Returns a text representation of a Collection.
Definition: MapsIndexTest.java:188
net.sf.gridarta.model.index.Index.addIndexListener
void addIndexListener(@NotNull IndexListener< V > listener)
Adds an IndexListener to be notified of changes.
net.sf.gridarta.model.index.MapsIndexTest
Regression tests for MapsIndex.
Definition: MapsIndexTest.java:35
net.sf.gridarta.model.index.MapsIndexTest.testTransaction
void testTransaction()
Checks that Index#beginUpdate() and Index#endUpdate() works as expected.
Definition: MapsIndexTest.java:101
net.sf.gridarta.model.index.MapsIndexTest.test1
void test1()
Checks that values are correctly marked as pending.
Definition: MapsIndexTest.java:41
net.sf.gridarta.model.index.MapsIndexTest.testFind1
void testFind1()
Checks that Index#findPartialName(String) works as expected.
Definition: MapsIndexTest.java:80
net.sf.gridarta.model.index.AbstractIndex
Abstract base class for Index implementations.
Definition: AbstractIndex.java:42
net.sf.gridarta.model.index.IndexListener
Interface for listeners interested in Index related events.
Definition: IndexListener.java:30
net.sf.gridarta.model.index.Index
An index of values.
Definition: Index.java:41
net.sf.gridarta.model.index.MapsIndexTest.Listener
An IndexListener that records a text representation of all generated events.
Definition: MapsIndexTest.java:206
net.sf.gridarta.model.index.Index.add
void add(@NotNull V value, long timestamp)
Adds a value to the cache.
net.sf.gridarta.model.index.Index.beginUpdate
void beginUpdate()
Starts an update.
net.sf.gridarta.model.index.MapsIndexTest.testListener
void testListener()
Checks that listeners are notified.
Definition: MapsIndexTest.java:124
net.sf.gridarta.model.index.MapsIndexTest.Listener.valueRemoved
void valueRemoved(@NotNull final File value)
Definition: MapsIndexTest.java:220
net.sf.gridarta.model.index.MapsIndexTest.Listener.getAndClearEvents
String getAndClearEvents()
Returns the accumulated events.
Definition: MapsIndexTest.java:244
it
This document describes some hints and requirements for general development on the CrossfireEditor If you plan to make changes to the editor code or setup please read the following and keep it in derived from a basic editor application called Gridder by Pasi Ker�nen so please communicate with best through the cf devel mailing before considering any fundamental changes About code DO NOT USE TABS No matter what Java development platform you are please configure insert indent Tabs are displayed totally different in every editor and there are millions of different editors out there The insertion of tabs in the source code is messing up the syntax formatting in a way that is UNREPAIRABLE Apart from please keep code indentation accurate This is not just good it helps to keep code readable and in that way dramatically decreases the chance for overlooked bugs Everyone is welcomed to correct indentation errors wherever they are spotted Before you start to do this please double check that your editor is really configured to insert spaces Line feeds may be checked in either in windows or in unix linux style All reasonable text and java editors can deal with both linefeed formats Converting line feeds is but in this case please make sure that only linefeed characters are changed and nothing else is affected Due to the platform independent nature of the editor has the potential to run on almost any given operating system the build process differs greatly between systems as well as java environments In the several people have attempted to add build scripts along with structural changes to optimize the setup on one particular system environment which has led to conflict Please do *not *attempt to change the structure or any directories for the mere purpose of improving a build process or performance in a java environment Build scripts may be placed in the root it would be especially fine if it is just one or two files but the latter is not required Please excuse me for placing such restriction I and many users of the editor greatly appreciate build scripts We just had some real troubles over this issue in the past and I don t want to have them repeated the editor has relatively high performance requirements I ve spent a lot of extra work to keep everything as fast and memory efficient as possible when you add new data fields or calculations in the archetype please make sure they are as efficient as possible and worth both the time and space they consume Now don t be afraid too much No development would be possible without adding calculations and data at all Just bear in mind unlike for many other open source performance does make a difference for the CrossfireEditor The for as many systems as possible In case you are unexperienced with java and note that the graphics look different on every and with every font They also have different sizes proportions and behave different A seemingly trivial and effectless change can wreck havoc for the same GUI run on another system please don t be totally afraid of it
Definition: Developer_README.txt:76
net.sf.gridarta.model.index.Index.setName
void setName(@NotNull V value, long timestamp, @NotNull String name)
Associates a value with a name.