Scripted update of if statements
[yangtools.git] / yang / yang-data-jaxen / src / test / java / org / opendaylight / yangtools / yang / data / jaxen / TestUtils.java
index 406475315b2e82092ded239d53db627332a0081a..15987f2d45cfde80825612c44949b9da334f4211 100644 (file)
@@ -13,10 +13,13 @@ import static org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes.ma
 import static org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes.mapNodeBuilder;
 
 import java.io.File;
+import java.io.FileInputStream;
 import java.io.FileNotFoundException;
 import java.io.IOException;
 import java.net.URI;
+import java.net.URISyntaxException;
 import java.util.ArrayList;
+import java.util.Collection;
 import java.util.List;
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
@@ -25,6 +28,12 @@ import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
 import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
+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;
 
 final class TestUtils {
 
@@ -47,19 +56,46 @@ final class TestUtils {
     private TestUtils() {
     }
 
-    public static SchemaContext loadSchemaContext(final URI resourceDirectory) throws IOException {
-        final YangParserImpl parser = new YangParserImpl();
-        final File testDir = new File(resourceDirectory);
+    static SchemaContext loadModules(final String resourceDirectory) throws IOException, URISyntaxException,
+            ReactorException {
+        URI path = TestUtils.class.getResource(resourceDirectory).toURI();
+        final File testDir = new File(path);
         final String[] fileList = testDir.list();
         final List<File> testFiles = new ArrayList<>();
         if (fileList == null) {
-            throw new FileNotFoundException(resourceDirectory.toString());
+            throw new FileNotFoundException(resourceDirectory);
         }
         for (String fileName : fileList) {
-            testFiles.add(new File(testDir, fileName));
+            if (!new File(testDir, fileName).isDirectory()) {
+                testFiles.add(new File(testDir, fileName));
+            }
         }
-        SchemaContext ctx = parser.parseFiles(testFiles);
-        return ctx;
+        return parseYangSources(testFiles);
+    }
+
+    public static SchemaContext parseYangSources(StatementStreamSource... sources)
+            throws SourceException, ReactorException {
+
+        CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR
+                .newBuild();
+        reactor.addSources(sources);
+
+        return reactor.buildEffective();
+    }
+
+    public static SchemaContext parseYangSources(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]));
+        }
+
+        return parseYangSources(sources);
+    }
+
+    public static SchemaContext parseYangSources(Collection<File> files) throws SourceException, ReactorException, FileNotFoundException {
+        return parseYangSources(files.toArray(new File[files.size()]));
     }
 
     /**