View Javadoc
1   package org.apache.commons.jcs.engine.logging;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import junit.framework.TestCase;
23  import org.apache.commons.jcs.engine.logging.behavior.ICacheEvent;
24  import org.apache.log4j.Level;
25  import org.apache.log4j.Logger;
26  import org.apache.log4j.PatternLayout;
27  import org.apache.log4j.WriterAppender;
28  
29  import java.io.StringWriter;
30  
31  /** Unit tests for the debug implementation */
32  public class CacheEventLoggerDebugLoggerUnitTest
33      extends TestCase
34  {
35  
36      /** verify that we can log */
37      public void testLogICacheEvent_normal()
38      {
39          // SETUP
40          String logCategoryName = "testLogEvent_normal";
41  
42          String source = "mySource";
43          String region = "my region";
44          String eventName = "MyEventName";
45          String optionalDetails = "SomeExtraData";
46          String key = "my key";
47  
48          StringWriter stringWriter = new StringWriter();
49          configureLogger( stringWriter, logCategoryName );
50  
51          CacheEventLoggerDebugLogger logger = new CacheEventLoggerDebugLogger();
52          logger.setLogCategoryName( logCategoryName );
53  
54          ICacheEvent<String> event = logger.createICacheEvent( source, region, eventName, optionalDetails, key );
55  
56          // DO WORK
57          logger.logICacheEvent( event );
58  
59          // VERIFY
60          String result = stringWriter.toString();
61          assertTrue( "An event with the source should have been logged:" + result, result.indexOf( source ) != -1 );
62          assertTrue( "An event with the region should have been logged:" + result, result.indexOf( region ) != -1 );
63          assertTrue( "An event with the event name should have been logged:" + result, result.indexOf( eventName ) != -1 );
64          assertTrue( "An event with the optionalDetails should have been logged:" + result, result.indexOf( optionalDetails ) != -1 );
65          assertTrue( "An event with the key should have been logged:" + result, result.indexOf( key ) != -1 );
66      }
67  
68      /** verify that we can log */
69      public void testLogApplicationEvent_normal()
70      {
71          // SETUP
72          String logCategoryName = "testLogApplicationEvent_normal";
73  
74          String source = "mySource";
75          String eventName = "MyEventName";
76          String optionalDetails = "SomeExtraData";
77  
78          StringWriter stringWriter = new StringWriter();
79          configureLogger( stringWriter, logCategoryName );
80  
81          CacheEventLoggerDebugLogger logger = new CacheEventLoggerDebugLogger();
82          logger.setLogCategoryName( logCategoryName );
83  
84          // DO WORK
85          logger.logApplicationEvent( source, eventName, optionalDetails );
86  
87          // VERIFY
88          String result = stringWriter.toString();
89          assertTrue( "An event with the source should have been logged:" + result, result.indexOf( source ) != -1 );
90          assertTrue( "An event with the event name should have been logged:" + result, result.indexOf( eventName ) != -1 );
91          assertTrue( "An event with the optionalDetails should have been logged:" + result, result.indexOf( optionalDetails ) != -1 );
92      }
93  
94      /** verify that we can log */
95      public void testLogError_normal()
96      {
97          // SETUP
98          String logCategoryName = "testLogApplicationEvent_normal";
99  
100         String source = "mySource";
101         String eventName = "MyEventName";
102         String errorMessage = "SomeExtraData";
103 
104         StringWriter stringWriter = new StringWriter();
105         configureLogger( stringWriter, logCategoryName );
106 
107         CacheEventLoggerDebugLogger logger = new CacheEventLoggerDebugLogger();
108         logger.setLogCategoryName( logCategoryName );
109 
110         // DO WORK
111         logger.logError( source, eventName, errorMessage );
112 
113         // VERIFY
114         String result = stringWriter.toString();
115         assertTrue( "An event with the source should have been logged:" + result, result.indexOf( source ) != -1 );
116         assertTrue( "An event with the event name should have been logged:" + result, result.indexOf( eventName ) != -1 );
117         assertTrue( "An event with the errorMessage should have been logged:" + result, result.indexOf( errorMessage ) != -1 );
118     }
119 
120     /**
121      * Configures a logger for the given name. This allows us to check the log output.
122      * <p>
123      * @param stringWriter
124      * @param loggerName
125      */
126     private void configureLogger( StringWriter stringWriter, String loggerName )
127     {
128         Logger logger = Logger.getLogger( loggerName );
129         WriterAppender appender = null;
130 
131         try
132         {
133             appender = new WriterAppender( new PatternLayout(), stringWriter );
134         }
135         catch ( Exception e )
136         {
137             // NOOP
138         }
139 
140         logger.addAppender( appender );
141         logger.setLevel( Level.DEBUG );
142     }
143 }