From: tcofoegbu Date: Thu, 4 May 2017 17:12:23 +0000 (+0100) Subject: JAL-1648 resolved CR-JAL-41 review issues X-Git-Tag: Release_2_10_2~3^2~45^2~6 X-Git-Url: http://source.jalview.org/gitweb/?a=commitdiff_plain;h=a3092a461be828656c8d3665a6fdb1234012afa2;p=jalview.git JAL-1648 resolved CR-JAL-41 review issues --- diff --git a/src/jalview/io/cache/AppCache.java b/src/jalview/io/cache/AppCache.java index 9663c98..091d30e 100644 --- a/src/jalview/io/cache/AppCache.java +++ b/src/jalview/io/cache/AppCache.java @@ -3,10 +3,8 @@ package jalview.io.cache; import jalview.bin.Cache; -import java.util.Arrays; import java.util.Hashtable; import java.util.LinkedHashSet; -import java.util.List; /** * A singleton class used for querying and persisting cache items. @@ -16,15 +14,17 @@ import java.util.List; */ public class AppCache { - private static AppCache instance = null; + public static final String DEFAULT_LIMIT = "99"; - private Hashtable> cacheItems; + public static final String CACHE_DELIMITER = ";"; + + private static AppCache instance = null; private static final String DEFAULT_LIMIT_KEY = ".DEFAULT_LIMIT"; - private static final String DEFAULT_LIMIT = "99"; - private static final String CACHE_DELIMITER = ";"; + + private Hashtable> cacheItems; private AppCache() { @@ -50,7 +50,7 @@ public class AppCache /** - * Returns an singleton instance of AppCache + * Returns a singleton instance of AppCache * * @return */ @@ -63,33 +63,7 @@ public class AppCache return instance; } - /** - * Method for initialising cache items for a given cache key - * - * @param cacheKey - */ - public void initCache(String cacheKey) - { - String separatedStr = Cache.getProperty(cacheKey); - if (separatedStr == null || separatedStr.isEmpty()) - { - return; - } - List persistedCacheItems = Arrays.asList(separatedStr.split(CACHE_DELIMITER)); - - LinkedHashSet foundCacheItems = cacheItems.get(cacheKey); - if (foundCacheItems == null) - { - foundCacheItems = new LinkedHashSet(); - } - - for (String cacheItem : persistedCacheItems) - { - foundCacheItems.add(cacheItem); - } - cacheItems.put(cacheKey, foundCacheItems); - } /** * Method for persisting cache items for a given cache key @@ -114,9 +88,10 @@ public class AppCache } /** - * Method for deleted cached items for a given cache key + * Method for deleting cached items for a given cache key * * @param cacheKey + * the cache key */ public void deleteCacheItems(String cacheKey) { @@ -128,9 +103,10 @@ public class AppCache * Method for obtaining the preset maximum cache limit for a given cache key * * @param cacheKey - * @return + * the cache key + * @return the max number of items that could be cached */ - public String getCacheLmit(String cacheKey) + public String getCacheLimit(String cacheKey) { String uniqueKey = cacheKey + DEFAULT_LIMIT_KEY; return Cache.getDefault(uniqueKey, DEFAULT_LIMIT); @@ -140,13 +116,17 @@ public class AppCache * Method for updating the preset maximum cache limit for a given cache key * * @param cacheKey + * the cache key * @param newLimit + * the max number of items that could be cached for the given cache + * key * @return */ - public int updateCacheLimit(String cacheKey, String newLimit) + public int updateCacheLimit(String cacheKey, int newUserLimit) { + String newLimit = String.valueOf(newUserLimit); String uniqueKey = cacheKey + DEFAULT_LIMIT_KEY; - String formerLimit = getCacheLmit(cacheKey); + String formerLimit = getCacheLimit(cacheKey); if (newLimit != null && !newLimit.isEmpty() && !formerLimit.equals(newLimit)) { @@ -161,20 +141,13 @@ public class AppCache * structure * * @param cacheKey + * the cache key * @param cacheItems + * the items to add to the cache */ - public void putCache(String cacheKey, LinkedHashSet cacheItems) + public void putCache(String cacheKey, LinkedHashSet newCacheItems) { - getCacheItems().put(cacheKey, cacheItems); + cacheItems.put(cacheKey, newCacheItems); } - /** - * Getter method for obtaining cache data structure - * - * @return - */ - Hashtable> getCacheItems() - { - return cacheItems; - } } diff --git a/src/jalview/io/cache/JvCacheableInputBox.java b/src/jalview/io/cache/JvCacheableInputBox.java index 2cf1678..06302c3 100644 --- a/src/jalview/io/cache/JvCacheableInputBox.java +++ b/src/jalview/io/cache/JvCacheableInputBox.java @@ -1,5 +1,6 @@ package jalview.io.cache; +import jalview.bin.Cache; import jalview.util.MessageManager; import java.awt.Color; @@ -7,6 +8,7 @@ import java.awt.Dimension; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collections; import java.util.LinkedHashSet; import java.util.List; @@ -29,35 +31,67 @@ public class JvCacheableInputBox extends JComboBox private static final long serialVersionUID = 5774610435079326695L; - private String cacheKey; + private static final int INPUT_LIMIT = 2; - private AppCache appCache; + private static final int LEFT_BOARDER_WIDTH = 2; - JPanel pnlDefaultCache = new JPanel(); + private String cacheKey; - JLabel lblDefaultCacheSize = new JLabel(); + private AppCache appCache; - JTextField txtDefaultCacheSize = new JTextField(); + private JPanel pnlDefaultCache = new JPanel(); - JPopupMenu popup = new JPopupMenu(); + private JLabel lblDefaultCacheSize = new JLabel(); - JMenuItem menuItemClearCache = new JMenuItem(); + private JTextField txtDefaultCacheSize = new JTextField(); - final static int INPUT_LIMIT = 2; + private JPopupMenu popup = new JPopupMenu(); + private JMenuItem menuItemClearCache = new JMenuItem(); - public JvCacheableInputBox(String cacheKey) + public JvCacheableInputBox(String newCacheKey) { super(); - this.cacheKey = cacheKey; + this.cacheKey = newCacheKey; setEditable(true); setPrototypeDisplayValue("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"); appCache = AppCache.getInstance(); initCachePopupMenu(); - appCache.initCache(cacheKey); + initCache(newCacheKey); updateCache(); } + /** + * Method for initialising cache items for a given cache key and populating + * the in-memory cache with persisted cache items + * + * @param cacheKey + */ + private void initCache(String cacheKey) + { + // obtain persisted cache items from properties file as a delimited string + String delimitedCacheStr = Cache.getProperty(cacheKey); + if (delimitedCacheStr == null || delimitedCacheStr.isEmpty()) + { + return; + } + // convert delimited cache items to a list of strings + List persistedCacheItems = Arrays.asList(delimitedCacheStr + .split(AppCache.CACHE_DELIMITER)); + + LinkedHashSet foundCacheItems = appCache + .getAllCachedItemsFor(cacheKey);// cacheItems.get(cacheKey); + if (foundCacheItems == null) + { + foundCacheItems = new LinkedHashSet(); + } + // populate memory cache + for (String cacheItem : persistedCacheItems) + { + foundCacheItems.add(cacheItem); + } + appCache.putCache(cacheKey, foundCacheItems); + } /** * Initialise this cache's pop-up menu @@ -66,7 +100,8 @@ public class JvCacheableInputBox extends JComboBox { pnlDefaultCache.setBackground(Color.WHITE); // pad panel so as to align with other menu items - pnlDefaultCache.setBorder(BorderFactory.createEmptyBorder(0, 19, 0, 0)); + pnlDefaultCache.setBorder(BorderFactory.createEmptyBorder(0, + LEFT_BOARDER_WIDTH, 0, 0)); txtDefaultCacheSize.setPreferredSize(new Dimension(45, 20)); lblDefaultCacheSize.setText(MessageManager .getString("label.default_cache_size")); @@ -85,7 +120,7 @@ public class JvCacheableInputBox extends JComboBox } } }); - txtDefaultCacheSize.setText(appCache.getCacheLmit(cacheKey)); + txtDefaultCacheSize.setText(appCache.getCacheLimit(cacheKey)); pnlDefaultCache.add(lblDefaultCacheSize); pnlDefaultCache.add(txtDefaultCacheSize); menuItemClearCache.setText(MessageManager @@ -136,8 +171,10 @@ public class JvCacheableInputBox extends JComboBox @Override public void run() { - int cacheLimit = appCache.updateCacheLimit(cacheKey, - txtDefaultCacheSize.getText()); + int userLimit = txtDefaultCacheSize.getText().trim().isEmpty() ? Integer + .valueOf(AppCache.DEFAULT_LIMIT) : Integer + .valueOf(txtDefaultCacheSize.getText()); + int cacheLimit = appCache.updateCacheLimit(cacheKey, userLimit); String userInput = getUserInput(); if (userInput != null && !userInput.isEmpty()) { @@ -206,7 +243,10 @@ public class JvCacheableInputBox extends JComboBox public void persistCache() { appCache.persistCache(cacheKey); - appCache.updateCacheLimit(cacheKey, txtDefaultCacheSize.getText()); + int userLimit = txtDefaultCacheSize.getText().trim().isEmpty() ? Integer + .valueOf(AppCache.DEFAULT_LIMIT) : Integer + .valueOf(txtDefaultCacheSize.getText()); + appCache.updateCacheLimit(cacheKey, userLimit); } /** diff --git a/test/jalview/io/cache/AppCacheTest.java b/test/jalview/io/cache/AppCacheTest.java index ef75869..5638028 100644 --- a/test/jalview/io/cache/AppCacheTest.java +++ b/test/jalview/io/cache/AppCacheTest.java @@ -48,26 +48,14 @@ public class AppCacheTest @Test(groups = { "Functional" }) public void appCacheLimitTest() { - String limit = appCache.getCacheLmit(TEST_CACHE_KEY); + String limit = appCache.getCacheLimit(TEST_CACHE_KEY); Assert.assertEquals(limit, "99"); - limit = String.valueOf(appCache.updateCacheLimit(TEST_CACHE_KEY, "20")); + limit = String.valueOf(appCache.updateCacheLimit(TEST_CACHE_KEY, 20)); Assert.assertEquals(limit, "20"); - limit = appCache.getCacheLmit(TEST_CACHE_KEY); + limit = appCache.getCacheLimit(TEST_CACHE_KEY); Assert.assertEquals(limit, "20"); - appCache.updateCacheLimit(TEST_CACHE_KEY, "99"); + appCache.updateCacheLimit(TEST_CACHE_KEY, 99); } - @Test(groups = { "Functional" }) - public void initCacheTest() - { - appCache.initCache(TEST_CACHE_KEY); - Assert.assertTrue(appCache.getCacheItems().containsKey(TEST_CACHE_KEY)); - generateTestCacheItems(); - // appCache. - appCache.initCache(TEST_CACHE_KEY); - // - Assert.assertTrue(appCache.getCacheItems().containsKey(TEST_CACHE_KEY)); - appCache.deleteCacheItems(TEST_CACHE_KEY); - } } diff --git a/test/jalview/io/cache/JvCacheableInputBoxTest.java b/test/jalview/io/cache/JvCacheableInputBoxTest.java new file mode 100644 index 0000000..dfd7973 --- /dev/null +++ b/test/jalview/io/cache/JvCacheableInputBoxTest.java @@ -0,0 +1,70 @@ +package jalview.io.cache; + +import java.util.LinkedHashSet; + +import org.junit.Assert; +import org.testng.annotations.BeforeClass; +import org.testng.annotations.Test; + +public class JvCacheableInputBoxTest +{ + + private AppCache appCache; + + private static final String TEST_CACHE_KEY = "CACHE.UNIT_TEST"; + + private JvCacheableInputBox cacheBox = new JvCacheableInputBox( + TEST_CACHE_KEY); + + @BeforeClass(alwaysRun = true) + private void setUpCache() + { + appCache = AppCache.getInstance(); + } + + @Test(groups = { "Functional" }) + public void getUserInputTest() + { + String userInput = cacheBox.getUserInput(); + Assert.assertEquals("", userInput); + + String testInput = "TestInput"; + cacheBox.addItem(testInput); + cacheBox.setSelectedItem(testInput); + + try + { + // This 1ms 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); + } + + @Test(groups = { "Functional" }) + public void updateCacheTest() + { + String testInput = "TestInput"; + cacheBox.addItem(testInput); + cacheBox.setSelectedItem(testInput); + cacheBox.updateCache(); + try + { + // This 1ms delay is essential to prevent the + // assertion below from executing before + // cacheBox.updateCache() finishes updating the cache + Thread.sleep(100); + } catch (InterruptedException e) + { + e.printStackTrace(); + } + LinkedHashSet foundCache = appCache + .getAllCachedItemsFor(TEST_CACHE_KEY); + Assert.assertTrue(foundCache.contains(testInput)); + } +}