From: Ivan Hrasko Date: Mon, 11 Apr 2016 07:21:07 +0000 (+0200) Subject: Bug 5693: IOException after first parsing phase - stream closed X-Git-Tag: release/beryllium-sr3~8 X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=yangtools.git;a=commitdiff_plain;h=0d9b03cafcddea2e86fdb7160107fb29250018e0 Bug 5693: IOException after first parsing phase - stream closed Solved by creating new stream for every parsing phase and resetting original stream. Change-Id: I12c45e5f3c92c17ce5f07f98a551b288245698bb Signed-off-by: Ivan Hrasko --- diff --git a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/YinStatementSourceImpl.java b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/YinStatementSourceImpl.java index 3ae5a3e217..3dee29f4fd 100644 --- a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/YinStatementSourceImpl.java +++ b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/YinStatementSourceImpl.java @@ -9,6 +9,9 @@ package org.opendaylight.yangtools.yang.parser.stmt.rfc6020; import com.google.common.base.Preconditions; +import com.google.common.io.ByteStreams; +import java.io.BufferedInputStream; +import java.io.ByteArrayInputStream; import java.io.File; import java.io.IOException; import java.io.InputStream; @@ -45,7 +48,8 @@ public class YinStatementSourceImpl implements StatementStreamSource { public YinStatementSourceImpl(final InputStream inputStream) { yinStatementModelParser = new YinStatementParserImpl(inputStream.toString()); - this.inputStream = inputStream; + this.inputStream = new BufferedInputStream(inputStream); + this.inputStream.mark(Integer.MAX_VALUE); } public YinStatementSourceImpl(final String fileName, final boolean isAbsolute) { @@ -81,10 +85,13 @@ public class YinStatementSourceImpl implements StatementStreamSource { private void initializeReader() { try { if (fileName != null) { - this.inputStream = loadFile(fileName, isAbsolute); + inputStream = loadFile(fileName, isAbsolute); + streamReader = xmlInputFactory.createXMLStreamReader(inputStream); + } else { + inputStream.reset(); + streamReader = xmlInputFactory.createXMLStreamReader(new ByteArrayInputStream(ByteStreams.toByteArray + (inputStream))); } - streamReader = xmlInputFactory.createXMLStreamReader(inputStream); - } catch (XMLStreamException e) { LOG.warn("Error while creating XMLStreamReader from input stream", e); } catch (URISyntaxException e) { @@ -115,4 +122,4 @@ public class YinStatementSourceImpl implements StatementStreamSource { return new NamedFileInputStream(file, fileName); } -} \ No newline at end of file +} diff --git a/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/stmt/retest/Bug5693Test.java b/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/stmt/retest/Bug5693Test.java new file mode 100644 index 0000000000..a4cec623a6 --- /dev/null +++ b/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/stmt/retest/Bug5693Test.java @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ +package org.opendaylight.yangtools.yang.stmt.retest; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +import java.io.File; +import java.io.FileNotFoundException; +import org.junit.Before; +import org.junit.Test; +import org.opendaylight.yangtools.yang.model.api.Module; +import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException; +import org.opendaylight.yangtools.yang.parser.util.NamedFileInputStream; + +public class Bug5693Test { + + private final String pathToModuleFoo = getClass().getResource("/bugs/bug5693/foo.xml").getPath(); + private Module foo; + + /** + * Use input stream to load Yin module. + */ + @Before + public void initTest() throws FileNotFoundException, ReactorException { + foo = TestUtils.loadYinModule(new NamedFileInputStream(new File(pathToModuleFoo), pathToModuleFoo)); + assertNotNull(foo); + } + + /** + * Test presence of testing feature (parsed in the last phase), if it is present then parsing was successful. + * Meaning that stream was not closed after the first parsing phase. + */ + @Test + public void bug5693Test() { + assertNotNull(foo.getFeatures()); + assertEquals("Module should has exactly one feature", 1, foo.getFeatures().size()); + assertEquals("Present feature should has expected local name", "test-input-stream-not-closed", + foo.getFeatures().iterator().next().getQName().getLocalName()); + } +} diff --git a/yang/yang-parser-impl/src/test/resources/bugs/bug5693/foo.xml b/yang/yang-parser-impl/src/test/resources/bugs/bug5693/foo.xml new file mode 100644 index 0000000000..239e7ddda8 --- /dev/null +++ b/yang/yang-parser-impl/src/test/resources/bugs/bug5693/foo.xml @@ -0,0 +1,22 @@ + + + + + + + + + + + + + + + + Presence of this feature in Module indicates that parsing of this file was successful. + Input stream was not closed after completing the first phase of parsing, so + statements defined in the last phase (like feature) were successfully parsed. + + + + \ No newline at end of file