您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

FakeUpdates.java 9.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. /*
  2. * Copyright (c) 2006-2017 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
  5. * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
  6. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
  7. * permit persons to whom the Software is furnished to do so, subject to the following conditions:
  8. *
  9. * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
  10. * Software.
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  13. * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
  14. * OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  15. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  16. */
  17. package com.dmdirc.addons.debug.commands;
  18. import com.dmdirc.addons.debug.Debug;
  19. import com.dmdirc.addons.debug.DebugCommand;
  20. import com.dmdirc.commandparser.CommandArguments;
  21. import com.dmdirc.commandparser.commands.context.CommandContext;
  22. import com.dmdirc.interfaces.WindowModel;
  23. import com.dmdirc.updater.UpdateComponent;
  24. import com.dmdirc.updater.Version;
  25. import com.dmdirc.updater.checking.BaseCheckResult;
  26. import com.dmdirc.updater.checking.UpdateCheckResult;
  27. import com.dmdirc.updater.checking.UpdateCheckStrategy;
  28. import com.dmdirc.updater.installing.TypeSensitiveInstallationStrategy;
  29. import com.dmdirc.updater.installing.UpdateInstallationListener;
  30. import com.dmdirc.updater.manager.UpdateManager;
  31. import com.dmdirc.updater.retrieving.BaseRetrievalResult;
  32. import com.dmdirc.updater.retrieving.TypeSensitiveRetrievalStrategy;
  33. import com.dmdirc.updater.retrieving.UpdateRetrievalListener;
  34. import com.dmdirc.updater.retrieving.UpdateRetrievalResult;
  35. import com.dmdirc.util.collections.ListenerList;
  36. import java.util.Collection;
  37. import java.util.HashMap;
  38. import java.util.Map;
  39. import javax.annotation.Nonnull;
  40. import javax.inject.Inject;
  41. import javax.inject.Provider;
  42. /**
  43. * Generates some fake updates.
  44. */
  45. public class FakeUpdates extends DebugCommand {
  46. /** The update manager to add fake updates to. */
  47. private final UpdateManager updateManager;
  48. /**
  49. * Creates a new instance of the command.
  50. *
  51. * @param commandProvider The provider to use to access the main debug command.
  52. * @param updateManager The update manager to add fake updates to.
  53. */
  54. @Inject
  55. public FakeUpdates(
  56. final Provider<Debug> commandProvider,
  57. final UpdateManager updateManager) {
  58. super(commandProvider);
  59. this.updateManager = updateManager;
  60. }
  61. @Override
  62. public String getName() {
  63. return "fakeupdates";
  64. }
  65. @Override
  66. public String getUsage() {
  67. return " - Initialises a fake update handling chain";
  68. }
  69. @Override
  70. public void execute(@Nonnull final WindowModel origin,
  71. final CommandArguments args, final CommandContext context) {
  72. updateManager.addCheckStrategy(new FakeUpdateCheckStrategy());
  73. updateManager.addRetrievalStrategy(new FakeUpdateRetriever());
  74. updateManager.addInstallationStrategy(new FakeUpdateInstaller());
  75. }
  76. /**
  77. * A fake update check strategy which produces fake update check results.
  78. */
  79. private static class FakeUpdateCheckStrategy
  80. implements UpdateCheckStrategy {
  81. @Override
  82. public Map<UpdateComponent, UpdateCheckResult> checkForUpdates(
  83. final Collection<UpdateComponent> components) {
  84. final Map<UpdateComponent, UpdateCheckResult> res = new HashMap<>();
  85. components.stream().filter(component -> Math.random() * components.size() < 10)
  86. .forEach(component -> res.put(component, new FakeUpdateCheckResult(component)));
  87. return res;
  88. }
  89. }
  90. /**
  91. * A fake update check result.
  92. */
  93. private static class FakeUpdateCheckResult extends BaseCheckResult {
  94. /**
  95. * Creates a new fake result for the given component.
  96. *
  97. * @param component The component this result is for
  98. */
  99. public FakeUpdateCheckResult(final UpdateComponent component) {
  100. super(component, true, "shiny newness", new Version("1.337"));
  101. }
  102. }
  103. private static class FakeUpdateRetriever
  104. extends TypeSensitiveRetrievalStrategy<FakeUpdateCheckResult> {
  105. private final ListenerList listeners = new ListenerList();
  106. /**
  107. * Creates a new {@link FakeUpdateRetriever}.
  108. */
  109. public FakeUpdateRetriever() {
  110. super(FakeUpdateCheckResult.class);
  111. }
  112. protected void fireRetrievalProgressChanged(final UpdateComponent component,
  113. final double progress) {
  114. listeners.getCallable(UpdateRetrievalListener.class).retrievalProgressChanged(component,
  115. progress);
  116. }
  117. protected void fireRetrievalFailed(final UpdateComponent component) {
  118. listeners.getCallable(UpdateRetrievalListener.class).retrievalFailed(component);
  119. }
  120. protected void fireRetrievalCompleted(final UpdateComponent component) {
  121. listeners.getCallable(UpdateRetrievalListener.class).retrievalCompleted(component);
  122. }
  123. @Override
  124. public void addUpdateRetrievalListener(final UpdateRetrievalListener listener) {
  125. listeners.add(UpdateRetrievalListener.class, listener);
  126. }
  127. @Override
  128. public void removeUpdateRetrievalListener(final UpdateRetrievalListener listener) {
  129. listeners.remove(UpdateRetrievalListener.class, listener);
  130. }
  131. @Override
  132. protected UpdateRetrievalResult retrieveImpl(
  133. final FakeUpdateCheckResult checkResult) {
  134. try {
  135. fireRetrievalProgressChanged(checkResult.getComponent(), 0);
  136. final int max = (int) (Math.random() * 50);
  137. for (int i = 0; i < max; i++) {
  138. Thread.sleep(100);
  139. fireRetrievalProgressChanged(checkResult.getComponent(),
  140. 100 * i / max);
  141. }
  142. if (Math.random() < 0.75) {
  143. fireRetrievalCompleted(checkResult.getComponent());
  144. return new FakeRetrievalResult(checkResult, true);
  145. } else {
  146. fireRetrievalFailed(checkResult.getComponent());
  147. return new FakeRetrievalResult(checkResult, false);
  148. }
  149. } catch (InterruptedException ex) {
  150. // Don't care
  151. }
  152. return new FakeRetrievalResult(checkResult, false);
  153. }
  154. }
  155. /**
  156. * Fake retrieval result.
  157. */
  158. private static class FakeRetrievalResult extends BaseRetrievalResult {
  159. /**
  160. * Creates a new fake retrieval result.
  161. *
  162. * @param checkResult The check result that was retrieved
  163. * @param success Whether the result is successful or not
  164. */
  165. public FakeRetrievalResult(final UpdateCheckResult checkResult,
  166. final boolean success) {
  167. super(checkResult, success);
  168. }
  169. }
  170. /**
  171. * A fake update installer.
  172. */
  173. private static class FakeUpdateInstaller
  174. extends TypeSensitiveInstallationStrategy<UpdateComponent, FakeRetrievalResult> {
  175. private final ListenerList listeners = new ListenerList();
  176. /**
  177. * Creates a new {@link FakeUpdateInstaller}.
  178. */
  179. public FakeUpdateInstaller() {
  180. super(UpdateComponent.class, FakeRetrievalResult.class);
  181. }
  182. protected void fireInstallCompleted(final UpdateComponent component) {
  183. listeners.getCallable(UpdateInstallationListener.class).installCompleted(component);
  184. }
  185. protected void fireInstallFailed(final UpdateComponent component) {
  186. listeners.getCallable(UpdateInstallationListener.class).installFailed(component);
  187. }
  188. protected void fireInstallProgressChanged(final UpdateComponent component,
  189. final double progress) {
  190. listeners.getCallable(UpdateInstallationListener.class).
  191. installProgressChanged(component, progress);
  192. }
  193. @Override
  194. public void addUpdateInstallationListener(final UpdateInstallationListener listener) {
  195. listeners.add(UpdateInstallationListener.class, listener);
  196. }
  197. @Override
  198. public void removeUpdateInstallationListener(final UpdateInstallationListener listener) {
  199. listeners.remove(UpdateInstallationListener.class, listener);
  200. }
  201. @Override
  202. protected void installImpl(final UpdateComponent component,
  203. final FakeRetrievalResult retrievalResult) {
  204. try {
  205. fireInstallProgressChanged(component, 0);
  206. final int max = (int) (Math.random() * 50);
  207. for (int i = 0; i < max; i++) {
  208. Thread.sleep(100);
  209. fireInstallProgressChanged(component, 100 * i / max);
  210. }
  211. if (Math.random() < 0.75) {
  212. fireInstallCompleted(component);
  213. } else {
  214. fireInstallFailed(component);
  215. }
  216. } catch (InterruptedException ex) {
  217. // Don't care
  218. }
  219. }
  220. }
  221. }