1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
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 |
|
|
|
|
| 86.8% |
Uncovered Elements: 5 (38) |
Complexity: 9 |
Complexity Density: 0.29 |
|
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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
42 |
2 |
@BeforeClass(alwaysRun = true)... |
43 |
|
private void setUpCache() |
44 |
|
{ |
45 |
2 |
appCache = AppCache.getInstance(); |
46 |
|
} |
47 |
|
|
|
|
| 84.6% |
Uncovered Elements: 2 (13) |
Complexity: 3 |
Complexity Density: 0.23 |
1PASS
|
|
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 |
|
|
61 |
|
|
62 |
|
|
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 |
|
|
|
|
| 85.7% |
Uncovered Elements: 3 (21) |
Complexity: 5 |
Complexity Density: 0.29 |
1PASS
|
|
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 |
|
|
89 |
|
|
90 |
1 |
SwingUtilities.invokeLater(() -> { |
91 |
1 |
done[0] = true; |
92 |
|
}); |
93 |
1 |
long t = 0; |
94 |
24 |
while (!done[0] && t < 200) |
95 |
|
{ |
96 |
23 |
try |
97 |
|
{ |
98 |
23 |
Thread.sleep(7); |
99 |
23 |
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 |
|
} |