3e0aa367a8b64029697d21f617e5f475f05a3b1e
[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" })
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       // This delay is essential to prevent the assertion below from executing
61       // before swing thread finishes updating the combo-box
62       SwingUtilities.invokeAndWait(() -> {
63         try
64         {
65           Thread.sleep(1);
66         } catch (InterruptedException e)
67         {
68           e.printStackTrace();
69         }
70       });
71     } catch (InvocationTargetException | InterruptedException e)
72     {
73       e.printStackTrace();
74     }
75     userInput = cacheBox.getUserInput();
76     Assert.assertEquals(testInput, userInput);
77   }
78
79   @Test(groups = { "Functional" })
80   public void updateCacheTest()
81   {
82     String testInput = "TestInput";
83     cacheBox.addItem(testInput);
84     cacheBox.setSelectedItem(testInput);
85     cacheBox.updateCache();
86
87     try
88     {
89       // This delay is to let cacheBox.updateCache() finish updating the cache
90       SwingUtilities.invokeAndWait(() -> {
91         try
92         {
93           Thread.sleep(1);
94         } catch (InterruptedException e)
95         {
96           e.printStackTrace();
97         }
98       });
99     } catch (InvocationTargetException | InterruptedException e)
100     {
101       e.printStackTrace();
102     }
103     LinkedHashSet<String> foundCache = appCache
104             .getAllCachedItemsFor(TEST_CACHE_KEY);
105     Assert.assertTrue(foundCache.contains(testInput));
106   }
107 }