[quick-junit-devel 3] テストメソッドに匿名クラスとテストが内部クラス

Back to archive index

Koichi Kobayashi koich****@impro*****
2005年 5月 31日 (火) 02:30:17 JST


小林 (koichik) です.
こちらの ML は初投稿になります.よろしくお願いします.

Quick JUnit プラグインをありがたく使わせて頂いています.
しかし,ほんの少しだけ気になる点があります.

Quick JUnit プラグインは,Java エディタでテストクラスのメソッドに
カーソルがあるとそのメソッドだけを実行 (またはデバッグ) してくれます.
しかし,そのメソッドの中の匿名クラスにカーソルがあるとテストクラス
全体を実行してしまいます.
その例.
----------------------------------------------------------------------
public class FooTest extends TestCase {
    public FooTest() {
    }

    public FooTest(String name) {
        super(name);
    }
    
    public void test1() throws Exception {
        new Runnable() {
            public void run() {
                assertEquals(1, new Foo().f());
            }
        }.run();
    }
    
    public void test2() throws Exception {
    }
}
----------------------------------------------------------------------
このようなテストクラスにおいて,test1() の中の匿名クラスにカーソルを
置いて「JUnit テスト」または「JUnit デバッグ」すると,テストメソッドが
二つとも実行されることが確認できます (テスト対象クラスは下の Foo です).
できれば匿名クラスの中にカーソルがあっても,そのメソッドだけを
実行できる方が望ましいと思います.


また,テストクラスが対象クラスの内部クラスになっている場合に
「JUnit テストケースを選択してください」となってしまいます.
その例.
----------------------------------------------------------------------
public class Foo {
    public int f() {
        return 1;
    }

    public static class Test extends TestCase {
        public Test() {
        }

        public Test(String name) {
            super(name);
        }

        public void test() throws Exception {
            assertEquals(1, new Foo().f());
        }
    }
}
----------------------------------------------------------------------
この内部クラスのどこにカーソルがあっても「JUnit テスト」できません.
このような場合にもテストを実行できる方が望ましいと思います.


以上 2 点についてパッチを作成してみたので取り込んで頂けると幸いです.
なお,大切な方の UML 弱者なのでパッチの作り方がこれでいいのかどうか
よく分かっていません.Eclipse が作ってくれたものをそのまま貼ってます.
不備があったらご指摘ください.


Index: JUnitLaunchAction.java
===================================================================
RCS file: /cvsroot/quick-junit/junit.extensions.eclipse.quick/src/junit/extensions/eclipse/quick/JUnitLaunchAction.java,v
retrieving revision 1.1
diff -u -r1.1 JUnitLaunchAction.java
--- JUnitLaunchAction.java	27 Mar 2005 07:19:06 -0000	1.1
+++ JUnitLaunchAction.java	30 May 2005 17:06:35 -0000
@@ -3,9 +3,7 @@
 import org.eclipse.debug.ui.ILaunchShortcut;
 import org.eclipse.jdt.core.IJavaElement;
 import org.eclipse.jdt.core.IType;
-import org.eclipse.jdt.core.ITypeHierarchy;
 import org.eclipse.jdt.core.JavaModelException;
-import org.eclipse.jdt.internal.junit.ui.JUnitPlugin;
 import org.eclipse.jdt.internal.ui.actions.SelectionConverter;
 import org.eclipse.jface.action.IAction;
 import org.eclipse.jface.viewers.ISelection;
@@ -24,28 +22,22 @@
     private IJavaElement getElementOfJavaEditor() throws JavaModelException {
         if (javaEditor == null)
             return null;
-        IJavaElement element = SelectionConverter.getElementAtOffset(javaEditor);
-        if (JavaElements.isTestMethod(element))
-            return element;
-        return JavaElements.getPrimaryTypeOf(element);
+        return SelectionConverter.getElementAtOffset(javaEditor);
     }
 
     private IJavaElement getTargetElement(IAction action) throws JavaModelException {
         IJavaElement element = getSelectedElement();
         if (element == null || element.getElementType() < IJavaElement.COMPILATION_UNIT)
             return element;
-        
+
+        IJavaElement testableElement = JavaElements.getTestMethodOrClass(element);
+        if (testableElement != null)
+            return testableElement;
+
         IType type = JavaElements.getPrimaryTypeOf(element);
         if (type == null)
             return element;
 
-        ITypeHierarchy superTypeHierarchy = type.newSupertypeHierarchy(null);
-        IType superTypes[] = superTypeHierarchy.getAllInterfaces();
-        for (int i = 0; i < superTypes.length; ++i) {
-            IType superType = superTypes[i];
-            if (superType.getFullyQualifiedName().equals(JUnitPlugin.TEST_INTERFACE_NAME))
-                return element;
-        }
         openInformation(action, Messages.getString("JUnitLaunchAction.notJUnitElement")); //$NON-NLS-1$
         return null;
     }
Index: JavaElements.java
===================================================================
RCS file: /cvsroot/quick-junit/junit.extensions.eclipse.quick/src/junit/extensions/eclipse/quick/JavaElements.java,v
retrieving revision 1.1
diff -u -r1.1 JavaElements.java
--- JavaElements.java	27 Mar 2005 07:19:06 -0000	1.1
+++ JavaElements.java	30 May 2005 17:06:35 -0000
@@ -6,7 +6,9 @@
 import org.eclipse.jdt.core.IMember;
 import org.eclipse.jdt.core.IMethod;
 import org.eclipse.jdt.core.IType;
+import org.eclipse.jdt.core.ITypeHierarchy;
 import org.eclipse.jdt.core.JavaModelException;
+import org.eclipse.jdt.internal.junit.ui.JUnitPlugin;
 
 
 public class JavaElements {
@@ -35,4 +37,34 @@
         }
         return cu != null ? cu.findPrimaryType() : null; 
     }
+    
+    public static boolean isTestClass(IJavaElement element) throws JavaModelException {
+        if (!(element instanceof IType))
+            return false;
+        IType type = (IType) element;
+        if (!type.isClass())
+            return false;
+        int flags = type.getFlags();
+        if (Flags.isAbstract(flags) || !Flags.isPublic(flags))
+            return false;
+        ITypeHierarchy superTypeHierarchy = type.newSupertypeHierarchy(null);
+        IType superTypes[] = superTypeHierarchy.getAllInterfaces();
+        for (int i = 0; i < superTypes.length; ++i) {
+            IType superType = superTypes[i];
+            if (superType.getFullyQualifiedName().equals(JUnitPlugin.TEST_INTERFACE_NAME))
+                return true;
+        }
+        return false;
+    }
+
+    public static IJavaElement getTestMethodOrClass(IJavaElement element) throws JavaModelException {
+        while (element != null) {
+            if (isTestMethod(element))
+                return element;
+            if (isTestClass(element))
+                return element;
+            element = element.getParent();
+        }
+        return null;
+    }
 }


-- 
<signature>
    <name>Koichi Kobayashi</name>
    <e-mail>koich****@impro*****</e-mail>
</signature>




Quick-junit-devel メーリングリストの案内
Back to archive index