Catch exceptions caused by fetching with invalid indices

This commit is contained in:
Masaki Mori
2015-09-09 11:15:56 +09:00
parent e7daae3912
commit b846605894
2 changed files with 24 additions and 0 deletions
@@ -8,6 +8,7 @@ import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
/**
* A collector that does not use reflection and can be used with GWT.
@@ -58,6 +59,8 @@ public abstract class BasicCollector implements Mustache.Collector
return ((List<?>)ctx).get(Integer.parseInt(name));
} catch (NumberFormatException nfe) {
return Template.NO_FETCHER_FOUND;
} catch (IndexOutOfBoundsException e) {
return Template.NO_FETCHER_FOUND;
}
}
};
@@ -68,6 +71,8 @@ public abstract class BasicCollector implements Mustache.Collector
return ((Object[])ctx)[Integer.parseInt(name)];
} catch (NumberFormatException nfe) {
return Template.NO_FETCHER_FOUND;
} catch (IndexOutOfBoundsException e) {
return Template.NO_FETCHER_FOUND;
}
}
};
@@ -80,6 +85,8 @@ public abstract class BasicCollector implements Mustache.Collector
return iter.next();
} catch (NumberFormatException nfe) {
return Template.NO_FETCHER_FOUND;
} catch (NoSuchElementException e) {
return Template.NO_FETCHER_FOUND;
}
}
};
@@ -122,6 +122,11 @@ public class MustacheTest
"foo", Arrays.asList(context("bar", "baz"), context("bar", "bif"))));
}
@Test public void testListIndexOutOfBoundsSection () {
test("", "{{#foo.2}}{{bar}}{{/foo.2}}", context(
"foo", Arrays.asList(context("bar", "baz"), context("bar", "bif"))));
}
@Test public void testListItemSection() {
test("baz", "{{foo.0.bar}}", context(
"foo", Arrays.asList(context("bar", "baz"), context("bar", "bif"))));
@@ -139,6 +144,12 @@ public class MustacheTest
context("bar", "baz"), context("bar", "bif") }));
}
@Test public void testArrayIndexOutOfBoundsSection () {
test("", "{{#foo.2}}{{bar}}{{/foo.2}}",
context("foo", new Object[] {
context("bar", "baz"), context("bar", "bif") }));
}
@Test public void testArrayItemSection () {
test("baz", "{{foo.0.bar}}",
context("foo", new Object[] {
@@ -157,6 +168,12 @@ public class MustacheTest
context("bar", "bif")).iterator()));
}
@Test public void testIteratorIndexOutOfBoundsSection () {
test("", "{{#foo.2}}{{bar}}{{/foo.2}}",
context("foo", Arrays.asList(context("bar", "baz"),
context("bar", "bif")).iterator()));
}
@Test public void testIteratorItemSection () {
test("baz", "{{foo.0.bar}}",
context("foo", Arrays.asList(context("bar", "baz"),