blob: a534462ba8f45623b0cb5d909aadbece2e134c73 [file] [log] [blame]
A. J. David Bosschaert105897e2015-12-09 13:48:15 +00001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19package org.osgi.util.tracker;
20
21import static org.mockito.Mockito.mock;
22import static org.mockito.Mockito.verify;
23import static org.mockito.Mockito.when;
24
25import org.junit.Test;
26import org.mockito.ArgumentCaptor;
27import org.mockito.Mockito;
28import org.osgi.framework.Bundle;
29import org.osgi.framework.BundleContext;
30import org.osgi.framework.BundleEvent;
31import org.osgi.framework.BundleListener;
32
33/**
34 * Checks that BundleTracker actually tracks and untracks a bundle that is entering and then
35 * leaving the observed states
36 */
37public class BundleTrackerTest {
38
39 @Test
40 public void testTracking() {
41 Bundle bundle = mock(Bundle.class);
42 when(bundle.getState()).thenReturn(Bundle.ACTIVE);
43
44 BundleContext context = Mockito.mock(BundleContext.class);
45
46 @SuppressWarnings("unchecked")
47 BundleTrackerCustomizer<Bundle> customizer = mock(BundleTrackerCustomizer.class);
48 when(customizer.addingBundle(Mockito.eq(bundle), Mockito.any(BundleEvent.class))).thenReturn(bundle);
49
50 BundleTracker<Bundle> tracker = new BundleTracker<Bundle>(context , Bundle.ACTIVE | Bundle.STARTING, customizer);
51 tracker.open();
52 ArgumentCaptor<BundleListener> listenerCaptor = ArgumentCaptor.forClass(BundleListener.class);
53 verify(context).addBundleListener(listenerCaptor.capture());
54 BundleListener listener = listenerCaptor.getValue();
55
56 BundleEvent startedEvent = new BundleEvent(BundleEvent.STARTED, bundle);
57 listener.bundleChanged(startedEvent);
58 verify(customizer).addingBundle(bundle, startedEvent);
59
60 when(bundle.getState()).thenReturn(Bundle.INSTALLED);
61 BundleEvent stoppedEvent = new BundleEvent(BundleEvent.STOPPED, bundle);
62 listener.bundleChanged(stoppedEvent);
63 verify(customizer).removedBundle(bundle, stoppedEvent, bundle);
64 }
65}