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