Repository: tapestry-5 Updated Branches: refs/heads/master c61335a15 -> 340257f5b
TAP5-2516: Fix ArrayIndexOutOfBoundsException when a template resource stream cannot be opened Project: http://git-wip-us.apache.org/repos/asf/tapestry-5/repo Commit: http://git-wip-us.apache.org/repos/asf/tapestry-5/commit/340257f5 Tree: http://git-wip-us.apache.org/repos/asf/tapestry-5/tree/340257f5 Diff: http://git-wip-us.apache.org/repos/asf/tapestry-5/diff/340257f5 Branch: refs/heads/master Commit: 340257f5b1da777fb061760591d0ee48404ed365 Parents: c61335a Author: Jochen Kemnade <[email protected]> Authored: Thu Nov 12 11:27:59 2015 +0100 Committer: Jochen Kemnade <[email protected]> Committed: Thu Nov 12 11:27:59 2015 +0100 ---------------------------------------------------------------------- .../internal/services/XMLTokenStream.java | 33 ++++++++----- .../services/TemplateParserImplTest.java | 50 ++++++++++++++++++++ 2 files changed, 71 insertions(+), 12 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/340257f5/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/XMLTokenStream.java ---------------------------------------------------------------------- diff --git a/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/XMLTokenStream.java b/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/XMLTokenStream.java index b80c6d6..10f7f59 100644 --- a/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/XMLTokenStream.java +++ b/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/XMLTokenStream.java @@ -62,17 +62,25 @@ public class XMLTokenStream private Location getLocation() { - int line = locator.getLineNumber(); + if (locator == null) + { + if (cachedLocation == null) + { + cachedLocation = new LocationImpl(resource); + } + } else { + int line = locator.getLineNumber(); - if (currentLine != line) - cachedLocation = null; + if (currentLine != line) + cachedLocation = null; - if (cachedLocation == null) - { - // lineOffset accounts for the extra line when a doctype is injected. The line number reported - // from the XML parser inlcudes the phantom doctype line, the lineOffset is used to subtract one - // to get the real line number. - cachedLocation = new LocationImpl(resource, line + lineOffset); + if (cachedLocation == null) + { + // lineOffset accounts for the extra line when a doctype is injected. The line number reported + // from the XML parser inlcudes the phantom doctype line, the lineOffset is used to subtract one + // to get the real line number. + cachedLocation = new LocationImpl(resource, line + lineOffset); + } } return cachedLocation; @@ -311,10 +319,11 @@ public class XMLTokenStream reader.setEntityResolver(handler); reader.setProperty("http://xml.org/sax/properties/lexical-handler", handler); - InputStream stream = openStream(); + InputStream stream = null; try { + stream = openStream(); reader.parse(new InputSource(stream)); } catch (IOException ex) { @@ -345,7 +354,7 @@ public class XMLTokenStream private InputStream openStream() throws IOException { InputStream rawStream = resource.openStream(); - + String transformationEncoding = "UTF8"; InputStreamReader rawReader = new InputStreamReader(rawStream, transformationEncoding); @@ -430,7 +439,7 @@ public class XMLTokenStream private XMLToken token() { - return tokens.get(cursor); + return cursor == -1 ? null : tokens.get(cursor); } /** http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/340257f5/tapestry-core/src/test/java/org/apache/tapestry5/internal/services/TemplateParserImplTest.java ---------------------------------------------------------------------- diff --git a/tapestry-core/src/test/java/org/apache/tapestry5/internal/services/TemplateParserImplTest.java b/tapestry-core/src/test/java/org/apache/tapestry5/internal/services/TemplateParserImplTest.java index 1812043..b470a22 100644 --- a/tapestry-core/src/test/java/org/apache/tapestry5/internal/services/TemplateParserImplTest.java +++ b/tapestry-core/src/test/java/org/apache/tapestry5/internal/services/TemplateParserImplTest.java @@ -17,6 +17,7 @@ import org.apache.tapestry5.internal.test.InternalBaseTestCase; import org.apache.tapestry5.ioc.Locatable; import org.apache.tapestry5.ioc.Location; import org.apache.tapestry5.ioc.Resource; +import org.apache.tapestry5.ioc.internal.util.AbstractResource; import org.apache.tapestry5.ioc.internal.util.ClasspathResource; import org.apache.tapestry5.ioc.internal.util.CollectionFactory; import org.apache.tapestry5.ioc.internal.util.TapestryException; @@ -26,10 +27,15 @@ import org.testng.annotations.Test; import java.util.Arrays; import java.util.List; +import java.util.Locale; import java.util.Map; import static java.lang.String.format; +import java.io.IOException; +import java.io.InputStream; +import java.net.URL; + /** * This is used to test the template parser ... and in some cases, the underlying behavior of the SAX APIs. */ @@ -1080,4 +1086,48 @@ public class TemplateParserImplTest extends InternalBaseTestCase assertEquals(t3.text.trim(), "END"); } + + @Test + //TAP5-2516 + public void resource_that_throws_exception() throws Exception + { + + Resource resource = new AbstractResource("throwfoo") { + + @Override + public URL toURL() { + return null; + } + + @Override + public boolean exists() { + return true; + } + + @Override + protected Resource newResource(String path) { + return null; + } + + @Override + public InputStream openStream() throws IOException { + throw new IOException("foo"); + } + }; + + + try + { + getParser().parseTemplate(resource); + unreachable(); + } catch (RuntimeException ex) + { + if (ex.getCause() instanceof TapestryException && ex.getCause().getCause() instanceof IOException) + { + assertMessageContains(ex, "foo"); + } else { + throw ex; + } + } + } }
