### Question:
Activator implements BundleActivator, ServiceListener { @Override public void start( BundleContext bundleContext ) throws Exception { this.bundleContext = bundleContext; if (null == activatorHelper) { this.activatorHelper = getActivatorHelper(); } ServiceReference configurationAdminServiceReference = bundleContext.getServiceReference(CONFIG_ADMIN_NAME); if (null != configurationAdminServiceReference) { activatorHelper.verifyConfiguration(configurationAdminServiceReference); } else { bundleContext.addServiceListener(this, String.format("(%s=%s)", Constants.OBJECTCLASS, CONFIG_ADMIN_NAME)); } AccessManagerFactoryTracker accessManagerFactoryTracker = getAccessManagerFactoryTracker(); if (null == accessManagerFactoryTracker) { accessManagerFactoryTracker = this.accessManagerFactoryTracker = activatorHelper.createAccessManagerFactoryTracker(); } accessManagerFactoryTracker.open(); ClassLoader bundleClassLoader = new BundleClassLoaderFactory(bundleContext).getClassLoader((String[])null); I18n.setLocalizationRepository(new ClasspathLocalizationRepository(bundleClassLoader)); }  @Override void serviceChanged( ServiceEvent event ); @Override void start( BundleContext bundleContext ); @Override void stop( BundleContext bundleContext );  }### Answer:
@Test public void shouldVerifyConfigurationOnStartWhenConfigurationAdminServiceReferenceIsFound() throws Exception { ServiceReference configurationAdminServiceReference = mock(ServiceReference.class); BundleContext bundleContext = mock(BundleContext.class); when(bundleContext.getServiceReference(CONFIG_ADMIN_NAME)).thenReturn(configurationAdminServiceReference); final AccessManagerFactoryTracker accessManagerFactoryTracker = mock(AccessManagerFactoryTracker.class); final ActivatorHelper activatorHelper = mock(ActivatorHelper.class); when(activatorHelper.createAccessManagerFactoryTracker()).thenReturn(accessManagerFactoryTracker); Activator activator = new Activator() { @Override protected ActivatorHelper getActivatorHelper() { return activatorHelper; } @Override protected AccessManagerFactoryTracker getAccessManagerFactoryTracker() { return accessManagerFactoryTracker; } }; activator.start(bundleContext); verify(activatorHelper).verifyConfiguration(configurationAdminServiceReference); }
@Test public void shouldRegisterItselfAsListenerOnStartWhenNoConfigurationAdminServiceReferenceIsFound() throws Exception { BundleContext bundleContext = mock(BundleContext.class); when(bundleContext.getServiceReference(CONFIG_ADMIN_NAME)).thenReturn(null); Activator activator = new Activator() { @Override protected AccessManagerFactoryTracker getAccessManagerFactoryTracker() { return mock(AccessManagerFactoryTracker.class); } }; activator.start(bundleContext); verify(bundleContext).addServiceListener(activator, "(" + Constants.OBJECTCLASS + "=" + CONFIG_ADMIN_NAME + ")"); }
@Test public void shouldCreateAccessManagerFactoryTrackerOnStartOnlyTheFirstTime() throws Exception { BundleContext bundleContext = mock(BundleContext.class); final AccessManagerFactoryTracker accessManagerFactoryTracker = mock(AccessManagerFactoryTracker.class); final ActivatorHelper activatorHelper = mock(ActivatorHelper.class); when(activatorHelper.createAccessManagerFactoryTracker()).thenReturn(accessManagerFactoryTracker); Activator activator = new Activator() { @Override protected AccessManagerFactoryTracker getAccessManagerFactoryTracker() { return null; } @Override protected ActivatorHelper getActivatorHelper() { return activatorHelper; } }; activator.start(bundleContext); verify(activatorHelper).createAccessManagerFactoryTracker(); }