summaryrefslogtreecommitdiffstats
path: root/org.eclipse.jgit.junit
diff options
context:
space:
mode:
authorShawn Pearce <sop@google.com>2013-11-01 10:49:06 -0600
committerGerrit Code Review @ Eclipse.org <gerrit@eclipse.org>2013-11-24 05:27:00 -0500
commit573d36a4647c79ba0d2350f65c9ad8ac889e17ee (patch)
treebcd100af3881600612b4aadf29ecb828880f7e96 /org.eclipse.jgit.junit
parent7f7dd171df7c02d2e9b95c640909cc11acda2644 (diff)
downloadjgit-573d36a4647c79ba0d2350f65c9ad8ac889e17ee.tar.gz
jgit-573d36a4647c79ba0d2350f65c9ad8ac889e17ee.zip
Support running from JARs in JGitTestUtil
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
Diffstat (limited to 'org.eclipse.jgit.junit')
-rw-r--r--org.eclipse.jgit.junit/src/org/eclipse/jgit/junit/JGitTestUtil.java35
-rw-r--r--org.eclipse.jgit.junit/src/org/eclipse/jgit/junit/SampleDataRepositoryTestCase.java8
2 files changed, 38 insertions, 5 deletions
diff --git a/org.eclipse.jgit.junit/src/org/eclipse/jgit/junit/JGitTestUtil.java b/org.eclipse.jgit.junit/src/org/eclipse/jgit/junit/JGitTestUtil.java
index c6fe4e4d91..1079d98439 100644
--- a/org.eclipse.jgit.junit/src/org/eclipse/jgit/junit/JGitTestUtil.java
+++ b/org.eclipse.jgit.junit/src/org/eclipse/jgit/junit/JGitTestUtil.java
@@ -46,8 +46,10 @@
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();
}
diff --git a/org.eclipse.jgit.junit/src/org/eclipse/jgit/junit/SampleDataRepositoryTestCase.java b/org.eclipse.jgit.junit/src/org/eclipse/jgit/junit/SampleDataRepositoryTestCase.java
index fb4b9e9f80..0b03a2c413 100644
--- a/org.eclipse.jgit.junit/src/org/eclipse/jgit/junit/SampleDataRepositoryTestCase.java
+++ b/org.eclipse.jgit.junit/src/org/eclipse/jgit/junit/SampleDataRepositoryTestCase.java
@@ -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"));
}
}