Class |
Line # |
Actions |
|||
---|---|---|---|---|---|
JvCacheableInputBoxTest | 32 | 31 | 9 |
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 | 2 | @BeforeClass(alwaysRun = true) |
43 | private void setUpCache() | |
44 | { | |
45 | 2 | appCache = AppCache.getInstance(); |
46 | } | |
47 | ||
48 | 1 | @Test(groups = { "Functional", "testTask2" }) |
49 | public void getUserInputTest() | |
50 | { | |
51 | 1 | String userInput = cacheBox.getUserInput(); |
52 | 1 | Assert.assertEquals("", userInput); |
53 | ||
54 | 1 | String testInput = "TestInput"; |
55 | 1 | cacheBox.addItem(testInput); |
56 | 1 | cacheBox.setSelectedItem(testInput); |
57 | ||
58 | 1 | 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 | 1 | SwingUtilities.invokeAndWait(() -> { |
64 | 1 | try |
65 | { | |
66 | 1 | Thread.sleep(1); |
67 | } catch (InterruptedException e) | |
68 | { | |
69 | 0 | e.printStackTrace(); |
70 | } | |
71 | }); | |
72 | } catch (InvocationTargetException | InterruptedException e) | |
73 | { | |
74 | 0 | e.printStackTrace(); |
75 | } | |
76 | 1 | userInput = cacheBox.getUserInput(); |
77 | 1 | Assert.assertEquals(testInput, userInput); |
78 | } | |
79 | ||
80 | 1 | @Test(groups = { "Functional" }) |
81 | public void updateCacheTest() | |
82 | { | |
83 | 1 | String testInput = "TestInput"; |
84 | 1 | cacheBox.addItem(testInput); |
85 | 1 | cacheBox.setSelectedItem(testInput); |
86 | 1 | cacheBox.updateCache(); |
87 | 1 | boolean done[] = new boolean[] { false }; |
88 | // this event gets processed after updateCache's update event on the swing | |
89 | // thread | |
90 | 1 | SwingUtilities.invokeLater(() -> { |
91 | 1 | done[0] = true; |
92 | }); | |
93 | 1 | long t = 0; |
94 | 2 | while (!done[0] && t < 200) |
95 | { | |
96 | 1 | try |
97 | { | |
98 | 1 | Thread.sleep(7); |
99 | 1 | t++; |
100 | } catch (InterruptedException e) | |
101 | { | |
102 | 0 | e.printStackTrace(); |
103 | } | |
104 | } | |
105 | 1 | if (!done[0]) |
106 | { | |
107 | 0 | Assert.fail("Giving up after 1.4s waiting for cache to be updated."); |
108 | } | |
109 | ||
110 | 1 | LinkedHashSet<String> foundCache = appCache |
111 | .getAllCachedItemsFor(TEST_CACHE_KEY); | |
112 | 1 | Assert.assertTrue(foundCache.contains(testInput)); |
113 | } | |
114 | } |