Skip to content

Instantly share code, notes, and snippets.

@janos-ss
Last active Apr 13, 2018
Embed
What would you like to do?
LoggerTest.java
package org.sonarlint.eclipse.its;
import java.util.ArrayList;
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.core.runtime.Status;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
public class LoggerTest {
private static final CustomLogListener LISTENER = new CustomLogListener();
private static final class CustomLogListener implements ILogListener {
private final List<IStatus> mStatus = new ArrayList<IStatus>();
public void logging(IStatus status, String plugin) {
mStatus.add(status);
}
}
@BeforeClass
public static void registerLog() {
getLog().addLogListener(LISTENER);
}
@AfterClass
public static void unregisterLog() {
getLog().removeLogListener(LISTENER);
}
public static ILog getLog() {
return Platform.getLog(Platform.getBundle("org.eclipse.swtbot"));
}
// A small test case to proof that it is working
@Test(expected = NullPointerException.class)
public void testLogging() throws Throwable {
// This should be done in a @Before or setUp method. To make sure that
// errors that happened in the other tests don't pollute this test.
clearExceptions();
// Just to test
getLog().log(new Status(IStatus.ERROR,
"org.eclipse.swtbot",
"fake error",
new NullPointerException("Fake NPE")));
// This should be done in a @After or tearDown method.
checkExceptions();
}
public static void clearExceptions() {
LISTENER.mStatus.clear();
}
public static void checkExceptions() throws Throwable {
for (IStatus status : LISTENER.mStatus) {
if (status.getException() != null) {
// Since it's a list, the first exception that gets logged will be thrown.
throw status.getException();
}
}
}
}
-------------------------------------------------------------------------------
Test set: org.sonarlint.eclipse.its.LoggerTest
-------------------------------------------------------------------------------
Tests run: 2, Failures: 0, Errors: 2, Skipped: 0, Time elapsed: 0.101 sec <<< FAILURE! - in org.sonarlint.eclipse.its.LoggerTest
org.sonarlint.eclipse.its.LoggerTest Time elapsed: 0.095 sec <<< ERROR!
java.lang.IllegalArgumentException: Logging bundle must not be null.
at org.eclipse.core.internal.runtime.Log.<init>(Log.java:30)
at org.eclipse.core.internal.runtime.InternalPlatform.getLog(InternalPlatform.java:351)
at org.eclipse.core.runtime.Platform.getLog(Platform.java:976)
at org.sonarlint.eclipse.its.LoggerTest.getLog(LoggerTest.java:56)
at org.sonarlint.eclipse.its.LoggerTest.registerLog(LoggerTest.java:47)
org.sonarlint.eclipse.its.LoggerTest Time elapsed: 0.101 sec <<< ERROR!
java.lang.IllegalArgumentException: Logging bundle must not be null.
at org.eclipse.core.internal.runtime.Log.<init>(Log.java:30)
at org.eclipse.core.internal.runtime.InternalPlatform.getLog(InternalPlatform.java:351)
at org.eclipse.core.runtime.Platform.getLog(Platform.java:976)
at org.sonarlint.eclipse.its.LoggerTest.getLog(LoggerTest.java:56)
at org.sonarlint.eclipse.its.LoggerTest.unregisterLog(LoggerTest.java:52)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment