Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 491c5344a11385b52ab61177005e9d55a0731214 (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
/*******************************************************************************
 * Copyright (c) 2008, 2015 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.osgi.tests.bundles;

import java.io.IOException;
import java.net.URL;
import junit.framework.Test;
import junit.framework.TestSuite;
import org.eclipse.osgi.tests.OSGiTestsActivator;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleException;

public class BundleExceptionTests extends AbstractBundleTests {
	public static Test suite() {
		return new TestSuite(BundleExceptionTests.class);
	}

	// test throwing exception from activator constructor
	public void testInvalidBundleActivator01() throws BundleException {
		Bundle error1 = installer.installBundle("activator.error1"); //$NON-NLS-1$
		try {
			error1.start();
			fail("Expected a start failure on invalid activator"); //$NON-NLS-1$
		} catch (BundleException e) {
			assertEquals("Expected activator error", BundleException.ACTIVATOR_ERROR, e.getType()); //$NON-NLS-1$
		}
	}

	// test throwing exception from activator start
	public void testInvalidBundleActivator02() throws BundleException {
		Bundle error1 = installer.installBundle("activator.error2"); //$NON-NLS-1$
		try {
			error1.start();
			fail("Expected a start failure on invalid activator"); //$NON-NLS-1$
		} catch (BundleException e) {
			assertEquals("Expected activator error", BundleException.ACTIVATOR_ERROR, e.getType()); //$NON-NLS-1$
		}
	}

	// test throwing exception from activator stop
	public void testInvalidBundleActivator03() throws BundleException {
		Bundle error1 = installer.installBundle("activator.error3"); //$NON-NLS-1$
		error1.start();
		try {
			error1.stop();
			fail("Expected a stop failure on invalid activator"); //$NON-NLS-1$
		} catch (BundleException e) {
			assertEquals("Expected activator error", BundleException.ACTIVATOR_ERROR, e.getType()); //$NON-NLS-1$
		}
	}

	// test throwing exception when installing duplicate bundles
	public void testDuplicateError01() throws BundleException {
		installer.installBundle("activator.error1"); //$NON-NLS-1$
		try {
			installer.installBundle("activator.error4"); //$NON-NLS-1$;
			fail("Expected an install failure on duplicate bundle"); //$NON-NLS-1$
		} catch (BundleException e) {
			assertEquals("Expected duplicate error", BundleException.DUPLICATE_BUNDLE_ERROR, e.getType()); //$NON-NLS-1$
		}
	}

	// test throwing exception when updating to a duplicate bundle
	public void testDuplicateError02() throws BundleException {
		installer.installBundle("activator.error1"); //$NON-NLS-1$
		Bundle error2 = installer.installBundle("activator.error2"); //$NON-NLS-1$
		try {
			URL updateURL = new URL(installer.getBundleLocation("activator.error4")); //$NON-NLS-1$
			error2.update(updateURL.openStream());
			fail("Expected an update failure on duplicate bundle"); //$NON-NLS-1$
		} catch (BundleException e) {
			assertEquals("Expected duplicate error", BundleException.DUPLICATE_BUNDLE_ERROR, e.getType()); //$NON-NLS-1$
		} catch (IOException e) {
			fail("Unexpected io exception updating", e); //$NON-NLS-1$
		}
	}

	// test uninstalling the system bundle
	public void testUninstallSystemBundle() {
		Bundle systemBundle = OSGiTestsActivator.getContext().getBundle(0);
		assertNotNull("System Bundle is null!!", systemBundle); //$NON-NLS-1$
		try {
			systemBundle.uninstall();
			fail("Expected error on uninstall of system bundle"); //$NON-NLS-1$
		} catch (BundleException e) {
			assertEquals("Expected invalid error", BundleException.INVALID_OPERATION, e.getType()); //$NON-NLS-1$
		}
	}
}

Back to the top