LUCENE-6835: add test case confirming listAll is sorted; fix dir impls that weren't

This commit is contained in:
Mike McCandless 2016-02-06 09:41:46 -05:00
parent 8e784699f0
commit 3c15c3f03d
3 changed files with 33 additions and 2 deletions

View File

@ -21,6 +21,7 @@ import java.io.IOException;
import java.nio.file.AtomicMoveNotSupportedException; import java.nio.file.AtomicMoveNotSupportedException;
import java.nio.file.NoSuchFileException; import java.nio.file.NoSuchFileException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
@ -118,7 +119,9 @@ public class FileSwitchDirectory extends Directory {
if (exc != null && files.isEmpty()) { if (exc != null && files.isEmpty()) {
throw exc; throw exc;
} }
return files.toArray(new String[files.size()]); String[] result = files.toArray(new String[files.size()]);
Arrays.sort(result);
return result;
} }
/** Utility method to return a file's extension. */ /** Utility method to return a file's extension. */

View File

@ -106,7 +106,9 @@ public class NRTCachingDirectory extends FilterDirectory implements Accountable
"cache=" + Arrays.toString(cache.listAll()) + ",delegate=" + Arrays.toString(in.listAll())); "cache=" + Arrays.toString(cache.listAll()) + ",delegate=" + Arrays.toString(in.listAll()));
} }
} }
return files.toArray(new String[files.size()]); String[] result = files.toArray(new String[files.size()]);
Arrays.sort(result);
return result;
} }
@Override @Override

View File

@ -1295,4 +1295,30 @@ public abstract class BaseDirectoryTestCase extends LuceneTestCase {
assertTrue(Arrays.asList(fsDir.listAll()).contains(fileName)); assertTrue(Arrays.asList(fsDir.listAll()).contains(fileName));
} }
} }
public void testListAllIsSorted() throws IOException {
try (Directory dir = getDirectory(createTempDir())) {
int count = atLeast(20);
Set<String> names = new HashSet<>();
while(names.size() < count) {
String name = TestUtil.randomSimpleString(random());
if (name.length() == 0) {
continue;
}
if (random().nextInt(5) == 1) {
IndexOutput out = dir.createTempOutput(name, "foo", IOContext.DEFAULT);
names.add(out.getName());
out.close();
} else if (names.contains(name) == false) {
IndexOutput out = dir.createOutput(name, IOContext.DEFAULT);
names.add(out.getName());
out.close();
}
}
String[] actual = dir.listAll();
String[] expected = actual.clone();
Arrays.sort(expected);
assertEquals(expected, actual);
}
}
} }