VersionChecker.java

1
package sh.okx.rankup.util;
2
3
import com.google.common.base.Charsets;
4
import com.google.common.io.CharStreams;
5
import java.io.IOException;
6
import java.io.InputStreamReader;
7
import java.net.URL;
8
import org.bukkit.Bukkit;
9
import org.bukkit.plugin.Plugin;
10
11
public class VersionChecker {
12
  public static final int RESOURCE_ID = 76964;
13
14
  private final Plugin plugin;
15
  private final String currentVersion;
16
  private String latestVersion;
17
  private boolean checked = false;
18
19
  public VersionChecker(Plugin plugin) {
20
    this.currentVersion = plugin.getDescription().getVersion();
21
    this.plugin = plugin;
22
  }
23
24
  public Plugin getPlugin() {
25 1 1. getPlugin : replaced return value with null for sh/okx/rankup/util/VersionChecker::getPlugin → KILLED
    return plugin;
26
  }
27
28
  /**
29
   * Checks if the version checker has already made an asynchronous call to the web server to check
30
   * the version, so future checks will run instantly.
31
   *
32
   * @return true if the version checker already knows the latest version, false otherwise
33
   */
34
  public boolean hasChecked() {
35 2 1. hasChecked : replaced boolean return with false for sh/okx/rankup/util/VersionChecker::hasChecked → NO_COVERAGE
2. hasChecked : replaced boolean return with true for sh/okx/rankup/util/VersionChecker::hasChecked → NO_COVERAGE
    return checked;
36
  }
37
38
  public void checkVersion(VersionCheckerCallback callback) {
39 1 1. checkVersion : negated conditional → NO_COVERAGE
    if (latestVersion != null) {
40 1 1. checkVersion : negated conditional → NO_COVERAGE
      if (currentVersion.equals(latestVersion)) {
41 1 1. checkVersion : removed call to sh/okx/rankup/util/VersionChecker$VersionCheckerCallback::onLatestVersion → NO_COVERAGE
        callback.onLatestVersion(currentVersion);
42
      } else {
43 1 1. checkVersion : removed call to sh/okx/rankup/util/VersionChecker$VersionCheckerCallback::onOutdatedVersion → NO_COVERAGE
        callback.onOutdatedVersion(currentVersion, latestVersion);
44
      }
45 1 1. checkVersion : negated conditional → NO_COVERAGE
    } else if (currentVersion.contains("alpha")
46 1 1. checkVersion : negated conditional → NO_COVERAGE
        || currentVersion.contains("beta")
47 1 1. checkVersion : negated conditional → NO_COVERAGE
        || currentVersion.contains("rc")) {
48
      checked = true;
49 1 1. checkVersion : removed call to sh/okx/rankup/util/VersionChecker$VersionCheckerCallback::onPreReleaseVersion → NO_COVERAGE
      callback.onPreReleaseVersion(currentVersion);
50
    } else {
51 1 1. lambda$checkVersion$0 : removed call to sh/okx/rankup/util/VersionChecker::checkVersionAsync → NO_COVERAGE
      Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> checkVersionAsync(callback));
52
    }
53
  }
54
55
  private void checkVersionAsync(VersionCheckerCallback callback) {
56
    try {
57
      latestVersion = getLatestVersion();
58
      checked = true;
59 1 1. checkVersionAsync : negated conditional → NO_COVERAGE
      if (currentVersion.equals(latestVersion)) {
60 1 1. checkVersionAsync : removed call to sh/okx/rankup/util/VersionChecker$VersionCheckerCallback::onLatestVersion → NO_COVERAGE
        callback.onLatestVersion(currentVersion);
61
      } else {
62 1 1. checkVersionAsync : removed call to sh/okx/rankup/util/VersionChecker$VersionCheckerCallback::onOutdatedVersion → NO_COVERAGE
        callback.onOutdatedVersion(currentVersion, latestVersion);
63
      }
64
65
    } catch (IOException e) {
66 1 1. checkVersionAsync : removed call to sh/okx/rankup/util/VersionChecker$VersionCheckerCallback::onFailure → NO_COVERAGE
      callback.onFailure();
67
    }
68
  }
