Skip to main content
summaryrefslogtreecommitdiffstats
blob: 9a557abe09b50f11b7172ac12997f5b0a04724d6 (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
/*******************************************************************************
 * 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.Comparator;
import java.util.List;
import org.eclipse.osee.framework.jdk.core.type.NamedId;
import org.eclipse.osee.framework.jdk.core.type.OseeCoreException;
import org.junit.Assert;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runners.MethodSorters;

/**
 * Test Case for {@link AbstractOseeCache}
 *
 * @author Roberto E. Escobar
 */
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public abstract class AbstractOseeCacheTest<T extends NamedId> {
   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<>(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.getId()));
      }
      Assert.assertFalse(cache.existsByGuid(createKey()));
   }

   @org.junit.Test
   public void testCacheByGuid() throws OseeCoreException {
      for (T expected : data) {
         T actual = cache.getByGuid(expected.getId());
         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.getId()));
         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.getId()));
         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();
      cache.decache(item1);

      item1.setName(item2.getName());

      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());
      item1.setName(originalName);
   }

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

         Assert.assertEquals(0, cache.size());
         if (cache instanceof IOseeLoadingCache) {
            Assert.assertTrue(((IOseeLoadingCache<?>) cache).reloadCache());
         }
         Assert.assertEquals(fullCacheSize, cache.size());
      }
   }

   protected abstract Long createKey();

   //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;
      }
   }
}

Back to the top