@Override
public void run()
{
- int cacheLimit = Integer.parseInt(appCache.getCacheLimit(cacheKey));
- String userInput = getUserInput();
- if (userInput != null && !userInput.isEmpty())
- {
- LinkedHashSet<String> foundCache = appCache
- .getAllCachedItemsFor(cacheKey);
- // remove old cache item so as to place current input at the top of
- // the result
- foundCache.remove(userInput);
- foundCache.add(userInput);
- appCache.putCache(cacheKey, foundCache);
- }
+ updateCacheNow();
+ }
+ });
+ }
- String lastSearch = userInput;
- if (comboBox.getItemCount() > 0)
- {
- comboBox.removeAllItems();
- }
- Set<String> cacheItems = appCache.getAllCachedItemsFor(cacheKey);
- List<String> reversedCacheItems = new ArrayList<>();
- reversedCacheItems.addAll(cacheItems);
- cacheItems = null;
- Collections.reverse(reversedCacheItems);
- if (lastSearch.isEmpty())
- {
- comboBox.addItem("");
- }
+ /**
+ * For TestNG
+ *
+ * @author Bob Hanson 2019.08.28
+ */
+ public void updateCacheNow()
+ {
+ int cacheLimit = Integer.parseInt(appCache.getCacheLimit(cacheKey));
+ String userInput = getUserInput();
+ if (userInput != null && !userInput.isEmpty())
+ {
+ LinkedHashSet<String> foundCache = appCache
+ .getAllCachedItemsFor(cacheKey);
+ // remove old cache item so as to place current input at the top of
+ // the result
+ foundCache.remove(userInput);
+ foundCache.add(userInput);
+ appCache.putCache(cacheKey, foundCache);
+ }
- if (reversedCacheItems != null && !reversedCacheItems.isEmpty())
+ String lastSearch = userInput;
+ if (comboBox.getItemCount() > 0)
+ {
+ comboBox.removeAllItems();
+ }
+ Set<String> cacheItems = appCache.getAllCachedItemsFor(cacheKey);
+ List<String> reversedCacheItems = new ArrayList<>();
+ reversedCacheItems.addAll(cacheItems);
+ cacheItems = null;
+ Collections.reverse(reversedCacheItems);
+ if (lastSearch.isEmpty())
+ {
+ comboBox.addItem("");
+ }
+
+ if (reversedCacheItems != null && !reversedCacheItems.isEmpty())
+ {
+ LinkedHashSet<String> foundCache = appCache
+ .getAllCachedItemsFor(cacheKey);
+ boolean prune = reversedCacheItems.size() > cacheLimit;
+ int count = 1;
+ boolean limitExceeded = false;
+ for (String cacheItem : reversedCacheItems)
+ {
+ limitExceeded = (count++ > cacheLimit);
+ if (prune)
{
- LinkedHashSet<String> foundCache = appCache
- .getAllCachedItemsFor(cacheKey);
- boolean prune = reversedCacheItems.size() > cacheLimit;
- int count = 1;
- boolean limitExceeded = false;
- for (String cacheItem : reversedCacheItems)
+ if (limitExceeded)
{
- limitExceeded = (count++ > cacheLimit);
- if (prune)
- {
- if (limitExceeded)
- {
- foundCache.remove(cacheItem);
- }
- else
- {
- comboBox.addItem(cacheItem);
- }
- }
- else
- {
- comboBox.addItem(cacheItem);
- }
+ foundCache.remove(cacheItem);
}
- appCache.putCache(cacheKey, foundCache);
+ else
+ {
+ comboBox.addItem(cacheItem);
+ }
+ }
+ else
+ {
+ comboBox.addItem(cacheItem);
}
- setSelectedItem(lastSearch.isEmpty() ? "" : lastSearch);
}
- });
+ appCache.putCache(cacheKey, foundCache);
+ }
+ setSelectedItem(lastSearch.isEmpty() ? "" : lastSearch);
+
}
/**
String testInput = "TestInput";
cacheBox.addItem(testInput);
cacheBox.setSelectedItem(testInput);
+ cacheBox.updateCacheNow();
- try
- {
- // This delay is essential to prevent the
- // assertion below from executing before
- // swing thread finishes updating the combo-box
- Thread.sleep(100);
- } catch (InterruptedException e)
- {
- e.printStackTrace();
- }
+ // try
+ // {
+ // // This delay is essential to prevent the
+ // // assertion below from executing before
+ // // swing thread finishes updating the combo-box
+ // Thread.sleep(100);
+ // } catch (InterruptedException e)
+ // {
+ // e.printStackTrace();
+ // }
userInput = cacheBox.getUserInput();
Assert.assertEquals(testInput, userInput);
}
String testInput = "TestInput";
cacheBox.addItem(testInput);
cacheBox.setSelectedItem(testInput);
- cacheBox.updateCache();
- synchronized (this)
- {
- try
- {
- wait(100);
- // This delay is to let
- // cacheBox.updateCache() finish updating the cache
- Thread.sleep(200);
- } catch (InterruptedException e)
- {
- e.printStackTrace();
- }
- }
+ cacheBox.updateCacheNow();
+ // synchronized (this)
+ // {
+ // try
+ // {
+ // wait(100);
+ // // This delay is to let
+ // // cacheBox.updateCache() finish updating the cache
+ // Thread.sleep(200);
+ // } catch (InterruptedException e)
+ // {
+ // e.printStackTrace();
+ // }
+ // }
LinkedHashSet<String> foundCache = appCache
.getAllCachedItemsFor(TEST_CACHE_KEY);
Assert.assertTrue(foundCache.contains(testInput));