Clover icon

Coverage Report

  1. Project Clover database Mon Nov 18 2024 09:38:20 GMT
  2. Package jalview.io.cache

File JvCacheableInputBoxTest.java

 
updateCacheTest: Giving up after 1.4s waiting for cache to be updated.
 

Code metrics

4
31
3
1
114
76
9
0.29
10.33
3
3

Classes

Class Line # Actions
JvCacheableInputBoxTest 32 31 9
0.052631585.3%
 

Contributing tests

This file is covered by 1 test. .

Source view

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