diff options
author | Robin Rosenberg <robin.rosenberg@dewire.com> | 2010-12-31 11:44:54 +0100 |
---|---|---|
committer | Shawn O. Pearce <spearce@spearce.org> | 2010-12-31 14:00:05 -0800 |
commit | d9e07a574a946693e491668e0d94619bc5d886a5 (patch) | |
tree | 5bce4e9d47090d6b427986de4db30be17cc99c6d /org.eclipse.jgit.junit | |
parent | 7cf8b8812f7baa1636c138113f4ed015eed8cc31 (diff) | |
download | jgit-d9e07a574a946693e491668e0d94619bc5d886a5.tar.gz jgit-d9e07a574a946693e491668e0d94619bc5d886a5.zip |
Convert all JGit unit tests to JUnit 4
Eclipse has some problem re-running single JUnit tests if
the tests are in Junit 3 format, but the JUnit 4 launcher
is used. This was quite unnecessary and the move was not
completed. We still have no JUnit4 test.
This completes the extermination of JUnit3. Most of the
work was global searce/replace using regular expression,
followed by numerous invocarions of quick-fix and organize
imports and verification that we had the same number of
tests before and after.
- Annotations were introduced.
- All references to JUnit3 classes removed
- Half-good replacement for getting the test name. This was
needed to make the TestRngs work. The initialization of
TestRngs was also made lazily since we can not longer find
out the test name in runtime in the @Before methods.
- Renamed test classes to end with Test, with the exception
of TestTranslateBundle, which fails from Maven
- Moved JGitTestUtil to the junit support bundle
Change-Id: Iddcd3da6ca927a7be773a9c63ebf8bb2147e2d13
Signed-off-by: Robin Rosenberg <robin.rosenberg@dewire.com>
Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
Diffstat (limited to 'org.eclipse.jgit.junit')
4 files changed, 153 insertions, 31 deletions
diff --git a/org.eclipse.jgit.junit/META-INF/MANIFEST.MF b/org.eclipse.jgit.junit/META-INF/MANIFEST.MF index 63f0403994..b5de6baf7a 100644 --- a/org.eclipse.jgit.junit/META-INF/MANIFEST.MF +++ b/org.eclipse.jgit.junit/META-INF/MANIFEST.MF @@ -7,8 +7,7 @@ Bundle-Localization: plugin Bundle-Vendor: %provider_name Bundle-ActivationPolicy: lazy Bundle-RequiredExecutionEnvironment: J2SE-1.5 -Import-Package: junit.framework;version="[4.0.0,5.0.0)", - org.eclipse.jgit.diff;version="[0.11.0,0.12.0)", +Import-Package: org.eclipse.jgit.diff;version="[0.11.0,0.12.0)", org.eclipse.jgit.dircache;version="[0.11.0,0.12.0)", org.eclipse.jgit.errors;version="[0.11.0,0.12.0)", org.eclipse.jgit.fnmatch;version="[0.11.0,0.12.0)", @@ -24,6 +23,7 @@ Import-Package: junit.framework;version="[4.0.0,5.0.0)", org.eclipse.jgit.treewalk;version="[0.11.0,0.12.0)", org.eclipse.jgit.treewalk.filter;version="[0.11.0,0.12.0)", org.eclipse.jgit.util;version="[0.11.0,0.12.0)", - org.eclipse.jgit.util.io;version="[0.11.0,0.12.0)" + org.eclipse.jgit.util.io;version="[0.11.0,0.12.0)", + org.junit;version="[4.0.0,5.0.0)" Export-Package: org.eclipse.jgit.junit;version="0.11.0" Require-Bundle: com.jcraft.jsch;bundle-version="[0.1.37,0.2.0)" 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 new file mode 100644 index 0000000000..38a451fbfd --- /dev/null +++ b/org.eclipse.jgit.junit/src/org/eclipse/jgit/junit/JGitTestUtil.java @@ -0,0 +1,130 @@ +/* + * Copyright (C) 2008-2009, Google Inc. + * Copyright (C) 2008, Imran M Yousuf <imyousuf@smartitengineering.com> + * Copyright (C) 2008, Jonas Fonseca <fonseca@diku.dk> + * and other copyright owners as documented in the project's IP log. + * + * This program and the accompanying materials are made available + * under the terms of the Eclipse Distribution License v1.0 which + * accompanies this distribution, is reproduced below, and is + * available at http://www.eclipse.org/org/documents/edl-v10.php + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or + * without modification, are permitted provided that the following + * conditions are met: + * + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * - Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following + * disclaimer in the documentation and/or other materials provided + * with the distribution. + * + * - Neither the name of the Eclipse Foundation, Inc. nor the + * names of its contributors may be used to endorse or promote + * products derived from this software without specific prior + * written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND + * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +package org.eclipse.jgit.junit; + +import java.io.File; +import java.lang.reflect.Method; +import java.net.URISyntaxException; +import java.net.URL; + +import org.eclipse.jgit.util.RawParseUtils; +import org.junit.Assert; +import org.junit.Test; + +public abstract class JGitTestUtil { + public static final String CLASSPATH_TO_RESOURCES = "org/eclipse/jgit/test/resources/"; + + private JGitTestUtil() { + throw new UnsupportedOperationException(); + } + + public static String getName() { + GatherStackTrace stack; + try { + throw new GatherStackTrace(); + } catch (GatherStackTrace wanted) { + stack = wanted; + } + + try { + for (StackTraceElement stackTrace : stack.getStackTrace()) { + String className = stackTrace.getClassName(); + String methodName = stackTrace.getMethodName(); + Method method; + try { + method = Class.forName(className) // + .getMethod(methodName, (Class[]) null); + } catch (NoSuchMethodException e) { + // could be private, i.e. not a test method + // could have arguments, not handled + continue; + } + + Test annotation = method.getAnnotation(Test.class); + if (annotation != null) + return methodName; + } + } catch (ClassNotFoundException shouldNeverOccur) { + // Fall through and crash. + } + + throw new AssertionError("Cannot determine name of current test"); + } + + @SuppressWarnings("serial") + private static class GatherStackTrace extends Exception { + // Thrown above to collect the stack frame. + } + + public static void assertEquals(byte[] exp, byte[] act) { + Assert.assertEquals(s(exp), s(act)); + } + + private static String s(byte[] raw) { + return RawParseUtils.decode(raw); + } + + public static File getTestResourceFile(final String fileName) { + if (fileName == null || fileName.length() <= 0) { + return null; + } + final URL url = cl().getResource(CLASSPATH_TO_RESOURCES + fileName); + if (url == null) { + // If URL is null then try to load it as it was being + // loaded previously + return new File("tst", fileName); + } + try { + return new File(url.toURI()); + } catch(URISyntaxException e) { + return new File(url.getPath()); + } + } + + private static ClassLoader cl() { + return JGitTestUtil.class.getClassLoader(); + } +} diff --git a/org.eclipse.jgit.junit/src/org/eclipse/jgit/junit/LocalDiskRepositoryTestCase.java b/org.eclipse.jgit.junit/src/org/eclipse/jgit/junit/LocalDiskRepositoryTestCase.java index dd143826ec..3917c487d7 100644 --- a/org.eclipse.jgit.junit/src/org/eclipse/jgit/junit/LocalDiskRepositoryTestCase.java +++ b/org.eclipse.jgit.junit/src/org/eclipse/jgit/junit/LocalDiskRepositoryTestCase.java @@ -45,6 +45,9 @@ package org.eclipse.jgit.junit; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.fail; + import java.io.File; import java.io.FileOutputStream; import java.io.IOException; @@ -57,10 +60,6 @@ import java.util.List; import java.util.Map; import java.util.concurrent.TimeUnit; -import junit.framework.Assert; -import junit.framework.TestCase; - -import org.eclipse.jgit.lib.AnyObjectId; import org.eclipse.jgit.lib.Constants; import org.eclipse.jgit.lib.PersonIdent; import org.eclipse.jgit.lib.Repository; @@ -72,6 +71,8 @@ import org.eclipse.jgit.storage.file.WindowCacheConfig; import org.eclipse.jgit.util.FS; import org.eclipse.jgit.util.IO; import org.eclipse.jgit.util.SystemReader; +import org.junit.After; +import org.junit.Before; /** * JUnit TestCase with specialized support for temporary local repository. @@ -90,7 +91,7 @@ import org.eclipse.jgit.util.SystemReader; * a test, or tests may fail altogether if there is insufficient file * descriptors or address space for the test process. */ -public abstract class LocalDiskRepositoryTestCase extends TestCase { +public abstract class LocalDiskRepositoryTestCase { private static Thread shutdownHook; private static int testCount; @@ -110,9 +111,8 @@ public abstract class LocalDiskRepositoryTestCase extends TestCase { private MockSystemReader mockSystemReader; - @Override - protected void setUp() throws Exception { - super.setUp(); + @Before + public void setUp() throws Exception { synchronized(this) { if (shutdownHook == null) { @@ -131,7 +131,7 @@ public abstract class LocalDiskRepositoryTestCase extends TestCase { Runtime.getRuntime().addShutdownHook(shutdownHook); } } - recursiveDelete(testName(), trash, true, false); + recursiveDelete(testId(), trash, true, false); mockSystemReader = new MockSystemReader(); mockSystemReader.userGitConfig = new FileBasedConfig(new File(trash, @@ -174,8 +174,8 @@ public abstract class LocalDiskRepositoryTestCase extends TestCase { return stringBuilder.toString(); } - @Override - protected void tearDown() throws Exception { + @After + public void tearDown() throws Exception { RepositoryCache.clear(); for (Repository r : toClose) r.close(); @@ -188,8 +188,7 @@ public abstract class LocalDiskRepositoryTestCase extends TestCase { if (useMMAP) System.gc(); - recursiveDelete(testName(), trash, false, true); - super.tearDown(); + recursiveDelete(testId(), trash, false, true); } /** Increment the {@link #author} and {@link #committer} times. */ @@ -210,7 +209,7 @@ public abstract class LocalDiskRepositoryTestCase extends TestCase { * the recursively directory to delete, if present. */ protected void recursiveDelete(final File dir) { - recursiveDelete(testName(), dir, false, true); + recursiveDelete(testId(), dir, false, true); } private static boolean recursiveDelete(final String testName, @@ -414,10 +413,6 @@ public abstract class LocalDiskRepositoryTestCase extends TestCase { return new String(body, 0, body.length, "UTF-8"); } - protected static void assertEquals(AnyObjectId exp, AnyObjectId act) { - Assert.assertEquals(exp, act); - } - private static String[] toEnvArray(final Map<String, String> env) { final String[] envp = new String[env.size()]; int i = 0; @@ -431,7 +426,7 @@ public abstract class LocalDiskRepositoryTestCase extends TestCase { return new HashMap<String, String>(System.getenv()); } - private String testName() { - return getClass().getName() + "." + getName(); + private String testId() { + return getClass().getName() + "." + testCount; } } diff --git a/org.eclipse.jgit.junit/src/org/eclipse/jgit/junit/TestRepository.java b/org.eclipse.jgit.junit/src/org/eclipse/jgit/junit/TestRepository.java index 71446a0dce..eea7811c09 100644 --- a/org.eclipse.jgit.junit/src/org/eclipse/jgit/junit/TestRepository.java +++ b/org.eclipse.jgit.junit/src/org/eclipse/jgit/junit/TestRepository.java @@ -43,6 +43,8 @@ package org.eclipse.jgit.junit; +import static org.junit.Assert.*; + import java.io.BufferedOutputStream; import java.io.File; import java.io.FileOutputStream; @@ -56,9 +58,6 @@ import java.util.HashSet; import java.util.List; import java.util.Set; -import junit.framework.Assert; -import junit.framework.AssertionFailedError; - import org.eclipse.jgit.dircache.DirCache; import org.eclipse.jgit.dircache.DirCacheBuilder; import org.eclipse.jgit.dircache.DirCacheEditor; @@ -277,12 +276,10 @@ public class TestRepository<R extends Repository> { * @param path * the path to find the entry of. * @return the parsed object entry at this path, never null. - * @throws AssertionFailedError - * if the path does not exist in the given tree. * @throws Exception */ public RevObject get(final RevTree tree, final String path) - throws AssertionFailedError, Exception { + throws Exception { final TreeWalk tw = new TreeWalk(pool.getObjectReader()); tw.setFilter(PathFilterGroup.createFromStrings(Collections .singleton(path))); @@ -296,7 +293,7 @@ public class TestRepository<R extends Repository> { final FileMode entmode = tw.getFileMode(0); return pool.lookupAny(entid, entmode.getObjectType()); } - Assert.fail("Can't find " + path + " in tree " + tree.name()); + fail("Can't find " + path + " in tree " + tree.name()); return null; // never reached. } @@ -598,7 +595,7 @@ public class TestRepository<R extends Repository> { md.update(Constants.encodeASCII(bin.length)); md.update((byte) 0); md.update(bin); - Assert.assertEquals(id, ObjectId.fromRaw(md.digest())); + assertEquals(id, ObjectId.fromRaw(md.digest())); } /** |