View Javadoc
1   package org.apache.commons.jcs3.auxiliary;
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  
24  import org.apache.commons.jcs3.engine.control.MockElementSerializer;
25  import org.apache.commons.jcs3.engine.logging.MockCacheEventLogger;
26  import org.apache.commons.jcs3.engine.behavior.IElementSerializer;
27  import org.apache.commons.jcs3.utils.serialization.StandardSerializer;
28  
29  import java.util.Properties;
30  
31  /** Unit tests for the auxiliary cache configurator. */
32  public class AuxiliaryCacheConfiguratorUnitTest
33      extends TestCase
34  {
35      /**
36       * Verify that we don't get an error.
37       */
38      public void testParseCacheEventLogger_Null()
39      {
40          // SETUP
41          final Properties props = new Properties();
42  
43          // DO WORK
44          final MockCacheEventLogger result = (MockCacheEventLogger) AuxiliaryCacheConfigurator.parseCacheEventLogger( props,
45                                                                                                                 "junk" );
46  
47          // VERIFY
48          assertNull( "Should not have a logger.", result );
49      }
50  
51      /**
52       * Verify that we don't get an error.
53       */
54      public void testParseCacheEventLogger_NullName()
55      {
56          // SETUP
57          final Properties props = new Properties();
58  
59          // DO WORK
60          final MockCacheEventLogger result = (MockCacheEventLogger) AuxiliaryCacheConfigurator.parseCacheEventLogger( props,
61                                                                                                                 null );
62  
63          // VERIFY
64          assertNull( "Should not have a logger.", result );
65      }
66  
67      /**
68       * Verify that we can parse the event logger.
69       */
70      public void testParseCacheEventLogger_Normal()
71      {
72          // SETUP
73          final String auxPrefix = "jcs.auxiliary." + "MYAux";
74          final String testPropertyValue = "This is the value";
75          final String className = MockCacheEventLogger.class.getName();
76  
77          final Properties props = new Properties();
78          props.put( auxPrefix + AuxiliaryCacheConfigurator.CACHE_EVENT_LOGGER_PREFIX, className );
79          props.put( auxPrefix + AuxiliaryCacheConfigurator.CACHE_EVENT_LOGGER_PREFIX
80              + AuxiliaryCacheConfigurator.ATTRIBUTE_PREFIX + ".testProperty", testPropertyValue );
81  
82          // DO WORK
83          final MockCacheEventLogger result = (MockCacheEventLogger) AuxiliaryCacheConfigurator
84              .parseCacheEventLogger( props, auxPrefix );
85  
86          // VERIFY
87          assertNotNull( "Should have a logger.", result );
88          assertEquals( "Property should be set.", testPropertyValue, result.getTestProperty() );
89      }
90  
91      /**
92       * Verify that we can parse the ElementSerializer.
93       */
94      public void testParseElementSerializer_Normal()
95      {
96          // SETUP
97          final String auxPrefix = "jcs.auxiliary." + "MYAux";
98          final String testPropertyValue = "This is the value";
99          final String className = MockElementSerializer.class.getName();
100 
101         final Properties props = new Properties();
102         props.put( auxPrefix + AuxiliaryCacheConfigurator.SERIALIZER_PREFIX, className );
103         props.put( auxPrefix + AuxiliaryCacheConfigurator.SERIALIZER_PREFIX
104             + AuxiliaryCacheConfigurator.ATTRIBUTE_PREFIX + ".testProperty", testPropertyValue );
105 
106         // DO WORK
107         final MockElementSerializer result = (MockElementSerializer) AuxiliaryCacheConfigurator
108             .parseElementSerializer( props, auxPrefix );
109 
110         // VERIFY
111         assertNotNull( "Should have a Serializer.", result );
112         assertEquals( "Property should be set.", testPropertyValue, result.getTestProperty() );
113     }
114 
115     /**
116      * Verify that we can parse the ElementSerializer.
117      */
118     public void testParseElementSerializer_Null()
119     {
120         // SETUP
121         final Properties props = new Properties();
122 
123         // DO WORK
124         final IElementSerializer result = AuxiliaryCacheConfigurator
125             .parseElementSerializer( props, "junk" );
126 
127         // VERIFY
128         assertTrue( "Should have the default Serializer.", result instanceof StandardSerializer );
129     }
130 }