Skip to main content
summaryrefslogtreecommitdiffstats
blob: 1955e3f487b10a3026bb6b0a1630a4ba5cab2de6 (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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*******************************************************************************
 * Copyright (c) 2007, 2009 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
 *******************************************************************************/
package org.eclipse.equinox.log.test;

import java.io.*;
import java.util.Hashtable;
import junit.framework.TestCase;
import org.eclipse.equinox.log.internal.ExtendedLogReaderServiceFactory;
import org.osgi.framework.*;
import org.osgi.service.log.*;

public class LogReaderServiceTest extends TestCase {

	private LogService log;
	private ServiceReference logReference;
	private LogReaderService reader;
	private ServiceReference readerReference;

	public LogReaderServiceTest(String name) {
		super(name);
	}

	protected void setUp() throws Exception {
		Activator.getBundle("org.eclipse.equinox.log").start(); //$NON-NLS-1$
		logReference = Activator.getBundleContext().getServiceReference(LogService.class.getName());
		readerReference = Activator.getBundleContext().getServiceReference(LogReaderService.class.getName());

		log = (LogService) Activator.getBundleContext().getService(logReference);
		reader = (LogReaderService) Activator.getBundleContext().getService(readerReference);
	}

	protected void tearDown() throws Exception {
		Activator.getBundleContext().ungetService(logReference);
		Activator.getBundleContext().ungetService(readerReference);
		Activator.getBundle("org.eclipse.equinox.log").stop(); //$NON-NLS-1$
	}

	public void testaddListener() throws Exception {
		TestListener listener = new TestListener();
		reader.addLogListener(listener);
		synchronized (listener) {
			log.log(LogService.LOG_INFO, "info"); //$NON-NLS-1$
			listener.wait();
		}
		assertTrue(listener.getEntry().getLevel() == LogService.LOG_INFO);
	}

	public void testaddListenerTwice() throws Exception {
		TestListener listener = new TestListener();
		reader.addLogListener(listener);
		reader.addLogListener(listener);
		synchronized (listener) {
			log.log(LogService.LOG_INFO, "info"); //$NON-NLS-1$
			listener.wait();
		}
		assertTrue(listener.getEntry().getLevel() == LogService.LOG_INFO);
	}

	public void testaddNullListener() throws Exception {
		try {
			reader.addLogListener(null);
		} catch (IllegalArgumentException t) {
			return;
		}
		fail();
	}

	public void testBadListener() throws Exception {
		LogListener listener = new LogListener() {
			public synchronized void logged(LogEntry entry) {
				notifyAll();
				throw new RuntimeException("Expected error for testBadListener."); //$NON-NLS-1$
			}
		};
		reader.addLogListener(listener);

		ExtendedLogReaderServiceFactory.setErrorStream(new PrintStream(new OutputStream() {
			public void write(int arg0) throws IOException {

			}
		}));

		synchronized (listener) {
			log.log(LogService.LOG_INFO, "info"); //$NON-NLS-1$
			listener.wait();
		}
	}

	public void testLogEntry() throws Exception {
		TestListener listener = new TestListener();
		reader.addLogListener(listener);
		long timeBeforeLog = System.currentTimeMillis();
		synchronized (listener) {
			log.log(logReference, LogService.LOG_INFO, "info", new Throwable("test")); //$NON-NLS-1$ //$NON-NLS-2$
			listener.wait();
		}
		assertTrue(listener.getEntry().getBundle() == Activator.getBundleContext().getBundle());
		assertTrue(listener.getEntry().getMessage().equals("info")); //$NON-NLS-1$
		assertTrue(listener.getEntry().getException().getMessage().equals("test")); //$NON-NLS-1$
		assertTrue(listener.getEntry().getServiceReference() == logReference);
		assertTrue(listener.getEntry().getTime() >= timeBeforeLog);
		assertTrue(listener.getEntry().getLevel() == LogService.LOG_INFO);
	}

	public void testLogBundleEventInfo() throws Exception {

		// this is just a bundle that is harmless to start/stop
		Bundle servicesBundle = Activator.getBundle("org.eclipse.osgi.services"); //$NON-NLS-1$
		if (servicesBundle == null)
			return; // ignore

		servicesBundle.stop();

		TestListener listener = new TestListener();
		reader.addLogListener(listener);
		synchronized (listener) {
			servicesBundle.start();
			listener.wait();
		}
		assertTrue(listener.getEntry().getLevel() == LogService.LOG_INFO);
	}

	public void testLogServiceEventInfo() throws Exception {
		TestListener listener = new TestListener();
		reader.addLogListener(listener);
		synchronized (listener) {
			Activator.getBundleContext().registerService(Object.class.getName(), new Object(), null);
			listener.wait();
		}
		assertTrue(listener.getEntry().getLevel() == LogService.LOG_INFO);
	}

	public void testLogServiceEventDebug() throws Exception {
		ServiceRegistration registration = Activator.getBundleContext().registerService(Object.class.getName(), new Object(), null);

		TestListener listener = new TestListener();
		reader.addLogListener(listener);
		synchronized (listener) {
			registration.setProperties(new Hashtable());
			listener.wait();
		}
		assertTrue(listener.getEntry().getLevel() == LogService.LOG_DEBUG);
	}

	public void testLogFrameworkEvent() throws Exception {
		TestListener listener = new TestListener();
		reader.addLogListener(listener);
		synchronized (listener) {
			Activator.refreshPackages();
			listener.wait();
		}
		assertTrue(listener.getEntry().getLevel() == LogService.LOG_INFO);
	}
}

Back to the top