1
/*******************************************************************************
2
* Copyright (c) 2008 Institute for Software, HSR Hochschule fuer Technik
3
* Rapperswil, University of applied sciences and others
4
* All rights reserved. This program and the accompanying materials
5
* are made available under the terms of the Eclipse Public License v1.0
6
* which accompanies this distribution, and is available at
7
* http://www.eclipse.org/legal/epl-v10.html
10
* Institute for Software - initial API and implementation
11
*******************************************************************************/
12
package org.eclipse.cdt.core.parser.tests.rewrite;
14
import java.io.BufferedReader;
15
import java.io.InputStreamReader;
17
import java.util.TreeMap;
18
import java.util.Vector;
20
import org.eclipse.cdt.core.tests.BaseTestFramework;
21
import org.eclipse.core.resources.IFile;
22
import org.eclipse.core.runtime.ILogListener;
23
import org.eclipse.core.runtime.IStatus;
24
import org.eclipse.core.runtime.NullProgressMonitor;
25
import org.eclipse.core.runtime.Path;
26
import org.eclipse.jface.text.TextSelection;
29
* @author Guido Zgraggen IFS
32
public abstract class RewriteBaseTest extends BaseTestFramework implements ILogListener{
33
protected static final NullProgressMonitor NULL_PROGRESS_MONITOR = new NullProgressMonitor();
35
protected TreeMap<String, TestSourceFile> fileMap = new TreeMap<String, TestSourceFile>();
36
protected String fileWithSelection;
37
protected TextSelection selection;
39
protected RewriteBaseTest(String name) {
43
public RewriteBaseTest(String name, Vector<TestSourceFile> files) {
45
for (TestSourceFile file : files) {
46
fileMap.put(file.getName(), file);
51
protected abstract void runTest() throws Throwable;
55
protected void setUp() throws Exception {
57
for (TestSourceFile testFile : fileMap.values()) {
58
if(testFile.getSource().length() > 0) {
59
importFile(testFile.getName(), testFile.getSource());
64
protected void assertEquals(TestSourceFile file, IFile file2) throws Exception {
65
StringBuffer code = getCodeFromIFile(file2);
66
assertEquals(file.getExpectedSource(), TestHelper.unifyNewLines(code.toString()));
69
protected void compareFiles(Map<String,TestSourceFile> testResourceFiles) throws Exception {
70
for (String fileName : testResourceFiles.keySet()) {
71
TestSourceFile file = testResourceFiles.get(fileName);
72
IFile iFile = project.getFile(new Path(fileName));
73
StringBuffer code = getCodeFromIFile(iFile);
74
assertEquals(TestHelper.unifyNewLines(file.getExpectedSource()), TestHelper.unifyNewLines(code.toString()));
78
protected StringBuffer getCodeFromIFile(IFile file) throws Exception {
79
BufferedReader br = new BufferedReader(new InputStreamReader(file.getContents()));
80
StringBuffer code = new StringBuffer();
82
while((line = br.readLine()) != null) {
91
protected void tearDown() throws Exception {
93
fileManager.closeAllFiles();
97
public void logging(IStatus status, String plugin) {
98
Throwable ex = status.getException();
99
StringBuffer stackTrace = new StringBuffer();
101
stackTrace.append('\n');
102
for(StackTraceElement ste : ex.getStackTrace()) {
103
stackTrace.append(ste.toString());
106
fail("Log-Message: " + status.getMessage() + stackTrace.toString()); //$NON-NLS-1$
109
public void setFileWithSelection(String fileWithSelection) {
110
this.fileWithSelection = fileWithSelection;
113
public void setSelection(TextSelection selection) {
114
this.selection = selection;