mil.dod.th.ose.core.integration/src/mil/dod/th/ose/core/integration/TestAssetDirectoryService.java
@SuppressWarnings("unchecked")
    public final void testShutdown() 
        throws IllegalArgumentException, AssetException, IllegalStateException, 
            FactoryException, InterruptedException, BundleException 
    {
        AssetDirectoryService assetDirectoryService = ServiceUtils.getService(m_Context, AssetDirectoryService.class);
        
        final String assetName = "shutdownAsset";
        Asset exampleAsset = assetDirectoryService.createAsset(ExampleAsset.class.getName(), assetName);
        
        Map<String, Object> properties = exampleAsset.getProperties();
        properties.put(ExampleAssetAttributes.CONFIG_PROP_DEVICE_POWER_NAME, "exampleA");
        exampleAsset.setProperties(properties);
        
        for (Asset asset : assetDirectoryService.getAssets())
        {
            if (asset.getActiveStatus() != AssetActiveStatus.ACTIVATED)
            {
                AssetUtils.activateAsset(m_Context, asset, 5);
            }
        }
        
        LogReaderService logReader = ServiceUtils.getService(m_Context, LogReaderService.class);
        
        final Semaphore semaphore = new Semaphore(0);
        
        logReader.addLogListener(new LogListener()
        {
            @Override
            public void logged(LogEntry entry)
            {
                if ("Example asset deactivated".equals(entry.getMessage()))
                {
                    semaphore.release();
                }
            }
        });
        
        ServiceReference<AssetDirectoryService> adsServiceRef = 
                ServiceUtils.getServiceReference(m_Context, AssetDirectoryService.class);
        Bundle adsBundle = adsServiceRef.getBundle();
        adsBundle.stop();
        
        // check if asset was deactivated on service shutdown
        boolean assetDeactivatedOnSeviceShutdown = semaphore.tryAcquire(5, TimeUnit.SECONDS);
        
        adsBundle.start();
        
        // verify asset was shutdown, this check is asserted after restarting service bundle so that if this assert
        // fails, we still restart bundle so other tests will still run
        assertThat("Asset did not deactivate on shutdown", assetDeactivatedOnSeviceShutdown, is(true));
        
        // get service, old one is no longer valid and wait some time as bundle is starting back up
        assetDirectoryService = ServiceUtils.getService(m_Context, AssetDirectoryService.class, 5000);
        assertThat(assetDirectoryService, is(notNullValue()));
        
        // wait for asset factory descriptor to signal core has loaded all old assets
        FactoryUtils.assertFactoryDescriptorAvailable(m_Context, ExampleAsset.class, 5000);
        
        Asset restoredAsset = null;
        try
        {
            restoredAsset = assetDirectoryService.getAssetByName(assetName);
        }
        catch (final IllegalArgumentException exception)
        {
            fail(String.format("AssetDirectory did not contain the restored asset with name: %s", assetName));
        }
        assertThat(restoredAsset, is(notNullValue()));
    }
/**
     * Test shutdown of the asset directory service.
     * Verify that assets are deactivated before the service is deactivated.
     */
