1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-07-23 17:05:26 +02:00

186214: add test case (plus some tidy)

This commit is contained in:
Andrew Ferguson 2007-05-09 17:55:10 +00:00
parent 6c54ac2de7
commit a923a730aa

View file

@ -14,6 +14,9 @@ import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URI;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.regex.Pattern;
import junit.framework.Test;
@ -33,71 +36,39 @@ import org.eclipse.cdt.core.testplugin.CProjectHelper;
import org.eclipse.cdt.core.testplugin.CTestPlugin;
import org.eclipse.cdt.core.testplugin.util.BaseTestCase;
import org.eclipse.cdt.core.testplugin.util.TestSourceReader;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.filesystem.URIUtil;
import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Platform;
import org.osgi.framework.Bundle;
public class IndexLocationTest extends BaseTestCase {
protected ICProject cproject, emptyCProject;
protected File movedLocation;
protected File externalHeader;
protected IFolder linkedFolder;
protected boolean isWin;
private static final boolean isWin= Platform.getOS().equals(Platform.OS_WIN32);
protected List projects= new ArrayList();
protected ICProject cproject;
public static Test suite() {
return suite(IndexLocationTest.class);
}
protected void setUp() throws Exception {
cproject= CProjectHelper.createCProject("LocationTests", "bin", IPDOMManager.ID_NO_INDEXER);
linkedFolder= cproject.getProject().getFolder("linkedFolder");
emptyCProject= CProjectHelper.createCProject("Empty", "bin", IPDOMManager.ID_NO_INDEXER);
Bundle b = CTestPlugin.getDefault().getBundle();
StringBuffer[] testData = TestSourceReader.getContentsForTest(b, "parser", getClass(), getName(), 3);
movedLocation = CProjectHelper.freshDir();
externalHeader = new File(CProjectHelper.freshDir(),"external.h");
IFile file1 = TestSourceReader.createFile(cproject.getProject(), "header.h", testData[0].toString());
createExternalFile(externalHeader, testData[1].toString());
String content = testData[2].toString().replaceAll("ABS_EXTERNAL", externalHeader.getAbsolutePath().replaceAll("\\\\","\\\\\\\\"));
IFile file3 = TestSourceReader.createFile(cproject.getProject(), "source.cpp", content);
CCorePlugin.getIndexManager().setIndexerId(cproject, IPDOMManager.ID_FAST_INDEXER);
assertTrue(CCorePlugin.getIndexManager().joinIndexer(10000, new NullProgressMonitor()));
isWin= Platform.getOS().equals(Platform.OS_WIN32);
super.setUp();
}
private void createExternalFile(File dest, String content) throws IOException {
FileOutputStream fos = new FileOutputStream(dest);
fos.write(content.getBytes());
fos.close();
cproject= CProjectHelper.createCProject("LocationTests"+System.currentTimeMillis(), "bin", IPDOMManager.ID_FAST_INDEXER);
deleteOnTearDown(cproject);
}
protected void tearDown() throws Exception {
if (cproject != null) {
cproject.getProject().delete(IResource.FORCE | IResource.ALWAYS_DELETE_PROJECT_CONTENT, new NullProgressMonitor());
for(Iterator i= projects.iterator(); i.hasNext(); ) {
ICProject ptd= (ICProject) i.next();
if (ptd != null) {
ptd.getProject().delete(IResource.FORCE | IResource.ALWAYS_DELETE_PROJECT_CONTENT, new NullProgressMonitor());
}
}
if (emptyCProject != null) {
emptyCProject.getProject().delete(IResource.FORCE | IResource.ALWAYS_DELETE_PROJECT_CONTENT, new NullProgressMonitor());
}
movedLocation.delete();
externalHeader.delete();
externalHeader.getParentFile().delete();
super.tearDown();
}
@ -111,61 +82,133 @@ public class IndexLocationTest extends BaseTestCase {
// #include "header.h"
// #include "ABS_EXTERNAL"
// class baz {};
public void testBasicLocations() throws CoreException, ExecutionException, InterruptedException {
public void testBasicLocations() throws Exception {
File externalHeader = new File(CProjectHelper.freshDir(),"external.h");
try {
Bundle b = CTestPlugin.getDefault().getBundle();
StringBuffer[] testData = TestSourceReader.getContentsForTest(b, "parser", getClass(), getName(), 3);
IFile file1 = TestSourceReader.createFile(cproject.getProject(), "header.h", testData[0].toString());
createExternalFile(externalHeader, testData[1].toString());
String content = testData[2].toString().replaceAll("ABS_EXTERNAL", externalHeader.getAbsolutePath().replaceAll("\\\\","\\\\\\\\"));
IFile file3 = TestSourceReader.createFile(cproject.getProject(), "source.cpp", content);
CCorePlugin.getIndexManager().reindex(cproject);
assertTrue(CCorePlugin.getIndexManager().joinIndexer(10000, new NullProgressMonitor()));
IIndex index = CCorePlugin.getIndexManager().getIndex(cproject);
index.acquireReadLock();
try {
IBinding[] bs1 = index.findBindings(Pattern.compile("foo"), true, IndexFilter.ALL, new NullProgressMonitor());
IBinding[] bs2 = index.findBindings(Pattern.compile("bar"), true, IndexFilter.ALL, new NullProgressMonitor());
IBinding[] bs3 = index.findBindings(Pattern.compile("baz"), true, IndexFilter.ALL, new NullProgressMonitor());
assertEquals(1, bs1.length);
assertEquals(1, bs2.length);
assertEquals(1, bs3.length);
bs1 = index.findBindings("foo".toCharArray(), IndexFilter.ALL, new NullProgressMonitor());
bs2 = index.findBindings("bar".toCharArray(), IndexFilter.ALL, new NullProgressMonitor());
bs3 = index.findBindings("baz".toCharArray(), IndexFilter.ALL, new NullProgressMonitor());
assertEquals(1, bs1.length);
assertEquals(1, bs2.length);
assertEquals(1, bs3.length);
IIndexName[] nms1 = index.findNames(bs1[0], IIndex.FIND_ALL_OCCURENCES);
IIndexName[] nms2 = index.findNames(bs2[0], IIndex.FIND_ALL_OCCURENCES);
IIndexName[] nms3 = index.findNames(bs3[0], IIndex.FIND_ALL_OCCURENCES);
assertEquals(1, nms1.length);
assertEquals(1, nms2.length);
assertEquals(1, nms3.length);
URI workspaceRoot = ResourcesPlugin.getWorkspace().getRoot().getLocationURI();
assertEquals(
ResourcesPlugin.getWorkspace().getRoot().getFile(new Path(cproject.getProject().getName()+"/header.h")).getLocationURI(),
nms1[0].getFile().getLocation().getURI()
);
assertEquals(
externalHeader.toURI(),
nms2[0].getFile().getLocation().getURI()
);
assertEquals(
ResourcesPlugin.getWorkspace().getRoot().getFile(new Path(cproject.getProject().getName()+"/source.cpp")).getLocationURI(),
nms3[0].getFile().getLocation().getURI()
);
assertEquals(
ResourcesPlugin.getWorkspace().getRoot().getFile(new Path(cproject.getProject().getName()+"/header.h")).getFullPath(),
new Path(nms1[0].getFile().getLocation().getFullPath())
);
assertEquals(
null,
nms2[0].getFile().getLocation().getFullPath()
);
assertEquals(
ResourcesPlugin.getWorkspace().getRoot().getFile(new Path(cproject.getProject().getName()+"/source.cpp")).getFullPath(),
new Path(nms3[0].getFile().getLocation().getFullPath())
);
}
finally {
index.releaseReadLock();
}
} finally {
externalHeader.delete();
externalHeader.getParentFile().delete();
}
}
public void testLinkedFilesIndexedAsWithinProject() throws Exception {
File location = new File(CProjectHelper.freshDir(), "external2.h");
createExternalFile(location, "struct External {};\n");
IFolder content= cproject.getProject().getFolder("content");
content.createLink(new Path(location.getParentFile().getAbsolutePath()), IResource.NONE, NPM);
CCorePlugin.getIndexManager().reindex(cproject);
assertTrue(CCorePlugin.getIndexManager().joinIndexer(10000, new NullProgressMonitor()));
IIndex index = CCorePlugin.getIndexManager().getIndex(cproject);
index.acquireReadLock();
try {
IBinding[] bs1 = index.findBindings(Pattern.compile("foo"), true, IndexFilter.ALL, new NullProgressMonitor());
IBinding[] bs2 = index.findBindings(Pattern.compile("bar"), true, IndexFilter.ALL, new NullProgressMonitor());
IBinding[] bs3 = index.findBindings(Pattern.compile("baz"), true, IndexFilter.ALL, new NullProgressMonitor());
assertEquals(1, bs1.length);
assertEquals(1, bs2.length);
assertEquals(1, bs3.length);
bs1 = index.findBindings("foo".toCharArray(), IndexFilter.ALL, new NullProgressMonitor());
bs2 = index.findBindings("bar".toCharArray(), IndexFilter.ALL, new NullProgressMonitor());
bs3 = index.findBindings("baz".toCharArray(), IndexFilter.ALL, new NullProgressMonitor());
assertEquals(1, bs1.length);
assertEquals(1, bs2.length);
assertEquals(1, bs3.length);
IIndexName[] nms1 = index.findNames(bs1[0], IIndex.FIND_ALL_OCCURENCES);
IIndexName[] nms2 = index.findNames(bs2[0], IIndex.FIND_ALL_OCCURENCES);
IIndexName[] nms3 = index.findNames(bs3[0], IIndex.FIND_ALL_OCCURENCES);
assertEquals(1, nms1.length);
assertEquals(1, nms2.length);
assertEquals(1, nms3.length);
URI workspaceRoot = ResourcesPlugin.getWorkspace().getRoot().getLocationURI();
assertEquals(
ResourcesPlugin.getWorkspace().getRoot().getFile(new Path("LocationTests/header.h")).getLocationURI(),
nms1[0].getFile().getLocation().getURI()
);
assertEquals(
externalHeader.toURI(),
nms2[0].getFile().getLocation().getURI()
);
assertEquals(
ResourcesPlugin.getWorkspace().getRoot().getFile(new Path("LocationTests/source.cpp")).getLocationURI(),
nms3[0].getFile().getLocation().getURI()
);
assertEquals(
ResourcesPlugin.getWorkspace().getRoot().getFile(new Path("LocationTests/header.h")).getFullPath(),
new Path(nms1[0].getFile().getLocation().getFullPath())
);
assertEquals(
null,
nms2[0].getFile().getLocation().getFullPath()
);
assertEquals(
ResourcesPlugin.getWorkspace().getRoot().getFile(new Path("LocationTests/source.cpp")).getFullPath(),
new Path(nms3[0].getFile().getLocation().getFullPath())
);
IBinding[] bs= index.findBindings("External".toCharArray(), IndexFilter.ALL, NPM);
assertEquals(1, bs.length);
IIndexName[] nms= index.findNames(bs[0], IIndex.FIND_ALL_OCCURENCES);
assertEquals(1, nms.length);
IIndexFileLocation ilocation= nms[0].getFile().getLocation();
assertEquals("/"+cproject.getProject().getName()+"/content/external2.h", ilocation.getFullPath());
} finally {
index.releaseReadLock();
}
finally {
}
public void _testSameFileLinkedToOnceInTwoProjects_186214() throws Exception {
File location = new File(CProjectHelper.freshDir(),"external2.h");
createExternalFile(location, "struct External {};\n");
IFolder content= cproject.getProject().getFolder("content");
content.createLink(new Path(location.getParentFile().getAbsolutePath()), IResource.NONE, null);
ICProject cproject2= CProjectHelper.createCProject("LocationTests2"+System.currentTimeMillis(), "bin", IPDOMManager.ID_NO_INDEXER);
deleteOnTearDown(cproject2);
IFolder content2= cproject2.getProject().getFolder("content");
content2.createLink(new Path(location.getParentFile().getAbsolutePath()), IResource.NONE, null);
CCorePlugin.getIndexManager().reindex(cproject);
assertTrue(CCorePlugin.getIndexManager().joinIndexer(10000, new NullProgressMonitor()));
IIndex index = CCorePlugin.getIndexManager().getIndex(cproject);
index.acquireReadLock();
try {
IBinding[] bs= index.findBindings("External".toCharArray(), IndexFilter.ALL, NPM);
assertEquals(1, bs.length);
IIndexName[] nms= index.findNames(bs[0], IIndex.FIND_ALL_OCCURENCES);
assertEquals(1, nms.length);
IIndexFileLocation ilocation= nms[0].getFile().getLocation();
assertEquals("/"+cproject.getProject().getName()+"/content/external2.h", ilocation.getFullPath());
} finally {
index.releaseReadLock();
}
}
public void testResourceContainerRelativeLocationConverter() throws Exception {
ICProject emptyCProject= CProjectHelper.createCProject("Empty", "bin", IPDOMManager.ID_NO_INDEXER);
deleteOnTearDown(emptyCProject);
String[] paths = new String[] {"this.cpp", "inc/header.h", "a b c/d/e f/g.h", "a \\b /c.d"};
for(int i=0; i<paths.length; i++) {
IFile file= cproject.getProject().getFile(paths[i]);
@ -177,8 +220,8 @@ public class IndexLocationTest extends BaseTestCase {
IIndexFileLocation ifl2= prlc2.fromInternalFormat(r1);
assertNotNull(ifl2);
assertEquals(
new Path(ifl1.getFullPath()).removeFirstSegments(1),
new Path(ifl2.getFullPath()).removeFirstSegments(1)
new Path(ifl1.getFullPath()).removeFirstSegments(1),
new Path(ifl2.getFullPath()).removeFirstSegments(1)
);
}
}
@ -259,6 +302,7 @@ public class IndexLocationTest extends BaseTestCase {
}
public void testURLC_RCRLC_Interaction3() throws Exception {
IFolder linkedFolder= cproject.getProject().getFolder("linkedFolder");
String[] winPaths = new String[] {
"a b c/d/e f/g.h",
"a \\b /c.d",
@ -293,4 +337,16 @@ public class IndexLocationTest extends BaseTestCase {
assertEquals(URIUtil.toURI(basePath+paths[i]).normalize(), ifl2.getURI());
}
}
private void deleteOnTearDown(ICProject cproject) {
if(cproject!=null) {
projects.add(cproject);
}
}
private void createExternalFile(File dest, String content) throws IOException {
FileOutputStream fos = new FileOutputStream(dest);
fos.write(content.getBytes());
fos.close();
}
}