diff --git a/core/org.eclipse.cdt.core.tests/META-INF/MANIFEST.MF b/core/org.eclipse.cdt.core.tests/META-INF/MANIFEST.MF index fb68408aabe..33e56b373eb 100644 --- a/core/org.eclipse.cdt.core.tests/META-INF/MANIFEST.MF +++ b/core/org.eclipse.cdt.core.tests/META-INF/MANIFEST.MF @@ -26,6 +26,7 @@ Require-Bundle: org.eclipse.core.resources, org.eclipse.ui.ide, org.eclipse.jface, org.eclipse.ui, - org.eclipse.jface.text + org.eclipse.jface.text, + org.eclipse.core.filebuffers Eclipse-LazyStart: true Bundle-Vendor: Eclipse.org diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/internal/pdom/tests/ClassTests.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/internal/pdom/tests/ClassTests.java index 13dcf43eeac..c87e55a0ee9 100644 --- a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/internal/pdom/tests/ClassTests.java +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/internal/pdom/tests/ClassTests.java @@ -7,6 +7,7 @@ * * Contributors: * QNX - Initial API and implementation + * Markus Schorn (Wind River Systems) *******************************************************************************/ package org.eclipse.cdt.internal.pdom.tests; @@ -56,7 +57,7 @@ public class ClassTests extends PDOMTestBase { IName [] Bf_refs = pdom.getReferences(Bf); assertEquals(1, Bf_refs.length); IASTFileLocation loc = Bf_refs[0].getFileLocation(); - assertEquals(offset(95, 84), loc.getNodeOffset()); + assertEquals(offset("class.cpp", "b.f()") + 2, loc.getNodeOffset()); } public void testNested() throws Exception { @@ -74,12 +75,12 @@ public class ClassTests extends PDOMTestBase { IName[] refs = pdom.getReferences(NestedB); assertEquals(1, refs.length); IASTFileLocation loc = refs[0].getFileLocation(); - assertEquals(offset(96, 87), loc.getNodeOffset()); + assertEquals(offset("nested.cpp", "::NestedB") + 2, loc.getNodeOffset()); refs = pdom.getReferences(NestedB_x); assertEquals(1, refs.length); loc = refs[0].getFileLocation(); - assertEquals(offset(118, 108), loc.getNodeOffset()); + assertEquals(offset("nested.cpp", "x.x") + 2, loc.getNodeOffset()); } public void failedTest147903() throws Exception { diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/internal/pdom/tests/EnumerationTests.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/internal/pdom/tests/EnumerationTests.java index 8c4018fc13d..a9bb4f2f979 100644 --- a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/internal/pdom/tests/EnumerationTests.java +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/internal/pdom/tests/EnumerationTests.java @@ -7,6 +7,7 @@ * * Contributors: * QNX - Initial API and implementation + * Markus Schorn (Wind River Systems) *******************************************************************************/ package org.eclipse.cdt.internal.pdom.tests; @@ -65,13 +66,13 @@ public class EnumerationTests extends PDOMTestBase { IName[] enumRefs = pdom.getReferences(enumeration); assertEquals(1, enumRefs.length); loc = enumRefs[0].getFileLocation(); - assertEquals(offset(46, 40), loc.getNodeOffset()); + assertEquals(offset("enumTest.c", "TestCEnum test"), loc.getNodeOffset()); // Reference to a IName[] aRefs = pdom.getReferences(enumerators[0]); assertEquals(1, aRefs.length); loc = aRefs[0].getFileLocation(); - assertEquals(offset(74, 67), loc.getNodeOffset()); + assertEquals(offset("enumTest.c", "ca;"), loc.getNodeOffset()); } public void testCPP() throws Exception { @@ -97,13 +98,13 @@ public class EnumerationTests extends PDOMTestBase { IName[] enumRefs = pdom.getReferences(enumeration); assertEquals(1, enumRefs.length); loc = enumRefs[0].getFileLocation(); - assertEquals(offset(49, 43), loc.getNodeOffset()); + assertEquals(offset("enumTest.cpp", "TestCPPEnum test"), loc.getNodeOffset()); // Reference to a IName[] aRefs = pdom.getReferences(enumerators[0]); assertEquals(1, aRefs.length); loc = aRefs[0].getFileLocation(); - assertEquals(offset(79, 72), loc.getNodeOffset()); + assertEquals(offset("enumTest.cpp", "cppa;"), loc.getNodeOffset()); } } diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/internal/pdom/tests/PDOMTestBase.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/internal/pdom/tests/PDOMTestBase.java index a806acbeed8..40ad96e68dc 100644 --- a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/internal/pdom/tests/PDOMTestBase.java +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/internal/pdom/tests/PDOMTestBase.java @@ -7,6 +7,7 @@ * * Contributors: * QNX - Initial API and implementation + * Markus Schorn (Wind River Systems) *******************************************************************************/ package org.eclipse.cdt.internal.pdom.tests; @@ -20,6 +21,7 @@ import org.eclipse.cdt.core.CProjectNature; import org.eclipse.cdt.core.dom.IPDOMManager; import org.eclipse.cdt.core.model.ICProject; import org.eclipse.cdt.core.testplugin.CTestPlugin; +import org.eclipse.cdt.core.testplugin.util.TestSourceReader; import org.eclipse.cdt.internal.core.pdom.indexer.fast.PDOMFastIndexer; import org.eclipse.cdt.internal.core.pdom.indexer.fast.PDOMFastReindex; import org.eclipse.cdt.internal.core.pdom.indexer.nulli.PDOMNullIndexer; @@ -33,8 +35,8 @@ import org.eclipse.core.runtime.IPath; import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.core.runtime.IStatus; import org.eclipse.core.runtime.Path; -import org.eclipse.core.runtime.Platform; 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; @@ -45,11 +47,12 @@ import org.eclipse.ui.wizards.datatransfer.ImportOperation; public class PDOMTestBase extends TestCase { static IPath rootPath = new Path("resources/pdomtests"); + private String projectName= null; protected ICProject createProject(String folderName) throws CoreException { // Create the project - final String projectName = "ProjTest_" + System.currentTimeMillis(); + projectName = "ProjTest_" + System.currentTimeMillis(); final File rootDir = CTestPlugin.getDefault().getFileInPlugin(rootPath.append(folderName)); final IWorkspace workspace = ResourcesPlugin.getWorkspace(); final ICProject cprojects[] = new ICProject[1]; @@ -112,15 +115,8 @@ public class PDOMTestBase extends TestCase { return cprojects[0]; } - /** - * Use to pick the right offset depending on what platform we're - * running. Windows has the extra character for new lines. - * - * @param winNum - * @param nixNum - * @return - */ - protected int offset(int winNum, int nixNum) { - return Platform.getOS().equals(Platform.OS_WIN32) ? winNum : nixNum; + protected int offset(String projectRelativePath, String lookfor) throws BadLocationException, CoreException { + Path path= new Path(projectName + "/" + projectRelativePath); + return TestSourceReader.indexOfInFile(lookfor, path); } } diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/internal/pdom/tests/TypesTests.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/internal/pdom/tests/TypesTests.java index ae095e27c0a..73bdb6ea212 100644 --- a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/internal/pdom/tests/TypesTests.java +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/internal/pdom/tests/TypesTests.java @@ -7,6 +7,7 @@ * * Contributors: * QNX - Initial API and implementation + * Markus Schorn (Wind River Systems) *******************************************************************************/ package org.eclipse.cdt.internal.pdom.tests; @@ -59,7 +60,7 @@ public class TypesTests extends PDOMTestBase { IName[] xRefs = pdom.getReferences(x); assertEquals(1, xRefs.length); IASTFileLocation loc = xRefs[0].getFileLocation(); - assertEquals(offset(85, 77), loc.getNodeOffset()); + assertEquals(offset("typedef.c", "x->x") + 3, loc.getNodeOffset()); } public void testCPP() throws Exception { @@ -76,7 +77,7 @@ public class TypesTests extends PDOMTestBase { IName[] fRefs = pdom.getReferences(f); assertEquals(1, fRefs.length); IASTFileLocation loc = fRefs[0].getFileLocation(); - assertEquals(offset(84, 74), loc.getNodeOffset()); + assertEquals(offset("typedef.cpp", "x->f") + 3, loc.getNodeOffset()); } public void test145351() throws Exception { @@ -86,7 +87,7 @@ public class TypesTests extends PDOMTestBase { IName [] refs = pdom.getReferences(spinlock_t); assertEquals(1, refs.length); IASTFileLocation loc = refs[0].getFileLocation(); - assertEquals(offset(44, 40), loc.getNodeOffset()); + assertEquals(offset("bug145351.c", "spinlock_t global_bh_lock"), loc.getNodeOffset()); } } diff --git a/core/org.eclipse.cdt.core.tests/suite/org/eclipse/cdt/core/testplugin/util/TestSourceReader.java b/core/org.eclipse.cdt.core.tests/suite/org/eclipse/cdt/core/testplugin/util/TestSourceReader.java new file mode 100644 index 00000000000..35e685c1ad6 --- /dev/null +++ b/core/org.eclipse.cdt.core.tests/suite/org/eclipse/cdt/core/testplugin/util/TestSourceReader.java @@ -0,0 +1,129 @@ +/******************************************************************************* + * Copyright (c) 2006 Wind River Systems, Inc. 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: + * Markus Schorn - initial API and implementation + *******************************************************************************/ + +package org.eclipse.cdt.core.testplugin.util; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.LineNumberReader; + +import junit.framework.Assert; + +import org.eclipse.cdt.core.testplugin.CTestPlugin; +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.runtime.CoreException; +import org.eclipse.core.runtime.FileLocator; +import org.eclipse.core.runtime.IPath; +import org.eclipse.core.runtime.NullProgressMonitor; +import org.eclipse.core.runtime.Path; + +public class TestSourceReader { + + /** + * Searches for the offset of the first occurrence of a string in a workspace file. + * @param lookfor string to be searched for + * @param fullPath full path of the workspace file + * @return the offset or -1 + * @throws CoreException + * @since 4.0 + */ + public static int indexOfInFile(String lookfor, Path fullPath) throws CoreException { + 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 result= content.indexOf(lookfor); + Assert.assertTrue("Could not find '" + lookfor + "' in " + fullPath.toString(), result >= 0); + return result; + } + finally { + fbm.disconnect(fullPath, new NullProgressMonitor()); + } + } + + /** + * Reads a section in comments form the source of the given class. The section + * is started with '// {tag}' and ends with the first line not started by '//' + * @since 4.0 + */ + public static String readTaggedComment(Class clazz, final String tag) throws IOException { + IPath filePath= new Path("ui/" + clazz.getName().replace('.', '/') + ".java"); + + InputStream in= FileLocator.openStream(CTestPlugin.getDefault().getBundle(), filePath, false); + LineNumberReader reader= new LineNumberReader(new InputStreamReader(in)); + boolean found= false; + final StringBuffer content= new StringBuffer(); + try { + String line= reader.readLine(); + while (line != null) { + line= line.trim(); + if (line.startsWith("//")) { + line= line.substring(2); + if (found) { + content.append(line); + content.append('\n'); + } + else { + line= line.trim(); + if (line.startsWith("{" + tag)) { + if (line.length() == tag.length()+1 || + !Character.isJavaIdentifierPart(line.charAt(tag.length()+1))) { + found= true; + } + } + } + } + else if (found) { + break; + } + line= reader.readLine(); + } + } + finally { + reader.close(); + } + Assert.assertTrue("Tag '" + tag + "' is not defined inside of '" + filePath + "'.", found); + return content.toString(); + } + + /** + * Creates a file with content at the given path inside the given container. + * If the file exists its content is replaced. + * @param container a container to create the file in + * @param filePath the path relative to the container to create the file at + * @param contents the content for the file + * @return a file object. + * @throws Exception + * @since 4.0 + */ + public static IFile createFile(IContainer container, IPath filePath, String contents) throws Exception { + //Obtain file handle + IFile file = container.getFile(filePath); + + InputStream stream = new ByteArrayInputStream(contents.getBytes()); + //Create file input stream + if (file.exists()) { + file.setContents(stream, false, false, new NullProgressMonitor()); + } + else { + file.create(stream, false, new NullProgressMonitor()); + } + return file; + } +} diff --git a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/BaseTestCase.java b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/BaseTestCase.java index 95a1610be01..dee089763ae 100644 --- a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/BaseTestCase.java +++ b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/BaseTestCase.java @@ -11,11 +11,7 @@ package org.eclipse.cdt.ui.tests; -import java.io.ByteArrayInputStream; import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.io.LineNumberReader; import java.lang.reflect.Method; import java.lang.reflect.Modifier; import java.util.Vector; @@ -30,9 +26,6 @@ import junit.framework.TestSuite; import org.eclipse.core.resources.IContainer; import org.eclipse.core.resources.IFile; import org.eclipse.core.runtime.CoreException; -import org.eclipse.core.runtime.FileLocator; -import org.eclipse.core.runtime.IPath; -import org.eclipse.core.runtime.NullProgressMonitor; import org.eclipse.core.runtime.Path; import org.eclipse.swt.widgets.Display; @@ -42,7 +35,7 @@ import org.eclipse.cdt.core.model.ICElement; import org.eclipse.cdt.core.model.ICProject; import org.eclipse.cdt.core.model.ILanguage; import org.eclipse.cdt.core.model.ITranslationUnit; -import org.eclipse.cdt.ui.testplugin.CTestPlugin; +import org.eclipse.cdt.core.testplugin.util.TestSourceReader; import org.eclipse.cdt.internal.core.pdom.PDOM; import org.eclipse.cdt.internal.core.pdom.dom.PDOMFile; @@ -137,87 +130,17 @@ public class BaseTestCase extends TestCase { fBugnumber= bugnumber; } - /** - * Reads a section in comments form the source of the given class. The section - * is started with '// {tag}' and ends with the first line not started by '//' - * @since 4.0 - */ - protected String readTaggedComment(Class clazz, final String tag) throws IOException { - IPath filePath= new Path("ui/" + clazz.getName().replace('.', '/') + ".java"); - - InputStream in= FileLocator.openStream(CTestPlugin.getDefault().getBundle(), filePath, false); - LineNumberReader reader= new LineNumberReader(new InputStreamReader(in)); - boolean found= false; - final StringBuffer content= new StringBuffer(); - try { - String line= reader.readLine(); - while (line != null) { - line= line.trim(); - if (line.startsWith("//")) { - line= line.substring(2); - if (found) { - content.append(line); - content.append('\n'); - } - else { - line= line.trim(); - if (line.startsWith("{" + tag)) { - if (line.length() == tag.length()+1 || - !Character.isJavaIdentifierPart(line.charAt(tag.length()+1))) { - found= true; - } - } - } - } - else if (found) { - break; - } - line= reader.readLine(); - } - } - finally { - reader.close(); - } - assertTrue("Tag '" + tag + "' is not defined inside of '" + filePath + "'.", found); - return content.toString(); - } - /** * Reads a section in comments form the source of the given class. Fully * equivalent to readTaggedComment(getClass(), tag) * @since 4.0 */ protected String readTaggedComment(final String tag) throws IOException { - return readTaggedComment(getClass(), tag); + return TestSourceReader.readTaggedComment(getClass(), tag); } - /** - * Creates a file with content at the given path inside the given container. - * If the file exists its content is replaced. - * @param container a container to create the file in - * @param filePath the path relative to the container to create the file at - * @param contents the content for the file - * @return a file object. - * @throws Exception - * @since 4.0 - */ - protected IFile createFile(IContainer container, IPath filePath, String contents) throws Exception { - //Obtain file handle - IFile file = container.getFile(filePath); - - InputStream stream = new ByteArrayInputStream(contents.getBytes()); - //Create file input stream - if (file.exists()) { - file.setContents(stream, false, false, new NullProgressMonitor()); - } - else { - file.create(stream, false, new NullProgressMonitor()); - } - return file; - } - protected IFile createFile(IContainer container, String fileName, String contents) throws Exception { - return createFile(container, new Path(fileName), contents); + return TestSourceReader.createFile(container, new Path(fileName), contents); } protected IASTTranslationUnit createPDOMBasedAST(ICProject project, IFile file) throws CModelException, CoreException {