summaryrefslogtreecommitdiffstats
blob: bda475179e4662836ef63fe1f98c9026c27267f6 (plain)
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/*******************************************************************************
 * Copyright (c) 2004, 2007 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.framework.core.model.cache;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import org.eclipse.osee.framework.core.exception.OseeArgumentException;
import org.eclipse.osee.framework.core.exception.OseeCoreException;
import org.eclipse.osee.framework.core.exception.OseeStateException;
import org.eclipse.osee.framework.core.exception.OseeTypeDoesNotExist;
import org.eclipse.osee.framework.core.model.AbstractOseeType;
import org.eclipse.osee.framework.jdk.core.util.GUID;
import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;

/**
 * Test Case for {@link AbstractOseeCache}
 * 
 * @author Roberto E. Escobar
 */
public abstract class AbstractOseeCacheTest<T extends AbstractOseeType> {
   private final List<T> data;
   private final AbstractOseeCache<T> cache;
   private final TypeComparator comparator;

   public AbstractOseeCacheTest(List<T> artifactTypes, AbstractOseeCache<T> typeCache) {
      this.comparator = new TypeComparator();
      this.data = artifactTypes;
      this.cache = typeCache;
   }

   @org.junit.Test
   public void testAllItems() throws OseeCoreException {
      List<T> actualTypes = new ArrayList<T>(cache.getAll());
      java.util.Collections.sort(actualTypes, comparator);

      java.util.Collections.sort(data, comparator);
      Assert.assertEquals(data.size(), actualTypes.size());
      for (int index = 0; index < data.size(); index++) {
         Assert.assertNotNull(actualTypes.get(index));
         checkEquals(data.get(index), actualTypes.get(index));
      }
   }

   @org.junit.Test
   public void testExistByGuid() throws OseeCoreException {
      for (T expected : data) {
         Assert.assertTrue(cache.existsByGuid(expected.getGuid()));
      }
      Assert.assertFalse(cache.existsByGuid("notExist"));
   }

   @org.junit.Test
   public void testCacheByGuid() throws OseeCoreException {
      for (T expected : data) {
         T actual = cache.getByGuid(expected.getGuid());
         Assert.assertNotNull(actual);
         checkEquals(expected, actual);
      }
   }

   @org.junit.Test
   public void testCacheById() throws OseeCoreException {
      for (T expected : data) {
         T actual = cache.getById(expected.getId());
         Assert.assertNotNull(actual);
         checkEquals(expected, actual);
      }
   }

   @org.junit.Test
   public void testCacheByName() throws OseeCoreException {
      for (T expected : data) {
         T actual = cache.getUniqueByName(expected.getName());
         Assert.assertNotNull(actual);
         checkEquals(expected, actual);
      }
   }

   @SuppressWarnings("unchecked")
   @org.junit.Test
   public void testDecache() throws OseeCoreException {
      T item1 = data.get(0);
      checkCached(item1, true);

      cache.decache(item1);
      checkCached(item1, false);

      cache.cache(item1);
      checkCached(item1, true);

      T item2 = data.get(1);
      checkCached(item2, true);

      cache.decache(item1, item2);
      checkCached(item1, false);
      checkCached(item2, false);

      cache.cache(item1, item2);
      checkCached(item1, true);
      checkCached(item2, true);
   }

   private void checkCached(T item, boolean isInCacheExpected) throws OseeCoreException {
      if (isInCacheExpected) {
         Assert.assertEquals(item, cache.getByGuid(item.getGuid()));
         Assert.assertEquals(item, cache.getById(item.getId()));
         Assert.assertEquals(item, cache.getUniqueByName(item.getName()));
         Assert.assertTrue(cache.getAll().contains(item));
      } else {
         Assert.assertNull(cache.getByGuid(item.getGuid()));
         Assert.assertNull(cache.getById(item.getId()));
         Assert.assertNull(cache.getUniqueByName(item.getName()));
         Assert.assertFalse(cache.getAll().contains(item));
      }
   }

   @org.junit.Test
   public void testGetByName() throws OseeCoreException {
      for (T expected : data) {
         Collection<T> actual = cache.getByName(expected.getName());
         Assert.assertNotNull(actual);
         Assert.assertEquals(1, actual.size());
         checkEquals(expected, actual.iterator().next());
      }
   }

