forked from tikv/client-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestUtils.java
More file actions
47 lines (40 loc) · 1.17 KB
/
Copy pathTestUtils.java
File metadata and controls
47 lines (40 loc) · 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package org.tikv.util;
import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;
public class TestUtils {
public static String getEnv(String key) {
String tmp = System.getenv(key);
if (tmp != null && !tmp.equals("")) {
return tmp;
}
tmp = System.getProperty(key);
if (tmp != null && !tmp.equals("")) {
return tmp;
}
return null;
}
public static byte[] genRandomKey(String keyPrefix, int keyLength) {
int length = keyLength - keyPrefix.length();
if (length <= 0) {
length = 0;
}
return (keyPrefix + genRandomString(length)).getBytes();
}
public static byte[] genRandomValue(int length) {
return genRandomString(length).getBytes();
}
private static String genRandomString(int length) {
Random rnd = ThreadLocalRandom.current();
StringBuilder ret = new StringBuilder(length);
for (int i = 0; i < length; i++) {
boolean isChar = (rnd.nextInt(2) % 2 == 0);
if (isChar) {
int choice = rnd.nextInt(2) % 2 == 0 ? 65 : 97;
ret.append((char) (choice + rnd.nextInt(26)));
} else {
ret.append(rnd.nextInt(10));
}
}
return ret.toString();
}
}