aboutsummaryrefslogtreecommitdiffstats
blob: 4b5f6d4f2252a35bd3064984dc76dfb361d76ba6 (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
/*******************************************************************************
 * Copyright (c) 2010 SAP AG
 * 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:
 *   Hristo Iliev, SAP AG - initial contribution
 *******************************************************************************/
package org.eclipse.virgo.kernel.osgicommand.helper.test;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

import java.util.Arrays;
import java.util.HashMap;

import org.eclipse.virgo.kernel.osgicommand.helper.ClassLoadingHelper;
import org.eclipse.virgo.kernel.test.AbstractKernelIntegrationTest;
import org.eclipse.virgo.test.framework.dmkernel.DmKernelTestRunner;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
import org.osgi.service.packageadmin.PackageAdmin;

/**
 * Class for integration testing {@link org.eclipse.virgo.kernel.osgicommand.helper.ClassLoadingHelper}
 */
@RunWith(DmKernelTestRunner.class)
public class ClassLoadingHelperIntegrationTests extends AbstractKernelIntegrationTest {
    private static final String SHELL_COMMANDS_BUNDLE_NAME = "org.eclipse.virgo.kernel.osgicommand";
    private static final String CLASSLOADING_PACKAGE = "org.eclipse.virgo.kernel.osgicommand.helper";
    private static final String TEST_CLASS_NAME = ClassLoadingHelperIntegrationTests.class.getName();
    private static final String TEST_CLASS_PACKAGE = ClassLoadingHelperIntegrationTests.class.getPackage().getName();
    private final String FRAMEWORK_CLASS_PACKAGE = BundleContext.class.getPackage().getName();
    private final String FRAMEWORK_CLASS_NAME = BundleContext.class.getName();

    private Bundle currentBundle = null;
    private Bundle shellCommandsBundle = null;
    private Bundle systemBundle = null;

    private final String UNEXPORTED_ERROR_MESSAGE = "Package [%s] is reported as exported by [%s] with id [%s], but it is not";
    private final String EXPORTED_ERROR_MESSAGE = "Package [%s] is reported as not exported by [%s] with id [%s], but it is";
    private final String LOADED = "Class [%s] was loaded by by bundle [%s] with id [%s], but it should be available only in [%s]";
    private final String NOT_LOADED = "Class [%s] was not loaded from bundle [%s] with id [%s]";

    @Before
    public void setUp() throws Exception {
        // execute initialization code
        super.setup();

        // get osgicommand bundle
        ServiceReference reference = context.getServiceReference(PackageAdmin.class.getName());
        PackageAdmin packageAdmin = (PackageAdmin) context.getService(reference);
        Bundle[] bundles = packageAdmin.getBundles(SHELL_COMMANDS_BUNDLE_NAME, null);
        assertNotNull("No bundles with symbolic name [" + SHELL_COMMANDS_BUNDLE_NAME + "] found in bundles set " + Arrays.toString(context.getBundles()),
                      bundles);
        assertTrue("Found [" + bundles.length + "] bundles with symbolic name [" + SHELL_COMMANDS_BUNDLE_NAME + "] in bundles set " + Arrays.toString(context.getBundles()),
                   bundles.length == 1);
        this.shellCommandsBundle = bundles[0];

        // get this bundle
        this.currentBundle = context.getBundle();

        // get the system bundle
        this.systemBundle = context.getBundle(0);
    }

    @Test
    public void testIsPackageExportedMethod() throws Exception {
        // Check which bundles export CLASSLOADING_PACKAGE
        assertTrue(String.format(EXPORTED_ERROR_MESSAGE, CLASSLOADING_PACKAGE, SHELL_COMMANDS_BUNDLE_NAME, shellCommandsBundle.getBundleId()),
                   ClassLoadingHelper.isPackageExported(context, CLASSLOADING_PACKAGE, shellCommandsBundle));
        assertFalse(String.format(UNEXPORTED_ERROR_MESSAGE, CLASSLOADING_PACKAGE, currentBundle.getSymbolicName(), currentBundle.getBundleId()),
                    ClassLoadingHelper.isPackageExported(context, CLASSLOADING_PACKAGE, currentBundle));
        assertFalse(String.format(UNEXPORTED_ERROR_MESSAGE, CLASSLOADING_PACKAGE, systemBundle.getSymbolicName(), systemBundle.getBundleId()),
                    ClassLoadingHelper.isPackageExported(context, CLASSLOADING_PACKAGE, systemBundle));

        // Check which bundles export CLASSLOADING_TEST_PACKAGE
        assertFalse(String.format(UNEXPORTED_ERROR_MESSAGE, TEST_CLASS_PACKAGE, SHELL_COMMANDS_BUNDLE_NAME, shellCommandsBundle.getBundleId()),
                    ClassLoadingHelper.isPackageExported(context, TEST_CLASS_PACKAGE, shellCommandsBundle));
        assertTrue(String.format(EXPORTED_ERROR_MESSAGE, TEST_CLASS_PACKAGE, currentBundle.getSymbolicName(), currentBundle.getBundleId()),
                   ClassLoadingHelper.isPackageExported(context, TEST_CLASS_PACKAGE, currentBundle));
        assertFalse(String.format(UNEXPORTED_ERROR_MESSAGE, TEST_CLASS_PACKAGE, systemBundle.getSymbolicName(), systemBundle.getBundleId()),
                    ClassLoadingHelper.isPackageExported(context, TEST_CLASS_PACKAGE, systemBundle));
    }

    @Test
    public void testGetBundlesLoadingClassMethod() throws Exception {
        final String CAN_LOAD_ERROR_MESSAGE = "Bundle [%s] is returned as bundle that can load the test class [%s]. The returned set of bundles is %s";
        final String CANNOT_LOAD_ERROR_MESSAGE = "Bundle [%s] is not returned as bundle that can load the test class [%s]. The returned set of bundles is %s";
        final String ORIGINATING_ERROR_MESSAGE = "Bundle [%s] is returned as originating bundle for class [%s]. The returned set of bundles is %s";
        final String NON_ORIGINATING_ERROR_MESSAGE = "Bundle [%s] is not returned as originating bundle for class [%s]. The returned set of bundles is %s";

        // Check which bundles can load this class
        HashMap<Bundle, Bundle> result = ClassLoadingHelper.getBundlesLoadingClass(context, TEST_CLASS_NAME);
        assertFalse(String.format(CAN_LOAD_ERROR_MESSAGE, SHELL_COMMANDS_BUNDLE_NAME, TEST_CLASS_NAME, Arrays.toString(result.keySet().toArray())),
                    result.containsKey(shellCommandsBundle));
        assertFalse(String.format(ORIGINATING_ERROR_MESSAGE, SHELL_COMMANDS_BUNDLE_NAME, TEST_CLASS_NAME, Arrays.toString(result.values().toArray())),
                    result.containsValue(shellCommandsBundle));
        assertTrue(String.format(CANNOT_LOAD_ERROR_MESSAGE, currentBundle.getSymbolicName(), TEST_CLASS_NAME, Arrays.toString(result.keySet().toArray())),
                   result.containsKey(currentBundle));
        assertTrue(String.format(NON_ORIGINATING_ERROR_MESSAGE, currentBundle.getSymbolicName(), TEST_CLASS_NAME, Arrays.toString(result.values().toArray())),
                   result.containsValue(currentBundle));

        // Check how osgicommand bundle can load BundleContext
        result = ClassLoadingHelper.getBundlesLoadingClass(context, FRAMEWORK_CLASS_NAME, SHELL_COMMANDS_BUNDLE_NAME);
        assertTrue(String.format(CANNOT_LOAD_ERROR_MESSAGE, SHELL_COMMANDS_BUNDLE_NAME, FRAMEWORK_CLASS_NAME, Arrays.toString(result.keySet().toArray())),
                   result.containsKey(shellCommandsBundle));
        assertTrue(String.format(NON_ORIGINATING_ERROR_MESSAGE, systemBundle.getSymbolicName(), FRAMEWORK_CLASS_NAME, Arrays.toString(result.values().toArray())),
                   result.containsValue(systemBundle));
    }

    @Test
    public void testTryToLoadClassMethod() throws Exception {
        assertNotNull(String.format(NOT_LOADED, TEST_CLASS_NAME, currentBundle.getSymbolicName(), currentBundle.getBundleId()),
                      ClassLoadingHelper.tryToLoadClass(TEST_CLASS_NAME, currentBundle));
        assertNull(String.format(LOADED, TEST_CLASS_NAME, SHELL_COMMANDS_BUNDLE_NAME, shellCommandsBundle.getBundleId(), currentBundle.getSymbolicName()),
                   ClassLoadingHelper.tryToLoadClass(TEST_CLASS_NAME, shellCommandsBundle));
    }

    @Test
    public void testExportAndLoad() throws Exception {
        // Check TEST_CLASS_* export and load
        assertFalse(String.format(UNEXPORTED_ERROR_MESSAGE, TEST_CLASS_PACKAGE, shellCommandsBundle.getSymbolicName(), shellCommandsBundle.getBundleId()),
                    ClassLoadingHelper.isPackageExported(context, TEST_CLASS_PACKAGE, shellCommandsBundle));
        assertNull(String.format(LOADED, TEST_CLASS_NAME, SHELL_COMMANDS_BUNDLE_NAME, shellCommandsBundle.getBundleId(), SHELL_COMMANDS_BUNDLE_NAME),
                   ClassLoadingHelper.tryToLoadClass(TEST_CLASS_NAME, shellCommandsBundle));
        assertTrue(String.format(EXPORTED_ERROR_MESSAGE, TEST_CLASS_PACKAGE, currentBundle.getSymbolicName(), currentBundle.getBundleId()),
                   ClassLoadingHelper.isPackageExported(context, TEST_CLASS_PACKAGE, currentBundle));
        assertNotNull(String.format(NOT_LOADED, TEST_CLASS_NAME, currentBundle.getSymbolicName(), currentBundle.getBundleId()),
                      ClassLoadingHelper.tryToLoadClass(TEST_CLASS_NAME, currentBundle));

        // Check FRAMEWORK_CLASS_* export and load
        assertFalse(String.format(UNEXPORTED_ERROR_MESSAGE, FRAMEWORK_CLASS_PACKAGE, currentBundle.getSymbolicName(), currentBundle.getBundleId()),
                    ClassLoadingHelper.isPackageExported(context, FRAMEWORK_CLASS_PACKAGE, currentBundle));
        assertNotNull(String.format(NOT_LOADED, FRAMEWORK_CLASS_NAME, currentBundle.getSymbolicName(), currentBundle.getBundleId()),
                      ClassLoadingHelper.tryToLoadClass(FRAMEWORK_CLASS_NAME, currentBundle));
        assertTrue(String.format(EXPORTED_ERROR_MESSAGE, FRAMEWORK_CLASS_PACKAGE, systemBundle.getSymbolicName(), systemBundle.getBundleId()),
                   ClassLoadingHelper.isPackageExported(context, FRAMEWORK_CLASS_PACKAGE, systemBundle));
        assertNotNull(String.format(NOT_LOADED, FRAMEWORK_CLASS_NAME, currentBundle.getSymbolicName(), currentBundle.getBundleId()),
                      ClassLoadingHelper.tryToLoadClass(FRAMEWORK_CLASS_NAME, systemBundle));
    }
}