001/*
002 *  jDTAUS Core Test Suite
003 *  Copyright (C) 2005 Christian Schulte
004 *  <cs@schulte.it>
005 *
006 *  This library is free software; you can redistribute it and/or
007 *  modify it under the terms of the GNU Lesser General Public
008 *  License as published by the Free Software Foundation; either
009 *  version 2.1 of the License, or any later version.
010 *
011 *  This library is distributed in the hope that it will be useful,
012 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
013 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014 *  Lesser General Public License for more details.
015 *
016 *  You should have received a copy of the GNU Lesser General Public
017 *  License along with this library; if not, write to the Free Software
018 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
019 *
020 */
021package org.jdtaus.core.text.spi.it;
022
023import java.util.Locale;
024import junit.framework.Assert;
025import org.jdtaus.core.text.Message;
026import org.jdtaus.core.text.MessageEvent;
027import org.jdtaus.core.text.MessageEventSource;
028import org.jdtaus.core.text.MessageListener;
029import org.jdtaus.core.text.it.MessageEventSourceTest;
030import org.jdtaus.core.text.spi.ApplicationLogger;
031
032/**
033 * Testcase for {@code ApplicationLogger} implementations.
034 *
035 * @author <a href="mailto:cs@schulte.it">Christian Schulte</a>
036 * @version $JDTAUS: ApplicationLoggerTest.java 8641 2012-09-27 06:45:17Z schulte $
037 */
038public class ApplicationLoggerTest extends MessageEventSourceTest
039{
040    //--MessageEventSourceTest--------------------------------------------------
041
042    /**
043     * {@inheritDoc}
044     * @see #getApplicationLogger()
045     */
046    public final MessageEventSource getMessageEventSource()
047    {
048        return this.logger;
049    }
050
051    //--------------------------------------------------MessageEventSourceTest--
052    //--ApplicationLoggerTest---------------------------------------------------
053
054    /** Implementation to test. */
055    private ApplicationLogger logger;
056
057    /**
058     * Gets the {@code ApplicationLogger} implementation tests are performed
059     * with.
060     *
061     * @return the {@code ApplicationLogger} implementation tests are performed
062     * with.
063     */
064    public ApplicationLogger getApplicationLogger()
065    {
066        return this.logger;
067    }
068
069    /**
070     * Sets the {@code ApplicationLogger} implementation tests are performed
071     * with.
072     *
073     * @param value the {@code ApplicationLogger} implementation to perform
074     * tests with.
075     */
076    public final void setApplicationLogger( final ApplicationLogger value )
077    {
078        this.logger = value;
079        this.setMessageEventSource( value );
080    }
081
082    /** {@code MessageListener} implementation tests are performed with. */
083    public static final class TestMessageListener implements MessageListener
084    {
085
086        /** The last event provided to this listener. */
087        private MessageEvent lastEvent;
088
089        /**
090         * Gets the last event provided to this listener.
091         *
092         * @return the last event provided to this listener.
093         */
094        public MessageEvent getLastEvent()
095        {
096            return this.lastEvent;
097        }
098
099        public void onMessage( final MessageEvent messageEvent )
100        {
101            this.lastEvent = messageEvent;
102        }
103
104    }
105
106    //---------------------------------------------------ApplicationLoggerTest--
107    //--Tests-------------------------------------------------------------------
108
109    /**
110     * Tests the {@link ApplicationLogger#log(MessageEvent)} method to handle
111     * illegal arguments correctly by throwing a corresponding
112     * {@code NullPointerException}.
113     */
114    public void testIllegalArguments() throws Exception
115    {
116        assert this.getApplicationLogger() != null;
117
118        try
119        {
120            this.getApplicationLogger().log( null );
121            throw new AssertionError();
122        }
123        catch ( NullPointerException e )
124        {
125            Assert.assertNotNull( e.getMessage() );
126            System.out.println( e.toString() );
127        }
128
129    }
130
131    /**
132     * Tests the {@link ApplicationLogger#log(MessageEvent)} method to fire
133     * corresponding events.
134     */
135    public void testLog() throws Exception
136    {
137        assert this.getApplicationLogger() != null;
138
139        final TestMessageListener listener = new TestMessageListener();
140        final Message message = new Message()
141        {
142
143            public Object[] getFormatArguments( final Locale locale )
144            {
145                return new Object[ 0 ];
146            }
147
148            public String getText( final Locale locale )
149            {
150                return ApplicationLoggerTest.class.getName();
151            }
152
153        };
154
155        this.getMessageEventSource().addMessageListener( listener );
156
157        MessageEvent evt = new MessageEvent( this, message,
158                                             MessageEvent.INFORMATION );
159
160        this.getApplicationLogger().log( evt );
161        Assert.assertNotNull( listener.getLastEvent() );
162        Assert.assertTrue( listener.getLastEvent() == evt );
163    }
164
165    //-------------------------------------------------------------------Tests--
166}