mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-06-06 17:26:01 +02:00
162172: Add initial test case, fix bug
This commit is contained in:
parent
53f01e7086
commit
94a253b327
4 changed files with 154 additions and 6 deletions
|
@ -0,0 +1,149 @@
|
|||
/*******************************************************************************
|
||||
* Copyright (c) 2006 Symbian Software Systems 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:
|
||||
* Andrew Ferguson (Symbian) - Initial implementation
|
||||
*******************************************************************************/
|
||||
package org.eclipse.cdt.internal.index.tests;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.net.URI;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import junit.framework.Test;
|
||||
|
||||
import org.eclipse.cdt.core.CCorePlugin;
|
||||
import org.eclipse.cdt.core.dom.IPDOMManager;
|
||||
import org.eclipse.cdt.core.dom.ast.IBinding;
|
||||
import org.eclipse.cdt.core.index.IIndex;
|
||||
import org.eclipse.cdt.core.index.IIndexName;
|
||||
import org.eclipse.cdt.core.index.IndexFilter;
|
||||
import org.eclipse.cdt.core.model.ICProject;
|
||||
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.resources.IFile;
|
||||
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.osgi.framework.Bundle;
|
||||
|
||||
public class IndexLocationTest extends BaseTestCase {
|
||||
ICProject cproject;
|
||||
File movedLocation;
|
||||
File externalHeader;
|
||||
|
||||
public static Test suite() {
|
||||
return suite(IndexLocationTest.class);
|
||||
}
|
||||
|
||||
protected void setUp() throws Exception {
|
||||
cproject= CProjectHelper.createCProject("LocationTests", "bin", IPDOMManager.ID_NO_INDEXER);
|
||||
|
||||
Bundle b = CTestPlugin.getDefault().getBundle();
|
||||
StringBuffer[] testData = TestSourceReader.getContentsForTest(b, "parser", getClass(), getName(), 3);
|
||||
|
||||
movedLocation = freshDir();
|
||||
externalHeader = new File(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.getPDOMManager().setIndexerId(cproject, IPDOMManager.ID_FAST_INDEXER);
|
||||
assertTrue(CCorePlugin.getIndexManager().joinIndexer(10000, new NullProgressMonitor()));
|
||||
|
||||
super.setUp();
|
||||
}
|
||||
|
||||
private void createExternalFile(File dest, String content) throws IOException {
|
||||
FileOutputStream fos = new FileOutputStream(dest);
|
||||
fos.write(content.getBytes());
|
||||
fos.close();
|
||||
}
|
||||
|
||||
protected void tearDown() throws Exception {
|
||||
if (cproject != null) {
|
||||
cproject.getProject().delete(IResource.FORCE | IResource.ALWAYS_DELETE_PROJECT_CONTENT, new NullProgressMonitor());
|
||||
}
|
||||
movedLocation.delete();
|
||||
externalHeader.delete();
|
||||
externalHeader.getParentFile().delete();
|
||||
super.tearDown();
|
||||
}
|
||||
|
||||
// //header
|
||||
// class foo {};
|
||||
|
||||
// // external.h
|
||||
// class bar {};
|
||||
|
||||
// //source
|
||||
// #include "header.h"
|
||||
// #include "ABS_EXTERNAL"
|
||||
// class baz {};
|
||||
public void testBasicLocations() throws CoreException, ExecutionException {
|
||||
IIndex index = CCorePlugin.getIndexManager().getIndex(cproject);
|
||||
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);
|
||||
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())
|
||||
);
|
||||
}
|
||||
|
||||
private File freshDir() throws IOException {
|
||||
File tempDir = new File(System.getProperty("java.io.tmpdir")).getCanonicalFile();
|
||||
for(int i=0; i<Integer.MAX_VALUE; i++) {
|
||||
File candidate = new File(tempDir, "__testData/"+i);
|
||||
if(!candidate.exists()) {
|
||||
candidate.mkdirs();
|
||||
return candidate;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
|
@ -1,5 +0,0 @@
|
|||
package org.eclipse.cdt.internal.index.tests;
|
||||
|
||||
public class IndexLocationTests {
|
||||
|
||||
}
|
|
@ -21,6 +21,7 @@ public class IndexTests extends TestSuite {
|
|||
public static Test suite() {
|
||||
TestSuite suite = new IndexTests();
|
||||
suite.addTest(IndexListenerTest.suite());
|
||||
suite.addTest(IndexLocationTest.suite());
|
||||
suite.addTest(IndexSearchTest.suite());
|
||||
suite.addTest(IndexIncludeTest.suite());
|
||||
suite.addTest(IndexBugsTests.suite());
|
||||
|
|
|
@ -16,6 +16,7 @@ import java.net.URISyntaxException;
|
|||
import org.eclipse.cdt.core.index.IIndexFileLocation;
|
||||
import org.eclipse.cdt.internal.core.index.IndexFileLocation;
|
||||
import org.eclipse.core.resources.IProject;
|
||||
import org.eclipse.core.resources.IResource;
|
||||
import org.eclipse.core.resources.IWorkspaceRoot;
|
||||
import org.eclipse.core.resources.ResourcesPlugin;
|
||||
import org.eclipse.core.runtime.Path;
|
||||
|
@ -46,7 +47,9 @@ public class PDOMProjectIndexLocationConverter implements IIndexLocationConverte
|
|||
uri = null;
|
||||
}
|
||||
} else {
|
||||
uri = project.findMember(raw).getLocationURI();
|
||||
fullPath = "/"+project.getName()+"/"+raw; //$NON-NLS-1$//$NON-NLS-2$
|
||||
IResource member = project.findMember(raw);
|
||||
uri = member == null ? null : member.getLocationURI();
|
||||
}
|
||||
return new IndexFileLocation(uri, fullPath);
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue