diff options
author | Christian W. Damus | 2015-07-28 15:34:59 +0000 |
---|---|---|
committer | Christian W. Damus | 2015-07-28 18:29:27 +0000 |
commit | 82bcb214bc5803f425dd24fa23f50c76378ef05a (patch) | |
tree | 29a59ac58b00ff5cb1b0cf1248cd27a37e84c856 /tests/junit/plugins/junit/org.eclipse.papyrus.junit.utils/src/org | |
parent | eaf62bc725a26dab3dedef991a609b19c8ac0ba7 (diff) | |
download | org.eclipse.papyrus-82bcb214bc5803f425dd24fa23f50c76378ef05a.tar.gz org.eclipse.papyrus-82bcb214bc5803f425dd24fa23f50c76378ef05a.tar.xz org.eclipse.papyrus-82bcb214bc5803f425dd24fa23f50c76378ef05a.zip |
Bug 473154: FileNotFoundException in LocalFile.openInputStream (369)
https://bugs.eclipse.org/bugs/show_bug.cgi?id=473154
Ensure that we only attempt to index files that are synchronized
as far as the workspace is concerned. Includes a JUnit test that
fails without the fix on detection of the same exception logged
as in the original bug report.
(cherry picked from commit e334737bc2fe21da7ab91bfc87fcd49944275bdd)
Change-Id: Idf211d87b7c59cea73d399419e8dcabede66dde8
Diffstat (limited to 'tests/junit/plugins/junit/org.eclipse.papyrus.junit.utils/src/org')
2 files changed, 198 insertions, 0 deletions
diff --git a/tests/junit/plugins/junit/org.eclipse.papyrus.junit.utils/src/org/eclipse/papyrus/junit/matchers/MoreMatchers.java b/tests/junit/plugins/junit/org.eclipse.papyrus.junit.utils/src/org/eclipse/papyrus/junit/matchers/MoreMatchers.java index 83d694294b1..9765f99db05 100644 --- a/tests/junit/plugins/junit/org.eclipse.papyrus.junit.utils/src/org/eclipse/papyrus/junit/matchers/MoreMatchers.java +++ b/tests/junit/plugins/junit/org.eclipse.papyrus.junit.utils/src/org/eclipse/papyrus/junit/matchers/MoreMatchers.java @@ -15,9 +15,11 @@ package org.eclipse.papyrus.junit.matchers; import java.util.regex.Pattern; +import org.eclipse.core.runtime.IStatus; import org.hamcrest.BaseMatcher; import org.hamcrest.Description; import org.hamcrest.Matcher; +import org.hamcrest.core.CombinableMatcher; import com.google.common.base.Strings; import com.google.common.collect.Iterables; @@ -61,6 +63,11 @@ public class MoreMatchers { }; } + /** + * Match empty iterables of any kind. + * + * @see #emptyIterable() + */ public static Matcher<Iterable<?>> isEmpty() { return new BaseMatcher<Iterable<?>>() { @Override @@ -75,6 +82,27 @@ public class MoreMatchers { }; } + /** + * The {@link CombinableMatcher}s of Hamcrest require matching generic signatures, + * for which the wildcard of the {@link #isEmpty()} matcher doesn't work very well, + * so this equivalent matcher may be used instead in those cases. + * + * @see #isEmpty() + */ + public static <E> Matcher<Iterable<E>> emptyIterable() { + return new BaseMatcher<Iterable<E>>() { + @Override + public void describeTo(Description description) { + description.appendText("is empty"); + } + + @Override + public boolean matches(Object item) { + return Iterables.isEmpty((Iterable<?>) item); + } + }; + } + public static Matcher<String> regexMatches(final String pattern) { return new BaseMatcher<String>() { @Override @@ -106,4 +134,42 @@ public class MoreMatchers { } }; } + + public static Matcher<IStatus> statusWithMessage(final Matcher<? super String> matcher) { + return new BaseMatcher<IStatus>() { + @Override + public void describeTo(Description description) { + description.appendText("status with message ").appendDescriptionOf(matcher); + } + + @Override + public boolean matches(Object item) { + boolean result = false; + if (item instanceof IStatus) { + IStatus status = (IStatus) item; + result = matcher.matches(status.getMessage()); + } + return result; + } + }; + } + + public static Matcher<IStatus> statusWithException(final Matcher<?> matcher) { + return new BaseMatcher<IStatus>() { + @Override + public void describeTo(Description description) { + description.appendText("status with exception ").appendDescriptionOf(matcher); + } + + @Override + public boolean matches(Object item) { + boolean result = false; + if (item instanceof IStatus) { + IStatus status = (IStatus) item; + result = matcher.matches(status.getException()); + } + return result; + } + }; + } } diff --git a/tests/junit/plugins/junit/org.eclipse.papyrus.junit.utils/src/org/eclipse/papyrus/junit/utils/LogTracker.java b/tests/junit/plugins/junit/org.eclipse.papyrus.junit.utils/src/org/eclipse/papyrus/junit/utils/LogTracker.java new file mode 100644 index 00000000000..36ee26cc460 --- /dev/null +++ b/tests/junit/plugins/junit/org.eclipse.papyrus.junit.utils/src/org/eclipse/papyrus/junit/utils/LogTracker.java @@ -0,0 +1,132 @@ +/***************************************************************************** + * Copyright (c) 2015 Christian W. Damus 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: + * Christian W. Damus - Initial API and implementation + * + *****************************************************************************/ + +package org.eclipse.papyrus.junit.utils; + +import static org.hamcrest.CoreMatchers.both; +import static org.hamcrest.CoreMatchers.either; +import static org.hamcrest.CoreMatchers.everyItem; +import static org.hamcrest.CoreMatchers.not; +import static org.hamcrest.MatcherAssert.assertThat; + +import java.util.List; + +import org.eclipse.core.runtime.ILog; +import org.eclipse.core.runtime.ILogListener; +import org.eclipse.core.runtime.IStatus; +import org.eclipse.core.runtime.Platform; +import org.eclipse.papyrus.junit.matchers.MoreMatchers; +import org.hamcrest.Matcher; + +import com.google.common.base.Predicate; +import com.google.common.base.Predicates; +import com.google.common.collect.Lists; + +/** + * A configurable log listener to help tests to make assertions about the + * messages that are logged. + */ +public class LogTracker implements ILogListener { + + private String bundle; + + private Predicate<? super IStatus> filter; + + private ILog log; + + private final List<IStatus> messages = Lists.newArrayList(); + + public LogTracker() { + super(); + } + + /** + * Start tracking the specified {@code bundle}'s log. + * + * @param bundle + * the symbolic name of the bundle whose log is to be tracked + */ + public void start(String bundle) { + start(bundle, null); + } + + /** + * Start tracking the specified {@code bundle}'s log for particular messages. + * + * @param bundle + * the symbolic name of the bundle whose log is to be tracked + * @param filter + * a filter matching messages that should be recorded, or {@code null} to record all messages + */ + public void start(String bundle, Predicate<? super IStatus> filter) { + if (filter == null) { + filter = Predicates.alwaysTrue(); + } + + this.bundle = bundle; + this.filter = filter; + this.log = Platform.getLog(Platform.getBundle(bundle)); + + // Individual ILog instances don't notify listeners + Platform.addLogListener(this); + } + + public void dispose() { + if (log != null) { + Platform.removeLogListener(this); + log = null; + + clear(); + bundle = null; + filter = null; + } + } + + public void clear() { + messages.clear(); + } + + @Override + public void logging(IStatus status, String plugin) { + if ((plugin.equals(bundle) || status.getPlugin().equals(bundle)) && filter.apply(status)) { + messages.add(status); + } + } + + /** + * Assert that either there were no messages recorded, or they all satisfy an {@code assertion}. + */ + public void assertAll(Matcher<? super IStatus> assertion) { + @SuppressWarnings("unchecked") + Matcher<IStatus> hamcrestSignatureWorkaround = (Matcher<IStatus>) assertion; + assertThat(messages, either(MoreMatchers.<IStatus> emptyIterable()).or(everyItem(hamcrestSignatureWorkaround))); + } + + /** + * Assert at least one message was recorded, and all recorded messages satisfy an {@code assertion}. + */ + public void assertExistAll(Matcher<? super IStatus> assertion) { + @SuppressWarnings("unchecked") + Matcher<IStatus> hamcrestSignatureWorkaround = (Matcher<IStatus>) assertion; + assertThat(messages, both(not(MoreMatchers.<IStatus> emptyIterable())).and(everyItem(hamcrestSignatureWorkaround))); + } + + /** + * Assert that either there were no messages recorded, or they all satisfy an {@code assertion}. + */ + public void assertNone(Matcher<? super IStatus> assertion) { + @SuppressWarnings("unchecked") + Matcher<IStatus> hamcrestSignatureWorkaround = (Matcher<IStatus>) assertion; + assertThat(messages, everyItem(not(hamcrestSignatureWorkaround))); + } +} |