dfd797305286e8c6b687d5671326a1bea82e8752
[jalview.git] / test / jalview / io / cache / JvCacheableInputBoxTest.java
1 package jalview.io.cache;
2
3 import java.util.LinkedHashSet;
4
5 import org.junit.Assert;
6 import org.testng.annotations.BeforeClass;
7 import org.testng.annotations.Test;
8
9 public class JvCacheableInputBoxTest
10 {
11
12   private AppCache appCache;
13
14   private static final String TEST_CACHE_KEY = "CACHE.UNIT_TEST";
15
16   private JvCacheableInputBox<String> cacheBox = new JvCacheableInputBox<String>(
17           TEST_CACHE_KEY);
18
19   @BeforeClass(alwaysRun = true)
20   private void setUpCache()
21   {
22     appCache = AppCache.getInstance();
23   }
24
25   @Test(groups = { "Functional" })
26   public void getUserInputTest()
27   {
28     String userInput = cacheBox.getUserInput();
29     Assert.assertEquals("", userInput);
30
31     String testInput = "TestInput";
32     cacheBox.addItem(testInput);
33     cacheBox.setSelectedItem(testInput);
34
35     try
36     {
37       // This 1ms delay is essential to prevent the
38       // assertion below from executing before
39       // swing thread finishes updating the combo-box
40       Thread.sleep(100);
41     } catch (InterruptedException e)
42     {
43       e.printStackTrace();
44     }
45     userInput = cacheBox.getUserInput();
46     Assert.assertEquals(testInput, userInput);
47   }
48
49   @Test(groups = { "Functional" })
50   public void updateCacheTest()
51   {
52     String testInput = "TestInput";
53     cacheBox.addItem(testInput);
54     cacheBox.setSelectedItem(testInput);
55     cacheBox.updateCache();
56     try
57     {
58       // This 1ms delay is essential to prevent the
59       // assertion below from executing before
60       // cacheBox.updateCache() finishes updating the cache
61       Thread.sleep(100);
62     } catch (InterruptedException e)
63     {
64       e.printStackTrace();
65     }
66     LinkedHashSet<String> foundCache = appCache
67             .getAllCachedItemsFor(TEST_CACHE_KEY);
68     Assert.assertTrue(foundCache.contains(testInput));
69   }
70 }