2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
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.
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.
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.
21 package jalview.io.cache;
23 import java.lang.reflect.InvocationTargetException;
24 import java.util.LinkedHashSet;
26 import javax.swing.SwingUtilities;
28 import org.junit.Assert;
29 import org.testng.annotations.BeforeClass;
30 import org.testng.annotations.Test;
32 public class JvCacheableInputBoxTest
35 private AppCache appCache;
37 private static final String TEST_CACHE_KEY = "CACHE.UNIT_TEST";
39 private JvCacheableInputBox<String> cacheBox = new JvCacheableInputBox<>(
42 @BeforeClass(alwaysRun = true)
43 private void setUpCache()
45 appCache = AppCache.getInstance();
48 @Test(groups = { "Functional", "testTask2" })
49 public void getUserInputTest()
51 String userInput = cacheBox.getUserInput();
52 Assert.assertEquals("", userInput);
54 String testInput = "TestInput";
55 cacheBox.addItem(testInput);
56 cacheBox.setSelectedItem(testInput);
61 // This delay is essential to prevent the assertion below from executing
62 // before swing thread finishes updating the combo-box
63 SwingUtilities.invokeAndWait(() -> {
67 } catch (InterruptedException e)
72 } catch (InvocationTargetException | InterruptedException e)
76 userInput = cacheBox.getUserInput();
77 Assert.assertEquals(testInput, userInput);
80 @Test(groups = { "Functional" })
81 public void updateCacheTest()
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
90 SwingUtilities.invokeLater(() -> {
94 while (!done[0] && t < 200)
100 } catch (InterruptedException e)
107 Assert.fail("Giving up after 1.4s waiting for cache to be updated.");
110 LinkedHashSet<String> foundCache = appCache
111 .getAllCachedItemsFor(TEST_CACHE_KEY);
112 Assert.assertTrue(foundCache.contains(testInput));