From 66ae8046375bbef690753727308a48f6bc71b162 Mon Sep 17 00:00:00 2001 From: hansonr Date: Wed, 28 Aug 2019 11:44:19 -0500 Subject: [PATCH] JAL-3253-applet JAL-3423 CacheableInputBox direct update mode for testing removes need for wait. JalviewJS complete PASS on Windows (other than the usual three Xref fails) --- src/jalview/io/cache/JvCacheableInputBox.java | 115 +++++++++++--------- test/jalview/io/cache/JvCacheableInputBoxTest.java | 49 +++++---- 2 files changed, 88 insertions(+), 76 deletions(-) diff --git a/src/jalview/io/cache/JvCacheableInputBox.java b/src/jalview/io/cache/JvCacheableInputBox.java index 9ec3a9f..e02a396 100644 --- a/src/jalview/io/cache/JvCacheableInputBox.java +++ b/src/jalview/io/cache/JvCacheableInputBox.java @@ -237,65 +237,76 @@ public class JvCacheableInputBox @Override public void run() { - int cacheLimit = Integer.parseInt(appCache.getCacheLimit(cacheKey)); - String userInput = getUserInput(); - if (userInput != null && !userInput.isEmpty()) - { - LinkedHashSet 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 cacheItems = appCache.getAllCachedItemsFor(cacheKey); - List 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 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 cacheItems = appCache.getAllCachedItemsFor(cacheKey); + List reversedCacheItems = new ArrayList<>(); + reversedCacheItems.addAll(cacheItems); + cacheItems = null; + Collections.reverse(reversedCacheItems); + if (lastSearch.isEmpty()) + { + comboBox.addItem(""); + } + + if (reversedCacheItems != null && !reversedCacheItems.isEmpty()) + { + LinkedHashSet 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 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); + } /** diff --git a/test/jalview/io/cache/JvCacheableInputBoxTest.java b/test/jalview/io/cache/JvCacheableInputBoxTest.java index fb0cf07..73aae61 100644 --- a/test/jalview/io/cache/JvCacheableInputBoxTest.java +++ b/test/jalview/io/cache/JvCacheableInputBoxTest.java @@ -31,17 +31,18 @@ public class JvCacheableInputBoxTest 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); } @@ -52,20 +53,20 @@ public class JvCacheableInputBoxTest 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 foundCache = appCache .getAllCachedItemsFor(TEST_CACHE_KEY); Assert.assertTrue(foundCache.contains(testInput)); -- 1.7.10.2