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;
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) {
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) {
return new NamedFileInputStream(file, fileName);
}
-}
\ No newline at end of file
+}
--- /dev/null
+/*
+ * 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());
+ }
+}
--- /dev/null
+<?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