Fixed relative/absolute yang files directory resolving. 39/339/1
authorMartin Vitez <mvitez@cisco.com>
Tue, 14 May 2013 11:05:16 +0000 (13:05 +0200)
committerMartin Vitez <mvitez@cisco.com>
Tue, 14 May 2013 11:05:16 +0000 (13:05 +0200)
Signed-off-by: Martin Vitez <mvitez@cisco.com>
opendaylight/sal/yang-prototype/code-generator/maven-yang-plugin/src/main/java/org/opendaylight/controller/yang2sources/plugin/Util.java
opendaylight/sal/yang-prototype/code-generator/maven-yang-plugin/src/main/java/org/opendaylight/controller/yang2sources/plugin/YangToSourcesMojo.java
opendaylight/sal/yang-prototype/code-generator/maven-yang-plugin/src/test/java/org/opendaylight/controller/yang2sources/plugin/UtilTest.java

index 3739a1d6599184898a6b64548e86208c634f572d..5676530afec2eaecedab1469608654abb3ebc675 100644 (file)
@@ -45,10 +45,15 @@ final class Util {
      * List files recursively and return as array of String paths. Use cache of
      * size 1.
      */
-    static Collection<File> listFiles(String rootDir) {
+    static Collection<File> listFiles(String rootDir) throws FileNotFoundException {
         if (cache.get(rootDir) != null)
             return cache.get(rootDir);
 
+        File file = new File(rootDir);
+        if(!file.exists()) {
+            throw new FileNotFoundException();
+        }
+
         Collection<File> yangFiles = FileUtils.listFiles(new File(rootDir),
                 new String[] { YANG_SUFFIX }, true);
 
@@ -67,7 +72,7 @@ final class Util {
         return is;
     }
 
-    static String[] listFilesAsArrayOfPaths(String rootDir) {
+    static String[] listFilesAsArrayOfPaths(String rootDir) throws FileNotFoundException {
         String[] filesArray = new String[] {};
         Collection<File> yangFiles = listFiles(rootDir);
 
index 6a11042cb9c1075f2ca2ee96d904fffdc9e2cf41..cfaa8a0874aa24fcfd798679605eb7349d9983ef 100644 (file)
@@ -10,6 +10,7 @@ package org.opendaylight.controller.yang2sources.plugin;
 import java.io.Closeable;
 import java.io.File;
 import java.io.FileInputStream;
+import java.io.FileNotFoundException;
 import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
@@ -206,7 +207,20 @@ public final class YangToSourcesMojo extends AbstractMojo {
     }
 
     private Collection<File> getFilesFromYangRoot() {
-        Collection<File> yangFilesLoaded = Util.listFiles(yangFilesRootDir);
+        Collection<File> yangFilesLoaded = null;
+
+        File rootDir = new File(yangFilesRootDir);
+        try {
+            if(!rootDir.isAbsolute()) {
+                yangFilesLoaded = Util.listFiles(project.getBasedir().getAbsolutePath() + yangFilesRootDir);
+            } else {
+                yangFilesLoaded = Util.listFiles(yangFilesRootDir);
+            }
+
+        } catch(FileNotFoundException e) {
+            getLog().warn("Directory '" + yangFilesRootDir + "' does not exists.");
+            yangFilesLoaded = new ArrayList<File>();
+        }
         Collection<File> yangFiles = new ArrayList<File>(yangFilesLoaded);
 
         try {
index 0a17d9f13d5e8b06eb8820238ba868a3cc82e930..75e50f0261a1b5f2eba1ee4bc26fd4193464aa8e 100644 (file)
@@ -7,9 +7,10 @@
  */
 package org.opendaylight.controller.yang2sources.plugin;
 
-import static org.junit.Assert.*;
+import static org.junit.Assert.assertTrue;
 
 import java.io.File;
+import java.io.FileNotFoundException;
 import java.util.Collection;
 
 import org.junit.Test;
@@ -17,7 +18,7 @@ import org.junit.Test;
 public class UtilTest {
 
     @Test
-    public void testCache() {
+    public void testCache() throws FileNotFoundException {
         String yang = new File(getClass().getResource("/mock.yang").getFile())
                 .getParent();
         Collection<File> files = Util.listFiles(yang);