Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

FakeUpdates.java 9.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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.addons.debug.commands;
  23. import com.dmdirc.FrameContainer;
  24. import com.dmdirc.addons.debug.Debug;
  25. import com.dmdirc.addons.debug.DebugCommand;
  26. import com.dmdirc.commandparser.CommandArguments;
  27. import com.dmdirc.commandparser.commands.context.CommandContext;
  28. import com.dmdirc.updater.UpdateComponent;
  29. import com.dmdirc.updater.Version;
  30. import com.dmdirc.updater.checking.BaseCheckResult;
  31. import com.dmdirc.updater.checking.UpdateCheckResult;
  32. import com.dmdirc.updater.checking.UpdateCheckStrategy;
  33. import com.dmdirc.updater.installing.TypeSensitiveInstallationStrategy;
  34. import com.dmdirc.updater.installing.UpdateInstallationListener;
  35. import com.dmdirc.updater.manager.UpdateManager;
  36. import com.dmdirc.updater.retrieving.BaseRetrievalResult;
  37. import com.dmdirc.updater.retrieving.TypeSensitiveRetrievalStrategy;
  38. import com.dmdirc.updater.retrieving.UpdateRetrievalListener;
  39. import com.dmdirc.updater.retrieving.UpdateRetrievalResult;
  40. import com.dmdirc.util.collections.ListenerList;
  41. import java.util.Collection;
  42. import java.util.HashMap;
  43. import java.util.Map;
  44. import javax.annotation.Nonnull;
  45. import javax.inject.Inject;
  46. import javax.inject.Provider;
  47. /**
  48. * Generates some fake updates.
  49. */
  50. public class FakeUpdates extends DebugCommand {
  51. /** The update manager to add fake updates to. */
  52. private final UpdateManager updateManager;
  53. /**
  54. * Creates a new instance of the command.
  55. *
  56. * @param commandProvider The provider to use to access the main debug command.
  57. * @param updateManager The update manager to add fake updates to.
  58. */
  59. @Inject
  60. public FakeUpdates(
  61. final Provider<Debug> commandProvider,
  62. final UpdateManager updateManager) {
  63. super(commandProvider);
  64. this.updateManager = updateManager;
  65. }
  66. @Override
  67. public String getName() {
  68. return "fakeupdates";
  69. }
  70. @Override
  71. public String getUsage() {
  72. return " - Initialises a fake update handling chain";
  73. }
  74. @Override
  75. public void execute(@Nonnull final FrameContainer origin,
  76. final CommandArguments args, final CommandContext context) {
  77. updateManager.addCheckStrategy(new FakeUpdateCheckStrategy());
  78. updateManager.addRetrievalStrategy(new FakeUpdateRetriever());
  79. updateManager.addInstallationStrategy(new FakeUpdateInstaller());
  80. }
  81. /**
  82. * A fake update check strategy which produces fake update check results.
  83. */
  84. private static class FakeUpdateCheckStrategy
  85. implements UpdateCheckStrategy {
  86. @Override
  87. public Map<UpdateComponent, UpdateCheckResult> checkForUpdates(
  88. final Collection<UpdateComponent> components) {
  89. final Map<UpdateComponent, UpdateCheckResult> res = new HashMap<>();
  90. components.stream().filter(component -> Math.random() * components.size() < 10)
  91. .forEach(component -> res.put(component, new FakeUpdateCheckResult(component)));
  92. return res;
  93. }
  94. }
  95. /**
  96. * A fake update check result.
  97. */
  98. private static class FakeUpdateCheckResult extends BaseCheckResult {
  99. /**
  100. * Creates a new fake result for the given component.
  101. *
  102. * @param component The component this result is for
  103. */
  104. public FakeUpdateCheckResult(final UpdateComponent component) {
  105. super(component, true, "shiny newness", new Version("1.337"));
  106. }
  107. }
  108. private static class FakeUpdateRetriever
  109. extends TypeSensitiveRetrievalStrategy<FakeUpdateCheckResult> {
  110. private final ListenerList listeners = new ListenerList();
  111. /**
  112. * Creates a new {@link FakeUpdateRetriever}.
  113. */
  114. public FakeUpdateRetriever() {
  115. super(FakeUpdateCheckResult.class);
  116. }
  117. protected void fireRetrievalProgressChanged(final UpdateComponent component,
  118. final double progress) {
  119. listeners.getCallable(UpdateRetrievalListener.class).retrievalProgressChanged(component,
  120. progress);
  121. }
  122. protected void fireRetrievalFailed(final UpdateComponent component) {
  123. listeners.getCallable(UpdateRetrievalListener.class).retrievalFailed(component);
  124. }
  125. protected void fireRetrievalCompleted(final UpdateComponent component) {
  126. listeners.getCallable(UpdateRetrievalListener.class).retrievalCompleted(component);
  127. }
  128. @Override
  129. public void addUpdateRetrievalListener(final UpdateRetrievalListener listener) {
  130. listeners.add(UpdateRetrievalListener.class, listener);
  131. }
  132. @Override
  133. public void removeUpdateRetrievalListener(final UpdateRetrievalListener listener) {
  134. listeners.remove(UpdateRetrievalListener.class, listener);
  135. }
  136. @Override
  137. protected UpdateRetrievalResult retrieveImpl(
  138. final FakeUpdateCheckResult checkResult) {
  139. try {
  140. fireRetrievalProgressChanged(checkResult.getComponent(), 0);
  141. final int max = (int) (Math.random() * 50);
  142. for (int i = 0; i < max; i++) {
  143. Thread.sleep(100);
  144. fireRetrievalProgressChanged(checkResult.getComponent(),
  145. 100 * i / max);
  146. }
  147. if (Math.random() < 0.75) {
  148. fireRetrievalCompleted(checkResult.getComponent());
  149. return new FakeRetrievalResult(checkResult, true);
  150. } else {
  151. fireRetrievalFailed(checkResult.getComponent());
  152. return new FakeRetrievalResult(checkResult, false);
  153. }
  154. } catch (InterruptedException ex) {
  155. // Don't care
  156. }
  157. return new FakeRetrievalResult(checkResult, false);
  158. }
  159. }
  160. /**
  161. * Fake retrieval result.
  162. */
  163. private static class FakeRetrievalResult extends BaseRetrievalResult {
  164. /**
  165. * Creates a new fake retrieval result.
  166. *
  167. * @param checkResult The check result that was retrieved
  168. * @param success Whether the result is successful or not
  169. */
  170. public FakeRetrievalResult(final UpdateCheckResult checkResult,
  171. final boolean success) {
  172. super(checkResult, success);
  173. }
  174. }
  175. /**
  176. * A fake update installer.
  177. */
  178. private static class FakeUpdateInstaller
  179. extends TypeSensitiveInstallationStrategy<UpdateComponent, FakeRetrievalResult> {
  180. private final ListenerList listeners = new ListenerList();
  181. /**
  182. * Creates a new {@link FakeUpdateInstaller}.
  183. */
  184. public FakeUpdateInstaller() {
  185. super(UpdateComponent.class, FakeRetrievalResult.class);
  186. }
  187. protected void fireInstallCompleted(final UpdateComponent component) {
  188. listeners.getCallable(UpdateInstallationListener.class).installCompleted(component);
  189. }
  190. protected void fireInstallFailed(final UpdateComponent component) {
  191. listeners.getCallable(UpdateInstallationListener.class).installFailed(component);
  192. }
  193. protected void fireInstallProgressChanged(final UpdateComponent component,
  194. final double progress) {
  195. listeners.getCallable(UpdateInstallationListener.class).
  196. installProgressChanged(component, progress);
  197. }
  198. @Override
  199. public void addUpdateInstallationListener(final UpdateInstallationListener listener) {
  200. listeners.add(UpdateInstallationListener.class, listener);
  201. }
  202. @Override
  203. public void removeUpdateInstallationListener(final UpdateInstallationListener listener) {
  204. listeners.remove(UpdateInstallationListener.class, listener);
  205. }
  206. @Override
  207. protected void installImpl(final UpdateComponent component,
  208. final FakeRetrievalResult retrievalResult) {
  209. try {
  210. fireInstallProgressChanged(component, 0);
  211. final int max = (int) (Math.random() * 50);
  212. for (int i = 0; i < max; i++) {
  213. Thread.sleep(100);
  214. fireInstallProgressChanged(component, 100 * i / max);
  215. }
  216. if (Math.random() < 0.75) {
  217. fireInstallCompleted(component);
  218. } else {
  219. fireInstallFailed(component);
  220. }
  221. } catch (InterruptedException ex) {
  222. // Don't care
  223. }
  224. }
  225. }
  226. }