You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

UpdateManagerImplTest.java 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. * Copyright (c) 2006-2015 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. package com.dmdirc.updater.manager;
  23. import com.dmdirc.updater.UpdateComponent;
  24. import com.dmdirc.updater.checking.CheckResultConsolidator;
  25. import com.dmdirc.updater.checking.UpdateCheckResult;
  26. import com.dmdirc.updater.retrieving.UpdateRetrievalStrategy;
  27. import java.util.concurrent.Executor;
  28. import org.junit.Before;
  29. import org.junit.Test;
  30. import org.junit.runner.RunWith;
  31. import org.mockito.Mock;
  32. import org.mockito.runners.MockitoJUnitRunner;
  33. import static org.junit.Assert.assertNull;
  34. import static org.junit.Assert.assertSame;
  35. import static org.mockito.Mockito.any;
  36. import static org.mockito.Mockito.doAnswer;
  37. import static org.mockito.Mockito.when;
  38. @RunWith(MockitoJUnitRunner.class)
  39. public class UpdateManagerImplTest {
  40. @Mock private Executor executor;
  41. @Mock private CheckResultConsolidator checkResultConsolidator;
  42. @Mock private UpdateComponentPolicy updateComponentPolicy;
  43. @Mock private UpdateComponent component1;
  44. @Mock private UpdateComponent component2;
  45. @Mock private UpdateComponent component3;
  46. @Mock private UpdateStatusListener statusListener;
  47. @Mock private UpdateCheckResult checkResult1;
  48. @Mock private UpdateCheckResult checkResult2;
  49. @Mock private UpdateRetrievalStrategy strategy1;
  50. @Mock private UpdateRetrievalStrategy strategy2;
  51. private UpdateManagerImpl manager;
  52. @Before
  53. public void setup() {
  54. manager = new UpdateManagerImpl(executor, checkResultConsolidator, updateComponentPolicy);
  55. when(component1.getName()).thenReturn("test1");
  56. when(component2.getName()).thenReturn("test2");
  57. when(component3.getName()).thenReturn("test3");
  58. }
  59. @Test
  60. public void testRemovingComponentInStatusChangeListener() {
  61. manager.addComponent(component1);
  62. manager.addComponent(component2);
  63. manager.addUpdateStatusListener(statusListener);
  64. doAnswer(invocation -> {
  65. // Add a new component from the manager while it's checking for updates.
  66. // This potentially causes a CME, see CLIENT-404.
  67. manager.addComponent(component3);
  68. return null;
  69. }).when(statusListener).updateStatusChanged(component1, UpdateStatus.CHECKING, 0);
  70. when(updateComponentPolicy.canCheck(any(UpdateComponent.class))).thenReturn(true);
  71. manager.checkForUpdates();
  72. }
  73. @Test
  74. public void testGetSingleRetrievalStrategy() {
  75. // Given a single retrieval strategy that can handle anything
  76. when(strategy1.canHandle(any())).thenReturn(true);
  77. manager.addRetrievalStrategy(strategy1);
  78. // Then it is returned for any result.
  79. assertSame(strategy1, manager.getStrategy(checkResult1));
  80. assertSame(strategy1, manager.getStrategy(checkResult2));
  81. }
  82. @Test
  83. public void testGetMultipleRetrievalStrategy() {
  84. // Given two retrieval strategies that can handle different check results
  85. when(strategy1.canHandle(checkResult1)).thenReturn(true);
  86. when(strategy2.canHandle(checkResult2)).thenReturn(true);
  87. manager.addRetrievalStrategy(strategy1);
  88. manager.addRetrievalStrategy(strategy2);
  89. // Then the corresponding strategy is returned for each result
  90. assertSame(strategy1, manager.getStrategy(checkResult1));
  91. assertSame(strategy2, manager.getStrategy(checkResult2));
  92. }
  93. @Test
  94. public void testGetNoRetrievalStrategy() {
  95. // Given a retrieval strategies that can handle a specific check result
  96. manager.addRetrievalStrategy(strategy1);
  97. // Then null is returned for other check results
  98. assertNull(manager.getStrategy(checkResult2));
  99. }
  100. }