Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 6963b4af42beb21d738abb6ac0108e380e4aaf70 (plain) (blame)
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
/*******************************************************************************
 * Copyright (c) 2012 Google, Inc and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 * 	   Sergey Prigogin (Google) - initial API and implementation
 *******************************************************************************/
package org.eclipse.cdt.ui.tests.refactoring;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.net.URI;
import java.util.LinkedHashSet;
import java.util.Set;

import org.eclipse.core.filesystem.URIUtil;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Path;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.text.TextSelection;
import org.eclipse.ltk.core.refactoring.Change;
import org.eclipse.ltk.core.refactoring.Refactoring;
import org.eclipse.ltk.core.refactoring.RefactoringContext;
import org.eclipse.ltk.core.refactoring.RefactoringDescriptor;
import org.eclipse.ltk.core.refactoring.RefactoringDescriptorProxy;
import org.eclipse.ltk.core.refactoring.RefactoringStatus;
import org.eclipse.ltk.core.refactoring.RefactoringStatusEntry;
import org.eclipse.ltk.core.refactoring.history.RefactoringHistory;
import org.eclipse.ltk.internal.core.refactoring.history.RefactoringHistoryService;
import org.osgi.framework.Bundle;

import org.eclipse.cdt.core.CCorePlugin;
import org.eclipse.cdt.core.dom.IPDOMManager;
import org.eclipse.cdt.core.model.CoreModel;
import org.eclipse.cdt.core.model.ICProject;
import org.eclipse.cdt.core.model.ITranslationUnit;
import org.eclipse.cdt.core.testplugin.CProjectHelper;
import org.eclipse.cdt.core.testplugin.util.BaseTestCase;
import org.eclipse.cdt.core.testplugin.util.TestSourceReader;
import org.eclipse.cdt.ui.CUIPlugin;
import org.eclipse.cdt.ui.PreferenceConstants;
import org.eclipse.cdt.ui.testplugin.CTestPlugin;

import org.eclipse.cdt.internal.ui.refactoring.CRefactoring;
import org.eclipse.cdt.internal.ui.refactoring.CRefactoringContext;

/**
 * Common base for refactoring tests.
 */
public abstract class RefactoringTestBase extends BaseTestCase {
	private static final int INDEXER_TIMEOUT_SEC = 360;
	protected static final NullProgressMonitor NULL_PROGRESS_MONITOR = new NullProgressMonitor();

	/** Allows empty files to be created during test setup. */
	protected boolean createEmptyFiles = true;
	/** See {@link PreferenceConstants.CLASS_MEMBER_ASCENDING_VISIBILITY_ORDER} */
	protected boolean ascendingVisibilityOrder;
	/** Expected counts of errors, warnings and info messages */
	protected int expectedInitialErrors;
	protected int expectedInitialWarnings;
	protected int expectedFinalWarnings;
	protected int expectedFinalInfos;

	private boolean cpp = true;
	private ICProject cproject;
	private final Set<TestSourceFile> testFiles = new LinkedHashSet<TestSourceFile>();
	private TestSourceFile selectedFile;
	private TextSelection selection;
	private TestSourceFile historyScript;

    protected RefactoringTestBase() {
		super();
	}

    protected RefactoringTestBase(String name) {
		super(name);
	}

	@Override
	public void setUp() throws Exception {
		super.setUp();
		resetPreferences();
		cproject = cpp ?
				CProjectHelper.createCCProject(getName() + System.currentTimeMillis(), "bin", IPDOMManager.ID_NO_INDEXER) :
				CProjectHelper.createCProject(getName() + System.currentTimeMillis(), "bin", IPDOMManager.ID_NO_INDEXER);
		Bundle bundle = CTestPlugin.getDefault().getBundle();
		CharSequence[] testData = TestSourceReader.getContentsForTest(bundle, "ui", getClass(), getName(), 0);

		for (int i = 0; i < testData.length; i++) {
			CharSequence contents = testData[i];
			TestSourceFile testFile = null;
			boolean expectedResult = false;
			BufferedReader reader = new BufferedReader(new StringReader(contents.toString()));
			String line;
			while ((line = reader.readLine()) != null) {
				if (testFile == null) {
					testFile = new TestSourceFile(line.trim());
				} else if (isResultDelimiter(line.trim())) {
					expectedResult = true;
				} else if (expectedResult) {
					testFile.addLineToExpectedSource(line);
				} else {
					testFile.addLineToSource(line);
				}
			}
			reader.close();

			if (createEmptyFiles || !testFile.getSource().isEmpty()) {
				TestSourceReader.createFile(cproject.getProject(), new Path(testFile.getName()),
						testFile.getSource());
			}
			testFiles.add(testFile);
			if (testFile.getName().endsWith(".xml")) {
				historyScript = testFile;
			} else if (selection == null) {
				selection = testFile.getSelection();
				if (selection != null)
					selectedFile = testFile;
			}
		}
		if (selectedFile == null && !testFiles.isEmpty()) {
			selectedFile = testFiles.iterator().next();
		}
		CCorePlugin.getIndexManager().setIndexerId(cproject, IPDOMManager.ID_FAST_INDEXER);
		assertTrue(CCorePlugin.getIndexManager().joinIndexer(INDEXER_TIMEOUT_SEC * 1000,
				NULL_PROGRESS_MONITOR));
	}

