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 8743 2012-10-07 03:06:20Z 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    /** Creates a new {@code ApplicationLoggerTest} instance. */
058    public ApplicationLoggerTest()
059    {
060        super();
061    }
062
063    /**
064     * Gets the {@code ApplicationLogger} implementation tests are performed
065     * with.
066     *
067     * @return the {@code ApplicationLogger} implementation tests are performed
068     * with.
069     */
070    public ApplicationLogger getApplicationLogger()
071    {
072        return this.logger;
073    }
074
075    /**
076     * Sets the {@code ApplicationLogger} implementation tests are performed
077     * with.
078     *
079     * @param value the {@code ApplicationLogger} implementation to perform
080     * tests with.
081     */
082    public final void setApplicationLogger( final ApplicationLogger value )
083    {
084        this.logger = value;
085        this.setMessageEventSource( value );
086    }
087
088    /** {@code MessageListener} implementation tests are performed with. */
089    public static final class TestMessageListener implements MessageListener
090    {
091
092        /** The last event provided to this listener. */
093        private MessageEvent lastEvent;
094
095        /** Creates a new {@code TestMessageListener} instance. */
096        public TestMessageListener()
097        {
098            super();
099        }
100
101        /**
102         * Gets the last event provided to this listener.
103         *
104         * @return the last event provided to this listener.
105         */
106        public MessageEvent getLastEvent()
107        {
108            return this.lastEvent;
109        }
110
111        public void onMessage( final MessageEvent messageEvent )
112        {
113            this.lastEvent = messageEvent;
114        }
115
116    }
117
118    //---------------------------------------------------ApplicationLoggerTest--
119    //--Tests-------------------------------------------------------------------
120
121    /**
122     * Tests the {@link ApplicationLogger#log(MessageEvent)} method to handle
123     * illegal arguments correctly by throwing a corresponding
124     * {@code NullPointerException}.
125     */
126    public void testIllegalArguments() throws Exception
127    {
128        assert this.getApplicationLogger() != null;
129
130        try
131        {
132            this.getApplicationLogger().log( null );
133            throw new AssertionError();
134        }
135        catch ( final NullPointerException e )
136        {
137            Assert.assertNotNull( e.getMessage() );
138            System.out.println( e.toString() );
139        }
140
141    }
142
143    /**
144     * Tests the {@link ApplicationLogger#log(MessageEvent)} method to fire
145     * corresponding events.
146     */
147    public void testLog() throws Exception
148    {
149        assert this.getApplicationLogger() != null;
150
151        final TestMessageListener listener = new TestMessageListener();
152        final Message message = new Message()
153        {
154
155            public Object[] getFormatArguments( final Locale locale )
156            {
157                return new Object[ 0 ];
158            }
159
160            public String getText( final Locale locale )
161            {
162                return ApplicationLoggerTest.class.getName();
163            }
164
165        };
166
167        this.getMessageEventSource().addMessageListener( listener );
168
169        final MessageEvent evt =
170            new MessageEvent( this, message, MessageEvent.INFORMATION );
171
172        this.getApplicationLogger().log( evt );
173        Assert.assertNotNull( listener.getLastEvent() );
174        Assert.assertTrue( listener.getLastEvent() == evt );
175    }
176
177    //-------------------------------------------------------------------Tests--
178}