JAL-1889 unit test tweaks for running on server
[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 delay is to let
59       // cacheBox.updateCache() finish updating the cache
60       Thread.sleep(200);
61     } catch (InterruptedException e)
62     {
63       e.printStackTrace();
64     }
65     LinkedHashSet<String> foundCache = appCache
66             .getAllCachedItemsFor(TEST_CACHE_KEY);
67     Assert.assertTrue(foundCache.contains(testInput));
68   }
69 }