Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

NightlyCheckerTest.java 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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.checking;
  23. import com.dmdirc.config.binding.ConfigBinder;
  24. import com.dmdirc.config.provider.AggregateConfigProvider;
  25. import com.dmdirc.updater.UpdateComponent;
  26. import com.dmdirc.updater.Version;
  27. import com.dmdirc.util.io.Downloader;
  28. import com.dmdirc.util.io.FileUtils;
  29. import com.google.common.collect.Lists;
  30. import java.io.IOException;
  31. import java.nio.file.Files;
  32. import java.util.List;
  33. import java.util.Map;
  34. import java.util.stream.Collectors;
  35. import org.junit.Before;
  36. import org.junit.Test;
  37. import org.junit.runner.RunWith;
  38. import org.mockito.Mock;
  39. import org.mockito.runners.MockitoJUnitRunner;
  40. import static org.junit.Assert.assertEquals;
  41. import static org.junit.Assert.assertTrue;
  42. import static org.mockito.ArgumentMatchers.anyString;
  43. import static org.mockito.Mockito.when;
  44. @RunWith(MockitoJUnitRunner.class)
  45. public class NightlyCheckerTest {
  46. private NightlyChecker instance;
  47. @Mock private AggregateConfigProvider config;
  48. @Mock private Downloader downloader;
  49. @Mock private ConfigBinder configBinder;
  50. @Mock private UpdateComponent uiswingUpdateComponent;
  51. @Mock private UpdateComponent clientUpdateComponent;
  52. @Mock private UpdateComponent timeUpdateComponent;
  53. @Mock private UpdateComponent audioUpdateComponent;
  54. @Mock private UpdateComponent uiweb2UpdateComponent;
  55. @Mock private UpdateComponent randomtestUpdateComponent;
  56. @Before
  57. public void setUp() throws Exception {
  58. when(config.getBinder()).thenReturn(configBinder);
  59. when(downloader.getPage(anyString())).thenReturn(
  60. Files.readAllLines(
  61. FileUtils.getPathForResource(getClass().getResource("nightlies.json"))));
  62. when(uiswingUpdateComponent.getName()).thenReturn("ui_swing");
  63. when(clientUpdateComponent.getName()).thenReturn("client");
  64. when(timeUpdateComponent.getName()).thenReturn("time");
  65. when(audioUpdateComponent.getName()).thenReturn("audio");
  66. when(uiweb2UpdateComponent.getName()).thenReturn("ui_web2");
  67. when(randomtestUpdateComponent.getName()).thenReturn("random-test");
  68. when(uiswingUpdateComponent.getVersion()).thenReturn(new Version("1.2"));
  69. when(clientUpdateComponent.getVersion()).thenReturn(new Version("4.5"));
  70. when(timeUpdateComponent.getVersion()).thenReturn(new Version("0.1"));
  71. when(audioUpdateComponent.getVersion()).thenReturn(new Version("0.1"));
  72. when(uiweb2UpdateComponent.getVersion()).thenReturn(new Version("10.0"));
  73. when(randomtestUpdateComponent.getVersion()).thenReturn(new Version("10.0"));
  74. instance = new NightlyChecker(config, downloader);
  75. instance.setChannel("NIGHTLY");
  76. }
  77. @Test
  78. public void testUnknownChannel() throws Exception {
  79. instance.setChannel("RANDOM");
  80. final Map<UpdateComponent, UpdateCheckResult> updates =
  81. instance.checkForUpdates(Lists.newArrayList(timeUpdateComponent));
  82. assertEquals(0, updates.size());
  83. }
  84. @Test
  85. public void testNotNightly() throws Exception {
  86. instance.setChannel("STABLE");
  87. final Map<UpdateComponent, UpdateCheckResult> updates =
  88. instance.checkForUpdates(Lists.newArrayList(timeUpdateComponent));
  89. assertEquals(0, updates.size());
  90. }
  91. @Test
  92. public void testNightly() throws Exception {
  93. final Map<UpdateComponent, UpdateCheckResult> updates =
  94. instance.checkForUpdates(Lists.newArrayList(timeUpdateComponent));
  95. assertEquals(1, updates.size());
  96. }
  97. @Test
  98. public void testNoUpdates() throws Exception {
  99. final Map<UpdateComponent, UpdateCheckResult> updates = instance.checkForUpdates(
  100. Lists.newArrayList(uiswingUpdateComponent, clientUpdateComponent));
  101. assertEquals(0, updates.size());
  102. }
  103. @Test
  104. public void testOneUpdate() throws Exception {
  105. final Map<UpdateComponent, UpdateCheckResult> updates = instance.checkForUpdates(
  106. Lists.newArrayList(uiswingUpdateComponent, timeUpdateComponent));
  107. assertEquals(1, updates.size());
  108. }
  109. @Test
  110. public void testMultipleUpdate() throws Exception {
  111. final Map<UpdateComponent, UpdateCheckResult> updates = instance.checkForUpdates(
  112. Lists.newArrayList(uiswingUpdateComponent, clientUpdateComponent,
  113. timeUpdateComponent, audioUpdateComponent));
  114. assertEquals(2, updates.size());
  115. }
  116. @Test
  117. public void testMalformedPage() throws Exception {
  118. when(downloader.getPage(anyString())).thenThrow(new IOException("Failure."));
  119. final Map<UpdateComponent, UpdateCheckResult> updates =
  120. instance.checkForUpdates(Lists.newArrayList(timeUpdateComponent));
  121. assertEquals(0, updates.size());
  122. }
  123. @Test
  124. public void testNames() throws Exception {
  125. when(downloader.getPage(anyString())).thenReturn(
  126. Files.readAllLines(
  127. FileUtils.getPathForResource(getClass().getResource("nightlies2.json"))));
  128. final Map<UpdateComponent, UpdateCheckResult> updates =
  129. instance.checkForUpdates(Lists.newArrayList(uiswingUpdateComponent,
  130. clientUpdateComponent, timeUpdateComponent, audioUpdateComponent,
  131. randomtestUpdateComponent, uiweb2UpdateComponent));
  132. final List<String> updateNames = updates.keySet().stream()
  133. .map(UpdateComponent::getName)
  134. .collect(Collectors.toList());
  135. assertTrue(updateNames.contains("client"));
  136. assertTrue(updateNames.contains("time"));
  137. assertTrue(updateNames.contains("ui_swing"));
  138. assertTrue(updateNames.contains("ui_web2"));
  139. assertTrue(updateNames.contains("random-test"));
  140. assertTrue(updateNames.contains("audio"));
  141. }
  142. }