-
Notifications
You must be signed in to change notification settings - Fork 78
/
AbstractJunit4SelectionTestCase.java
214 lines (182 loc) · 7.2 KB
/
AbstractJunit4SelectionTestCase.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
/*******************************************************************************
* Copyright (c) 2020, 2024 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
*******************************************************************************/
package org.eclipse.jdt.ui.tests.refactoring;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import java.io.IOException;
import java.io.InputStream;
import org.junit.Before;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.resources.IResourceChangeListener;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.jface.text.ITextSelection;
import org.eclipse.jface.text.TextSelection;
import org.eclipse.ltk.core.refactoring.Change;
import org.eclipse.ltk.core.refactoring.CheckConditionsOperation;
import org.eclipse.ltk.core.refactoring.IUndoManager;
import org.eclipse.ltk.core.refactoring.PerformRefactoringOperation;
import org.eclipse.ltk.core.refactoring.Refactoring;
import org.eclipse.ltk.core.refactoring.RefactoringCore;
import org.eclipse.ltk.core.refactoring.RefactoringStatus;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.ui.tests.refactoring.infra.AbstractJunit4CUTestCase;
import org.eclipse.jdt.ui.tests.refactoring.infra.RefactoringTestPlugin;
/**
* Selection in the file read by {@link #getFileContents(InputStream)} is marked with
* /*]*/ and /*[*/ (excluding the marker comments) or
* /*[*/ and /*]*/ (including comments).
*/
public abstract class AbstractJunit4SelectionTestCase extends AbstractJunit4CUTestCase {
public static final String SQUARE_BRACKET_OPEN= "/*[*/";
public static final int SQUARE_BRACKET_OPEN_LENGTH= SQUARE_BRACKET_OPEN.length();
public static final String SQUARE_BRACKET_CLOSE= "/*]*/";
public static final int SQUARE_BRACKET_CLOSE_LENGTH= SQUARE_BRACKET_CLOSE.length();
enum TestMode { VALID_SELECTION, INVALID_SELECTION, COMPARE_WITH_OUTPUT, WARNING_FOR_SELECTION }
private final boolean fIgnoreSelectionMarker;
private int[] fSelection;
protected boolean fIsPreDeltaTest;
public AbstractJunit4SelectionTestCase() {
this(false);
}
public AbstractJunit4SelectionTestCase(boolean ignoreSelectionMarker) {
fIgnoreSelectionMarker= ignoreSelectionMarker;
}
@Before
public void setUp() throws Exception {
fIsPreDeltaTest= false;
}
protected int[] getSelection() {
return fSelection;
}
protected ITextSelection getTextSelection() {
int[] s= getSelection();
return new TextSelection(s[0], s[1]);
}
@Override
protected InputStream getFileInputStream(String fileName) throws IOException {
return RefactoringTestPlugin.getDefault().getTestResourceStream(fileName);
}
@Override
protected String getFileContents(InputStream in) throws IOException {
String result= super.getFileContents(in);
initializeSelection(result);
if (fIgnoreSelectionMarker) {
result= result.replaceAll("/\\*\\[\\*/", "");
result= result.replaceAll("/\\*\\]\\*/", "");
}
return result;
}
protected void performTest(final ICompilationUnit unit, final Refactoring refactoring, TestMode mode, final String out, boolean doUndo) throws Exception {
IProgressMonitor pm= new NullProgressMonitor();
switch (mode) {
case VALID_SELECTION:
assertTrue(checkPreconditions(refactoring, pm).isOK());
break;
case INVALID_SELECTION:
assertFalse(checkPreconditions(refactoring, pm).isOK());
break;
case WARNING_FOR_SELECTION:
assertTrue(checkPreconditions(refactoring, pm).hasWarning());
break;
case COMPARE_WITH_OUTPUT:
IUndoManager undoManager= RefactoringCore.getUndoManager();
undoManager.flush();
String original= unit.getSource();
final PerformRefactoringOperation op= new PerformRefactoringOperation(
refactoring, getCheckingStyle());
if (fIsPreDeltaTest) {
IWorkspace workspace= ResourcesPlugin.getWorkspace();
IResourceChangeListener listener= event -> TestModelProvider.assertTrue(event.getDelta());
try {
clearPreDelta();
workspace.checkpoint(false);
workspace.addResourceChangeListener(listener);
JavaCore.run(op, new NullProgressMonitor());
} finally {
workspace.removeResourceChangeListener(listener);
}
} else {
JavaCore.run(op, new NullProgressMonitor());
}
assertFalse("Precondition check failed: " + op.getConditionStatus().toString(), op.getConditionStatus().hasFatalError());
assertFalse("Validation check failed: " + op.getConditionStatus().toString(), op.getValidationStatus().hasFatalError());
assertNotNull("No Undo", op.getUndoChange());
compareSource(unit.getSource(), out);
Change undo= op.getUndoChange();
assertNotNull("Undo doesn't exist", undo);
assertTrue("Undo manager is empty", undoManager.anythingToUndo());
if (doUndo) {
undoManager.performUndo(null, new NullProgressMonitor());
assertFalse("Undo manager still has undo", undoManager.anythingToUndo());
assertTrue("Undo manager is empty", undoManager.anythingToRedo());
compareSource(original, unit.getSource());
}
break;
default:
break;
}
}
protected RefactoringStatus checkPreconditions(Refactoring refactoring, IProgressMonitor pm) throws CoreException {
CheckConditionsOperation op= new CheckConditionsOperation(refactoring, getCheckingStyle());
op.run(pm);
return op.getStatus();
}
protected int getCheckingStyle() {
return CheckConditionsOperation.ALL_CONDITIONS;
}
protected void clearPreDelta() {
TestModelProvider.clearDelta();
}
private void initializeSelection(String source) {
int start= -1;
int end= -1;
int includingStart= source.indexOf(SQUARE_BRACKET_OPEN);
int excludingStart= source.indexOf(SQUARE_BRACKET_CLOSE);
int includingEnd= source.lastIndexOf(SQUARE_BRACKET_CLOSE);
int excludingEnd= source.lastIndexOf(SQUARE_BRACKET_OPEN);
if (includingStart > excludingStart && excludingStart != -1) {
includingStart= -1;
} else if (excludingStart > includingStart && includingStart != -1) {
excludingStart= -1;
}
if (includingEnd < excludingEnd) {
includingEnd= -1;
} else if (excludingEnd < includingEnd) {
excludingEnd= -1;
}
if (includingStart != -1) {
start= includingStart;
} else {
start= excludingStart + SQUARE_BRACKET_CLOSE_LENGTH;
}
if (excludingEnd != -1) {
end= excludingEnd;
} else {
end= includingEnd + SQUARE_BRACKET_CLOSE_LENGTH;
}
assertTrue("Selection invalid", start >= 0);
assertTrue("Selection invalid", end >= 0);
assertTrue("Selection invalid", end >= start);
fSelection= new int[] {
start - (fIgnoreSelectionMarker ? SQUARE_BRACKET_CLOSE_LENGTH : 0),
end - start
};
// System.out.println("|"+ source.substring(result[0], result[0] + result[1]) + "|");
}
}