mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-06-07 09:46:02 +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.junit,
|
||||||
org.eclipse.core.runtime,
|
org.eclipse.core.runtime,
|
||||||
org.eclipse.ui.ide,
|
org.eclipse.ui.ide,
|
||||||
org.eclipse.jface,
|
|
||||||
org.eclipse.ui,
|
org.eclipse.ui,
|
||||||
org.eclipse.jface.text,
|
org.eclipse.jface.text
|
||||||
org.eclipse.core.filebuffers
|
|
||||||
Eclipse-LazyStart: true
|
Eclipse-LazyStart: true
|
||||||
Bundle-Vendor: Eclipse.org
|
Bundle-Vendor: Eclipse.org
|
||||||
Bundle-RequiredExecutionEnvironment: J2SE-1.4
|
Bundle-RequiredExecutionEnvironment: J2SE-1.4
|
||||||
|
|
|
@ -14,7 +14,6 @@ package org.eclipse.cdt.internal.pdom.tests;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
import junit.framework.Assert;
|
|
||||||
import junit.framework.AssertionFailedError;
|
import junit.framework.AssertionFailedError;
|
||||||
import junit.framework.Test;
|
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.IIndex;
|
||||||
import org.eclipse.cdt.core.index.IndexFilter;
|
import org.eclipse.cdt.core.index.IndexFilter;
|
||||||
import org.eclipse.cdt.core.model.ICProject;
|
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.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.CoreException;
|
||||||
import org.eclipse.core.runtime.NullProgressMonitor;
|
import org.eclipse.core.runtime.NullProgressMonitor;
|
||||||
import org.eclipse.core.runtime.Path;
|
import org.eclipse.core.runtime.Path;
|
||||||
|
@ -74,22 +71,22 @@ public class DefDeclTests extends PDOMTestBase {
|
||||||
}
|
}
|
||||||
|
|
||||||
private void checkReference(IBinding element, String mark, int checkCount)
|
private void checkReference(IBinding element, String mark, int checkCount)
|
||||||
throws CoreException, BadLocationException {
|
throws Exception {
|
||||||
checkUsage(element, mark, checkCount, IIndex.FIND_REFERENCES);
|
checkUsage(element, mark, checkCount, IIndex.FIND_REFERENCES);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void checkDeclaration(IBinding element, String mark, int num)
|
private void checkDeclaration(IBinding element, String mark, int num)
|
||||||
throws CoreException, BadLocationException {
|
throws Exception {
|
||||||
checkUsage(element, mark, num, IIndex.FIND_DECLARATIONS);
|
checkUsage(element, mark, num, IIndex.FIND_DECLARATIONS);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void checkDefinition(IBinding element, String mark, int countNum)
|
private void checkDefinition(IBinding element, String mark, int countNum)
|
||||||
throws CoreException, BadLocationException {
|
throws Exception {
|
||||||
checkUsage(element, mark, countNum, IIndex.FIND_DEFINITIONS);
|
checkUsage(element, mark, countNum, IIndex.FIND_DEFINITIONS);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void checkUsage(IBinding element, String mark, int countNum,
|
private void checkUsage(IBinding element, String mark, int countNum,
|
||||||
int flags) throws CoreException, BadLocationException {
|
int flags) throws Exception {
|
||||||
if (mark == null || countNum == 0) {
|
if (mark == null || countNum == 0) {
|
||||||
getFirstUsage(element, 0, flags);
|
getFirstUsage(element, 0, flags);
|
||||||
} else {
|
} else {
|
||||||
|
@ -145,45 +142,27 @@ public class DefDeclTests extends PDOMTestBase {
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void assertAtMark(IASTFileLocation loc, String mark)
|
protected void assertAtMark(IASTFileLocation loc, String mark)
|
||||||
throws CoreException, BadLocationException {
|
throws Exception {
|
||||||
String fileName = new File(loc.getFileName()).getName();
|
String fileName = new File(loc.getFileName()).getName();
|
||||||
int markLine = getMarkLine(mark, fileName);
|
int markLine = getMarkLine(mark, fileName);
|
||||||
int nodeLine = getLineNumber(loc.getNodeOffset(), fileName);
|
int nodeLine = getLineNumber(loc.getNodeOffset(), fileName);
|
||||||
assertEquals(markLine, nodeLine);
|
assertEquals(markLine, nodeLine);
|
||||||
}
|
}
|
||||||
|
|
||||||
private int getMarkLine(String mark, String fileName) throws CoreException,
|
private int getMarkLine(String mark, String fileName) throws Exception,
|
||||||
BadLocationException {
|
BadLocationException {
|
||||||
int markLine = getLineNumber(offset(fileName, mark), fileName);
|
int markLine = getLineNumber(offset(fileName, mark), fileName);
|
||||||
return markLine;
|
return markLine;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected int getLineNumber(int position, String projectRelativePath)
|
protected int getLineNumber(int position, String projectRelativePath)
|
||||||
throws CoreException {
|
throws Exception {
|
||||||
Path fullPath = new Path(projectName + "/" + projectRelativePath);
|
Path fullPath = new Path(projectName + "/" + projectRelativePath);
|
||||||
ITextFileBufferManager fbm = FileBuffers.getTextFileBufferManager();
|
return TestSourceReader.getLineNumber(position, fullPath);
|
||||||
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());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void assertDefDeclRef(String name, String testNum, int def,
|
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;
|
String elName = name + testNum;
|
||||||
IBinding binding = findSingleBinding(elName);
|
IBinding binding = findSingleBinding(elName);
|
||||||
checkDefinition(binding, "def" + testNum, def);
|
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.NullProgressMonitor;
|
||||||
import org.eclipse.core.runtime.Path;
|
import org.eclipse.core.runtime.Path;
|
||||||
import org.eclipse.core.runtime.Status;
|
import org.eclipse.core.runtime.Status;
|
||||||
import org.eclipse.jface.text.BadLocationException;
|
|
||||||
import org.eclipse.ui.dialogs.IOverwriteQuery;
|
import org.eclipse.ui.dialogs.IOverwriteQuery;
|
||||||
import org.eclipse.ui.wizards.datatransfer.FileSystemStructureProvider;
|
import org.eclipse.ui.wizards.datatransfer.FileSystemStructureProvider;
|
||||||
import org.eclipse.ui.wizards.datatransfer.ImportOperation;
|
import org.eclipse.ui.wizards.datatransfer.ImportOperation;
|
||||||
|
@ -104,7 +103,7 @@ public class PDOMTestBase extends BaseTestCase {
|
||||||
return cprojects[0];
|
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);
|
Path path= new Path(projectName + "/" + projectRelativePath);
|
||||||
return TestSourceReader.indexOfInFile(lookfor, path);
|
return TestSourceReader.indexOfInFile(lookfor, path);
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,22 +11,23 @@
|
||||||
|
|
||||||
package org.eclipse.cdt.core.testplugin.util;
|
package org.eclipse.cdt.core.testplugin.util;
|
||||||
|
|
||||||
|
import java.io.BufferedReader;
|
||||||
import java.io.ByteArrayInputStream;
|
import java.io.ByteArrayInputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.InputStreamReader;
|
import java.io.InputStreamReader;
|
||||||
import java.io.LineNumberReader;
|
import java.io.LineNumberReader;
|
||||||
|
import java.io.Reader;
|
||||||
|
import java.io.UnsupportedEncodingException;
|
||||||
|
|
||||||
import junit.framework.Assert;
|
import junit.framework.Assert;
|
||||||
|
|
||||||
import org.eclipse.cdt.core.CCorePlugin;
|
import org.eclipse.cdt.core.CCorePlugin;
|
||||||
import org.eclipse.cdt.core.index.IIndex;
|
import org.eclipse.cdt.core.index.IIndex;
|
||||||
import org.eclipse.cdt.core.index.IIndexFile;
|
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.IContainer;
|
||||||
import org.eclipse.core.resources.IFile;
|
import org.eclipse.core.resources.IFile;
|
||||||
|
import org.eclipse.core.resources.ResourcesPlugin;
|
||||||
import org.eclipse.core.runtime.CoreException;
|
import org.eclipse.core.runtime.CoreException;
|
||||||
import org.eclipse.core.runtime.FileLocator;
|
import org.eclipse.core.runtime.FileLocator;
|
||||||
import org.eclipse.core.runtime.IPath;
|
import org.eclipse.core.runtime.IPath;
|
||||||
|
@ -42,21 +43,54 @@ public class TestSourceReader {
|
||||||
* @param fullPath full path of the workspace file
|
* @param fullPath full path of the workspace file
|
||||||
* @return the offset or -1
|
* @return the offset or -1
|
||||||
* @throws CoreException
|
* @throws CoreException
|
||||||
|
* @throws UnsupportedEncodingException
|
||||||
* @since 4.0
|
* @since 4.0
|
||||||
*/
|
*/
|
||||||
public static int indexOfInFile(String lookfor, Path fullPath) throws CoreException {
|
public static int indexOfInFile(String lookfor, Path fullPath) throws Exception {
|
||||||
ITextFileBufferManager fbm= FileBuffers.getTextFileBufferManager();
|
IFile file= ResourcesPlugin.getWorkspace().getRoot().getFile(fullPath);
|
||||||
fbm.connect(fullPath, new NullProgressMonitor());
|
Reader reader= new BufferedReader(new InputStreamReader(file.getContents(), file.getCharset()));
|
||||||
|
Assert.assertTrue(lookfor.indexOf('\n') == -1);
|
||||||
try {
|
try {
|
||||||
ITextFileBuffer buf= FileBuffers.getTextFileBufferManager().getTextFileBuffer(fullPath);
|
int c= 0;
|
||||||
Assert.assertTrue("Could not find " + fullPath.toString(), buf.getModificationStamp() > 0);
|
int offset= 0;
|
||||||
String content= buf.getDocument().get();
|
StringBuffer buf= new StringBuffer();
|
||||||
int result= content.indexOf(lookfor);
|
while ( (c=reader.read()) >= 0) {
|
||||||
Assert.assertTrue("Could not find '" + lookfor + "' in " + fullPath.toString(), result >= 0);
|
buf.append((char) c);
|
||||||
return result;
|
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 {
|
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