Bug 5693: IOException after first parsing phase - stream closed 04/38004/6
authorIvan Hrasko <ivan.hrasko@pantheon.tech>
Mon, 11 Apr 2016 07:21:07 +0000 (09:21 +0200)
committerRobert Varga <nite@hq.sk>
Tue, 17 May 2016 07:23:50 +0000 (07:23 +0000)
Solved by creating new stream for every parsing phase and resetting
original stream.

Change-Id: I12c45e5f3c92c17ce5f07f98a551b288245698bb
Signed-off-by: Ivan Hrasko <ivan.hrasko@pantheon.tech>
yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/YinStatementSourceImpl.java
yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/stmt/retest/Bug5693Test.java [new file with mode: 0644]
yang/yang-parser-impl/src/test/resources/bugs/bug5693/foo.xml [new file with mode: 0644]

index 3ae5a3e217e015c0b9976109c3bc1d15a1cabb84..3dee29f4fd159563fbf048559ea9b4003787b8af 100644 (file)
@@ -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 (file)
index 0000000..a4cec62
--- /dev/null
@@ -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 (file)
index 0000000..239e7dd
--- /dev/null
@@ -0,0 +1,22 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<module xmlns="urn:ietf:params:xml:ns:yang:yin:1" xmlns:foo="foo" name="foo">
+    <yang-version value="1"></yang-version>
+    <namespace uri="foo"></namespace>
+    <prefix value="foo"></prefix>
+    <organization>
+        <text></text>
+    </organization>
+    <contact>
+        <text></text>
+    </contact>
+    <revision date="2016-04-01"></revision>
+    <feature name="test-input-stream-not-closed">
+        <description>
+            <text>
+                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.
+            </text>
+        </description>
+    </feature>
+</module>
\ No newline at end of file