2016-04-13 7 views
1

以下の列挙型は、JUNITテストケースの異なる場所でハードコードされたディレクトリ値を避けるのに役立ちます。これは、WINDOWのボックス上のIDE内の開発環境で動作するようです。windows/linuxボックスでのファイルセパレータの問題

このコードをLINUXボックスに入れると、JUNITクラスはディレクトリを見つけることができません。 Files.exists(sourcePath)はfalseを返します。同じコードがIDE(ウィンドウ)で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"); 
     } 
    } 

この事の仕事が、Linuxボックスに、取得sysout「ソースパスまたはターゲットパスが存在しません」(上記のcを参照) SYSOUTのためのODE)ファイルの区切り文字については

サンプル例外

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'はenum' DIRECTORY'でも役に立ちます。 –

+0

あなたはそれが失敗した理由を意味します。だから私はENUMのパス区切り記号を使用したいですか? –

+0

私はそれがパス区切りの問題ではないと思います。 ENUMを見れば、ファイルセパレータはLinux(/)で正しく使用されます。まったく失敗したら、LinuxではなくWINDOWSボックスで失敗しているはずです。うまくいけば例外がいくつかの光をスローする必要があります –

関連する問題