From eeb3c25c4ad44a6a6b2251b53579ad31faadf2c2 Mon Sep 17 00:00:00 2001 From: Andrew Ferguson Date: Wed, 21 Mar 2007 14:46:23 +0000 Subject: [PATCH] fail tests which log non-OK status objects in CCorePlugin --- .../index/tests/TeamSharedIndexTest.java | 9 ++- .../core/testplugin/util/BaseTestCase.java | 63 +++++++++++++++++-- 2 files changed, 64 insertions(+), 8 deletions(-) diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/internal/index/tests/TeamSharedIndexTest.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/internal/index/tests/TeamSharedIndexTest.java index a42e1216a8c..cde18b2b6e5 100644 --- a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/internal/index/tests/TeamSharedIndexTest.java +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/internal/index/tests/TeamSharedIndexTest.java @@ -90,13 +90,15 @@ public class TeamSharedIndexTest extends IndexTestBase { private ICProject recreateProject(final String prjName) throws Exception { final boolean[] changed= {false}; - CoreModel.getDefault().addElementChangedListener(new IElementChangedListener() { + IElementChangedListener waitListener= new IElementChangedListener() { public void elementChanged(ElementChangedEvent event) { synchronized (changed) { changed[0]= true; + changed.notifyAll(); } - changed.notifyAll(); - }}); + } + }; + CoreModel.getDefault().addElementChangedListener(waitListener); final IWorkspace workspace = ResourcesPlugin.getWorkspace(); final IProject prjHandle= workspace.getRoot().getProject(prjName); workspace.run(new IWorkspaceRunnable() { @@ -112,6 +114,7 @@ public class TeamSharedIndexTest extends IndexTestBase { assertTrue(changed[0]); } } + CoreModel.getDefault().removeElementChangedListener(waitListener); fPDOMManager.joinIndexer(INDEXER_WAIT_TIME, NPM); return CoreModel.getDefault().create(workspace.getRoot().getProject(prjName)); } diff --git a/core/org.eclipse.cdt.core.tests/suite/org/eclipse/cdt/core/testplugin/util/BaseTestCase.java b/core/org.eclipse.cdt.core.tests/suite/org/eclipse/cdt/core/testplugin/util/BaseTestCase.java index 625f8e1fae4..1d7b648bf85 100644 --- a/core/org.eclipse.cdt.core.tests/suite/org/eclipse/cdt/core/testplugin/util/BaseTestCase.java +++ b/core/org.eclipse.cdt.core.tests/suite/org/eclipse/cdt/core/testplugin/util/BaseTestCase.java @@ -7,17 +7,19 @@ * * Contributors: * Markus Schorn - initial API and implementation + * Andrew Ferguson (Symbian) *******************************************************************************/ package org.eclipse.cdt.core.testplugin.util; import java.lang.reflect.Method; import java.lang.reflect.Modifier; +import java.util.ArrayList; +import java.util.Collections; +import java.util.Iterator; +import java.util.List; import java.util.Vector; -import org.eclipse.core.runtime.IProgressMonitor; -import org.eclipse.core.runtime.NullProgressMonitor; - import junit.framework.AssertionFailedError; import junit.framework.Test; import junit.framework.TestCase; @@ -25,11 +27,18 @@ import junit.framework.TestFailure; import junit.framework.TestResult; import junit.framework.TestSuite; +import org.eclipse.cdt.core.CCorePlugin; +import org.eclipse.core.runtime.ILogListener; +import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.core.runtime.IStatus; +import org.eclipse.core.runtime.NullProgressMonitor; + public class BaseTestCase extends TestCase { protected static final IProgressMonitor NPM= new NullProgressMonitor(); private boolean fExpectFailure= false; private int fBugnumber= 0; + private boolean allowsCoreLogErrors= false; public BaseTestCase() { super(); @@ -74,7 +83,7 @@ public class BaseTestCase extends TestCase { if (name.startsWith("test") || (prefix != null && !name.startsWith(prefix))) { return; } - if (name.equals("tearDown") || name.equals("setUp")) { + if (name.equals("tearDown") || name.equals("setUp") || name.equals("runBare")) { return; } if (Modifier.isPublic(m.getModifiers())) { @@ -88,6 +97,40 @@ public class BaseTestCase extends TestCase { } } + public void runBare() throws Throwable { + final List statusLog= Collections.synchronizedList(new ArrayList()); + ILogListener logListener= new ILogListener() { + public void logging(IStatus status, String plugin) { + if(!status.isOK()) { + statusLog.add(status); + } + } + }; + CCorePlugin.getDefault().getLog().addLogListener(logListener); + + try { + super.runBare(); + + if(!allowsCoreLogErrors && !statusLog.isEmpty()) { + StringBuffer msg= new StringBuffer("Non-OK Status was logged to CCorePlugin: \n"); + Throwable cause= null; + for(Iterator i= statusLog.iterator(); i.hasNext(); ) { + IStatus status= (IStatus) i.next(); + if(cause==null) { + cause= status.getException(); + } + Throwable t= status.getException(); + msg.append("\t"+status.getMessage()+" "+(t!=null?t.getMessage():"")+"\n"); + } + AssertionFailedError afe= new AssertionFailedError(msg.toString()); + afe.initCause(cause); + throw afe; + } + } finally { + CCorePlugin.getDefault().getLog().removeLogListener(logListener); + } + } + public void run( TestResult result ) { if (!fExpectFailure) { super.run(result); @@ -121,4 +164,14 @@ public class BaseTestCase extends TestCase { fExpectFailure= true; fBugnumber= bugnumber; } -} + + /** + * The last value passed to this method in the body of a testXXX method + * will be used to determine whether or not the presence of non-OK status objects + * in the log should fail the test. + * @param value + */ + public void setAllowCCorePluginLogErrors(boolean value) { + allowsCoreLogErrors= value; + } +} \ No newline at end of file