IDEA-76479 (tests)
authorRoman Shevchenko <roman.shevchenko@jetbrains.com>
Fri, 24 Oct 2014 10:08:08 +0000 (12:08 +0200)
committerRoman Shevchenko <roman.shevchenko@jetbrains.com>
Fri, 24 Oct 2014 10:08:08 +0000 (12:08 +0200)
platform/util/testSrc/com/intellij/openapi/util/io/IoTestUtil.java

index 5dbe2dd1ae768a42386d14d7a40bd0ff2e2a44e9..1669c5eca11a249dd7a9cac61d2eefdf8e649bad 100644 (file)
@@ -18,6 +18,8 @@ package com.intellij.openapi.util.io;
 import com.intellij.openapi.util.SystemInfo;
 import com.intellij.openapi.util.text.StringUtil;
 import com.intellij.util.Function;
+import com.intellij.util.ObjectUtils;
+import com.intellij.util.Processor;
 import com.intellij.util.containers.ContainerUtil;
 import org.jetbrains.annotations.NotNull;
 import org.jetbrains.annotations.Nullable;
@@ -236,6 +238,34 @@ public class IoTestUtil {
     return jarFile;
   }
 
+  @NotNull
+  public static File createTestJar(@NotNull File jarFile, @NotNull final File root) throws IOException {
+    final ZipOutputStream stream = new ZipOutputStream(new FileOutputStream(jarFile));
+    try {
+      FileUtil.visitFiles(root, new Processor<File>() {
+        @Override
+        public boolean process(File file) {
+          if (file.isFile()) {
+            String path = FileUtil.toSystemIndependentName(ObjectUtils.assertNotNull(FileUtil.getRelativePath(root, file)));
+            try {
+              stream.putNextEntry(new ZipEntry(path));
+              stream.write(FileUtil.loadFileBytes(file));
+              stream.closeEntry();
+            }
+            catch (IOException e) {
+              throw new RuntimeException(e);
+            }
+          }
+          return true;
+        }
+      });
+    }
+    finally {
+      stream.close();
+    }
+    return jarFile;
+  }
+
   @NotNull
   public static File createTestDir(@NotNull String name) {
     return createTestDir(new File(FileUtil.getTempDirectory()), name);