JUnit如何找到正在测试的eclipse插件?

时间:2010-07-29 02:53:29

标签: junit eclipse-plugin eclipse-jdt

我正在编写一个插件(ClassRefactoringPlugin)来检查Eclipse 3.6.1中的源代码。该插件包含一个CallData类,该类检查Java源文件并确定从使用JDT操作的方法调用哪些Java元素。我为这个类写了一个JUnit 4测试,它也存在于ClassRefactoringPlugin项目中。当我把它作为JUnit插件测试运行时,我得到了:

Java Model Exception: Java Model Status [ClassRefactoringPlugin does not exist]

我做错了什么?配置指定使用所有工作空间和已启用的目标插件启动,并且ClassRefactoringPlugin位于我的dropins目录中。 (虽然生成的工作区不应该识别项目的插件版本吗?)

这是堆栈跟踪:

!MESSAGE CallData.calculateCalledMethods: Java Model Exception: Java Model Status [ClassRefactoringPlugin does not exist]
Java Model Exception: Java Model Status [ClassRefactoringPlugin does not exist]
        at org.eclipse.jdt.internal.core.JavaElement.newJavaModelException(JavaElement.java:502)
        at org.eclipse.jdt.internal.core.Openable.generateInfos(Openable.java:246)
        at org.eclipse.jdt.internal.core.Openable.openAncestors(Openable.java:504)
        at org.eclipse.jdt.internal.core.Openable.generateInfos(Openable.java:240)
        at org.eclipse.jdt.internal.core.Openable.openAncestors(Openable.java:504)
        at org.eclipse.jdt.internal.core.Openable.generateInfos(Openable.java:240)
        at org.eclipse.jdt.internal.core.Openable.openAncestors(Openable.java:504)
        at org.eclipse.jdt.internal.core.CompilationUnit.openAncestors(CompilationUnit.java:1170)
        at org.eclipse.jdt.internal.core.Openable.generateInfos(Openable.java:240)
        at org.eclipse.jdt.internal.core.SourceRefElement.generateInfos(SourceRefElement.java:107)
        at org.eclipse.jdt.internal.core.JavaElement.openWhenClosed(JavaElement.java:515)
        at org.eclipse.jdt.internal.core.JavaElement.getElementInfo(JavaElement.java:252)
        at org.eclipse.jdt.internal.core.JavaElement.getElementInfo(JavaElement.java:238)
        at org.eclipse.jdt.internal.core.JavaElement.getChildren(JavaElement.java:193)
        at org.eclipse.jdt.internal.core.JavaElement.getChildrenOfType(JavaElement.java:207)
        at org.eclipse.jdt.internal.core.SourceType.getMethods(SourceType.java:403)
        at nz.ac.vuw.ecs.kcassell.utils.EclipseSearchUtils.addDesiredMethods(EclipseSearchUtils.java:333)
        at nz.ac.vuw.ecs.kcassell.utils.EclipseSearchUtils.getMethods(EclipseSearchUtils.java:210)
        at nz.ac.vuw.ecs.kcassell.callgraph.CallData.collectMethodCallData(CallData.java:203)
        at nz.ac.vuw.ecs.kcassell.callgraph.CallData.calculateCalledMethods(CallData.java:176)
        at nz.ac.vuw.ecs.kcassell.callgraph.CallData.collectCallData(CallData.java:151)
        at nz.ac.vuw.ecs.kcassell.callgraph.CallDataTest.testCollectCallData(CallDataTest.java:67)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at junit.framework.TestCase.runTest(TestCase.java:168)
        at junit.framework.TestCase.runBare(TestCase.java:134)
        at junit.framework.TestResult$1.protect(TestResult.java:110)
        at junit.framework.TestResult.runProtected(TestResult.java:128)
        at junit.framework.TestResult.run(TestResult.java:113)
        at junit.framework.TestCase.run(TestCase.java:124)
        at junit.framework.TestSuite.runTest(TestSuite.java:232)
        at junit.framework.TestSuite.run(TestSuite.java:227)
        at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:83)
        at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:49)
        at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
        at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
        at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
        at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
        at org.eclipse.pde.internal.junit.runtime.RemotePluginTestRunner.main(RemotePluginTestRunner.java:62)
        at org.eclipse.pde.internal.junit.runtime.UITestApplication$1.run(UITestApplication.java:116)
        at org.eclipse.swt.widgets.RunnableLock.run(RunnableLock.java:35)
        at org.eclipse.swt.widgets.Synchronizer.runAsyncMessages(Synchronizer.java:134)
        at org.eclipse.swt.widgets.Display.runAsyncMessages(Display.java:3515)
        at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3164)
        at org.eclipse.ui.internal.Workbench.runEventLoop(Workbench.java:2640)
        at org.eclipse.ui.internal.Workbench.runUI(Workbench.java:2604)
        at org.eclipse.ui.internal.Workbench.access$4(Workbench.java:2438)
        at org.eclipse.ui.internal.Workbench$7.run(Workbench.java:671)
        at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:332)
        at org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Workbench.java:664)
        at org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.java:149)
        at org.eclipse.ui.internal.ide.application.IDEApplication.start(IDEApplication.java:115)
        at org.eclipse.pde.internal.junit.runtime.UITestApplication.start(UITestApplication.java:47)
        at org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:196)
        at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:110)
        at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:79)
        at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:369)
        at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:179)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:619)
        at org.eclipse.equinox.launcher.Main.basicRun(Main.java:574)
        at org.eclipse.equinox.launcher.Main.run(Main.java:1407)
        at org.eclipse.equinox.launcher.Main.main(Main.java:1383)

