Bug 4540: Yang parser exceptions should follow consistent path
[yangtools.git] / yang / yang-parser-impl / src / test / java / org / opendaylight / yangtools / yang / stmt / retest / TestUtils.java
index 5878f492aae4bdaa9495c5d4f713423445b33d9b..50af9ba031315e4a8ab4a472e4e6eda8a62889cc 100644 (file)
@@ -8,23 +8,17 @@
 package org.opendaylight.yangtools.yang.stmt.retest;
 
 import static org.junit.Assert.assertEquals;
-
-import java.io.FileInputStream;
-import java.io.FileNotFoundException;
-import java.net.URISyntaxException;
-import java.net.URL;
-import java.util.Collection;
-import org.opendaylight.yangtools.yang.model.api.SchemaContext;
-import org.opendaylight.yangtools.yang.parser.spi.source.StatementStreamSource;
-import org.opendaylight.yangtools.yang.stmt.test.StmtTestUtils;
-
 import java.io.File;
+import java.io.FileNotFoundException;
 import java.io.InputStream;
 import java.net.URI;
+import java.net.URISyntaxException;
+import java.net.URL;
 import java.text.DateFormat;
 import java.text.ParseException;
 import java.text.SimpleDateFormat;
 import java.util.ArrayList;
+import java.util.Collection;
 import java.util.Date;
 import java.util.List;
 import java.util.Set;
@@ -36,16 +30,24 @@ import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
 import org.opendaylight.yangtools.yang.model.api.Module;
 import org.opendaylight.yangtools.yang.model.api.ModuleImport;
+import org.opendaylight.yangtools.yang.model.api.SchemaContext;
 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
+import org.opendaylight.yangtools.yang.parser.spi.source.StatementStreamSource;
 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor;
 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangInferencePipeline;
 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangStatementSourceImpl;
+import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YinStatementSourceImpl;
 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.EffectiveSchemaContext;
+import org.opendaylight.yangtools.yang.parser.util.NamedFileInputStream;
+import org.opendaylight.yangtools.yang.stmt.test.StmtTestUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
-final class TestUtils {
+public final class TestUtils {
+    private static final Logger LOG = LoggerFactory.getLogger(TestUtils.class);
 
     private TestUtils() {
     }
@@ -57,8 +59,22 @@ final class TestUtils {
         File[] files = new File(resourceDirectory).listFiles();
 
         for (File file : files) {
-            addSources(reactor, new YangStatementSourceImpl(file.getPath(),
-                    true));
+            if (file.getName().endsWith(".yang")) {
+                addSources(reactor, new YangStatementSourceImpl(file.getPath(), true));
+            } else {
+                LOG.info("Ignoring non-yang file {}", file);
+            }
+        }
+
+        EffectiveSchemaContext ctx = reactor.buildEffective();
+        return ctx.getModules();
+    }
+
+    public static Set<Module> loadYinModules(final URI resourceDirectory) throws ReactorException {
+        final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
+
+        for (File file : new File(resourceDirectory).listFiles()) {
+            addYinSources(reactor, new YinStatementSourceImpl(file.getPath(), true));
         }
 
         EffectiveSchemaContext ctx = reactor.buildEffective();
@@ -66,9 +82,9 @@ final class TestUtils {
     }
 
     public static Set<Module> loadModules(final List<InputStream> streams)
-            throws SourceException, ReactorException {
+        throws SourceException, ReactorException {
         final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR
-                .newBuild();
+            .newBuild();
         for (InputStream inputStream : streams) {
             addSources(reactor, new YangStatementSourceImpl(inputStream));
         }
@@ -77,17 +93,34 @@ final class TestUtils {
         return ctx.getModules();
     }
 
+    public static Set<Module> loadYinModules(final List<InputStream> streams) throws SourceException, ReactorException {
+        final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
+        for (InputStream inputStream : streams) {
+            addYinSources(reactor, new YinStatementSourceImpl(inputStream));
+        }
+
+        EffectiveSchemaContext ctx = reactor.buildEffective();
+        return ctx.getModules();
+    }
+
     public static Module loadModule(final InputStream stream)
-            throws SourceException, ReactorException {
+        throws SourceException, ReactorException {
         final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR
-                .newBuild();
+            .newBuild();
         addSources(reactor, new YangStatementSourceImpl(stream));
         EffectiveSchemaContext ctx = reactor.buildEffective();
         return ctx.getModules().iterator().next();
     }
 
+    public static Module loadYinModule(final InputStream stream) throws SourceException, ReactorException {
+        final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
+        addYinSources(reactor, new YinStatementSourceImpl(stream));
+        EffectiveSchemaContext ctx = reactor.buildEffective();
+        return ctx.getModules().iterator().next();
+    }
+
     public static Module findModule(final Set<Module> modules,
-            final String moduleName) {
+        final String moduleName) {
         Module result = null;
         for (Module module : modules) {
             if (module.getName().equals(moduleName)) {
@@ -211,15 +244,15 @@ final class TestUtils {
     }
 
     private static void addSources(
-            CrossSourceStatementReactor.BuildAction reactor,
-            YangStatementSourceImpl... sources) {
+            final CrossSourceStatementReactor.BuildAction reactor,
+            final YangStatementSourceImpl... sources) {
         for (YangStatementSourceImpl source : sources) {
             reactor.addSource(source);
         }
     }
 
     public static SchemaContext parseYangSources(
-            StatementStreamSource... sources) throws SourceException,
+            final StatementStreamSource... sources) throws SourceException,
             ReactorException {
 
         CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR
@@ -229,25 +262,24 @@ final class TestUtils {
         return reactor.buildEffective();
     }
 
-    public static SchemaContext parseYangSources(File... files)
+    public static SchemaContext parseYangSources(final File... files)
             throws SourceException, ReactorException, FileNotFoundException {
 
         StatementStreamSource[] sources = new StatementStreamSource[files.length];
 
         for (int i = 0; i < files.length; i++) {
-            sources[i] = new YangStatementSourceImpl(new FileInputStream(
-                    files[i]));
+            sources[i] = new YangStatementSourceImpl(new NamedFileInputStream(files[i], files[i].getPath()));
         }
 
         return parseYangSources(sources);
     }
 
-    public static SchemaContext parseYangSources(Collection<File> files)
+    public static SchemaContext parseYangSources(final Collection<File> files)
             throws SourceException, ReactorException, FileNotFoundException {
         return parseYangSources(files.toArray(new File[files.size()]));
     }
 
-    public static SchemaContext parseYangSources(String yangSourcesDirectoryPath)
+    public static SchemaContext parseYangSources(final String yangSourcesDirectoryPath)
             throws SourceException, ReactorException, FileNotFoundException,
             URISyntaxException {
 
@@ -258,7 +290,7 @@ final class TestUtils {
         return parseYangSources(testSourcesDir.listFiles());
     }
 
-    public static SchemaContext parseYangSource(String yangSourceFilePath)
+    public static SchemaContext parseYangSource(final String yangSourceFilePath)
             throws SourceException, ReactorException, FileNotFoundException,
             URISyntaxException {
 
@@ -269,4 +301,11 @@ final class TestUtils {
         return parseYangSources(testSourcesFile);
     }
 
+    private static void addYinSources(final CrossSourceStatementReactor.BuildAction reactor,
+                                      final YinStatementSourceImpl... sources) {
+        for (YinStatementSourceImpl source : sources) {
+            reactor.addSource(source);
+        }
+    }
+
 }