69
70
  private String getLatestVersion() throws IOException {
71
    URL url = new URL("https://api.spigotmc.org/legacy/update.php?resource=" + RESOURCE_ID);
72 1 1. getLatestVersion : replaced return value with "" for sh/okx/rankup/util/VersionChecker::getLatestVersion → NO_COVERAGE
    return CharStreams.toString(new InputStreamReader(url.openStream(), Charsets.UTF_8));
73
  }
74
75
  /**
76
   * A callback used when a version check runs
77
   */
78
  public interface VersionCheckerCallback {
79
80
    /**
81
     * Called when the plugin is already on the latest version
82
     * May be called asynchronously
83
     *
84
     * @param version the current, and latest, version of the plugin
85
     */
86
    void onLatestVersion(String version);
87
88
    /**
89
     * Called when the plugin is on a version other than the latest on the SpigotMC plugin page.
90
     * May be called asynchronously.
91
     *
92
     * @param currentVersion the current version of the plugin specified in plugin.yml
93
     * @param latestVersion the latest version of the plugin specified on SpigotMC.
94
     */
95
    void onOutdatedVersion(String currentVersion, String latestVersion);
96
97
    /**
98
     * Called when the plugin is on a pre-release version and is exempt to the usual update system.
99
     *
100
     * @param version the current version of the plugin
101
     */
102
    void onPreReleaseVersion(String version);
103
104
    /**
105
     * Called when the version checker was unable to retrieve the latest version.
106
     * May be called asynchronously.
107
     */
108
    void onFailure();
109
  }
110
111
  /**
112
   * An implementation of {@link VersionCheckerCallback} that is called asynchronously, and then
113
   * forwards the calls an underlying VersionCheckerCallback synchronously on the main Bukkit
114
   * thread.
115
   */
116
  static class SyncVersionCheckerCallback implements VersionCheckerCallback {
117
118
    private final Plugin plugin;
119
    private final VersionCheckerCallback callback;
120
121
    SyncVersionCheckerCallback(Plugin plugin, VersionCheckerCallback callback) {
122
      this.plugin = plugin;
123
      this.callback = callback;
124
    }
125
126
    @Override
127
    public void onLatestVersion(String version) {
128 2 1. lambda$onLatestVersion$0 : removed call to sh/okx/rankup/util/VersionChecker$VersionCheckerCallback::onLatestVersion → NO_COVERAGE
2. onLatestVersion : removed call to sh/okx/rankup/util/VersionChecker$SyncVersionCheckerCallback::doSync → NO_COVERAGE
      doSync(() -> callback.onLatestVersion(version));
129
    }
130
131
    @Override
132
    public void onOutdatedVersion(String currentVersion, String latestVersion) {
133 2 1. lambda$onOutdatedVersion$1 : removed call to sh/okx/rankup/util/VersionChecker$VersionCheckerCallback::onOutdatedVersion → NO_COVERAGE
2. onOutdatedVersion : removed call to sh/okx/rankup/util/VersionChecker$SyncVersionCheckerCallback::doSync → NO_COVERAGE
      doSync(() -> callback.onOutdatedVersion(currentVersion, latestVersion));
134
    }
135
136
    @Override
137
    public void onPreReleaseVersion(String version) {
138 2 1. lambda$onPreReleaseVersion$2 : removed call to sh/okx/rankup/util/VersionChecker$VersionCheckerCallback::onPreReleaseVersion → NO_COVERAGE
2. onPreReleaseVersion : removed call to sh/okx/rankup/util/VersionChecker$SyncVersionCheckerCallback::doSync → NO_COVERAGE
      doSync(() -> callback.onPreReleaseVersion(version));
139
    }
140
141
    @Override
142
    public void onFailure() {
143 1 1. onFailure : removed call to sh/okx/rankup/util/VersionChecker$SyncVersionCheckerCallback::doSync → NO_COVERAGE
      doSync(callback::onFailure);
144
    }
145
146
    private void doSync(Runnable r) {
147
      Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, r);
148
    }
149
  }
150
}

Mutations

25

