From 2737d2c708e30b8b983411062a3c8ec13632ec2a Mon Sep 17 00:00:00 2001 From: Sergey Prigogin Date: Tue, 3 Jan 2012 16:14:11 -0800 Subject: [PATCH] Fixed reporting of "Test data not found" errors. --- .../tests/IndexBindingResolutionTestBase.java | 26 ++--- ...ngResolutionBugsSingleProjectFirstAST.java | 7 +- .../testplugin/util/TestSourceReader.java | 107 ++++++++++-------- .../cdt/ui/tests/search/BasicSearchTest.java | 19 ++-- .../DefaultCCommentAutoEditStrategyTest.java | 6 +- .../DoxygenCCommentAutoEditStrategyTest.java | 18 +-- 6 files changed, 99 insertions(+), 84 deletions(-) diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/internal/index/tests/IndexBindingResolutionTestBase.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/internal/index/tests/IndexBindingResolutionTestBase.java index 5e0d7a9d55a..bae3b12d150 100644 --- a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/internal/index/tests/IndexBindingResolutionTestBase.java +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/internal/index/tests/IndexBindingResolutionTestBase.java @@ -120,12 +120,12 @@ public abstract class IndexBindingResolutionTestBase extends BaseTestCase { len= section.length()+len; } IASTName name= findName(section, len); - assertNotNull("name not found for \""+section+"\"", name); + assertNotNull("Name not found for \"" + section + "\"", name); assertEquals(section.substring(0, len), name.getRawSignature()); IBinding binding = name.resolveBinding(); - assertNotNull("No binding for "+name.getRawSignature(), binding); - assertFalse("Binding is a ProblemBinding for name "+name.getRawSignature(), IProblemBinding.class.isAssignableFrom(name.resolveBinding().getClass())); + assertNotNull("No binding for " + name.getRawSignature(), binding); + assertFalse("Binding is a ProblemBinding for name \"" + name.getRawSignature() + "\"", IProblemBinding.class.isAssignableFrom(name.resolveBinding().getClass())); assertInstance(binding, clazz, cs); return clazz.cast(binding); } @@ -135,15 +135,15 @@ public abstract class IndexBindingResolutionTestBase extends BaseTestCase { */ protected T getBindingFromASTName(String section, int len) { if (len <= 0) - len+= section.length(); + len += section.length(); IASTName name= findName(section, len); - assertNotNull("name not found for \""+section+"\"", name); + assertNotNull("Name not found for \"" + section + "\"", name); assertEquals(section.substring(0, len), name.getRawSignature()); IBinding binding = name.resolveBinding(); - assertNotNull("No binding for "+name.getRawSignature(), binding); - assertFalse("Binding is a ProblemBinding for name "+name.getRawSignature(), IProblemBinding.class.isAssignableFrom(name.resolveBinding().getClass())); + assertNotNull("No binding for " + name.getRawSignature(), binding); + assertFalse("Binding is a ProblemBinding for name \"" + name.getRawSignature() + "\"", IProblemBinding.class.isAssignableFrom(name.resolveBinding().getClass())); return (T) binding; } @@ -155,12 +155,12 @@ public abstract class IndexBindingResolutionTestBase extends BaseTestCase { */ protected IBinding getProblemFromASTName(String section, int len) { IASTName name= findName(section, len); - assertNotNull("name not found for \""+section+"\"", name); + assertNotNull("Name not found for \"" + section + "\"", name); assertEquals(section.substring(0, len), name.getRawSignature()); IBinding binding = name.resolveBinding(); - assertNotNull("No binding for "+name.getRawSignature(), binding); - assertTrue("Binding is not a ProblemBinding for name "+name.getRawSignature(), IProblemBinding.class.isAssignableFrom(name.resolveBinding().getClass())); + assertNotNull("No binding for " + name.getRawSignature(), binding); + assertTrue("Binding is not a ProblemBinding for name \"" + name.getRawSignature() + "\"", IProblemBinding.class.isAssignableFrom(name.resolveBinding().getClass())); return name.resolveBinding(); } @@ -200,10 +200,10 @@ public abstract class IndexBindingResolutionTestBase extends BaseTestCase { } protected static T assertInstance(Object o, Class clazz, Class ... cs) { - assertNotNull("Expected "+clazz.getName()+" but got null", o); - assertTrue("Expected "+clazz.getName()+" but got "+o.getClass().getName(), clazz.isInstance(o)); + assertNotNull("Expected " + clazz.getName() + " but got null", o); + assertTrue("Expected " + clazz.getName() + " but got " + o.getClass().getName(), clazz.isInstance(o)); for (Class c : cs) { - assertTrue("Expected "+clazz.getName()+" but got "+o.getClass().getName(), c.isInstance(o)); + assertTrue("Expected " + clazz.getName() + " but got " + o.getClass().getName(), c.isInstance(o)); } return clazz.cast(o); } diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/internal/index/tests/IndexCPPBindingResolutionBugsSingleProjectFirstAST.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/internal/index/tests/IndexCPPBindingResolutionBugsSingleProjectFirstAST.java index 1cd104e79be..b537ab53a38 100644 --- a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/internal/index/tests/IndexCPPBindingResolutionBugsSingleProjectFirstAST.java +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/internal/index/tests/IndexCPPBindingResolutionBugsSingleProjectFirstAST.java @@ -16,8 +16,11 @@ public class IndexCPPBindingResolutionBugsSingleProjectFirstAST extends IndexCPP public IndexCPPBindingResolutionBugsSingleProjectFirstAST() { setStrategy(new SinglePDOMTestFirstASTStrategy(true)); } - public static TestSuite suite() {return suite(IndexCPPBindingResolutionBugsSingleProjectFirstAST.class);} - // invalid tests for this strategy, they assume that the second file is already indexed. + public static TestSuite suite() { + return suite(IndexCPPBindingResolutionBugsSingleProjectFirstAST.class); + } + + // Invalid tests for this strategy, they assume that the second file is already indexed. @Override public void testBug208558() {} @Override public void testBug176708_CCE() {} @Override public void testIsSameAnonymousType_Bug193962() {} 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 index e6a90d99bf7..e4bffbbbc57 100644 --- 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 @@ -23,7 +23,6 @@ import java.util.ArrayList; import java.util.List; import junit.framework.Assert; -import junit.framework.TestCase; import org.eclipse.cdt.core.CCorePlugin; import org.eclipse.cdt.core.dom.ILinkage; @@ -54,10 +53,10 @@ import org.osgi.framework.Bundle; * Utilities for reading test source code from plug-in .java sources */ public class TestSourceReader { - /** * Returns an array of StringBuilder objects for each comment section found preceding the named - * test in the source code. + * test in the source code. + * * @param bundle the bundle containing the source, if null can try to load using classpath * (source folder has to be in the classpath for this to work) * @param srcRoot the directory inside the bundle containing the packages @@ -71,54 +70,61 @@ public class TestSourceReader { */ public static StringBuilder[] getContentsForTest(Bundle bundle, String srcRoot, Class clazz, final String testName, int sections) throws IOException { - String fqn = clazz.getName().replace('.', '/'); - fqn = fqn.indexOf("$") == -1 ? fqn : fqn.substring(0, fqn.indexOf("$")); - String classFile = fqn + ".java"; - IPath filePath= new Path(srcRoot + '/' + classFile); - - InputStream in; - try { - if (bundle != null) { - in = FileLocator.openStream(bundle, filePath, false); - } else { - in = clazz.getResourceAsStream('/' + classFile); + while (true) { + String fqn = clazz.getName().replace('.', '/'); + fqn = fqn.indexOf("$") == -1 ? fqn : fqn.substring(0, fqn.indexOf("$")); + String classFile = fqn + ".java"; + IPath filePath= new Path(srcRoot + '/' + classFile); + + InputStream in; + Class superclass = clazz.getSuperclass(); + try { + if (bundle != null) { + in = FileLocator.openStream(bundle, filePath, false); + } else { + in = clazz.getResourceAsStream('/' + classFile); + } + } catch (IOException e) { + if (superclass == null || !superclass.getPackage().equals(clazz.getPackage())) { + throw e; + } + clazz = superclass; + continue; } - } catch (IOException e) { - if (clazz.getSuperclass() != null && !clazz.equals(TestCase.class)) { - return getContentsForTest(bundle, srcRoot, clazz.getSuperclass(), testName, sections); + + BufferedReader br = new BufferedReader(new InputStreamReader(in)); + try { + List contents = new ArrayList(); + StringBuilder content = new StringBuilder(); + for (String line = br.readLine(); line != null; line = br.readLine()) { + line = line.replaceFirst("^\\s*", ""); // Replace leading whitespace, preserve trailing + if (line.startsWith("//")) { + content.append(line.substring(2) + "\n"); + } else { + if (content.length() > 0) { + contents.add(content); + if (sections > 0 && contents.size() == sections + 1) + contents.remove(0); + content = new StringBuilder(); + } + if (line.length() > 0 && !contents.isEmpty()) { + int idx= line.indexOf(testName); + if (idx != -1 && !Character.isJavaIdentifierPart(line.charAt(idx + testName.length()))) { + return contents.toArray(new StringBuilder[contents.size()]); + } + contents.clear(); + } + } + } + } finally { + br.close(); } - throw e; + + if (superclass == null || !superclass.getPackage().equals(clazz.getPackage())) { + throw new IOException("Test data not found for " + clazz.getName() + "." + testName); + } + clazz = superclass; } - - BufferedReader br = new BufferedReader(new InputStreamReader(in)); - - List contents = new ArrayList(); - StringBuilder content = new StringBuilder(); - for (String line = br.readLine(); line != null; line = br.readLine()) { - line = line.replaceFirst("^\\s*", ""); // replace leading whitespace, preserve trailing - if (line.startsWith("//")) { - content.append(line.substring(2) + "\n"); - } else { - if (content.length() > 0) { - contents.add(content); - if (sections > 0 && contents.size() == sections + 1) - contents.remove(0); - content = new StringBuilder(); - } - if (line.length() > 0 && !contents.isEmpty()) { - int idx= line.indexOf(testName); - if (idx != -1 && !Character.isJavaIdentifierPart(line.charAt(idx + testName.length()))) { - return contents.toArray(new StringBuilder[contents.size()]); - } - contents.clear(); - } - } - } - - if (clazz.getSuperclass() != null && !clazz.equals(TestCase.class)) { - return getContentsForTest(bundle, srcRoot, clazz.getSuperclass(), testName, sections); - } - throw new IOException("Test data not found for " + clazz + " " + testName); } /** @@ -251,6 +257,7 @@ public class TestSourceReader { } result[0]= file; } + private void createFolders(IResource res) throws CoreException { IContainer container= res.getParent(); if (!container.exists() && container instanceof IFolder) { @@ -305,7 +312,7 @@ public class TestSourceReader { Thread.sleep(50); timeLeft= (int) (endTime - System.currentTimeMillis()); } - Assert.fail("Indexing " + file.getFullPath() + " did not complete in time!"); + Assert.fail("Indexing " + file.getFullPath() + " did not complete in " + maxmillis / 1000. + " sec"); } private static boolean areAllFilesNotOlderThan(IIndexFile[] files, long timestamp) throws CoreException { @@ -323,7 +330,7 @@ public class TestSourceReader { ITranslationUnit tu= (ITranslationUnit) elem; return tu.getAST(index, ITranslationUnit.AST_SKIP_INDEXED_HEADERS); } - Assert.fail("Could not create ast for " + file.getFullPath()); + Assert.fail("Could not create AST for " + file.getFullPath()); return null; } } diff --git a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/search/BasicSearchTest.java b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/search/BasicSearchTest.java index 9babe2f4839..ce22c312882 100644 --- a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/search/BasicSearchTest.java +++ b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/search/BasicSearchTest.java @@ -6,10 +6,10 @@ * http://www.eclipse.org/legal/epl-v10.html * * Contributors: - * Andrew Ferguson (Symbian) - Initial implementation - * Markus Schorn (Wind River Systems) - * IBM Corporation - * Ed Swartz (Nokia) + * Andrew Ferguson (Symbian) - Initial implementation + * Markus Schorn (Wind River Systems) + * IBM Corporation + * Ed Swartz (Nokia) *******************************************************************************/ package org.eclipse.cdt.ui.tests.search; @@ -67,9 +67,10 @@ public class BasicSearchTest extends BaseUITestCase { @Override protected void setUp() throws Exception { super.setUp(); - fCProject = CProjectHelper.createCCProject(getName()+System.currentTimeMillis(), "bin", IPDOMManager.ID_NO_INDEXER); + fCProject = CProjectHelper.createCCProject(getName() + System.currentTimeMillis(), "bin", IPDOMManager.ID_NO_INDEXER); Bundle b = CTestPlugin.getDefault().getBundle(); testData = TestSourceReader.getContentsForTest(b, "ui", this.getClass(), getName(), 2); + assertEquals("Incomplete test data", 2, testData.length); IFile file = TestSourceReader.createFile(fCProject.getProject(), new Path("header.h"), testData[0].toString()); CCorePlugin.getIndexManager().setIndexerId(fCProject, IPDOMManager.ID_FAST_INDEXER); @@ -142,7 +143,8 @@ public class BasicSearchTest extends BaseUITestCase { } // int x, y, xx, yy; - + + // // empty public void testNoIndexerEnabled_158955() throws Exception { // rebuild the index with no indexer CCorePlugin.getIndexManager().setIndexerId(fCProject, IPDOMManager.ID_NO_INDEXER); @@ -156,7 +158,7 @@ public class BasicSearchTest extends BaseUITestCase { ISearchResultViewPart vp= NewSearchUI.getSearchResultView(); ISearchResultPage page= vp.getActivePage(); - assertTrue(""+page, page instanceof PDOMSearchViewPage); + assertTrue("" + page, page instanceof PDOMSearchViewPage); PDOMSearchViewPage pdomsvp= (PDOMSearchViewPage) page; StructuredViewer viewer= pdomsvp.getViewer(); @@ -177,7 +179,8 @@ public class BasicSearchTest extends BaseUITestCase { final int INDEXER_IN_PROGRESS_STRUCT_COUNT = 100; // #include "hugeHeader0.h" - + + // // empty public void testIndexerInProgress() throws Exception { // make an external file File dir= CProjectHelper.freshDir(); diff --git a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/DefaultCCommentAutoEditStrategyTest.java b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/DefaultCCommentAutoEditStrategyTest.java index 8b7d5590037..cac270f78cb 100644 --- a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/DefaultCCommentAutoEditStrategyTest.java +++ b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/DefaultCCommentAutoEditStrategyTest.java @@ -7,10 +7,9 @@ * * Contributors: * Anton Leherbauer (Wind River Systems) - initial API and implementation - * Sergey Prigogin, Google + * Sergey Prigogin (Google) * Andrew Ferguson (Symbian) *******************************************************************************/ - package org.eclipse.cdt.ui.tests.text; import java.util.HashMap; @@ -36,12 +35,11 @@ import org.eclipse.cdt.ui.text.doctools.DefaultMultilineCommentAutoEditStrategy; import org.eclipse.cdt.internal.ui.text.CAutoIndentStrategy; import org.eclipse.cdt.internal.ui.text.CTextTools; - /** * Testing the auto indent strategies. */ public class DefaultCCommentAutoEditStrategyTest extends AbstractAutoEditTest { - private HashMap fOptions; + private HashMap fOptions; /** * @param name diff --git a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/doctools/doxygen/DoxygenCCommentAutoEditStrategyTest.java b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/doctools/doxygen/DoxygenCCommentAutoEditStrategyTest.java index 124f077eee4..54f82adec39 100644 --- a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/doctools/doxygen/DoxygenCCommentAutoEditStrategyTest.java +++ b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/doctools/doxygen/DoxygenCCommentAutoEditStrategyTest.java @@ -7,11 +7,13 @@ * * Contributors: * Anton Leherbauer (Wind River Systems) - initial API and implementation - * Sergey Prigogin, Google + * Sergey Prigogin (Google) * Andrew Ferguson (Symbian) *******************************************************************************/ package org.eclipse.cdt.ui.tests.text.doctools.doxygen; +import java.util.HashMap; + import junit.framework.Test; import org.eclipse.core.resources.IFile; @@ -20,13 +22,14 @@ import org.eclipse.jface.text.BadLocationException; import org.eclipse.jface.text.Document; import org.eclipse.jface.text.IDocument; +import org.eclipse.cdt.core.CCorePlugin; import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit; import org.eclipse.cdt.core.model.CoreModel; import org.eclipse.cdt.core.model.ICProject; import org.eclipse.cdt.core.testplugin.CProjectHelper; import org.eclipse.cdt.core.testplugin.util.TestSourceReader; import org.eclipse.cdt.ui.CUIPlugin; -import org.eclipse.cdt.ui.tests.text.DefaultCCommentAutoEditStrategyTest; +import org.eclipse.cdt.ui.tests.text.AbstractAutoEditTest; import org.eclipse.cdt.ui.text.ICPartitions; import org.eclipse.cdt.ui.text.doctools.DefaultMultilineCommentAutoEditStrategy; import org.eclipse.cdt.ui.text.doctools.doxygen.DoxygenMultilineAutoEditStrategy; @@ -36,13 +39,13 @@ import org.eclipse.cdt.internal.core.model.TranslationUnit; import org.eclipse.cdt.internal.ui.text.CAutoIndentStrategy; import org.eclipse.cdt.internal.ui.text.CTextTools; - /** * Testing the auto indent strategies. */ -public class DoxygenCCommentAutoEditStrategyTest extends DefaultCCommentAutoEditStrategyTest { +public class DoxygenCCommentAutoEditStrategyTest extends AbstractAutoEditTest { + private HashMap fOptions; protected ICProject fCProject; - + /** * @param name */ @@ -57,7 +60,8 @@ public class DoxygenCCommentAutoEditStrategyTest extends DefaultCCommentAutoEdit @Override protected void setUp() throws Exception { super.setUp(); - fCProject= CProjectHelper.createCCProject("test"+System.currentTimeMillis(), null); + fOptions= CCorePlugin.getOptions(); + fCProject= CProjectHelper.createCCProject("test" + System.currentTimeMillis(), null); } /* @@ -65,6 +69,7 @@ public class DoxygenCCommentAutoEditStrategyTest extends DefaultCCommentAutoEdit */ @Override protected void tearDown() throws Exception { + CCorePlugin.setOptions(fOptions); CProjectHelper.delete(fCProject); super.tearDown(); } @@ -75,7 +80,6 @@ public class DoxygenCCommentAutoEditStrategyTest extends DefaultCCommentAutoEdit textTools.setupCDocument(doc); AutoEditTester tester = new AutoEditTester(doc, ICPartitions.C_PARTITIONING); - tester.setAutoEditStrategy(IDocument.DEFAULT_CONTENT_TYPE, new CAutoIndentStrategy(ICPartitions.C_PARTITIONING, null)); tester.setAutoEditStrategy(ICPartitions.C_MULTI_LINE_COMMENT, new DefaultMultilineCommentAutoEditStrategy()); tester.setAutoEditStrategy(ICPartitions.C_PREPROCESSOR, new CAutoIndentStrategy(ICPartitions.C_PARTITIONING, null));