首页 > 解决方案 > 在 Eclipse 中创建自定义 JUnit 启动配置

问题描述

我正在尝试创建自定义 JUnit 插件测试启动配置。

我已经扩展了类JUnitWorkbenchLaunchShortcut并重写了createLaunchConfiguration()编写自定义配置的方法。

@Override
protected ILaunchConfigurationWorkingCopy createLaunchConfiguration(final IJavaElement element)
        throws CoreException {
    ILaunchConfigurationWorkingCopy configuration = super.createLaunchConfiguration(element);
    configuration.setAttribute(IJavaLaunchConfigurationConstants.ATTR_VM_ARGUMENTS,
            XMX2048M_XX_USE_PARALLEL_GC_DOSGI_FRAMEWORK_EXTENSIONS_ORG_ECLIPSE_EQUINOX_WEAVING_HOOK_DEQUINOX_USE_DS_TRUE);
    configuration.setAttribute(IPDELauncherConstants.USE_PRODUCT, false);
    configuration.setAttribute(IPDELauncherConstants.APPLICATION, "org.eclipse.ui.ide.workbench");
    configuration.setAttribute(IPDELauncherConstants.USE_DEFAULT, false);
    configuration.setAttribute(IPDELauncherConstants.AUTOMATIC_ADD, false);
    addDependencies(configuration);
    return configuration;
}


private void addDependencies(final ILaunchConfigurationWorkingCopy configuration) throws CoreException {
    PluginModelNameBuffer wBuffer = new PluginModelNameBuffer();
    PluginModelNameBuffer tBuffer = new PluginModelNameBuffer();
    Set<IPluginModelBase> addedModels = new HashSet<>();

    String projectName = configuration.getAttribute(IJavaLaunchConfigurationConstants.ATTR_PROJECT_NAME, "");
    IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(projectName);
    IPluginModelBase model = PluginRegistry.findModel(project);
    wBuffer.add(model);
    addedModels.add(model);

    IPluginModelBase[] externalModels = PluginRegistry.getExternalModels();
    for (IPluginModelBase externalModel : externalModels) {
        String id = externalModel.getPluginBase().getId();
        if (id != null) {
            switch (id) {
            case "org.eclipse.ui.ide.application":
            case "org.eclipse.equinox.ds":
            case "org.eclipse.equinox.event":
                tBuffer.add(externalModel);
                addedModels.add(externalModel);
                break;
            default:
                break;
            }
        }
    }

    TreeSet<String> checkedWorkspace = new TreeSet<>();
    IPluginModelBase[] workspaceModels = PluginRegistry.getWorkspaceModels();
    for (IPluginModelBase workspaceModel : workspaceModels) {
        checkedWorkspace.add(workspaceModel.getPluginBase().getId());
    }

    EclipsePluginValidationOperation eclipsePluginValidationOperation = new EclipsePluginValidationOperation(
            configuration);
    eclipsePluginValidationOperation.run(null);

    while (eclipsePluginValidationOperation.hasErrors()) {
        Set<String> additionalIds = DependencyManager.getDependencies(addedModels.toArray(), true, null);
        if (additionalIds.isEmpty()) {
            break;
        }
        additionalIds.stream().map(PluginRegistry::findEntry).filter(Objects::nonNull).map(ModelEntry::getModel)
                .forEach(addedModels::add);

        for (String id : additionalIds) {
            IPluginModelBase plugin = findPlugin(id);
            if (!plugin.getPluginBase().getId().endsWith("tests")) {
                if (checkedWorkspace.contains(plugin.getPluginBase().getId())) {
                    wBuffer.add(plugin);
                } else {
                    tBuffer.add(plugin);
                }
            }
        }
        eclipsePluginValidationOperation.run(null);
    }

    configuration.setAttribute(IPDELauncherConstants.SELECTED_WORKSPACE_PLUGINS, wBuffer.toString());
    configuration.setAttribute(IPDELauncherConstants.SELECTED_TARGET_PLUGINS, tBuffer.toString());
}

protected IPluginModelBase findPlugin(final String id) {
    ModelEntry entry = PluginRegistry.findEntry(id);
    if (entry != null) {
        return entry.getModel();
      }
    return null;
   }
}

启动配置完全按照其应有的方式创建,但在打开工作台时插件测试不会运行。仅当打开运行配置对话框并单击其中的run按钮时才会执行测试。

标签: javaeclipseeclipse-plugineclipse-rcpeclipse-jdt

解决方案


推荐阅读