	@Override
	public void tearDown() throws Exception {
		if (cproject != null) {
			cproject.getProject().delete(IResource.FORCE | IResource.ALWAYS_DELETE_PROJECT_CONTENT,
					NULL_PROGRESS_MONITOR);
		}
		resetPreferences();
		super.tearDown();
	}

	protected void assertRefactoringSuccess() throws Exception {
		executeRefactoring(true);
		compareFiles();
	}

	protected void assertRefactoringFailure() throws Exception {
		executeRefactoring(false);
	}

	private void executeRefactoring(boolean expectedSuccess) throws Exception {
		if (ascendingVisibilityOrder) {
			getPreferenceStore().setValue(PreferenceConstants.CLASS_MEMBER_ASCENDING_VISIBILITY_ORDER,
					ascendingVisibilityOrder);
		}
		if (historyScript != null) {
			executeHistoryRefactoring(expectedSuccess);
			return;
		}

		Refactoring refactoring = createRefactoring();
		RefactoringContext context;
		if (refactoring instanceof CRefactoring) {
			context = new CRefactoringContext((CRefactoring) refactoring);
		} else {
			context = new RefactoringContext(refactoring);
		}
		executeRefactoring(refactoring, context, true, expectedSuccess);
	}

	protected void executeRefactoring(Refactoring refactoring, RefactoringContext context,
			boolean withUserInput, boolean expectedSuccess) throws CoreException, Exception {
		try {
			RefactoringStatus initialStatus = refactoring.checkInitialConditions(NULL_PROGRESS_MONITOR);
			if (!expectedSuccess) {
				assertStatusFatalError(initialStatus);
				return;
			}
			if (expectedInitialErrors != 0) {
				assertStatusError(initialStatus, expectedInitialErrors);
			} else if (expectedInitialWarnings != 0) {
				assertStatusWarning(initialStatus, expectedInitialWarnings);
			} else {
				assertStatusOk(initialStatus);
			}

			if (withUserInput)
				simulateUserInput();
			RefactoringStatus finalStatus = refactoring.checkFinalConditions(NULL_PROGRESS_MONITOR);
			if (expectedFinalWarnings != 0) {
				assertStatusWarning(finalStatus, expectedFinalWarnings);
			} else if (expectedFinalInfos != 0) {
				assertStatusInfo(finalStatus, expectedFinalInfos);
			} else {
				assertStatusOk(finalStatus);
			}
			Change change = refactoring.createChange(NULL_PROGRESS_MONITOR);
			change.perform(NULL_PROGRESS_MONITOR);
		} finally {
			if (context != null)
				context.dispose();
		}
	}

	private void executeHistoryRefactoring(boolean expectedSuccess) throws Exception {
		URI uri= URIUtil.toURI(cproject.getProject().getLocation());
		String scriptSource = historyScript.getSource().replaceAll("\\$\\{projectPath\\}", uri.getPath());
		RefactoringHistory history = RefactoringHistoryService.getInstance().readRefactoringHistory(
				new ByteArrayInputStream(scriptSource.getBytes()), 0);
		for (RefactoringDescriptorProxy proxy : history.getDescriptors()) {
			RefactoringDescriptor descriptor = proxy.requestDescriptor(NULL_PROGRESS_MONITOR);
			RefactoringStatus status = new RefactoringStatus();
			RefactoringContext context = descriptor.createRefactoringContext(status);
			assertTrue(status.isOK());
			executeRefactoring(context.getRefactoring(), context, false, expectedSuccess);
		}
	}

	/**
	 * Creates a refactoring object.
	 */
	protected abstract Refactoring createRefactoring();

