提交 9ad5632e 编写于 作者: K Kohsuke Kawaguchi

added a reasonable coverage

上级 980a1202
......@@ -4,6 +4,7 @@ import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.AbstractMap;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
......@@ -60,7 +61,8 @@ public abstract class Attempt2<R> extends AbstractMap<Integer,R> implements Sort
private void loadIdOnDisk() {
String[] buildDirs = dir.list(createDirectoryFilter());
if (buildDirs==null) buildDirs=new String[0];
idOnDisk = new SortedStringList(Arrays.asList(buildDirs));
// wrap into ArrayList to enable mutation
idOnDisk = new SortedStringList(new ArrayList<String>(Arrays.asList(buildDirs)));
}
public Comparator<? super Integer> comparator() {
......
......@@ -36,6 +36,16 @@ public class SortedStringList extends AbstractList<String> {
return data.size();
}
@Override
public String remove(int index) {
return data.remove(index);
}
@Override
public boolean remove(Object o) {
return data.remove(o);
}
/**
* Finds the index of the entry lower than v.
*/
......
......@@ -6,20 +6,26 @@ import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import java.io.IOException;
import java.util.Map;
import java.util.NoSuchElementException;
/**
* @author Kohsuke Kawaguchi
*/
public class Attempt2Test extends Assert {
// A=1, B=3, C=5
@Rule
public FakeMapBuilder aBuilder = new FakeMapBuilder();
private FakeMap a;
// empty map
@Rule
public FakeMapBuilder bBuilder = new FakeMapBuilder();
private FakeMap b;
@Rule
public FakeMapBuilder localBuilder = new FakeMapBuilder();
@Before
public void setUp() throws Exception {
......@@ -47,6 +53,13 @@ public class Attempt2Test extends Assert {
a.get(1).asserts(1,"A");
}
@Test
public void lookupWithBogusKeyType() {
assertNull(a.get(null));
assertNull(a.get("foo"));
assertNull(a.get(this));
}
@Test
public void firstKey() {
assertEquals(1, a.firstKey().intValue());
......@@ -76,4 +89,24 @@ public class Attempt2Test extends Assert {
assertNull(a.search( 99, Direction.ASC));
assertNull(a.search(-99, Direction.DESC));
}
/**
* If load fails, search needs to gracefully handle it
*/
@Test
public void unloadableData() throws IOException {
FakeMap m = localBuilder.add(1, "A").addUnloadable("B").add(5, "C").make();
assertNull(m.search(3,Direction.EXACT));
m.search(3,Direction.DESC).asserts(1,"A");
m.search(3,Direction.ASC ).asserts(5,"C");
}
@Test
public void eagerLoading() throws IOException {
Map.Entry[] b = a.entrySet().toArray(new Map.Entry[3]);
((Build)b[0].getValue()).asserts(1,"A");
((Build)b[1].getValue()).asserts(3,"B");
((Build)b[2].getValue()).asserts(5,"C");
}
}
......@@ -31,6 +31,12 @@ public class FakeMapBuilder implements TestRule {
return this;
}
public FakeMapBuilder addUnloadable(String id) throws IOException {
File build = new File(dir,id);
build.mkdir();
return this;
}
public FakeMap make() {
return new FakeMap(dir);
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册