-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Server version check for CSC activation (#3954)
* checking server version for CSC * fix format change * fix noauth hello exception in integration tests * fix version check * remove redundant check * remove unused imports * 'toString' for Version * rename to RedisVersion * moving RedisVersion package
- Loading branch information
Showing
6 changed files
with
134 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package redis.clients.jedis.csc; | ||
|
||
import java.util.Arrays; | ||
|
||
class RedisVersion implements Comparable<RedisVersion> { | ||
|
||
private String version; | ||
private Integer[] numbers; | ||
|
||
public RedisVersion(String version) { | ||
if (version == null) throw new IllegalArgumentException("Version can not be null"); | ||
this.version = version; | ||
this.numbers = Arrays.stream(version.split("\\.")).map(n -> Integer.parseInt(n)).toArray(Integer[]::new); | ||
} | ||
|
||
@Override | ||
public int compareTo(RedisVersion other) { | ||
int max = Math.max(this.numbers.length, other.numbers.length); | ||
for (int i = 0; i < max; i++) { | ||
int thisNumber = this.numbers.length > i ? this.numbers[i]:0; | ||
int otherNumber = other.numbers.length > i ? other.numbers[i]:0; | ||
if (thisNumber < otherNumber) return -1; | ||
if (thisNumber > otherNumber) return 1; | ||
} | ||
return 0; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return this.version; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object that) { | ||
if (this == that) return true; | ||
if (that == null) return false; | ||
if (this.getClass() != that.getClass()) return false; | ||
return this.compareTo((RedisVersion) that) == 0; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package redis.clients.jedis.csc; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
import org.junit.Test; | ||
|
||
public class VersionTest { | ||
|
||
@Test | ||
public void compareSameVersions() { | ||
RedisVersion a = new RedisVersion("5.2.4"); | ||
RedisVersion b = new RedisVersion("5.2.4"); | ||
assertEquals(a, b); | ||
|
||
RedisVersion c = new RedisVersion("5.2.0.0"); | ||
RedisVersion d = new RedisVersion("5.2"); | ||
assertEquals(a, b); | ||
} | ||
|
||
@Test | ||
public void compareDifferentVersions() { | ||
RedisVersion a = new RedisVersion("5.2.4"); | ||
RedisVersion b = new RedisVersion("5.1.4"); | ||
assertEquals(1, a.compareTo(b)); | ||
|
||
RedisVersion c = new RedisVersion("5.2.4"); | ||
RedisVersion d = new RedisVersion("5.2.5"); | ||
assertEquals(-1, c.compareTo(d)); | ||
} | ||
} |