	/**
	 * Subclasses can override to simulate user input.
	 */
	protected void simulateUserInput() {
	}

	protected ICProject getCProject() {
		return cproject;
	}

	protected TestSourceFile getSelectedTestFile() {
		return selectedFile;
	}

	protected IFile getSelectedFile() {
		if (selectedFile == null)
			return null;
		return cproject.getProject().getFile(new Path(selectedFile.getName()));
	}

	protected ITranslationUnit getSelectedTranslationUnit() {
		IFile file = getSelectedFile();
		if (file == null)
			return null;
		return (ITranslationUnit) CoreModel.getDefault().create(file);
	}

	protected TestSourceFile getHistoryScriptFile() {
		return historyScript;
	}

	protected TextSelection getSelection() {
		return selection;
	}

	protected boolean isCpp() {
		return cpp;
	}

	protected void setCpp(boolean cpp) {
		this.cpp = cpp;
	}

	private boolean isResultDelimiter(String str) {
		if (str.isEmpty())
			return false;
		for (int i = 0; i < str.length(); i++) {
			if (str.charAt(i) != '=')
				return false;
		}
		return true;
	}

	protected void assertStatusOk(RefactoringStatus status) {
		if (!status.isOK())
			fail("Error or warning status: " + status.getEntries()[0].getMessage());
	}

	protected void assertStatusWarning(RefactoringStatus status, int number) {
		if (number > 0) {
			assertTrue("Warning status expected", status.hasWarning());
		}
		RefactoringStatusEntry[] entries = status.getEntries();
		int count = 0;
		for (RefactoringStatusEntry entry : entries) {
			if (entry.isWarning()) {
				++count;
			}
		}
		assertEquals("Found " + count + " warnings instead of expected " + number, number, count);
	}

	protected void assertStatusInfo(RefactoringStatus status, int number) {
		if (number > 0) {
			assertTrue("Info status expected", status.hasInfo());
		}
		RefactoringStatusEntry[] entries = status.getEntries();
		int count = 0;
		for (RefactoringStatusEntry entry : entries) {
			if (entry.isInfo()) {
				++count;
			}
		}
		assertEquals("Found " + count + " informational messages instead of expected " + number, number, count);
	}

	protected void assertStatusError(RefactoringStatus status, int number) {
		if (number > 0) {
			assertTrue("Error status expected", status.hasError());
		}
		RefactoringStatusEntry[] entries = status.getEntries();
		int count = 0;
		for (RefactoringStatusEntry entry : entries) {
			if (entry.isError()) {
				++count;
			}
		}
		assertEquals("Found " + count + " errors instead of expected " + number, number, count);
	}

	protected void assertStatusFatalError(RefactoringStatus status, int number) {
		if (number > 0) {
			assertTrue("Fatal error status expected", status.hasFatalError());
		}
		RefactoringStatusEntry[] entries = status.getEntries();
		int count = 0;
		for (RefactoringStatusEntry entry : entries) {
			if (entry.isFatalError()) {
				++count;
			}
		}
		assertEquals("Found " + count + " fatal errors instead of expected " + number, number, count);
	}

	protected void assertStatusFatalError(RefactoringStatus status) {
		assertTrue("Fatal error status expected", status.hasFatalError());
	}

	protected void assertEquals(TestSourceFile testFile, IFile file) throws Exception {
		String actualSource = getFileContents(file);
		assertEquals(testFile.getExpectedSource(), actualSource);
	}

	protected void compareFiles() throws Exception {
		for (TestSourceFile testFile : testFiles) {
			String expectedSource = testFile.getExpectedSource();
			IFile file = cproject.getProject().getFile(new Path(testFile.getName()));
			String actualSource = getFileContents(file);
			assertEquals(expectedSource, actualSource);
		}
	}

	protected String getFileContents(IFile file) throws Exception {
		BufferedReader reader = new BufferedReader(new InputStreamReader(file.getContents(), "UTF-8"));
		StringBuilder buffer = new StringBuilder();
		char[] part= new char[2048];
		int read= 0;
		while ((read= reader.read(part)) != -1)
			buffer.append(part, 0, read);
		reader.close();
		return buffer.toString();
	}

	protected void resetPreferences() {
		getPreferenceStore().setToDefault(PreferenceConstants.CLASS_MEMBER_ASCENDING_VISIBILITY_ORDER);
	}

	protected IPreferenceStore getPreferenceStore() {
		return CUIPlugin.getDefault().getPreferenceStore();
	}
}

Back to the top