Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 157f8304ee4a0fd5ca5e8c82e1a7f6b4b8c7d8e4 (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
/*******************************************************************************
 * Copyright (c) 2004, 2015 IBM Corporation and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 * 
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.osgi.tests.listeners;

import java.io.IOException;
import java.net.MalformedURLException;
import junit.framework.TestCase;
import org.eclipse.core.tests.harness.BundleTestingHelper;
import org.eclipse.osgi.tests.OSGiTestsActivator;
import org.junit.Assert;
import org.osgi.framework.*;

public class ExceptionHandlerTests extends TestCase {
	//These tests exercise the code change for bug 73111.
	
	class FrameworkEventListenerWithResult implements FrameworkListener {
		FrameworkEvent event = null;
		
		public synchronized void frameworkEvent(FrameworkEvent newEvent) {
			if (newEvent.getType() != FrameworkEvent.ERROR)
				return;
			
			if (this.event != null)
				return;
			event = newEvent;
			notify();
		}
		
		public synchronized FrameworkEvent getResult(long timeout) {
			if (event != null) {
				FrameworkEvent tmp = event;
				event = null;
				return tmp;
			}
			try {
				wait(timeout);
			} catch (InterruptedException e) {
			}
			FrameworkEvent tmp = event;
			event = null;
			return tmp;
		}
	}

	
	protected void setUp() throws Exception {
		super.setUp();
	}
	
	
	public void testNonFatalException() {	
		FrameworkEventListenerWithResult fwkListener = new FrameworkEventListenerWithResult();
		OSGiTestsActivator.getContext().addFrameworkListener(fwkListener);

		BundleListener npeGenerator = new BundleListener() {
			public void bundleChanged(BundleEvent event) {
				throw new NullPointerException("Generated exception");
			}
		};	
		OSGiTestsActivator.getContext().addBundleListener(npeGenerator);
		
		try {
			BundleTestingHelper.installBundle(OSGiTestsActivator.getContext(), OSGiTestsActivator.TEST_FILES_ROOT + "internal/plugins/installTests/bundle09");
			FrameworkEvent eventReceived = fwkListener.getResult(60000);
			Assert.assertEquals(FrameworkEvent.ERROR, eventReceived.getType());
			Assert.assertEquals(true, eventReceived.getThrowable() instanceof NullPointerException);
		} catch (MalformedURLException e) {
			//Does not happen
		} catch (BundleException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		OSGiTestsActivator.getContext().removeFrameworkListener(fwkListener);
		OSGiTestsActivator.getContext().removeBundleListener(npeGenerator);
	}
	
	
	public void testFatalException() {	
		FrameworkEventListenerWithResult fwkListener = new FrameworkEventListenerWithResult();
		OSGiTestsActivator.getContext().addFrameworkListener(fwkListener);

		BundleListener fatalException = new BundleListener() {
			public void bundleChanged(BundleEvent event) {
				throw new OutOfMemoryError("Generated exception");
			}
		};
		OSGiTestsActivator.getContext().addBundleListener(fatalException);
		
	
		try {
			System.setProperty("eclipse.exitOnError","false"); //Here we set the value to false, because otherwise we would simply exit
			BundleTestingHelper.installBundle(OSGiTestsActivator.getContext(), OSGiTestsActivator.TEST_FILES_ROOT + "internal/plugins/installTests/bundle10");
			FrameworkEvent eventReceived = fwkListener.getResult(10000);
			Assert.assertEquals(FrameworkEvent.ERROR, eventReceived.getType());
			Assert.assertEquals(true, eventReceived.getThrowable() instanceof VirtualMachineError);
			System.setProperty("eclipse.exitOnError","true");
		} catch (MalformedURLException e) {
			//Does not happen
		} catch (BundleException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		OSGiTestsActivator.getContext().removeFrameworkListener(fwkListener);
		OSGiTestsActivator.getContext().removeBundleListener(fatalException);
	}
 
}

Back to the top