a4ade0aa2913834c383268a7cfce0e14d4c66f3b
[jalview.git] / test / jalview / io / cache / JvCacheableInputBoxTest.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
7  * Jalview is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License 
9  * as published by the Free Software Foundation, either version 3
10  * of the License, or (at your option) any later version.
11  *  
12  * Jalview is distributed in the hope that it will be useful, but 
13  * WITHOUT ANY WARRANTY; without even the implied warranty 
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
15  * PURPOSE.  See the GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
19  * The Jalview Authors are detailed in the 'AUTHORS' file.
20  */
21 package jalview.io.cache;
22
23 import java.lang.reflect.InvocationTargetException;
24 import java.util.LinkedHashSet;
25
26 import javax.swing.SwingUtilities;
27
28 import org.junit.Assert;
29 import org.testng.annotations.BeforeClass;
30 import org.testng.annotations.Test;
31
32 public class JvCacheableInputBoxTest
33 {
34
35   private AppCache appCache;
36
37   private static final String TEST_CACHE_KEY = "CACHE.UNIT_TEST";
38
39   private JvCacheableInputBox<String> cacheBox = new JvCacheableInputBox<>(
40           TEST_CACHE_KEY, 20);
41
42   @BeforeClass(alwaysRun = true)
43   private void setUpCache()
44   {
45     appCache = AppCache.getInstance();
46   }
47
48   @Test(groups = { "Functional", "testTask2" })
49   public void getUserInputTest()
50   {
51     String userInput = cacheBox.getUserInput();
52     Assert.assertEquals("", userInput);
53
54     String testInput = "TestInput";
55     cacheBox.addItem(testInput);
56     cacheBox.setSelectedItem(testInput);
57
58     try
59     {
60       // fix for JAL-4153
61       // This delay is essential to prevent the assertion below from executing
62       // before swing thread finishes updating the combo-box
63       SwingUtilities.invokeAndWait(() -> {
64         try
65         {
66           Thread.sleep(1);
67         } catch (InterruptedException e)
68         {
69           e.printStackTrace();
70         }
71       });
72     } catch (InvocationTargetException | InterruptedException e)
73     {
74       e.printStackTrace();
75     }
76     userInput = cacheBox.getUserInput();
77     Assert.assertEquals(testInput, userInput);
78   }
79
80   @Test(groups = { "Functional" })
81   public void updateCacheTest()
82   {
83     String testInput = "TestInput";
84     cacheBox.addItem(testInput);
85     cacheBox.setSelectedItem(testInput);
86     cacheBox.updateCache();
87     boolean done[] = new boolean[] { false };
88     // this event gets processed after updateCache's update event on the swing
89     // thread
90     SwingUtilities.invokeLater(() -> {
91       done[0] = true;
92     });
93     long t = 0;
94     while (!done[0] && t < 200)
95     {
96       try
97       {
98         Thread.sleep(7);
99         t++;
100       } catch (InterruptedException e)
101       {
102         e.printStackTrace();
103       }
104     }
105     if (!done[0])
106     {
107       Assert.fail("Giving up after 1.4s waiting for cache to be updated.");
108     }
109
110     LinkedHashSet<String> foundCache = appCache
111             .getAllCachedItemsFor(TEST_CACHE_KEY);
112     Assert.assertTrue(foundCache.contains(testInput));
113   }
114 }