   @org.junit.Test
   public void testMultipleGetByName() throws OseeCoreException {
      T item1 = data.get(0);
      T item2 = data.get(1);
      Assert.assertNotNull(item1);
      Assert.assertNotNull(item2);

      Collection<T> actual = cache.getByName(item1.getName());
      Assert.assertNotNull(actual);
      Assert.assertEquals(1, actual.size());
      checkEquals(item1, actual.iterator().next());

      actual = cache.getByName(item2.getName());
      Assert.assertNotNull(actual);
      Assert.assertEquals(1, actual.size());
      checkEquals(item2, actual.iterator().next());

      String originalName = item1.getName();
      if (item1 instanceof AbstractOseeType) {
         cache.decache(item1);

         ((AbstractOseeType) item1).setName(item2.getName());

         if (cache.isNameUniquenessEnforced()) {
            try {
               cache.cache(item1);
               Assert.fail("This line should not be executed");
            } catch (OseeCoreException ex) {
               Assert.assertTrue(ex instanceof OseeStateException);
            }

            actual = cache.getByName(originalName);
            Assert.assertNotNull(actual);
            Assert.assertEquals(0, actual.size());

         } else {
            cache.cache(item1);

            actual = cache.getByName(originalName);
            Assert.assertNotNull(actual);
            Assert.assertEquals(0, actual.size());

            actual = cache.getByName(item2.getName());
            Assert.assertNotNull(actual);
            Assert.assertEquals(2, actual.size());

            checkEquals(item2, actual.iterator().next());
         }
         ((AbstractOseeType) item1).setName(originalName);
      }
   }

   @Test
   public void testReload() throws OseeCoreException {
      int fullCacheSize = cache.size();
      Assert.assertTrue(fullCacheSize > 0);
      for (T type : cache.getAll()) {
         cache.decache(type);
      }

      Assert.assertEquals(0, cache.size());
      Assert.assertTrue(cache.reloadCache());
      Assert.assertEquals(fullCacheSize, cache.size());
   }

   @Test(expected = OseeTypeDoesNotExist.class)
   public void testNonExistingPersist() throws OseeCoreException {
      cache.storeByGuid(Collections.singleton(GUID.create()));
   }

   @SuppressWarnings("unchecked")
   @Test(expected = OseeArgumentException.class)
   public void testStoreNull2() throws OseeCoreException {
      cache.storeItems((T) null);
   }

   @Test(expected = OseeArgumentException.class)
   public void testStoreNull3() throws OseeCoreException {
      cache.storeItems((Collection<T>) null);
   }

   @Ignore
   @Test
   public void testPersist() {
      //      T item1 = data.get(0);
      //      T item2 = data.get(1);
      //
      //      String name1 = item1.getName();
      //      String name2 = item2.getName();
      //
      //      AbstractOseeType t1 = (AbstractOseeType) item1;
      //      AbstractOseeType t2 = (AbstractOseeType) item2;
      //
      //      t1.setName("changed_1");
      //      t2.setName("changed_2");
      //
      //      cache.storeByGuid(guids);

      //      T item2 = data.get(1);
      //
      //
      //      cache.storeItem(item);
      //      cache.storeItems(toStore);
      //      cache.storeItems(items);
      //      public void storeItem(AbstractOseeType item) throws OseeCoreException {

      //      public void storeItems(T... items) throws OseeCoreException {

      //      public void storeItems(Collection<T> toStore) throws OseeCoreException {
   }

   @SuppressWarnings("unused")
   //OseeCoreException is thrown by inheriting class.
   protected void checkEquals(T expected, T actual) throws OseeCoreException {
      Assert.assertEquals(expected, actual);
   }

   private final class TypeComparator implements Comparator<T> {

      @Override
      public int compare(T o1, T o2) {
         int result = -1;
         if (o1 == null && o2 == null) {
            result = 0;
         } else if (o1 != null && o2 != null) {
            result = o1.getName().compareTo(o2.getName());
         } else if (o2 == null) {
            result = 1;
         }
         return result;
      }
   }
}