Skip to main content
summaryrefslogtreecommitdiffstats
blob: d6ffbf01a84298dcfe331b357f8b86db7c4afe65 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/*******************************************************************************
 * Copyright (c) 2008 IBM Corporation 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:
 *     IBM Corporation - initial API and implementation
 *     Matthew Hall - bug 258774, 260316
 ******************************************************************************/
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);
			assertTrue("expecting severity", message.indexOf("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);
		}
	}
}

Back to the top