1 package net.sf.ehcache.config;
2
3 import net.sf.ehcache.Cache;
4 import net.sf.ehcache.CacheManager;
5 import net.sf.ehcache.Element;
6 import org.junit.Before;
7 import org.junit.Test;
8
9 import java.lang.reflect.Field;
10
11 import static org.hamcrest.CoreMatchers.equalTo;
12 import static org.hamcrest.CoreMatchers.nullValue;
13 import static org.junit.Assert.assertThat;
14 import static org.junit.Assert.fail;
15
16 /***
17 * @author Alex Snaps
18 */
19 public class CacheConfigurationTest {
20
21 private CacheManager cacheManager;
22
23 @Before
24 public void setup() {
25 this.cacheManager = CacheManager.getInstance();
26 }
27
28 @Test
29 public void testDiskStorePath() {
30
31 String name = "testTemp";
32 String path = "c://something//temp";
33
34 CacheConfiguration cacheConfiguration = new CacheConfiguration()
35 .name(name)
36 .diskStorePath(path)
37 .diskPersistent(true);
38 cacheManager.addCache(new Cache(cacheConfiguration));
39 Cache cache = cacheManager.getCache(name);
40 assertThat(getDiskStorePath(cache), equalTo(path));
41 cache.put(new Element("KEY", "VALUE"));
42 }
43
44 @Test
45 public void testReadPercentageProperly() {
46 CacheConfiguration configuration = new CacheConfiguration();
47 assertThat(configuration.getMaxBytesLocalOffHeapPercentage(), nullValue());
48 configuration.setMaxBytesLocalOffHeap("12%");
49 assertThat(configuration.getMaxBytesLocalOffHeapPercentage(), equalTo(12));
50 configuration.setMaxBytesLocalOffHeap("99%");
51 assertThat(configuration.getMaxBytesLocalOffHeapPercentage(), equalTo(99));
52 configuration.setMaxBytesLocalOffHeap("100%");
53 assertThat(configuration.getMaxBytesLocalOffHeapPercentage(), equalTo(100));
54 configuration.setMaxBytesLocalOffHeap("0%");
55 assertThat(configuration.getMaxBytesLocalOffHeapPercentage(), equalTo(0));
56 try {
57 configuration.setMaxBytesLocalOffHeap("101%");
58 fail("This should throw an IllegalArgumentException, 101% is above 100%");
59 } catch (IllegalArgumentException e) {
60
61 }
62 try {
63 configuration.setMaxBytesLocalOffHeap("-10%");
64 fail("This should throw an IllegalArgumentException, -10% is below 0%");
65 } catch (IllegalArgumentException e) {
66
67 }
68 }
69
70 private String getDiskStorePath(final Cache cache) {
71 try {
72 Field declaredField = Cache.class.getDeclaredField("diskStorePath");
73 declaredField.setAccessible(true);
74 return (String)declaredField.get(cache);
75 } catch (Exception e) {
76 throw new RuntimeException("Did you rename Cache.diskStorePath ?!", e);
77 }
78 }
79 }