Skip to content

Commit

Permalink
Add UI to select if JUnit3/4 should be enabled for JUnit Platform
Browse files Browse the repository at this point in the history
Currently one needs to edit the classpath directly what is a bit
inconvenient.

This adds a new checkbox displayed in case Junit 5 is used that allows
the user to choose if Junit3/4 compatibility layer (aka vintage engine)
should be used.
  • Loading branch information
laeubi committed May 31, 2024
1 parent 8903f4d commit 1f7024c
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ private static JUnitContainer getNewContainer(IPath containerPath, IClasspathAtt
entriesList.add(BuildPathSupport.getHamcrestCoreLibraryEntry());
break;
case JUNIT5:
boolean vintage = isVintage(attributes);
boolean vintage = JUnitCore.isVintage(attributes);
entriesList.add(BuildPathSupport.getJUnitJupiterApiLibraryEntry());
entriesList.add(BuildPathSupport.getJUnitJupiterEngineLibraryEntry());
entriesList.add(BuildPathSupport.getJUnitJupiterMigrationSupportLibraryEntry());
Expand Down Expand Up @@ -156,19 +156,6 @@ private static JUnitContainer getNewContainer(IPath containerPath, IClasspathAtt
return new JUnitContainer(containerPath, entries);
}


private static boolean isVintage(IClasspathAttribute[] attributes) {
if (attributes != null) {
for (IClasspathAttribute attribute : attributes) {
if (JUnitCore.VINTAGE_ATTRIBUTE.equals(attribute.getName())) {
return Boolean.parseBoolean(attribute.getValue());
}
}
}
// default is true for backward compat
return true;
}

private static boolean isValidJUnitContainerPath(IPath path) {
return path != null && path.segmentCount() == 2 && JUnitCore.JUNIT_CONTAINER_ID.equals(path.segment(0));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Status;

import org.eclipse.jdt.core.IClasspathAttribute;
import org.eclipse.jdt.core.IClasspathContainer;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.IType;
Expand Down Expand Up @@ -234,4 +235,24 @@ public static ITestRunSession importTestRunSession(final String url, IProgressMo
return null;
}
}

/**
* @param attributes the attributes to check here, can be <code>null</code>
* @return <code>true</code> if vintage engine is enabled it <code>false</code> if not
* @since 3.13
*/
public static boolean isVintage(IClasspathAttribute[] attributes) {
if (attributes != null) {
for (IClasspathAttribute attribute : attributes) {
if (JUnitCore.VINTAGE_ATTRIBUTE.equals(attribute.getName())) {
String value= attribute.getValue();
if (value != null && !value.isBlank()) {
return Boolean.parseBoolean(value);
}
}
}
}
// default is true for backward compat
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,15 @@
import org.eclipse.jdt.junit.JUnitCore;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.program.Program;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Link;
import org.eclipse.swt.widgets.Text;

import org.eclipse.core.runtime.IPath;
Expand All @@ -31,6 +35,8 @@

import org.eclipse.jface.layout.PixelConverter;

import org.eclipse.jdt.core.IAccessRule;
import org.eclipse.jdt.core.IClasspathAttribute;
import org.eclipse.jdt.core.IClasspathContainer;
import org.eclipse.jdt.core.IClasspathEntry;
import org.eclipse.jdt.core.IJavaProject;
Expand Down Expand Up @@ -60,6 +66,8 @@ public class JUnitContainerWizardPage extends NewElementWizardPage implements IC
private Combo fVersionCombo;
private Text fResolvedPath;
private Text fResolvedSourcePath;
private Button fVintage;
private Link fLink;

public JUnitContainerWizardPage() {
super("JUnitContainerPage"); //$NON-NLS-1$
Expand Down Expand Up @@ -119,9 +127,17 @@ public void createControl(Composite parent) {
label.setLayoutData(new GridData(GridData.FILL, GridData.CENTER, false, false, 1, 1));
label.setText(JUnitMessages.JUnitContainerWizardPage_combo_label);

fVersionCombo= new Combo(composite, SWT.READ_ONLY);
Composite comboComposite= new Composite(composite, SWT.NONE);
comboComposite.setLayout(new GridLayout(3, false));
fVersionCombo= new Combo(comboComposite, SWT.READ_ONLY);
fVersionCombo.setItems(JUnitMessages.JUnitContainerWizardPage_option_junit3, JUnitMessages.JUnitContainerWizardPage_option_junit4, JUnitMessages.JUnitContainerWizardPage_option_junit5);
fVersionCombo.setFont(composite.getFont());
fVersionCombo.setFont(comboComposite.getFont());
fVintage = new Button(comboComposite, SWT.CHECK);
fLink= new Link(comboComposite, SWT.NONE);
fLink.setText(JUnitMessages.JUnitContainerWizardPage_enableVintage);
fLink.setToolTipText(JUnitMessages.JUnitContainerWizardPage_enableVintage_tooltip);
fVintage.setToolTipText(JUnitMessages.JUnitContainerWizardPage_enableVintage_tooltip);
fLink.addSelectionListener(SelectionListener.widgetSelectedAdapter(e->Program.launch("https://junit.org/junit5/docs/current/user-guide/#overview-what-is-junit-5"))); //$NON-NLS-1$

GridData data= new GridData(GridData.BEGINNING, GridData.CENTER, false, false, 1, 1);
data.widthHint= converter.convertWidthInCharsToPixels(15);
Expand All @@ -133,8 +149,10 @@ public void createControl(Composite parent) {
fVersionCombo.select(1);
} else {
fVersionCombo.select(2);
fVintage.setSelection(fContainerEntryResult!=null && JUnitCore.isVintage(fContainerEntryResult.getExtraAttributes()));
}
fVersionCombo.addModifyListener(e -> doSelectionChanged());
fVintage.addSelectionListener(SelectionListener.widgetSelectedAdapter(e->doSelectionChanged()));

label= new Label(composite, SWT.NONE);
label.setFont(composite.getFont());
Expand Down Expand Up @@ -178,17 +196,27 @@ protected void doSelectionChanged() {

IClasspathEntry libEntry;
IPath containerPath;
IClasspathAttribute[] extraAttributes;
if (fVersionCombo != null && fVersionCombo.getSelectionIndex() == 2) {
containerPath= JUnitCore.JUNIT5_CONTAINER_PATH;
libEntry= BuildPathSupport.getJUnitJupiterApiLibraryEntry();
fVintage.setVisible(true);
fLink.setVisible(true);
extraAttributes= new IClasspathAttribute[] {JavaCore.newClasspathAttribute(JUnitCore.VINTAGE_ATTRIBUTE, Boolean.toString(fVintage.getSelection()))};
} else if (fVersionCombo != null && fVersionCombo.getSelectionIndex() == 1) {
containerPath= JUnitCore.JUNIT4_CONTAINER_PATH;
libEntry= BuildPathSupport.getJUnit4LibraryEntry();
fVintage.setVisible(false);
fLink.setVisible(false);
extraAttributes= new IClasspathAttribute[0];
} else {
containerPath= JUnitCore.JUNIT3_CONTAINER_PATH;
libEntry= BuildPathSupport.getJUnit3LibraryEntry();
if (libEntry == null)
libEntry= BuildPathSupport.getJUnit4as3LibraryEntry(); // JUnit 4 includes most of JUnit 3, so let's cheat
fVintage.setVisible(false);
fLink.setVisible(false);
extraAttributes= new IClasspathAttribute[0];
}

if (libEntry == null) {
Expand All @@ -202,7 +230,8 @@ protected void doSelectionChanged() {
status.setWarning(JUnitMessages.JUnitContainerWizardPage_warning_java8_required);
}
}
fContainerEntryResult= JavaCore.newContainerEntry(containerPath);
fContainerEntryResult= JavaCore.newContainerEntry(containerPath, new IAccessRule[0],extraAttributes,
false);

if (fResolvedPath != null && !fResolvedPath.isDisposed()) {
if (libEntry != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ public final class JUnitMessages extends NLS {

public static String JUnitContainerWizardPage_option_junit5;

public static String JUnitContainerWizardPage_enableVintage;

public static String JUnitContainerWizardPage_enableVintage_tooltip;

public static String JUnitClasspathFixProcessor_progress_desc;

public static String JUnitContainerWizardPage_resolved_label;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,8 @@ JUnitContainerWizardPage_wizard_title=JUnit Library
JUnitContainerWizardPage_option_junit3=JUnit 3
JUnitContainerWizardPage_option_junit4=JUnit 4
JUnitContainerWizardPage_option_junit5=JUnit 5
JUnitContainerWizardPage_enableVintage=Enable <a>JUnit 3/4 Support</a>
JUnitContainerWizardPage_enableVintage_tooltip=Enable the JUnit Vintage TestEngine for running JUnit 3 and JUnit 4 based tests on the JUnit Platform.
JUnitContainerWizardPage_resolved_label=Current location:
JUnitLaunchShortcut_message_launchfailed=Launching of JUnit tests unexpectedly failed. Check log for details.
JUnitLaunchConfigurationTab_error_noContainer=No project, source folder or package is specified
Expand Down

0 comments on commit 1f7024c

Please sign in to comment.