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.constructs.blocking;
18
19 import net.sf.ehcache.CacheException;
20 import net.sf.ehcache.Element;
21 import org.junit.Ignore;
22 import org.junit.Test;
23
24 import java.util.concurrent.ExecutionException;
25
26 import static junit.framework.Assert.assertSame;
27 import static org.junit.Assert.assertEquals;
28 import static org.junit.Assert.fail;
29
30
31 /***
32 * Test cases for the {@link UpdatingSelfPopulatingCache}.
33 *
34 * @author <a href="mailto:gluck@thoughtworks.com">Greg Luck</a>
35 * @version $Id: UpdatingSelfPopulatingCacheTest.html 13146 2011-08-01 17:12:39Z oletizi $
36 */
37 public class UpdatingSelfPopulatingCacheTest extends SelfPopulatingCacheTest {
38
39 /***
40 * Tests fetching an entry, and then an update.
41 * TODO FIXME: Was noticed breaking 30/8/10
42 */
43 @Test
44 @Ignore
45 public void testFetchAndUpdate() throws Exception {
46 final String value = "value";
47 final CountingCacheEntryFactory factory = new CountingCacheEntryFactory(value);
48 selfPopulatingCache = new UpdatingSelfPopulatingCache(cache, factory);
49
50
51
52 Element element = selfPopulatingCache.get(null);
53
54
55 element = selfPopulatingCache.get("key");
56 assertSame(value, element.getObjectValue());
57 assertEquals(2, factory.getCount());
58
59 Object actualValue = selfPopulatingCache.get("key").getObjectValue();
60 assertSame(value, actualValue);
61 assertEquals(3, factory.getCount());
62
63 actualValue = selfPopulatingCache.get("key").getObjectValue();
64 assertSame(value, actualValue);
65 assertEquals(4, factory.getCount());
66 }
67
68 /***
69 * Tests when fetch fails.
70 */
71 @Test
72 public void testFetchFail() throws Exception {
73 final Exception exception = new Exception("Failed.");
74 final UpdatingCacheEntryFactory factory = new UpdatingCacheEntryFactory() {
75 public Object createEntry(final Object key)
76 throws Exception {
77 throw exception;
78 }
79
80 public void updateEntryValue(Object key, Object value)
81 throws Exception {
82 throw exception;
83 }
84 };
85
86 selfPopulatingCache = new UpdatingSelfPopulatingCache(cache, factory);
87
88
89 try {
90 selfPopulatingCache.get("key");
91 fail();
92 } catch (final Exception e) {
93 Thread.sleep(20);
94
95
96 assertEquals("Could not update object for cache entry with key \"key\".", e.getMessage());
97 }
98
99 }
100
101 /***
102 * Tests refreshing the entries.
103 */
104 @Test
105 public void testRefresh() throws Exception {
106 final String value = "value";
107 final CountingCacheEntryFactory factory = new CountingCacheEntryFactory(value);
108 selfPopulatingCache = new UpdatingSelfPopulatingCache(cache, factory);
109
110
111 try {
112 selfPopulatingCache.refresh();
113 fail();
114 } catch (CacheException e) {
115
116 assertEquals("UpdatingSelfPopulatingCache objects should not be refreshed.", e.getMessage());
117 }
118
119 }
120
121 /***
122 * Tests the async load with a single item
123 */
124 @Override
125 @Test
126 public void testAsynchronousLoad() throws InterruptedException, ExecutionException {
127 super.testAsynchronousLoad();
128 }
129 }