001    /*
002     * Copyright 2005 Stephen J. McConnell.
003     *
004     * Licensed  under the  Apache License,  Version 2.0  (the "License");
005     * you may not use  this file  except in  compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     *   http://www.apache.org/licenses/LICENSE-2.0
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed  under the  License is distributed on an "AS IS" BASIS,
012     * WITHOUT  WARRANTIES OR CONDITIONS  OF ANY KIND, either  express  or
013     * implied.
014     *
015     * See the License for the specific language governing permissions and
016     * limitations under the License.
017     */
018    
019    package net.dpml.test.app;
020    
021    import net.dpml.logging.Logger;
022    
023    import net.dpml.metro.PartsManager;
024    import net.dpml.metro.ComponentHandler;
025    
026    /**
027     * The demo class is used to aggregate a collection of components and 
028     * provide some hooks for the testcase.
029     * 
030     * @author <a href="http://www.dpml.net">Digital Product Meta Library</a>
031     * @version 1.2.0 
032     */
033    public class Demo
034    {
035        private final PartsManager m_parts;
036        private final Logger m_logger;
037        
038       /**
039        * Creation of a new demo component.
040        * @param logger a logging channel
041        * @param parts the internal parts
042        */
043        public Demo( Logger logger, PartsManager parts )
044        {
045            m_logger = logger;
046            m_parts = parts;
047        }
048        
049       /**
050        * Hook for testcase to fire a message noytify request to the 
051        * server which in turn fires notifiy requests to listeners.
052        * @param message the notification message
053        * @return the number of notify messages fired to the listener
054        * @exception Exception if something goes wrong
055        */
056        public int test( String message ) throws Exception
057        {
058            m_logger.debug( "test: " + message );
059            DefaultServer server = getServer();
060            DefaultListener listener = getListener();
061            server.triggerNotify( message );
062            return listener.getCount();
063        }
064        
065        DefaultServer getServer() throws Exception
066        {
067            ComponentHandler handler = m_parts.getComponentHandler( "server" );
068            return (DefaultServer) handler.getProvider().getValue( false );
069        }
070        
071        DefaultListener getListener() throws Exception
072        {
073            ComponentHandler handler = m_parts.getComponentHandler( "listener" );
074            return (DefaultListener) handler.getProvider().getValue( false );
075        }
076    }