2016-04-13 27 views
1

下面的枚舉幫助我避免了JUNIT測試用例中不同位置的硬編碼目錄值。這似乎在IDE的WINDOW框中的開發環境中工作。windows/linux上的文件分隔符問題

只要我把這段代碼放在LINUX框上,JUNIT類就找不到目錄。 Files.exists(sourcePath)返回false。相同的代碼在IDE(windows)上返回true。任何關於什麼是錯誤的指針?枚舉的

public enum DIRECTORY { 

    OUTPUT("resources/output"), RESOURCE("resources/resource"), PROCESSED_OUTPUT(
      "resources/output/resources/resource"), EXPLODED_WEBAPPS(
      "temp/webapps"), WEBAPPS("webapps"); 

    private String _name; 

    private DIRECTORY(String _name) { 

     this._name = _name; 

    } 

    public String getDirectoryName() { 
     return _name; 
    } 

} 

使用示例代碼

private void restore_csv_files() { 

     Path _processed_output = Paths.get(DIRECTORY.PROCESSED_OUTPUT 
       .getDirectoryName()); 
     Path resource = Paths.get(DIRECTORY.RESOURCE.getDirectoryName()); 

     FileUtility.copy_files(_processed_output, resource); 
     FileUtility.delete_files(_processed_output); 
     FileUtility.delete_directory(_processed_output); 

    } 

文件的Util類

public static void copy_files(Path sourcePath, Path targetPath) { 
     if (Files.exists(sourcePath) && Files.exists(targetPath)) { 
      try { 
       Files.walkFileTree(sourcePath, new SimpleFileVisitor<Path>() { 

        @Override 
        public FileVisitResult visitFile(final Path file, 
          final BasicFileAttributes attrs) throws IOException { 
         Files.copy(file, 
           targetPath.resolve(sourcePath.relativize(file))); 
         return FileVisitResult.CONTINUE; 
        } 
       }); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
     } else { 
      System.out.println("Either Source or Target path does not exist"); 
     } 
    } 

基本上在Windows這個東西的工作,但在Linux中,得到sysout「源或目標路徑不存在」(請參閱​​上面的c頌爲系統輸出)

樣品異常

java.nio.file.NoSuchFileException: webapps/web.war 
    at sun.nio.fs.UnixException.translateToIOException(UnixException.java:86) 
    at sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:102) 
    at sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:107) 
    at sun.nio.fs.UnixFileAttributeViews$Basic.readAttributes(UnixFileAttributeViews.java:55) 
    at com.temp.utils.FileUtility.alter_date_time_of_file(Unknown Source) 
    at com.temp.webengine.WebEngineTest.test_web_app_updated_true(Unknown Source) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:498) 
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47) 
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) 
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44) 
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) 
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26) 
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27) 
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271) 
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70) 
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50) 
+1

資源不是文件,並且在CLASSPATH中存在,而不是在文件系統中。反思。 – EJP

回答

4

對於文件分隔符,使用System Properties

static String separator = System.getProperty("file.separator"); 
+1

我認爲'path.separator'對於枚舉'DIRECTORY'也是有用的 –

+0

你的意思是它失敗的原因。所以你希望我在ENUM中使用路徑分隔符? –

+0

我不認爲這是一個路徑分隔符問題。如果你看看ENUM,文件分隔符正確地用於linux(/)。如果完全失敗了,它應該在WINDOWS盒子而不是linux上失敗。希望添加的異常應該會拋出一些燈光 –