Test shutdown of the asset directory service.
Verify that assets are deactivated before the service is deactivated.
Test shutdown of the asset directory service . Verify that assets are deactivated before the service is deactivated .
@SuppressWarnings("unchecked")
    public final void testShutdown() 
        throws IllegalArgumentException, AssetException, IllegalStateException, 
            FactoryException, InterruptedException, BundleException 
    {
        AssetDirectoryService assetDirectoryService = ServiceUtils.getService(m_Context, AssetDirectoryService.class);
        final String assetName = "shutdownAsset";
        Asset exampleAsset = assetDirectoryService.createAsset(ExampleAsset.class.getName(), assetName);
        Map<String, Object> properties = exampleAsset.getProperties();
        properties.put(ExampleAssetAttributes.CONFIG_PROP_DEVICE_POWER_NAME, "exampleA");
        exampleAsset.setProperties(properties);
        for (Asset asset : assetDirectoryService.getAssets())
        {
            if (asset.getActiveStatus() != AssetActiveStatus.ACTIVATED)
            {
                AssetUtils.activateAsset(m_Context, asset, 5);
            }
        }
        LogReaderService logReader = ServiceUtils.getService(m_Context, LogReaderService.class);
        final Semaphore semaphore = new Semaphore(0);
        logReader.addLogListener(new LogListener()
        {
            @Override
            public void logged(LogEntry entry)
            {
                if ("Example asset deactivated".equals(entry.getMessage()))
                {
                    semaphore.release();
                }
            }
        });
        ServiceReference<AssetDirectoryService> adsServiceRef = 
                ServiceUtils.getServiceReference(m_Context, AssetDirectoryService.class);
        Bundle adsBundle = adsServiceRef.getBundle();
        adsBundle.stop();
        boolean assetDeactivatedOnSeviceShutdown = semaphore.tryAcquire(5, TimeUnit.SECONDS);
        adsBundle.start();
        assertThat("Asset did not deactivate on shutdown", assetDeactivatedOnSeviceShutdown, is(true));
        assetDirectoryService = ServiceUtils.getService(m_Context, AssetDirectoryService.class, 5000);
        assertThat(assetDirectoryService, is(notNullValue()));
        FactoryUtils.assertFactoryDescriptorAvailable(m_Context, ExampleAsset.class, 5000);
        Asset restoredAsset = null;
        try
        {
            restoredAsset = assetDirectoryService.getAssetByName(assetName);
        }
        catch (final IllegalArgumentException exception)
        {
            fail(String.format("AssetDirectory did not contain the restored asset with name: %s", assetName));
        }
        assertThat(restoredAsset, is(notNullValue()));
    }
@ SuppressWarnings ( "unchecked" ) public final void testShutdown ( ) throws IllegalArgumentException , AssetException , IllegalStateException , FactoryException , InterruptedException , BundleException { AssetDirectoryService assetDirectoryService = ServiceUtils . getService ( m_Context , AssetDirectoryService . class ) ; final String assetName = "shutdownAsset" ; Asset exampleAsset = assetDirectoryService . createAsset ( ExampleAsset . class . getName ( ) , assetName ) ; Map < String , Object > properties = exampleAsset . getProperties ( ) ; properties . put ( ExampleAssetAttributes . CONFIG_PROP_DEVICE_POWER_NAME , "exampleA" ) ; exampleAsset . setProperties ( properties ) ; for ( Asset asset : assetDirectoryService . getAssets ( ) ) { if ( asset . getActiveStatus ( ) != AssetActiveStatus . ACTIVATED ) { AssetUtils . activateAsset ( m_Context , asset , 5 ) ; } } LogReaderService logReader = ServiceUtils . getService ( m_Context , LogReaderService . class ) ; final Semaphore semaphore = new Semaphore ( 0 ) ; logReader . addLogListener ( new LogListener ( ) { @ Override public void logged ( LogEntry entry ) { if ( "Example asset deactivated" . equals ( entry . getMessage ( ) ) ) { semaphore . release ( ) ; } } } ) ; ServiceReference < AssetDirectoryService > adsServiceRef = ServiceUtils . getServiceReference ( m_Context , AssetDirectoryService . class ) ; Bundle adsBundle = adsServiceRef . getBundle ( ) ; adsBundle . stop ( ) ; boolean assetDeactivatedOnSeviceShutdown = semaphore . tryAcquire ( 5 , TimeUnit . SECONDS ) ; adsBundle . start ( ) ; assertThat ( "Asset did not deactivate on shutdown" , assetDeactivatedOnSeviceShutdown , is ( true ) ) ; assetDirectoryService = ServiceUtils . getService ( m_Context , AssetDirectoryService . class , 5000 ) ; assertThat ( assetDirectoryService , is ( notNullValue ( ) ) ) ; FactoryUtils . assertFactoryDescriptorAvailable ( m_Context , ExampleAsset . class , 5000 ) ; Asset restoredAsset = null ; try { restoredAsset = assetDirectoryService . getAssetByName ( assetName ) ; } catch ( final IllegalArgumentException exception ) { fail ( String . format ( "AssetDirectory did not contain the restored asset with name: %s" , assetName ) ) ; } assertThat ( restoredAsset , is ( notNullValue ( ) ) ) ; }
Test shutdown of the asset directory service.
Test shutdown of the asset directory service .
// check if asset was deactivated on service shutdown // verify asset was shutdown, this check is asserted after restarting service bundle so that if this assert // fails, we still restart bundle so other tests will still run // get service, old one is no longer valid and wait some time as bundle is starting back up // wait for asset factory descriptor to signal core has loaded all old assets