Skip to main content
summaryrefslogtreecommitdiffstats
blob: 66c51ed5b04a20358893532379749b84188f3b2b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/*******************************************************************************
 * Copyright (c) 2013 Boeing.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Boeing - initial API and implementation
 *******************************************************************************/
package org.eclipse.osee.cache.admin.internal;

import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import java.util.Arrays;
import java.util.concurrent.Callable;

import org.eclipse.osee.cache.admin.Cache;
import org.eclipse.osee.cache.admin.CacheConfiguration;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.mockito.MockitoAnnotations;

import com.google.common.collect.Iterables;

/**
 * Test Case for {@link CacheFactory, CacheProxy}
 * 
 * @author Roberto E. Escobar
 */
public class NoneLoadingCacheTest {

   private static final String KEY_1 = "key1";
   private static final String KEY_2 = "key2";
   private static final String KEY_3 = "key3";

   private static final Object OBJECT_1 = new Object();
   private static final Object OBJECT_2 = new Object();
   private static final Object OBJECT_3 = new Object();

   private Cache<String, Object> cache;
   private final CacheFactory factory = new CacheFactory();
   private final CacheConfiguration config = CacheConfiguration.newConfiguration();

   @Before
   public void setup() throws Exception {
      MockitoAnnotations.initMocks(this);

      cache = factory.createCache(config);
   }

   @Test
   public void testGetIfPresent() throws Exception {
      Object value = cache.getIfPresent(KEY_1);
      assertNull(value);

      value = cache.get(KEY_1, createCallable(OBJECT_1));
      assertEquals(OBJECT_1, value);

      assertEquals(1, cache.size());

      value = cache.getIfPresent(KEY_1);
      assertEquals(OBJECT_1, value);
   }

   @Test
   public void testGetAllPresent() throws Exception {
      Iterable<Object> values = cache.getAllPresent();
      assertEquals(false, values.iterator().hasNext());

      Object value1 = cache.get(KEY_1, createCallable(OBJECT_1));
      Object value2 = cache.get(KEY_2, createCallable(OBJECT_2));
      Object value3 = cache.get(KEY_3, createCallable(OBJECT_3));

      assertEquals(OBJECT_1, value1);
      assertEquals(OBJECT_2, value2);
      assertEquals(OBJECT_3, value3);

      assertEquals(3, cache.size());

      values = cache.getAllPresent();

      Assert.assertTrue(Iterables.contains(values, OBJECT_1));
      Assert.assertTrue(Iterables.contains(values, OBJECT_2));
      Assert.assertTrue(Iterables.contains(values, OBJECT_3));
   }

   @Test
   public void testGet() throws Exception {
      assertEquals(true, cache.isEmpty());

      Object value = cache.get(KEY_1);
      assertNull(value);

      Object value1 = cache.get(KEY_1, createCallable(OBJECT_1));

      assertEquals(OBJECT_1, value1);
      assertEquals(1, cache.size());
      assertEquals(false, cache.isEmpty());

      Object value2 = cache.get(KEY_1);
      assertEquals(OBJECT_1, value2);
      assertEquals(1, cache.size());
      assertEquals(false, cache.isEmpty());
   }

   @Test
   public void testGetAll() throws Exception {
      assertEquals(true, cache.isEmpty());

      cache.get(KEY_1, createCallable(OBJECT_1));
      cache.get(KEY_2, createCallable(OBJECT_2));
      cache.get(KEY_3, createCallable(OBJECT_3));

      Iterable<Object> value = cache.getAll();

      assertEquals(3, cache.size());
      assertEquals(false, cache.isEmpty());

      assertTrue(Iterables.contains(value, OBJECT_1));
      assertTrue(Iterables.contains(value, OBJECT_2));
      assertTrue(Iterables.contains(value, OBJECT_3));
   }

   @Test
   public void testGetAllKeys() throws Exception {
      assertEquals(true, cache.isEmpty());

      cache.get(KEY_1, createCallable(OBJECT_1));
      cache.get(KEY_2, createCallable(OBJECT_2));
      cache.get(KEY_3, createCallable(OBJECT_3));

      Iterable<? extends String> value = cache.getAllKeys();

      assertEquals(3, cache.size());
      assertEquals(false, cache.isEmpty());

      assertTrue(Iterables.contains(value, KEY_1));
      assertTrue(Iterables.contains(value, KEY_2));
      assertTrue(Iterables.contains(value, KEY_3));
   }

   @Test
   public void testInvalidateAll() throws Exception {
      assertEquals(true, cache.isEmpty());
      assertEquals(0, cache.size());

      cache.get(KEY_1, createCallable(OBJECT_1));
      cache.get(KEY_2, createCallable(OBJECT_2));
      cache.get(KEY_3, createCallable(OBJECT_3));

      assertEquals(3, cache.size());

      cache.invalidateAll();

      assertEquals(0, cache.size());
   }

   @Test
   public void testInvalidateKeys() throws Exception {
      assertEquals(true, cache.isEmpty());
      assertEquals(0, cache.size());

      cache.get(KEY_1, createCallable(OBJECT_1));
      cache.get(KEY_2, createCallable(OBJECT_2));
      cache.get(KEY_3, createCallable(OBJECT_3));

      assertEquals(3, cache.size());

      cache.invalidate(Arrays.asList(KEY_1, KEY_3));
      assertEquals(1, cache.size());

      assertNull(cache.getIfPresent(KEY_1));
      assertEquals(OBJECT_2, cache.getIfPresent(KEY_2));
      assertNull(cache.getIfPresent(KEY_3));
   }

   @Test
   public void testInvalidate() throws Exception {
      assertEquals(true, cache.isEmpty());
      assertEquals(0, cache.size());

      cache.get(KEY_1, createCallable(OBJECT_1));
      cache.get(KEY_2, createCallable(OBJECT_2));
      cache.get(KEY_3, createCallable(OBJECT_3));

      assertEquals(3, cache.size());

      cache.invalidate(KEY_2);
      assertEquals(2, cache.size());

      assertEquals(OBJECT_1, cache.getIfPresent(KEY_1));
      assertNull(cache.getIfPresent(KEY_2));
      assertEquals(OBJECT_3, cache.getIfPresent(KEY_3));
   }

   @Test
   public void testRefresh() throws Exception {
      assertEquals(true, cache.isEmpty());
      assertEquals(0, cache.size());

      @SuppressWarnings("unchecked")
      Callable<Object> mockedCallable = mock(Callable.class);

      when(mockedCallable.call()).thenReturn(OBJECT_2);

      cache.get(KEY_1, createCallable(OBJECT_1));

      cache.get(KEY_2, mockedCallable);
      verify(mockedCallable, times(1)).call();
      assertEquals(2, cache.size());

      cache.get(KEY_3, createCallable(OBJECT_3));
      assertEquals(3, cache.size());

      cache.refresh(KEY_2);

      verify(mockedCallable, times(1)).call();
   }

   private static Callable<Object> createCallable(final Object object) {
      return new Callable<Object>() {

         @Override
         public Object call() throws Exception {
            return object;
         }
      };
   }
}

Back to the top