| package org.eclipse.core.tests.databinding.util; |
| |
| import java.io.ByteArrayOutputStream; |
| import java.io.PrintStream; |
| |
| import junit.framework.TestCase; |
| |
| import org.eclipse.core.databinding.util.ILogger; |
| import org.eclipse.core.databinding.util.Policy; |
| import org.eclipse.core.runtime.IStatus; |
| import org.eclipse.core.runtime.Status; |
| |
| /** |
| * @since 3.2 |
| * |
| */ |
| public class PolicyTest extends TestCase { |
| |
| public void testConstructor() { |
| // cover the constructor too |
| new Policy(); |
| } |
| |
| public void testDummyLog() { |
| ILogger oldLog = Policy.getLog(); |
| PrintStream oldErr = System.err; |
| try { |
| // this should reset to using dummy log |
| Policy.setLog(null); |
| ByteArrayOutputStream os = new ByteArrayOutputStream(); |
| PrintStream ps = new PrintStream(os); |
| System.setErr(ps); |
| IStatus status = new Status(IStatus.CANCEL, "somePluginId", |
| "someMessage", new RuntimeException()); |
| Policy.getLog().log(status); |
| ps.flush(); |
| String message = new String(os.toByteArray()); |
| System.out.println("testDummyLog message: " + message); |
| if (true) { |
| return; |
| } |
| assertTrue("expecting severity", message.indexOf(IStatus.CANCEL |
| + "") != -1); |
| assertTrue("expecting plugin id", |
| message.indexOf("somePluginId") != -1); |
| assertTrue("expecting message", |
| message.indexOf("someMessage") != -1); |
| assertTrue("expecting RuntimeException", message |
| .indexOf("RuntimeException") != -1); |
| } finally { |
| Policy.setLog(oldLog); |
| System.setErr(oldErr); |
| } |
| } |
| |
| public void testCustomLog() { |
| ILogger oldLog = Policy.getLog(); |
| try { |
| final IStatus[] statusHolder = new IStatus[1]; |
| Policy.setLog(new ILogger() { |
| public void log(IStatus status) { |
| statusHolder[0] = status; |
| } |
| }); |
| IStatus status = new Status(IStatus.CANCEL, "somePluginId", |
| "someMessage", new RuntimeException()); |
| Policy.getLog().log(status); |
| assertEquals(status, statusHolder[0]); |
| } finally { |
| Policy.setLog(oldLog); |
| } |
| } |
| } |