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-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.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  //noinspection EmptyClass
72  return new AbstractIndex<File>() {
73 
74  };
75  }
76 
80  @Test
81  public void testFind1() {
82  final Index<File> index = newIndex();
83  Assert.assertEquals("", findPartialName(index, "a"));
84  index.add(new File("abc"), 1L);
85  index.setName(new File("abc"), 1L, "abC");
86  Assert.assertEquals("abc", findPartialName(index, "a"));
87  Assert.assertEquals("abc", findPartialName(index, "b"));
88  Assert.assertEquals("", findPartialName(index, "x"));
89  index.add(new File("bcd"), 1L);
90  index.setName(new File("bcd"), 1L, "BcD");
91  Assert.assertEquals("abc", findPartialName(index, "a"));
92  Assert.assertEquals("abc,bcd", findPartialName(index, "b"));
93  Assert.assertEquals("abc,bcd", findPartialName(index, "Bc"));
94  Assert.assertEquals("", findPartialName(index, "x"));
95  }
96 
101  @Test
102  public void testTransaction() {
103  final Index<File> index = newIndex();
104  index.add(new File("a"), 1L);
105  index.add(new File("b"), 1L);
106  index.add(new File("c"), 1L);
107  index.setName(new File("a"), 2L, "a");
108  index.setName(new File("b"), 2L, "a");
109  index.setName(new File("c"), 2L, "a");
110  index.beginUpdate();
111  index.add(new File("b"), 1L);
112  index.add(new File("c"), 2L);
113  index.add(new File("d"), 2L);
114  index.endUpdate();
115  index.setName(new File("b"), 2L, "a");
116  index.setName(new File("c"), 2L, "a");
117  index.setName(new File("d"), 2L, "a");
118  Assert.assertEquals("b,c,d", findPartialName(index, "a"));
119  }
120 
124  @Test
125  public void testListener() {
126  final Index<File> index = newIndex();
127  final Listener listener = new Listener();
128  index.addIndexListener(listener);
129 
130  index.add(new File("a"), 1L);
131  Assert.assertEquals("add a\n" + "pending changed\n", listener.getAndClearEvents());
132 
133  index.add(new File("a"), 1L);
134  Assert.assertEquals("", listener.getAndClearEvents());
135 
136  index.add(new File("a"), 2L);
137  Assert.assertEquals("", listener.getAndClearEvents());
138 
139  index.add(new File("b"), 2L);
140  index.add(new File("c"), 2L);
141  Assert.assertEquals("add b\n" + "add c\n", listener.getAndClearEvents());
142 
143  index.remove(new File("b"));
144  Assert.assertEquals("del b\n" + "name changed\n", listener.getAndClearEvents());
145 
146  index.setName(new File("a"), 2L, "name1");
147  index.setName(new File("b"), 2L, "name2");
148  index.setName(new File("c"), 2L, "name3");
149  Assert.assertEquals("name changed\n" + "name changed\n" + "name changed\n", listener.getAndClearEvents());
150  }
151 
158  @NotNull
159  private static String getPending(@NotNull final Index<File> index) {
160  final Collection<File> pendingFiles = new HashSet<>();
161  while (true) {
162  final File pendingFile = index.removePending();
163  if (pendingFile == null) {
164  break;
165  }
166  pendingFiles.add(pendingFile);
167  }
168  return format(pendingFiles);
169  }
170 
178  @NotNull
179  private static String findPartialName(@NotNull final Index<File> index, @NotNull final String name) {
180  return format(index.findPartialName(name));
181  }
182 
188  @NotNull
189  private static String format(@NotNull final Collection<File> files) {
190  final Iterable<File> tmp = new TreeSet<>(files);
191  final StringBuilder sb = new StringBuilder();
192  final Iterator<File> it = tmp.iterator();
193  if (it.hasNext()) {
194  sb.append(it.next());
195  while (it.hasNext()) {
196  sb.append(',');
197  sb.append(it.next());
198  }
199  }
200  return sb.toString();
201  }
202 
207  private static class Listener implements IndexListener<File> {
208 
212  @NotNull
213  private final StringBuilder stringBuilder = new StringBuilder();
214 
215  @Override
216  public void valueAdded(@NotNull final File value) {
217  stringBuilder.append("add ").append(value).append("\n");
218  }
219 
220  @Override
221  public void valueRemoved(@NotNull final File value) {
222  stringBuilder.append("del ").append(value).append("\n");
223  }
224 
225  @Override
226  public void nameChanged() {
227  stringBuilder.append("name changed\n");
228  }
229 
230  @Override
231  public void pendingChanged() {
232  stringBuilder.append("pending changed\n");
233  }
234 
235  @Override
236  public void indexingFinished() {
237  stringBuilder.append("indexing finished");
238  }
239 
244  @NotNull
245  public String getAndClearEvents() {
246  final String result = stringBuilder.toString();
247  stringBuilder.setLength(0);
248  return result;
249  }
250 
251  }
252 
253 }
void testListener()
Checks that listeners are notified.
static String format(@NotNull final Collection< File > files)
Returns a text representation of a Collection.
An IndexListener that records a text representation of all generated events.
void add(@NotNull V value, long timestamp)
Adds a value to the cache.
An index of values.
Definition: Index.java:41
Abstract base class for Index implementations.
Interface for listeners interested in Index related events.
void testFind1()
Checks that Index#findPartialName(String) works as expected.
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...
void setPending(@NotNull V value)
Marks a value as pending.
Regression tests for MapsIndex.
void test1()
Checks that values are correctly marked as pending.
void setName(@NotNull V value, long timestamp, @NotNull String name)
Associates a value with a name.
void testTransaction()
Checks that Index#beginUpdate() and Index#endUpdate() works as expected.
void remove(@NotNull V value)
Removes a value from the cache.
void addIndexListener(@NotNull IndexListener< V > listener)
Adds an IndexListener to be notified of changes.
static Index< File > newIndex()
Returns a new Index instance.
String getAndClearEvents()
Returns the accumulated events.
void endUpdate()
Ends an update.
static String getPending(@NotNull final Index< File > index)
Returns all pending values of a MapsIndex.
void beginUpdate()
Starts an update.
final StringBuilder stringBuilder
The recorded events in text representation.