mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-06-06 01:06:01 +02:00
Avoid usage of filebuffers in core tests (loads cdt-ui plugin)
This commit is contained in:
parent
de14fed9ad
commit
4fe0f611dd
4 changed files with 59 additions and 49 deletions
|
@ -24,10 +24,8 @@ Require-Bundle: org.eclipse.core.resources,
|
|||
org.junit,
|
||||
org.eclipse.core.runtime,
|
||||
org.eclipse.ui.ide,
|
||||
org.eclipse.jface,
|
||||
org.eclipse.ui,
|
||||
org.eclipse.jface.text,
|
||||
org.eclipse.core.filebuffers
|
||||
org.eclipse.jface.text
|
||||
Eclipse-LazyStart: true
|
||||
Bundle-Vendor: Eclipse.org
|
||||
Bundle-RequiredExecutionEnvironment: J2SE-1.4
|
||||
|
|
|
@ -14,7 +14,6 @@ package org.eclipse.cdt.internal.pdom.tests;
|
|||
import java.io.File;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import junit.framework.Assert;
|
||||
import junit.framework.AssertionFailedError;
|
||||
import junit.framework.Test;
|
||||
|
||||
|
@ -27,10 +26,8 @@ import org.eclipse.cdt.core.dom.ast.ITypedef;
|
|||
import org.eclipse.cdt.core.index.IIndex;
|
||||
import org.eclipse.cdt.core.index.IndexFilter;
|
||||
import org.eclipse.cdt.core.model.ICProject;
|
||||
import org.eclipse.cdt.core.testplugin.util.TestSourceReader;
|
||||
import org.eclipse.cdt.internal.core.pdom.PDOM;
|
||||
import org.eclipse.core.filebuffers.FileBuffers;
|
||||
import org.eclipse.core.filebuffers.ITextFileBuffer;
|
||||
import org.eclipse.core.filebuffers.ITextFileBufferManager;
|
||||
import org.eclipse.core.runtime.CoreException;
|
||||
import org.eclipse.core.runtime.NullProgressMonitor;
|
||||
import org.eclipse.core.runtime.Path;
|
||||
|
@ -74,22 +71,22 @@ public class DefDeclTests extends PDOMTestBase {
|
|||
}
|
||||
|
||||
private void checkReference(IBinding element, String mark, int checkCount)
|
||||
throws CoreException, BadLocationException {
|
||||
throws Exception {
|
||||
checkUsage(element, mark, checkCount, IIndex.FIND_REFERENCES);
|
||||
}
|
||||
|
||||
private void checkDeclaration(IBinding element, String mark, int num)
|
||||
throws CoreException, BadLocationException {
|
||||
throws Exception {
|
||||
checkUsage(element, mark, num, IIndex.FIND_DECLARATIONS);
|
||||
}
|
||||
|
||||
private void checkDefinition(IBinding element, String mark, int countNum)
|
||||
throws CoreException, BadLocationException {
|
||||
throws Exception {
|
||||
checkUsage(element, mark, countNum, IIndex.FIND_DEFINITIONS);
|
||||
}
|
||||
|
||||
private void checkUsage(IBinding element, String mark, int countNum,
|
||||
int flags) throws CoreException, BadLocationException {
|
||||
int flags) throws Exception {
|
||||
if (mark == null || countNum == 0) {
|
||||
getFirstUsage(element, 0, flags);
|
||||
} else {
|
||||
|
@ -145,45 +142,27 @@ public class DefDeclTests extends PDOMTestBase {
|
|||
}
|
||||
|
||||
protected void assertAtMark(IASTFileLocation loc, String mark)
|
||||
throws CoreException, BadLocationException {
|
||||
throws Exception {
|
||||
String fileName = new File(loc.getFileName()).getName();
|
||||
int markLine = getMarkLine(mark, fileName);
|
||||
int nodeLine = getLineNumber(loc.getNodeOffset(), fileName);
|
||||
assertEquals(markLine, nodeLine);
|
||||
}
|
||||
|
||||
private int getMarkLine(String mark, String fileName) throws CoreException,
|
||||
private int getMarkLine(String mark, String fileName) throws Exception,
|
||||
BadLocationException {
|
||||
int markLine = getLineNumber(offset(fileName, mark), fileName);
|
||||
return markLine;
|
||||
}
|
||||
|
||||
protected int getLineNumber(int position, String projectRelativePath)
|
||||
throws CoreException {
|
||||
throws Exception {
|
||||
Path fullPath = new Path(projectName + "/" + projectRelativePath);
|
||||
ITextFileBufferManager fbm = FileBuffers.getTextFileBufferManager();
|
||||
fbm.connect(fullPath, new NullProgressMonitor());
|
||||
try {
|
||||
ITextFileBuffer buf = FileBuffers.getTextFileBufferManager()
|
||||
.getTextFileBuffer(fullPath);
|
||||
Assert.assertTrue("Could not find " + fullPath.toString(), buf
|
||||
.getModificationStamp() > 0);
|
||||
String content = buf.getDocument().get();
|
||||
int len = content.length();
|
||||
int line = 1;
|
||||
for (int i = 0; i < len && i < position; i++) {
|
||||
char c = content.charAt(i);
|
||||
if (c == '\n')
|
||||
line++;
|
||||
}
|
||||
return line;
|
||||
} finally {
|
||||
fbm.disconnect(fullPath, new NullProgressMonitor());
|
||||
}
|
||||
return TestSourceReader.getLineNumber(position, fullPath);
|
||||
}
|
||||
|
||||
public void assertDefDeclRef(String name, String testNum, int def,
|
||||
int decl, int ref) throws CoreException, BadLocationException {
|
||||
int decl, int ref) throws Exception {
|
||||
String elName = name + testNum;
|
||||
IBinding binding = findSingleBinding(elName);
|
||||
checkDefinition(binding, "def" + testNum, def);
|
||||
|
|
|
@ -47,7 +47,6 @@ import org.eclipse.core.runtime.IStatus;
|
|||
import org.eclipse.core.runtime.NullProgressMonitor;
|
||||
import org.eclipse.core.runtime.Path;
|
||||
import org.eclipse.core.runtime.Status;
|
||||
import org.eclipse.jface.text.BadLocationException;
|
||||
import org.eclipse.ui.dialogs.IOverwriteQuery;
|
||||
import org.eclipse.ui.wizards.datatransfer.FileSystemStructureProvider;
|
||||
import org.eclipse.ui.wizards.datatransfer.ImportOperation;
|
||||
|
@ -104,7 +103,7 @@ public class PDOMTestBase extends BaseTestCase {
|
|||
return cprojects[0];
|
||||
}
|
||||
|
||||
protected int offset(String projectRelativePath, String lookfor) throws BadLocationException, CoreException {
|
||||
protected int offset(String projectRelativePath, String lookfor) throws Exception, CoreException {
|
||||
Path path= new Path(projectName + "/" + projectRelativePath);
|
||||
return TestSourceReader.indexOfInFile(lookfor, path);
|
||||
}
|
||||
|
|
|
@ -11,22 +11,23 @@
|
|||
|
||||
package org.eclipse.cdt.core.testplugin.util;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.LineNumberReader;
|
||||
import java.io.Reader;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
|
||||
import junit.framework.Assert;
|
||||
|
||||
import org.eclipse.cdt.core.CCorePlugin;
|
||||
import org.eclipse.cdt.core.index.IIndex;
|
||||
import org.eclipse.cdt.core.index.IIndexFile;
|
||||
import org.eclipse.core.filebuffers.FileBuffers;
|
||||
import org.eclipse.core.filebuffers.ITextFileBuffer;
|
||||
import org.eclipse.core.filebuffers.ITextFileBufferManager;
|
||||
import org.eclipse.core.resources.IContainer;
|
||||
import org.eclipse.core.resources.IFile;
|
||||
import org.eclipse.core.resources.ResourcesPlugin;
|
||||
import org.eclipse.core.runtime.CoreException;
|
||||
import org.eclipse.core.runtime.FileLocator;
|
||||
import org.eclipse.core.runtime.IPath;
|
||||
|
@ -42,21 +43,54 @@ public class TestSourceReader {
|
|||
* @param fullPath full path of the workspace file
|
||||
* @return the offset or -1
|
||||
* @throws CoreException
|
||||
* @throws UnsupportedEncodingException
|
||||
* @since 4.0
|
||||
*/
|
||||
public static int indexOfInFile(String lookfor, Path fullPath) throws CoreException {
|
||||
ITextFileBufferManager fbm= FileBuffers.getTextFileBufferManager();
|
||||
fbm.connect(fullPath, new NullProgressMonitor());
|
||||
public static int indexOfInFile(String lookfor, Path fullPath) throws Exception {
|
||||
IFile file= ResourcesPlugin.getWorkspace().getRoot().getFile(fullPath);
|
||||
Reader reader= new BufferedReader(new InputStreamReader(file.getContents(), file.getCharset()));
|
||||
Assert.assertTrue(lookfor.indexOf('\n') == -1);
|
||||
try {
|
||||
ITextFileBuffer buf= FileBuffers.getTextFileBufferManager().getTextFileBuffer(fullPath);
|
||||
Assert.assertTrue("Could not find " + fullPath.toString(), buf.getModificationStamp() > 0);
|
||||
String content= buf.getDocument().get();
|
||||
int result= content.indexOf(lookfor);
|
||||
Assert.assertTrue("Could not find '" + lookfor + "' in " + fullPath.toString(), result >= 0);
|
||||
return result;
|
||||
int c= 0;
|
||||
int offset= 0;
|
||||
StringBuffer buf= new StringBuffer();
|
||||
while ( (c=reader.read()) >= 0) {
|
||||
buf.append((char) c);
|
||||
if (c == '\n') {
|
||||
int idx= buf.indexOf(lookfor);
|
||||
if (idx >= 0) {
|
||||
return idx+offset;
|
||||
}
|
||||
offset+=buf.length();
|
||||
buf.setLength(0);
|
||||
}
|
||||
}
|
||||
int idx= buf.indexOf(lookfor);
|
||||
if (idx >= 0) {
|
||||
return idx+offset;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
finally {
|
||||
fbm.disconnect(fullPath, new NullProgressMonitor());
|
||||
reader.close();
|
||||
}
|
||||
}
|
||||
|
||||
public static int getLineNumber(int offset, Path fullPath) throws Exception {
|
||||
IFile file= ResourcesPlugin.getWorkspace().getRoot().getFile(fullPath);
|
||||
Reader reader= new BufferedReader(new InputStreamReader(file.getContents(), file.getCharset()));
|
||||
try {
|
||||
int line = 1;
|
||||
for (int i = 0; i < offset; i++) {
|
||||
int c= reader.read();
|
||||
Assert.assertTrue(c >= 0);
|
||||
if (c == '\n')
|
||||
line++;
|
||||
}
|
||||
return line;
|
||||
}
|
||||
finally {
|
||||
reader.close();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue