mil.dod.th.ose.core/test/mil/dod/th/ose/core/ConfigurationAdminMocker.java
public static ConfigurationAdmin createMockConfigAdmin()
    {
        m_Configs.clear();
        m_Properties.clear();
        m_MSF.clear();

        ConfigurationAdmin configAdmin = mock(ConfigurationAdmin.class);

        try
        {
            //create and store a new configuration
            when(configAdmin.createFactoryConfiguration(Mockito.anyString(), Mockito.anyString())).thenAnswer(
                    new Answer<Configuration>()
                    {
                        @Override
                        public Configuration answer(InvocationOnMock invocation) throws Throwable
                        {
                            String factoryPid = (String)invocation.getArguments()[0];
                            Preconditions.checkNotNull(factoryPid);
                            Configuration config = createMockConfiguration(factoryPid);
                            return config;
                        }
                    });
    
            //look up the configuration by PID from the config map
            when(configAdmin.getConfiguration(Mockito.anyString(), anyString())).thenAnswer(new Answer<Configuration>()
            {
                @Override
                public Configuration answer(InvocationOnMock invocation) throws Throwable
                {
                    String pid = (String)invocation.getArguments()[0];
                    return m_Configs.get(pid);
                }
            });
    
            //list all known configuration from config map
            when(configAdmin.listConfigurations(Mockito.anyString())).thenAnswer(new Answer<Configuration[]>()
            {
                @Override
                public Configuration[] answer(InvocationOnMock invocation) throws Throwable
                {
                    Collection<Configuration> configs = m_Configs.values();
                    configs = Collections2.filter(configs, new Predicate<Configuration>()
                    {
                        @Override
                        public boolean apply(Configuration config)
                        {
                            // only keep configs in list that have properties set
                            return config.getProperties() != null;
                        }
                    });
                    
                    if (configs.size() == 0)
                    {
                        return null;
                    }
                    return configs.toArray(new Configuration[configs.size()]);
                }
            });
        }
        catch (InvalidSyntaxException | IOException e)
        {
            throw new IllegalStateException(e);
        }

        return configAdmin;
    }
/**
     * Create a configuration admin mock. 
     * @return
     *      initialized configuration admin mock with built in mocking behavior
     */
Create a configuration admin mock.
@return
initialized configuration admin mock with built in mocking behavior
Create a configuration admin mock . @return initialized configuration admin mock with built in mocking behavior
public static ConfigurationAdmin createMockConfigAdmin()
    {
        m_Configs.clear();
        m_Properties.clear();
        m_MSF.clear();
        ConfigurationAdmin configAdmin = mock(ConfigurationAdmin.class);
        try
        {
            when(configAdmin.createFactoryConfiguration(Mockito.anyString(), Mockito.anyString())).thenAnswer(
                    new Answer<Configuration>()
                    {
                        @Override
                        public Configuration answer(InvocationOnMock invocation) throws Throwable
                        {
                            String factoryPid = (String)invocation.getArguments()[0];
                            Preconditions.checkNotNull(factoryPid);
                            Configuration config = createMockConfiguration(factoryPid);
                            return config;
                        }
                    });
            when(configAdmin.getConfiguration(Mockito.anyString(), anyString())).thenAnswer(new Answer<Configuration>()
            {
                @Override
                public Configuration answer(InvocationOnMock invocation) throws Throwable
                {
                    String pid = (String)invocation.getArguments()[0];
                    return m_Configs.get(pid);
                }
            });
            when(configAdmin.listConfigurations(Mockito.anyString())).thenAnswer(new Answer<Configuration[]>()
            {
                @Override
                public Configuration[] answer(InvocationOnMock invocation) throws Throwable
                {
                    Collection<Configuration> configs = m_Configs.values();
                    configs = Collections2.filter(configs, new Predicate<Configuration>()
                    {
                        @Override
                        public boolean apply(Configuration config)
                        {
                            return config.getProperties() != null;
                        }
                    });
                    if (configs.size() == 0)
                    {
                        return null;
                    }
                    return configs.toArray(new Configuration[configs.size()]);
                }
            });
        }
        catch (InvalidSyntaxException | IOException e)
        {
            throw new IllegalStateException(e);
        }
        return configAdmin;
    }
public static ConfigurationAdmin createMockConfigAdmin ( ) { m_Configs . clear ( ) ; m_Properties . clear ( ) ; m_MSF . clear ( ) ; ConfigurationAdmin configAdmin = mock ( ConfigurationAdmin . class ) ; try { when ( configAdmin . createFactoryConfiguration ( Mockito . anyString ( ) , Mockito . anyString ( ) ) ) . thenAnswer ( new Answer < Configuration > ( ) { @ Override public Configuration answer ( InvocationOnMock invocation ) throws Throwable { String factoryPid = ( String ) invocation . getArguments ( ) [ 0 ] ; Preconditions . checkNotNull ( factoryPid ) ; Configuration config = createMockConfiguration ( factoryPid ) ; return config ; } } ) ; when ( configAdmin . getConfiguration ( Mockito . anyString ( ) , anyString ( ) ) ) . thenAnswer ( new Answer < Configuration > ( ) { @ Override public Configuration answer ( InvocationOnMock invocation ) throws Throwable { String pid = ( String ) invocation . getArguments ( ) [ 0 ] ; return m_Configs . get ( pid ) ; } } ) ; when ( configAdmin . listConfigurations ( Mockito . anyString ( ) ) ) . thenAnswer ( new Answer < Configuration [ ] > ( ) { @ Override public Configuration [ ] answer ( InvocationOnMock invocation ) throws Throwable { Collection < Configuration > configs = m_Configs . values ( ) ; configs = Collections2 . filter ( configs , new Predicate < Configuration > ( ) { @ Override public boolean apply ( Configuration config ) { return config . getProperties ( ) != null ; } } ) ; if ( configs . size ( ) == 0 ) { return null ; } return configs . toArray ( new Configuration [ configs . size ( ) ] ) ; } } ) ; } catch ( InvalidSyntaxException | IOException e ) { throw new IllegalStateException ( e ) ; } return configAdmin ; }
Create a configuration admin mock.
Create a configuration admin mock .
//create and store a new configuration //look up the configuration by PID from the config map //list all known configuration from config map // only keep configs in list that have properties set