-
Notifications
You must be signed in to change notification settings - Fork 82
/
NewTestSuiteWizardPage.java
813 lines (715 loc) · 28.5 KB
/
NewTestSuiteWizardPage.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
/*******************************************************************************
* Copyright (c) 2000, 2022 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* IBM Corporation - initial API and implementation
* Johannes Utzig <mail@jutzig.de> - [JUnit] Update test suite wizard for JUnit 4: @RunWith(Suite.class)... - https://bugs.eclipse.org/155828
* Red Hat Inc. - Update test suite wizard for Junit 5 (@Suite suport)
*******************************************************************************/
package org.eclipse.jdt.junit.wizards;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.CheckboxTableViewer;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.Document;
import org.eclipse.jface.text.IDocument;
import org.eclipse.ui.PlatformUI;
import org.eclipse.jdt.core.IAnnotation;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.IMethod;
import org.eclipse.jdt.core.IPackageFragment;
import org.eclipse.jdt.core.IPackageFragmentRoot;
import org.eclipse.jdt.core.ISourceRange;
import org.eclipse.jdt.core.IType;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.internal.core.manipulation.StubUtility;
import org.eclipse.jdt.internal.corext.util.JavaConventionsUtil;
import org.eclipse.jdt.internal.junit.JUnitCorePlugin;
import org.eclipse.jdt.internal.junit.Messages;
import org.eclipse.jdt.internal.junit.ui.IJUnitHelpContextIds;
import org.eclipse.jdt.internal.junit.util.CoreTestSearchEngine;
import org.eclipse.jdt.internal.junit.util.JUnitStatus;
import org.eclipse.jdt.internal.junit.util.JUnitStubUtility;
import org.eclipse.jdt.internal.junit.util.LayoutUtil;
import org.eclipse.jdt.internal.junit.wizards.SuiteClassesContentProvider;
import org.eclipse.jdt.internal.junit.wizards.TestSuiteClassListRange;
import org.eclipse.jdt.internal.junit.wizards.UpdateTestSuite;
import org.eclipse.jdt.internal.junit.wizards.WizardMessages;
import org.eclipse.jdt.ui.JavaElementLabelProvider;
import org.eclipse.jdt.ui.wizards.NewTypeWizardPage;
import org.eclipse.jdt.internal.ui.util.Progress;
/**
* The class <code>NewTestSuiteWizardPage</code> contains controls and validation routines
* for the single page in the 'New JUnit TestSuite Wizard'.
* <p>
* Clients can use the page as-is and add it to their own wizard, or extend it to modify
* validation or add and remove controls.
* </p>
*
* @since 3.1
*/
public class NewTestSuiteWizardPage extends NewTypeWizardPage {
private static final String ALL_TESTS= "AllTests"; //$NON-NLS-1$
public static final String NON_COMMENT_END_MARKER = "$JUnit-END$"; //$NON-NLS-1$
public static final String NON_COMMENT_START_MARKER = "$JUnit-BEGIN$"; //$NON-NLS-1$
public static final String COMMENT_START = "//"; //$NON-NLS-1$
/**
* The string used to mark the beginning of the generated code
*/
public static final String START_MARKER= COMMENT_START + NON_COMMENT_START_MARKER;
/**
* The string used to mark the end of the generated code
*/
public static final String END_MARKER= COMMENT_START + NON_COMMENT_END_MARKER;
private final static String PAGE_NAME= "NewTestSuiteCreationWizardPage"; //$NON-NLS-1$
/** Field ID of the class in suite field. */
public final static String CLASSES_IN_SUITE= PAGE_NAME + ".classesinsuite"; //$NON-NLS-1$
/**
* Field ID of the junit4 toggle field.
*
* @since 3.7
*/
public final static String JUNIT4TOGGLE= PAGE_NAME + ".junit4toggle"; //$NON-NLS-1$
/**
* Field ID of the junit5 toggle field.
*/
private final static String JUNIT5TOGGLE= PAGE_NAME + ".junit5toggle"; //$NON-NLS-1$
private CheckboxTableViewer fClassesInSuiteTable;
private IStatus fClassesInSuiteStatus;
private Label fSelectedClassesLabel;
private boolean fUpdatedExistingClassButton;
private Button fJUnit5Toggle;
private Button fJUnit4Toggle;
private Button fJUnit3Toggle;
private boolean fIsJunit4;
private boolean fIsJunit4Enabled;
private boolean fIsJunit5;
private boolean fIsJunit5Enabled;
/**
* Creates a new <code>NewTestSuiteWizardPage</code>.
*/
public NewTestSuiteWizardPage() {
super(true, PAGE_NAME);
setTitle(WizardMessages.NewTestSuiteWizPage_title);
setDescription(WizardMessages.NewTestSuiteWizPage_description);
fClassesInSuiteStatus= new JUnitStatus();
}
@Override
public void createControl(Composite parent) {
initializeDialogUnits(parent);
Composite composite= new Composite(parent, SWT.NONE);
int nColumns= 4;
GridLayout layout= new GridLayout();
layout.numColumns= nColumns;
composite.setLayout(layout);
createJUnit4Controls(composite, nColumns);
createContainerControls(composite, nColumns);
createPackageControls(composite, nColumns);
//createSeparator(composite, nColumns);
createTypeNameControls(composite, nColumns);
createClassesInSuiteControl(composite, nColumns);
createMethodStubSelectionControls(composite, nColumns);
setControl(composite);
restoreWidgetValues();
Dialog.applyDialogFont(composite);
PlatformUI.getWorkbench().getHelpSystem().setHelp(composite, IJUnitHelpContextIds.NEW_TESTSUITE_WIZARD_PAGE);
}
/**
* Creates the controls for the method stub selection buttons. Expects a <code>GridLayout</code> with
* at least 3 columns.
*
* @param composite the parent composite
* @param nColumns number of columns to span
*/
protected void createMethodStubSelectionControls(Composite composite, int nColumns) {
}
/**
* Should be called from the wizard with the initial selection.
*
* @param selection the initial selection
*/
public void init(IStructuredSelection selection) {
IJavaElement jelem= getInitialJavaElement(selection);
initContainerPage(jelem, true);
initSuitePage(jelem);
boolean isJunit4= false;
boolean isJunit5= false;
if (jelem != null && jelem.getElementType() != IJavaElement.JAVA_MODEL) {
IJavaProject project= jelem.getJavaProject();
isJunit4= CoreTestSearchEngine.hasJUnit4TestAnnotation(project);
if (!isJunit4 && !CoreTestSearchEngine.hasTestCaseType(project) && JUnitStubUtility.is50OrHigher(project)) {
isJunit4= true;
}
isJunit5= CoreTestSearchEngine.hasJUnit5TestAnnotation(project);
}
setJUnit4or5(isJunit4, isJunit5, true);
ensureNoRecursiveTestSuiteInclusion();
doStatusUpdate();
}
private void initSuitePage(IJavaElement jelem) {
//parts of NewTypeWizardPage#initTypePage(..), see bug 132748
if (jelem != null) {
IPackageFragment pack= (IPackageFragment) jelem.getAncestor(IJavaElement.PACKAGE_FRAGMENT);
IJavaProject project= jelem.getJavaProject();
setPackageFragment(pack, true);
setAddComments(StubUtility.doAddComments(project), true); // from project or workspace
}
setTypeName(ALL_TESTS, true);
fTypeNameStatus= typeNameChanged(); //set status on initialization for this dialog - user must know that suite method will be overridden
}
@Override
protected void handleFieldChanged(String fieldName) {
super.handleFieldChanged(fieldName);
if (PACKAGE.equals(fieldName) || CONTAINER.equals(fieldName) || JUNIT4TOGGLE.equals(fieldName) || JUNIT5TOGGLE.equals(fieldName)) {
updateClassesInSuiteTable();
} else if (CLASSES_IN_SUITE.equals(fieldName)) {
fClassesInSuiteStatus= classesInSuiteChanged();
fTypeNameStatus= typeNameChanged(); //must check this one too
updateSelectedClassesLabel();
}
doStatusUpdate();
}
// ------ validation --------
private void doStatusUpdate() {
// status of all used components
IStatus[] status= new IStatus[] {
fContainerStatus,
fPackageStatus,
fTypeNameStatus,
fClassesInSuiteStatus
};
// the most severe status will be displayed and the OK button enabled/disabled.
updateStatus(status);
}
@Override
public void setVisible(boolean visible) {
if (visible) {
setFocus();
updateClassesInSuiteTable();
handleAllFieldsChanged();
} else {
saveWidgetValues();
}
super.setVisible(visible);
}
private void handleAllFieldsChanged() {
handleFieldChanged(PACKAGE);
handleFieldChanged(CONTAINER);
handleFieldChanged(CLASSES_IN_SUITE);
handleFieldChanged(TYPENAME);
}
/**
* Updates the classes in the suite table.
*/
protected void updateClassesInSuiteTable() {
if (fClassesInSuiteTable != null) {
IPackageFragment pack= getPackageFragment();
if (pack == null) {
IPackageFragmentRoot root= getPackageFragmentRoot();
if (root != null)
pack= root.getPackageFragment(""); //$NON-NLS-1$
else
return;
}
fClassesInSuiteTable.setInput(pack);
fClassesInSuiteTable.setAllChecked(true);
ensureNoRecursiveTestSuiteInclusion();
updateSelectedClassesLabel();
}
}
/**
* Creates the controls for the list of classes in the suite. Expects a <code>GridLayout</code> with
* at least 3 columns.
*
* @param parent the parent composite
* @param nColumns number of columns to span
*/
protected void createClassesInSuiteControl(Composite parent, int nColumns) {
if (fClassesInSuiteTable == null) {
Label label = new Label(parent, SWT.LEFT);
label.setText(WizardMessages.NewTestSuiteWizPage_classes_in_suite_label);
GridData gd= new GridData();
gd.horizontalAlignment = GridData.FILL;
gd.horizontalSpan= nColumns;
label.setLayoutData(gd);
fClassesInSuiteTable= CheckboxTableViewer.newCheckList(parent, SWT.BORDER);
gd= new GridData(GridData.FILL_BOTH);
gd.heightHint= 80;
gd.horizontalSpan= nColumns-1;
fClassesInSuiteTable.getTable().setLayoutData(gd);
fClassesInSuiteTable.setContentProvider(new SuiteClassesContentProvider(isJUnit4(), isJUnit5()));
fClassesInSuiteTable.setLabelProvider(new JavaElementLabelProvider());
fClassesInSuiteTable.addCheckStateListener(event -> handleFieldChanged(CLASSES_IN_SUITE));
Composite buttonContainer= new Composite(parent, SWT.NONE);
gd= new GridData(GridData.FILL_VERTICAL);
buttonContainer.setLayoutData(gd);
GridLayout buttonLayout= new GridLayout();
buttonLayout.marginWidth= 0;
buttonLayout.marginHeight= 0;
buttonContainer.setLayout(buttonLayout);
Button selectAllButton= new Button(buttonContainer, SWT.PUSH);
selectAllButton.setText(WizardMessages.NewTestSuiteWizPage_selectAll);
GridData bgd= new GridData(GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_BEGINNING);
bgd.widthHint = LayoutUtil.getButtonWidthHint(selectAllButton);
selectAllButton.setLayoutData(bgd);
selectAllButton.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
fClassesInSuiteTable.setAllChecked(true);
handleFieldChanged(CLASSES_IN_SUITE);
}
});
Button deselectAllButton= new Button(buttonContainer, SWT.PUSH);
deselectAllButton.setText(WizardMessages.NewTestSuiteWizPage_deselectAll);
bgd= new GridData(GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_BEGINNING);
bgd.widthHint = LayoutUtil.getButtonWidthHint(deselectAllButton);
deselectAllButton.setLayoutData(bgd);
deselectAllButton.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
fClassesInSuiteTable.setAllChecked(false);
handleFieldChanged(CLASSES_IN_SUITE);
}
});
// No of selected classes label
fSelectedClassesLabel= new Label(parent, SWT.LEFT | SWT.WRAP);
fSelectedClassesLabel.setFont(parent.getFont());
updateSelectedClassesLabel();
gd = new GridData();
gd.horizontalSpan = 2;
fSelectedClassesLabel.setLayoutData(gd);
}
}
@Override
protected void createTypeMembers(IType type, ImportsManager imports, IProgressMonitor monitor) throws CoreException {
writeImports(imports);
if(!isJUnit4() && !isJUnit5())
type.createMethod(getSuiteMethodString(type), null, false, null);
}
/*
* Returns the string content for creating a new suite() method.
*/
private String getSuiteMethodString(IType type) {
String typeName= type.getElementName();
StringBuilder suite= new StringBuilder("public static Test suite () {TestSuite suite= new TestSuite(" + typeName + ".class.getName());\n"); //$NON-NLS-1$ //$NON-NLS-2$
suite.append(getUpdatableString());
suite.append("\nreturn suite;}"); //$NON-NLS-1$
return suite.toString();
}
private String getUpdatableString() {
return UpdateTestSuite.getUpdatableString(fClassesInSuiteTable.getCheckedElements());
}
@Override
public void createType(IProgressMonitor monitor) throws CoreException, InterruptedException {
IPackageFragment pack= getPackageFragment();
ICompilationUnit cu= pack.getCompilationUnit(getTypeName() + ".java"); //$NON-NLS-1$
if (!cu.exists()) {
super.createType(monitor);
fUpdatedExistingClassButton= false;
} else {
updateExistingType(cu, monitor);
fUpdatedExistingClassButton= true;
}
}
private void updateExistingType(ICompilationUnit cu, IProgressMonitor monitor) throws JavaModelException {
if (!UpdateTestSuite.checkValidateEditStatus(cu, getShell()))
return;
IType suiteType= cu.getType(getTypeName());
monitor.beginTask(WizardMessages.NewTestSuiteWizPage_createType_beginTask, 10);
if (isJUnit4()) {
/* find TestClasses already in Test Suite */
IAnnotation suiteClasses= suiteType.getAnnotation("SuiteClasses"); //$NON-NLS-1$
if (suiteClasses.exists()) {
UpdateTestSuite.updateTestCasesInJunit4Suite(Progress.subMonitor(monitor, 5), cu, suiteClasses, fClassesInSuiteTable.getCheckedElements());
} else {
cannotUpdateSuiteError();
}
} else if (isJUnit5()) {
/* find TestClasses already in Test Suite */
IAnnotation selectClasses= suiteType.getAnnotation("SelectClasses"); //$NON-NLS-1$
if (selectClasses.exists()) {
UpdateTestSuite.updateTestCasesInJunit5Suite(Progress.subMonitor(monitor, 5), cu, selectClasses, fClassesInSuiteTable.getCheckedElements());
} else {
cannotUpdateSuiteError();
}
} else {
IMethod suiteMethod= suiteType.getMethod("suite", new String[] {}); //$NON-NLS-1$
monitor.worked(1);
String lineDelimiter= cu.findRecommendedLineSeparator();
if (suiteMethod.exists()) {
ISourceRange range= suiteMethod.getSourceRange();
if (range != null) {
try {
IDocument fullSource= new Document(cu.getBuffer().getContents());
String originalContent= fullSource.get(range.getOffset(), range.getLength());
TestSuiteClassListRange classListRange= UpdateTestSuite.getTestSuiteClassListRange(originalContent);
if (classListRange != null) {
UpdateTestSuite.updateTestCasesInSuite(monitor, cu, suiteMethod, fClassesInSuiteTable.getCheckedElements());
} else {
cannotUpdateSuiteError();
}
} catch (BadLocationException e) {
Assert.isTrue(false, "Should never happen"); //$NON-NLS-1$
}
} else {
MessageDialog.openError(getShell(), WizardMessages.NewTestSuiteWizPage_createType_updateErrorDialog_title, WizardMessages.NewTestSuiteWizPage_createType_updateErrorDialog_message);
}
} else {
suiteType.createMethod(getSuiteMethodString(suiteType), null, true, monitor);
String originalContent= cu.getSource();
monitor.worked(2);
String formattedContent= JUnitStubUtility.formatCompilationUnit(cu.getJavaProject(), originalContent, lineDelimiter);
cu.getBuffer().setContents(formattedContent);
monitor.worked(1);
cu.save(Progress.subMonitor(monitor, 1), false);
}
}
monitor.done();
}
/**
* Returns true iff an existing suite() method has been replaced.
*
* @return <code>true</code> is returned if an existing test suite has been replaced
*/
public boolean hasUpdatedExistingClass() {
return fUpdatedExistingClassButton;
}
private IStatus classesInSuiteChanged() {
JUnitStatus status= new JUnitStatus();
if (fClassesInSuiteTable.getCheckedElements().length <= 0)
status.setWarning(WizardMessages.NewTestSuiteWizPage_classes_in_suite_error_no_testclasses_selected);
return status;
}
private void updateSelectedClassesLabel() {
int noOfClassesChecked= fClassesInSuiteTable.getCheckedElements().length;
String key= (noOfClassesChecked==1) ? WizardMessages.NewTestClassWizPage_treeCaption_classSelected : WizardMessages.NewTestClassWizPage_treeCaption_classesSelected;
fSelectedClassesLabel.setText(Messages.format(key, Integer.valueOf(noOfClassesChecked)));
}
@Override
protected IStatus typeNameChanged() {
super.typeNameChanged();
JUnitStatus status= new JUnitStatus();
String typeName= getTypeName();
// must not be empty
if (typeName.length() == 0) {
status.setError(WizardMessages.NewTestSuiteWizPage_typeName_error_name_empty);
return status;
}
if (typeName.indexOf('.') != -1) {
status.setError(WizardMessages.NewTestSuiteWizPage_typeName_error_name_qualified);
return status;
}
IPackageFragment pack= getPackageFragment();
if (pack != null) {
ICompilationUnit cu= pack.getCompilationUnit(typeName + ".java"); //$NON-NLS-1$
//if this cu already exists, we need to disable the
//junit 3 option if it is a junit 4 suite and vice versa
if (cu.exists()) {
IType type= cu.findPrimaryType();
if (type != null) {
setJUnit4or5(type.getAnnotation("RunWith").exists(), type.getAnnotation("Suite").exists(), false); //$NON-NLS-1$ //$NON-NLS-2$
}
} else {
setJUnit4or5(isJUnit4(), isJUnit5(), true);
}
}
IStatus val= JavaConventionsUtil.validateJavaTypeName(typeName, getJavaProject());
if (val.getSeverity() == IStatus.ERROR) {
status.setError(WizardMessages.NewTestSuiteWizPage_typeName_error_name_not_valid+val.getMessage());
return status;
} else if (val.getSeverity() == IStatus.WARNING) {
status.setWarning(WizardMessages.NewTestSuiteWizPage_typeName_error_name_name_discouraged+val.getMessage());
// continue checking
}
IStatus recursiveSuiteInclusionStatus= checkRecursiveTestSuiteInclusion();
if (! recursiveSuiteInclusionStatus.isOK())
return recursiveSuiteInclusionStatus;
if (pack != null) {
ICompilationUnit cu= pack.getCompilationUnit(typeName + ".java"); //$NON-NLS-1$
if (cu.exists()) {
status.setWarning(isJUnit4()
? WizardMessages.NewTestSuiteWizPage_typeName_warning_already_exists_junit4
: isJUnit5()
? WizardMessages.NewTestSuiteWizPage_typeName_warning_already_exists_junit5
: WizardMessages.NewTestSuiteWizPage_typeName_warning_already_exists);
return status;
}
IResource resource= cu.getResource();
if (resource != null && !ResourcesPlugin.getWorkspace().validateFiltered(resource).isOK()) {
status.setError(WizardMessages.NewTestSuiteWizPage_typeName_error_filtered);
return status;
}
}
return status;
}
private IStatus checkRecursiveTestSuiteInclusion(){
if (fClassesInSuiteTable == null)
return new JUnitStatus();
String typeName= getTypeName();
JUnitStatus status= new JUnitStatus();
Object[] checkedClasses= fClassesInSuiteTable.getCheckedElements();
for (Object checkedClass : checkedClasses) {
if (((IType)checkedClass).getElementName().equals(typeName)){
status.setWarning(WizardMessages.NewTestSuiteCreationWizardPage_infinite_recursion);
return status;
}
}
return new JUnitStatus();
}
private void ensureNoRecursiveTestSuiteInclusion(){
if (fClassesInSuiteTable == null)
return;
String typeName= getTypeName();
Object[] checkedClasses= fClassesInSuiteTable.getCheckedElements();
for (Object checkedClass : checkedClasses) {
if (((IType)checkedClass).getElementName().equals(typeName)) {
fClassesInSuiteTable.setChecked(checkedClass, false);
return;
}
}
}
private void cannotUpdateSuiteError() {
MessageDialog.openError(getShell(), WizardMessages.NewTestSuiteWizPage_cannotUpdateDialog_title,
Messages.format(WizardMessages.NewTestSuiteWizPage_cannotUpdateDialog_message, new String[] { NewTestSuiteWizardPage.START_MARKER, NewTestSuiteWizardPage.END_MARKER}));
}
private void writeImports(ImportsManager imports) {
if (isJUnit4()) {
imports.addImport("org.junit.runner.RunWith"); //$NON-NLS-1$
imports.addImport("org.junit.runners.Suite"); //$NON-NLS-1$
imports.addImport("org.junit.runners.Suite.SuiteClasses"); //$NON-NLS-1$
} else if (isJUnit5()) {
imports.addImport("org.junit.platform.suite.api.Suite"); //$NON-NLS-1$
imports.addImport("org.junit.platform.suite.api.SelectClasses"); //$NON-NLS-1$
} else {
imports.addImport("junit.framework.Test"); //$NON-NLS-1$
imports.addImport("junit.framework.TestSuite"); //$NON-NLS-1$
}
}
/**
* Use the dialog store to restore widget values to the values that they held
* last time this wizard was used to completion
*/
private void restoreWidgetValues() {
}
/**
* Since Finish was pressed, write widget values to the dialog store so that they
* will persist into the next invocation of this wizard page
*/
private void saveWidgetValues() {
}
/**
* Creates the controls for the JUnit 4 toggle control. Expects a <code>GridLayout</code> with
* at least 3 columns.
*
* @param composite the parent composite
* @param nColumns number of columns to span
*
* @since 3.7
*/
protected void createJUnit4Controls(Composite composite, int nColumns) {
Composite inner= new Composite(composite, SWT.NONE);
inner.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, nColumns, 1));
GridLayout layout= new GridLayout(3, false);
layout.marginHeight= 0;
layout.marginWidth= 0;
inner.setLayout(layout);
SelectionAdapter listener= new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
boolean isSelected= ((Button) e.widget).getSelection();
internalSetJUnit4(isSelected);
}
};
SelectionAdapter listener2= new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
boolean isSelected= ((Button) e.widget).getSelection();
internalSetJUnit5(isSelected);
}
};
fJUnit3Toggle = new Button(inner, SWT.RADIO);
fJUnit3Toggle.setText(WizardMessages.NewTestClassWizPage_junit3_radio_label);
fJUnit3Toggle.setLayoutData(new GridData(GridData.FILL, GridData.CENTER, false, false, 1, 1));
fJUnit3Toggle.setSelection(!fIsJunit4);
fJUnit3Toggle.setEnabled(fIsJunit4Enabled);
fJUnit4Toggle= new Button(inner, SWT.RADIO);
fJUnit4Toggle.setText(WizardMessages.NewTestClassWizPage_junit4_radio_label);
fJUnit4Toggle.setSelection(fIsJunit4);
fJUnit4Toggle.setEnabled(fIsJunit4Enabled);
fJUnit4Toggle.setLayoutData(new GridData(GridData.FILL, GridData.CENTER, false, false, 1, 1));
fJUnit4Toggle.addSelectionListener(listener);
fJUnit5Toggle= new Button(inner, SWT.RADIO);
fJUnit5Toggle.setText(WizardMessages.NewTestClassWizPage_junit5_radio_label);
fJUnit5Toggle.setSelection(fIsJunit5);
fJUnit5Toggle.setEnabled(fIsJunit5Enabled);
fJUnit5Toggle.setLayoutData(new GridData(GridData.FILL, GridData.CENTER, false, false, 1, 1));
fJUnit5Toggle.addSelectionListener(listener2);
}
/**
* Specifies if the test should be created as JUnit 4 test.
*
* @param isJUnit4 If set, a JUnit 4 test will be created
* @param isEnabled if <code>true</code> the modifier fields are
* editable; otherwise they are read-only
*
* @since 3.7
* @deprecated Use setJUnit4or5 instead
*/
@Deprecated
public void setJUnit4(boolean isJUnit4, boolean isEnabled) {
fIsJunit4Enabled= isEnabled;
if (fJUnit4Toggle != null && !fJUnit4Toggle.isDisposed()) {
fJUnit4Toggle.setSelection(isJUnit4);
fJUnit3Toggle.setSelection(!isJUnit4);
fJUnit5Toggle.setSelection(false);
fJUnit4Toggle.setEnabled(isEnabled || isJUnit4);
fJUnit3Toggle.setEnabled(isEnabled || !isJUnit4);
}
internalSetJUnit4(isJUnit4);
}
/**
* Specifies if the test should be created as JUnit 5 test.
* @param isJUnit4 If set, a JUnit 4 test will be created
* @param isJUnit5 If set, a JUnit 5 test will be created
* @param isEnabled if <code>true</code> the modifier fields are
* editable; otherwise they are read-only
*
* @since 3.15
*/
public void setJUnit4or5(boolean isJUnit4, boolean isJUnit5, boolean isEnabled) {
fIsJunit5Enabled= isEnabled;
if (fJUnit5Toggle != null && !fJUnit5Toggle.isDisposed()) {
fJUnit3Toggle.setSelection(!isJUnit4 && !isJUnit5);
fJUnit4Toggle.setSelection(isJUnit4 && !isJUnit5);
fJUnit5Toggle.setSelection(isJUnit5);
fJUnit4Toggle.setEnabled(isEnabled || !isJUnit5);
fJUnit3Toggle.setEnabled(isEnabled || !isJUnit5 && !isJUnit4);
}
if (isJUnit5) {
internalSetJUnit5(true);
internalSetJUnit4(false);
} else {
internalSetJUnit5(false);
internalSetJUnit4(isJUnit4);
}
}
/**
* Returns <code>true</code> if the test suite should be created as Junit 5 suite
* @return returns <code>true</code> if the test suite should be created as Junit 5 test
*
* @since 3.15
*/
public boolean isJUnit5() {
return fIsJunit5;
}
private void internalSetJUnit5(boolean isJUnit5) {
if (fIsJunit5 == isJUnit5)
return;
fIsJunit5= isJUnit5;
if (fClassesInSuiteTable != null && fClassesInSuiteTable.getContentProvider() instanceof SuiteClassesContentProvider) {
SuiteClassesContentProvider provider= (SuiteClassesContentProvider)fClassesInSuiteTable.getContentProvider();
provider.setIncludeJunit5Tests(isJUnit5);
}
if (fIsJunit5) {
setSuperClass("java.lang.Object", false); //$NON-NLS-1$
} else {
setSuperClass(JUnitCorePlugin.TEST_SUPERCLASS_NAME, true);
}
handleFieldChanged(JUNIT5TOGGLE);
}
/**
* Returns <code>true</code> if the test suite should be created as Junit 4 suite
* @return returns <code>true</code> if the test suite should be created as Junit 4 test
*
* @since 3.7
*/
public boolean isJUnit4() {
return fIsJunit4;
}
private void internalSetJUnit4(boolean isJUnit4) {
if (fIsJunit4 == isJUnit4)
return;
fIsJunit4= isJUnit4;
if (fClassesInSuiteTable != null && fClassesInSuiteTable.getContentProvider() instanceof SuiteClassesContentProvider) {
SuiteClassesContentProvider provider= (SuiteClassesContentProvider)fClassesInSuiteTable.getContentProvider();
provider.setIncludeJunit4Tests(isJUnit4);
}
if (fIsJunit4) {
setSuperClass("java.lang.Object", false); //$NON-NLS-1$
} else {
setSuperClass(JUnitCorePlugin.TEST_SUPERCLASS_NAME, true);
}
handleFieldChanged(JUNIT4TOGGLE);
}
@Override
protected String constructCUContent(ICompilationUnit cu, String typeContent, String lineDelimiter) throws CoreException {
if (isJUnit4()) {
typeContent= appendAnnotations(typeContent, lineDelimiter);
} else if (isJUnit5()) {
typeContent= appendAnnotations5(typeContent, lineDelimiter);
}
return super.constructCUContent(cu, typeContent, lineDelimiter);
}
private String appendAnnotations(String typeContent, String lineDelimiter) {
Object[] checkedElements= fClassesInSuiteTable.getCheckedElements();
StringBuilder buffer = new StringBuilder("@RunWith(Suite.class)"); //$NON-NLS-1$
buffer.append(lineDelimiter);
buffer.append("@SuiteClasses({"); //$NON-NLS-1$
for (int i= 0; i < checkedElements.length; i++) {
if (checkedElements[i] instanceof IType) {
IType testType= (IType) checkedElements[i];
buffer.append(testType.getElementName());
buffer.append(".class"); //$NON-NLS-1$
if(i<checkedElements.length-1)
buffer.append(',');
}
}
buffer.append("})"); //$NON-NLS-1$
buffer.append(lineDelimiter);
buffer.append(typeContent);
return buffer.toString();
}
private String appendAnnotations5(String typeContent, String lineDelimiter) {
Object[] checkedElements= fClassesInSuiteTable.getCheckedElements();
StringBuilder buffer = new StringBuilder("@Suite"); //$NON-NLS-1$
buffer.append(lineDelimiter);
buffer.append("@SelectClasses({"); //$NON-NLS-1$
for (int i= 0; i < checkedElements.length; i++) {
if (checkedElements[i] instanceof IType) {
IType testType= (IType) checkedElements[i];
buffer.append(testType.getElementName());
buffer.append(".class"); //$NON-NLS-1$
if(i<checkedElements.length-1)
buffer.append(',');
}
}
buffer.append("})"); //$NON-NLS-1$
buffer.append(lineDelimiter);
buffer.append(typeContent);
return buffer.toString();
}
}