]> source.dussan.org Git - jgit.git/commitdiff
Support running from JARs in JGitTestUtil 90/17990/2
authorShawn Pearce <sop@google.com>
Fri, 1 Nov 2013 16:49:06 +0000 (10:49 -0600)
committerGerrit Code Review @ Eclipse.org <gerrit@eclipse.org>
Sun, 24 Nov 2013 10:27:00 +0000 (05:27 -0500)
Buck invokes JUnit tests from compiled JARs, not class directories.
When copying a resource back to the filesystem a jar: style URL is
obtained from the ClassLoader.

Change-Id: I28d702484ec13b0b309b87990da867050e4b5ec6

org.eclipse.jgit.junit/src/org/eclipse/jgit/junit/JGitTestUtil.java
org.eclipse.jgit.junit/src/org/eclipse/jgit/junit/SampleDataRepositoryTestCase.java

index c6fe4e4d91466b1b09dd650f592f47d5b8009771..1079d984399d28edae17e41dfb81c74f3c32d6ed 100644 (file)
 package org.eclipse.jgit.junit;
 
 import java.io.File;
+import java.io.FileNotFoundException;
 import java.io.FileOutputStream;
 import java.io.IOException;
+import java.io.InputStream;
 import java.io.OutputStreamWriter;
 import java.io.Writer;
 import java.lang.reflect.Method;
@@ -124,13 +126,44 @@ public abstract class JGitTestUtil {
                        // loaded previously
                        return new File("tst", fileName);
                }
+               if ("jar".equals(url.getProtocol())) {
+                       try {
+                               File tmp = File.createTempFile("tmp_", "_" + fileName);
+                               copyTestResource(fileName, tmp);
+                               return tmp;
+                       } catch (IOException err) {
+                               throw new RuntimeException("Cannot create temporary file", err);
+                       }
+               }
                try {
                        return new File(url.toURI());
-               } catch(URISyntaxException e) {
+               } catch (IllegalArgumentException e) {
+                       throw new IllegalArgumentException(e.getMessage() + " " + url);
+               } catch (URISyntaxException e) {
                        return new File(url.getPath());
                }
        }
 
+       public static void copyTestResource(String name, File dest)
+                       throws IOException {
+               URL url = cl().getResource(CLASSPATH_TO_RESOURCES + name);
+               if (url == null)
+                       throw new FileNotFoundException(name);
+               InputStream in = url.openStream();
+               try {
+                       FileOutputStream out = new FileOutputStream(dest);
+                       try {
+                               byte[] buf = new byte[4096];
+                               for (int n; (n = in.read(buf)) > 0;)
+                                       out.write(buf, 0, n);
+                       } finally {
+                               out.close();
+                       }
+               } finally {
+                       in.close();
+               }
+       }
+
        private static ClassLoader cl() {
                return JGitTestUtil.class.getClassLoader();
        }
index fb4b9e9f80be6354859cea6507cdf77f7cbedfac..0b03a2c4138da11d3fa3754d37ddc505e6e228ab 100644 (file)
@@ -66,11 +66,11 @@ public abstract class SampleDataRepositoryTestCase extends RepositoryTestCase {
                };
                final File packDir = new File(db.getObjectDatabase().getDirectory(), "pack");
                for (String n : packs) {
-                       copyFile(JGitTestUtil.getTestResourceFile(n + ".pack"), new File(packDir, n + ".pack"));
-                       copyFile(JGitTestUtil.getTestResourceFile(n + ".idx"), new File(packDir, n + ".idx"));
+                       JGitTestUtil.copyTestResource(n + ".pack", new File(packDir, n + ".pack"));
+                       JGitTestUtil.copyTestResource(n + ".idx", new File(packDir, n + ".idx"));
                }
 
-               copyFile(JGitTestUtil.getTestResourceFile("packed-refs"), new File(db
-                               .getDirectory(), "packed-refs"));
+               JGitTestUtil.copyTestResource("packed-refs",
+                               new File(db.getDirectory(), "packed-refs"));
        }
 }