View Javadoc

1   /***
2    *  Copyright 2003-2010 Terracotta, Inc.
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
15   */
16  
17  package net.sf.ehcache.statistics;
18  
19  import net.sf.ehcache.AbstractCacheTest;
20  import net.sf.ehcache.Cache;
21  
22  import org.junit.Test;
23  
24  import static org.junit.Assert.assertFalse;
25  import static org.junit.Assert.assertTrue;
26  
27  /***
28   * Tests that we pickup the cache statistics disabled setting from the config, and that we can enable stats programatically too.
29   *
30   * @author Chris Dennis
31   */
32  public class DisabledCacheStatisticsTest extends AbstractCacheTest {
33  
34      /***
35       * test enable disable
36       */
37      @Test
38      public void testEnableDisable() {
39          Cache cache = manager.getCache("disabledStats");
40  
41          // statistics are initially disabled
42          assertFalse(cache.isStatisticsEnabled());
43          assertFalse(cache.isSampledStatisticsEnabled());
44  
45          // enabling stats doesn't enable sampled stats
46          cache.setStatisticsEnabled(true);
47          assertTrue(cache.isStatisticsEnabled());
48          assertFalse(cache.isSampledStatisticsEnabled());
49  
50          // can disable again
51          cache.setStatisticsEnabled(false);
52          assertFalse(cache.isStatisticsEnabled());
53          assertFalse(cache.isSampledStatisticsEnabled());
54  
55          // enabling sampled enables both
56          cache.setSampledStatisticsEnabled(true);
57          assertTrue(cache.isStatisticsEnabled());
58          assertTrue(cache.isSampledStatisticsEnabled());
59  
60          cache.setSampledStatisticsEnabled(false);
61          assertTrue(cache.isStatisticsEnabled());
62          assertFalse(cache.isSampledStatisticsEnabled());
63  
64          // enable sampled again
65          cache.setSampledStatisticsEnabled(true);
66          assertTrue(cache.isStatisticsEnabled());
67          assertTrue(cache.isSampledStatisticsEnabled());
68  
69          // disabling live disables sampled too
70          cache.setStatisticsEnabled(false);
71          assertFalse(cache.isStatisticsEnabled());
72          assertFalse(cache.isSampledStatisticsEnabled());
73      }
74  }