我想知道错误信息是否可能是红鲱鱼。拨打电话时发生错误  IMethod[] methods = type.getMethods();

如果我在那里设置断点并在调试器的Variables视图中查看type,我看到:

CallDataTest (not open) [in CallDataTest.java [in nz.ac.vuw.ecs.kcassell.callgraph [in test [in ClassRefactoringPlugin]]]]

我想知道我是否省略了一些重要的初步步骤,以便让项目可供审查。首先,我尝试激活工作台,如下所示:

public static void activateWorkbench() {
    // possible for PlatformUI.getWorkbench to throw an IllegalStateException
    // if the workbench is not yet started e.g createAndRunWorkbench() has not yet been called
    IWorkbench workbench = PlatformUI.getWorkbench();
    IWorkbenchWindow workbenchWindow =
        workbench.getActiveWorkbenchWindow();
    workbenchWindow.getActivePage();
}

然后我尝试使用其句柄获取类型:

protected IType iType = EclipseUtils.getTypeFromHandle(
        "=ClassRefactoringPlugin/test<nz.ac.vuw.ecs.kcassell.callgraph{CallDataTest.java[CallDataTest");

public static IType getTypeFromHandle(String handle) {
IType type = null;
IJavaElement element = JavaCore.create(handle);
if (element == null) {
    System.err.println("  No element created from " + handle);
} else if (element instanceof IType) {
    type = (IType) element;
}
    return type;
}

我是插件开发的新手,所以非常感谢任何帮助。

干杯, 基思

2 个答案:

答案 0 :(得分:1)

我有点不确定你在这里想要做什么,但看起来你正试图为你的插件编写一个JUnit测试。这是对的吗?

根据您正在创建的java元素句柄标识符,您的工作区中应该有一个名为ClassRefactoringPlugin的项目,其中包含一个名为test的源文件夹和一个名为CallDataTest的java类在名为nz.ac.vuw.ecs.kcassell.callgraph的包中。

我猜这不是这种情况,你想在你刚创建的插件中引用一个类文件。

在执行任何操作之前,您需要将项目导入测试工作区,设置其类路径,然后使用句柄标识符访问其内容。

我可能误解了您要做的事情,但看起来您似乎正在尝试访问工作区中不存在的java文件。如果您可以确认这是您想要做的,我可以为您指出一些完全符合您要求的测试代码。


有一些开源项目正是这样做的,你可以借用一些源代码来达到你的目的。我熟悉的项目是Groovy-Eclipse http://groovy.codehaus.org/Eclipse+Plugin(因为我是该项目的负责人)。

以下是我们用于创建和管理测试项目的类的链接:

https://svn.codehaus.org/groovy/eclipse/trunk/ide-test/org.codehaus.groovy.eclipse.tests/src/org/codehaus/groovy/eclipse/test/TestProject.java

您可以将部分或全部代码用于自己的测试。请记住在每次测试结束时删除所有项目。

答案 1 :(得分:0)

由于不同的原因出现了类似的错误。重命名项目中的目标包后,我的错误开始了。在我从paskster尝试解决方案没有成功之后,我开始挖掘'。

实际问题是从清单文件重命名包名称无法按预期工作。我认为这会改变一切的地方。但是,旧的包名仍然在测试目录中的清单文件中,并且在资源的某些视图中。手动重命名并重建项目后,错误消失了。

这是另一个问题的公认答案: Java Model Exception: Java Model Status [gen [in MyApp] does not exist] after Eclipse Android project Clean

由于这个问题的拥有者将这个答案作为一个带有这个问题链接的解决方案投票,我想我也会在这里发布。