View Javadoc

1   /*
2    *  jDTAUS Core Test Suite
3    *  Copyright (C) 2005 Christian Schulte
4    *  <cs@schulte.it>
5    *
6    *  This library is free software; you can redistribute it and/or
7    *  modify it under the terms of the GNU Lesser General Public
8    *  License as published by the Free Software Foundation; either
9    *  version 2.1 of the License, or any later version.
10   *
11   *  This library is distributed in the hope that it will be useful,
12   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   *  Lesser General Public License for more details.
15   *
16   *  You should have received a copy of the GNU Lesser General Public
17   *  License along with this library; if not, write to the Free Software
18   *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19   *
20   */
21  package org.jdtaus.core.text.spi.it;
22  
23  import java.util.Locale;
24  import junit.framework.Assert;
25  import org.jdtaus.core.text.Message;
26  import org.jdtaus.core.text.MessageEvent;
27  import org.jdtaus.core.text.MessageEventSource;
28  import org.jdtaus.core.text.MessageListener;
29  import org.jdtaus.core.text.it.MessageEventSourceTest;
30  import org.jdtaus.core.text.spi.ApplicationLogger;
31  
32  /**
33   * Testcase for {@code ApplicationLogger} implementations.
34   *
35   * @author <a href="mailto:cs@schulte.it">Christian Schulte</a>
36   * @version $JDTAUS: ApplicationLoggerTest.java 8743 2012-10-07 03:06:20Z schulte $
37   */
38  public class ApplicationLoggerTest extends MessageEventSourceTest
39  {
40      //--MessageEventSourceTest--------------------------------------------------
41  
42      /**
43       * {@inheritDoc}
44       * @see #getApplicationLogger()
45       */
46      public final MessageEventSource getMessageEventSource()
47      {
48          return this.logger;
49      }
50  
51      //--------------------------------------------------MessageEventSourceTest--
52      //--ApplicationLoggerTest---------------------------------------------------
53  
54      /** Implementation to test. */
55      private ApplicationLogger logger;
56  
57      /** Creates a new {@code ApplicationLoggerTest} instance. */
58      public ApplicationLoggerTest()
59      {
60          super();
61      }
62  
63      /**
64       * Gets the {@code ApplicationLogger} implementation tests are performed
65       * with.
66       *
67       * @return the {@code ApplicationLogger} implementation tests are performed
68       * with.
69       */
70      public ApplicationLogger getApplicationLogger()
71      {
72          return this.logger;
73      }
74  
75      /**
76       * Sets the {@code ApplicationLogger} implementation tests are performed
77       * with.
78       *
79       * @param value the {@code ApplicationLogger} implementation to perform
80       * tests with.
81       */
82      public final void setApplicationLogger( final ApplicationLogger value )
83      {
84          this.logger = value;
85          this.setMessageEventSource( value );
86      }
87  
88      /** {@code MessageListener} implementation tests are performed with. */
89      public static final class TestMessageListener implements MessageListener
90      {
91  
92          /** The last event provided to this listener. */
93          private MessageEvent lastEvent;
94  
95          /** Creates a new {@code TestMessageListener} instance. */
96          public TestMessageListener()
97          {
98              super();
99          }
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 }