20 package net.sf.gridarta.model.index;
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;
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));
48 index.
add(
new File(
"d"), 1L);
50 index.
add(
new File(
"b"), 1L);
51 index.
add(
new File(
"d"), 1L);
53 index.
add(
new File(
"b"), 2L);
54 index.
add(
new File(
"d"), 2L);
59 index.
add(
new File(
"b"), 3L);
60 index.
add(
new File(
"c"), 3L);
61 index.
remove(
new File(
"b"));
84 index.
add(
new File(
"abc"), 1L);
85 index.
setName(
new File(
"abc"), 1L,
"abC");
89 index.
add(
new File(
"bcd"), 1L);
90 index.
setName(
new File(
"bcd"), 1L,
"BcD");
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");
111 index.
add(
new File(
"b"), 1L);
112 index.
add(
new File(
"c"), 2L);
113 index.
add(
new File(
"d"), 2L);
115 index.
setName(
new File(
"b"), 2L,
"a");
116 index.
setName(
new File(
"c"), 2L,
"a");
117 index.
setName(
new File(
"d"), 2L,
"a");
130 index.
add(
new File(
"a"), 1L);
131 Assert.assertEquals(
"add a\n" +
"pending changed\n", listener.
getAndClearEvents());
133 index.
add(
new File(
"a"), 1L);
136 index.
add(
new File(
"a"), 2L);
139 index.
add(
new File(
"b"), 2L);
140 index.
add(
new File(
"c"), 2L);
143 index.
remove(
new File(
"b"));
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());
160 final Collection<File> pendingFiles =
new HashSet<>();
162 final File pendingFile = index.removePending();
163 if (pendingFile == null) {
166 pendingFiles.add(pendingFile);
168 return format(pendingFiles);
180 return format(index.findPartialName(name));
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();
194 sb.append(it.next());
195 while (it.hasNext()) {
197 sb.append(it.next());
200 return sb.toString();
217 stringBuilder.append(
"add ").append(value).append(
"\n");
222 stringBuilder.append(
"del ").append(value).append(
"\n");
227 stringBuilder.append(
"name changed\n");
232 stringBuilder.append(
"pending changed\n");
237 stringBuilder.append(
"indexing finished");
246 final String result = stringBuilder.toString();
247 stringBuilder.setLength(0);
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.
Abstract base class for Index implementations.
Interface for listeners interested in Index related events.
void valueAdded(@NotNull final File value)
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.
void valueRemoved(@NotNull final File value)
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.