1.1
Location : getPlugin
Killed by : sh.okx.rankup.requirements.MobKillsRequirementsTest.[engine:junit-jupiter]/[class:sh.okx.rankup.requirements.MobKillsRequirementsTest]/[method:testMobKillsRequirements()]
replaced return value with null for sh/okx/rankup/util/VersionChecker::getPlugin → KILLED

35

1.1
Location : hasChecked
Killed by : none
replaced boolean return with false for sh/okx/rankup/util/VersionChecker::hasChecked → NO_COVERAGE

2.2
Location : hasChecked
Killed by : none
replaced boolean return with true for sh/okx/rankup/util/VersionChecker::hasChecked → NO_COVERAGE

39

1.1
Location : checkVersion
Killed by : none
negated conditional → NO_COVERAGE

40

1.1
Location : checkVersion
Killed by : none
negated conditional → NO_COVERAGE

41

1.1
Location : checkVersion
Killed by : none
removed call to sh/okx/rankup/util/VersionChecker$VersionCheckerCallback::onLatestVersion → NO_COVERAGE

43

1.1
Location : checkVersion
Killed by : none
removed call to sh/okx/rankup/util/VersionChecker$VersionCheckerCallback::onOutdatedVersion → NO_COVERAGE

45

1.1
Location : checkVersion
Killed by : none
negated conditional → NO_COVERAGE

46

1.1
Location : checkVersion
Killed by : none
negated conditional → NO_COVERAGE

47

1.1
Location : checkVersion
Killed by : none
negated conditional → NO_COVERAGE

49

1.1
Location : checkVersion
Killed by : none
removed call to sh/okx/rankup/util/VersionChecker$VersionCheckerCallback::onPreReleaseVersion → NO_COVERAGE

51

1.1
Location : lambda$checkVersion$0
Killed by : none
removed call to sh/okx/rankup/util/VersionChecker::checkVersionAsync → NO_COVERAGE

59

1.1
Location : checkVersionAsync
Killed by : none
negated conditional → NO_COVERAGE

60

1.1
Location : checkVersionAsync
Killed by : none
removed call to sh/okx/rankup/util/VersionChecker$VersionCheckerCallback::onLatestVersion → NO_COVERAGE

62

1.1
Location : checkVersionAsync
Killed by : none
removed call to sh/okx/rankup/util/VersionChecker$VersionCheckerCallback::onOutdatedVersion → NO_COVERAGE

66

1.1
Location : checkVersionAsync
Killed by : none
removed call to sh/okx/rankup/util/VersionChecker$VersionCheckerCallback::onFailure → NO_COVERAGE

72

1.1
Location : getLatestVersion
Killed by : none
replaced return value with "" for sh/okx/rankup/util/VersionChecker::getLatestVersion → NO_COVERAGE

128

1.1
Location : lambda$onLatestVersion$0
Killed by : none
removed call to sh/okx/rankup/util/VersionChecker$VersionCheckerCallback::onLatestVersion → NO_COVERAGE

2.2
Location : onLatestVersion
Killed by : none
removed call to sh/okx/rankup/util/VersionChecker$SyncVersionCheckerCallback::doSync → NO_COVERAGE

133

1.1
Location : lambda$onOutdatedVersion$1
Killed by : none
removed call to sh/okx/rankup/util/VersionChecker$VersionCheckerCallback::onOutdatedVersion → NO_COVERAGE

2.2
Location : onOutdatedVersion
Killed by : none
removed call to sh/okx/rankup/util/VersionChecker$SyncVersionCheckerCallback::doSync → NO_COVERAGE

138

1.1
Location : lambda$onPreReleaseVersion$2
Killed by : none
removed call to sh/okx/rankup/util/VersionChecker$VersionCheckerCallback::onPreReleaseVersion → NO_COVERAGE

2.2
Location : onPreReleaseVersion
Killed by : none
removed call to sh/okx/rankup/util/VersionChecker$SyncVersionCheckerCallback::doSync → NO_COVERAGE

143

1.1
Location : onFailure
Killed by : none
removed call to sh/okx/rankup/util/VersionChecker$SyncVersionCheckerCallback::doSync → NO_COVERAGE

Active mutators

Tests examined


Report generated by PIT 1.7.0