|
8 | 8 | import com.clickhouse.client.ClickHouseFormat; |
9 | 9 |
|
10 | 10 | public class ClickHouseConfigOptionTest { |
11 | | - static enum ClickHouseTestOption implements ClickHouseOption { |
12 | | - STR("string_option", "string", "string option"), |
13 | | - STR0("string_option0", "string0", "string option without environment variable support"), |
14 | | - STR1("string_option0", "string1", "string option without environment variable and system property support"), |
15 | | - INT("integer_option", 2333, "integer option"), |
16 | | - INT0("integer_option0", 23330, "integer option without environment variable support"), |
17 | | - INT1("integer_option1", 23331, "integer option without environment variable and system property support"), |
18 | | - BOOL("boolean_option", false, "boolean option"), |
19 | | - BOOL0("boolean_option0", true, "boolean option without environment variable support"), |
20 | | - BOOL1("boolean_option1", false, "boolean option without environment variable and system property support"); |
21 | | - |
22 | | - private final String key; |
23 | | - private final Serializable defaultValue; |
24 | | - private final Class<? extends Serializable> clazz; |
25 | | - private final String description; |
26 | | - |
27 | | - <T extends Serializable> ClickHouseTestOption(String key, T defaultValue, String description) { |
28 | | - this.key = ClickHouseChecker.nonNull(key, "key"); |
29 | | - this.defaultValue = defaultValue; |
30 | | - this.clazz = defaultValue.getClass(); |
31 | | - this.description = ClickHouseChecker.nonNull(description, "description"); |
| 11 | + static enum ClickHouseTestOption implements ClickHouseOption { |
| 12 | + STR("string_option", "string", "string option"), |
| 13 | + STR0("string_option0", "string0", "string option without environment variable support"), |
| 14 | + STR1("string_option0", "string1", |
| 15 | + "string option without environment variable and system property support"), |
| 16 | + INT("integer_option", 2333, "integer option"), |
| 17 | + INT0("integer_option0", 23330, "integer option without environment variable support"), |
| 18 | + INT1("integer_option1", 23331, |
| 19 | + "integer option without environment variable and system property support"), |
| 20 | + BOOL("boolean_option", false, "boolean option"), |
| 21 | + BOOL0("boolean_option0", true, "boolean option without environment variable support"), |
| 22 | + BOOL1("boolean_option1", false, |
| 23 | + "boolean option without environment variable and system property support"); |
| 24 | + |
| 25 | + private final String key; |
| 26 | + private final Serializable defaultValue; |
| 27 | + private final Class<? extends Serializable> clazz; |
| 28 | + private final String description; |
| 29 | + |
| 30 | + <T extends Serializable> ClickHouseTestOption(String key, T defaultValue, String description) { |
| 31 | + this.key = ClickHouseChecker.nonNull(key, "key"); |
| 32 | + this.defaultValue = defaultValue; |
| 33 | + this.clazz = defaultValue.getClass(); |
| 34 | + this.description = ClickHouseChecker.nonNull(description, "description"); |
| 35 | + } |
| 36 | + |
| 37 | + @Override |
| 38 | + public String getKey() { |
| 39 | + return key; |
| 40 | + } |
| 41 | + |
| 42 | + @Override |
| 43 | + public Serializable getDefaultValue() { |
| 44 | + return defaultValue; |
| 45 | + } |
| 46 | + |
| 47 | + @Override |
| 48 | + public Class<? extends Serializable> getValueType() { |
| 49 | + return clazz; |
| 50 | + } |
| 51 | + |
| 52 | + @Override |
| 53 | + public String getDescription() { |
| 54 | + return description; |
| 55 | + } |
32 | 56 | } |
33 | 57 |
|
34 | | - @Override |
35 | | - public String getKey() { |
36 | | - return key; |
| 58 | + @Test(groups = { "unit" }) |
| 59 | + public void testFromString() { |
| 60 | + Assert.assertThrows(IllegalArgumentException.class, |
| 61 | + () -> ClickHouseOption.fromString(null, String.class)); |
| 62 | + Assert.assertEquals(ClickHouseOption.fromString("", String.class), ""); |
| 63 | + |
| 64 | + Assert.assertEquals(ClickHouseOption.fromString("", Boolean.class), Boolean.FALSE); |
| 65 | + Assert.assertEquals(ClickHouseOption.fromString("Yes", Boolean.class), Boolean.FALSE); |
| 66 | + Assert.assertEquals(ClickHouseOption.fromString("1", Boolean.class), Boolean.TRUE); |
| 67 | + Assert.assertEquals(ClickHouseOption.fromString("true", Boolean.class), Boolean.TRUE); |
| 68 | + Assert.assertEquals(ClickHouseOption.fromString("True", Boolean.class), Boolean.TRUE); |
| 69 | + |
| 70 | + Assert.assertEquals(ClickHouseOption.fromString("", Integer.class), Integer.valueOf(0)); |
| 71 | + Assert.assertEquals(ClickHouseOption.fromString("0", Integer.class), Integer.valueOf(0)); |
| 72 | + Assert.assertThrows(IllegalArgumentException.class, |
| 73 | + () -> ClickHouseOption.fromString(null, Integer.class)); |
| 74 | + |
| 75 | + Assert.assertEquals(ClickHouseOption.fromString("0.1", Float.class), Float.valueOf(0.1F)); |
| 76 | + Assert.assertEquals(ClickHouseOption.fromString("NaN", Float.class), Float.valueOf(Float.NaN)); |
| 77 | + |
| 78 | + Assert.assertEquals(ClickHouseOption.fromString("Map", ClickHouseDataType.class), |
| 79 | + ClickHouseDataType.Map); |
| 80 | + Assert.assertEquals(ClickHouseOption.fromString("RowBinary", ClickHouseFormat.class), |
| 81 | + ClickHouseFormat.RowBinary); |
| 82 | + Assert.assertThrows(IllegalArgumentException.class, |
| 83 | + () -> ClickHouseOption.fromString("NonExistFormat", ClickHouseFormat.class)); |
37 | 84 | } |
38 | 85 |
|
39 | | - @Override |
40 | | - public Serializable getDefaultValue() { |
41 | | - return defaultValue; |
| 86 | + @Test(groups = { "unit" }) |
| 87 | + public void testGetEffectiveDefaultValue() { |
| 88 | + // environment variables are set in pom.xml |
| 89 | + Assert.assertEquals(ClickHouseTestOption.STR.getEffectiveDefaultValue(), |
| 90 | + ClickHouseTestOption.STR.getDefaultValueFromEnvVar().get()); |
| 91 | + Assert.assertEquals(ClickHouseTestOption.INT.getEffectiveDefaultValue(), |
| 92 | + Integer.parseInt(ClickHouseTestOption.INT.getDefaultValueFromEnvVar().get())); |
| 93 | + Assert.assertEquals(ClickHouseTestOption.BOOL.getEffectiveDefaultValue(), |
| 94 | + Boolean.valueOf(ClickHouseTestOption.BOOL.getDefaultValueFromEnvVar().get())); |
| 95 | + |
| 96 | + String sv = "system.property"; |
| 97 | + int iv = 12345; |
| 98 | + boolean bv = true; |
| 99 | + System.setProperty(ClickHouseTestOption.STR0.getPrefix().toLowerCase() + "_" |
| 100 | + + ClickHouseTestOption.STR0.name().toLowerCase(), sv); |
| 101 | + System.setProperty(ClickHouseTestOption.INT0.getPrefix().toLowerCase() + "_" |
| 102 | + + ClickHouseTestOption.INT0.name().toLowerCase(), String.valueOf(iv)); |
| 103 | + System.setProperty(ClickHouseTestOption.BOOL0.getPrefix().toLowerCase() + "_" |
| 104 | + + ClickHouseTestOption.BOOL0.name().toLowerCase(), String.valueOf(bv)); |
| 105 | + |
| 106 | + Assert.assertEquals(ClickHouseTestOption.STR0.getEffectiveDefaultValue(), sv); |
| 107 | + Assert.assertEquals(ClickHouseTestOption.INT0.getEffectiveDefaultValue(), iv); |
| 108 | + Assert.assertEquals(ClickHouseTestOption.BOOL0.getEffectiveDefaultValue(), bv); |
| 109 | + |
| 110 | + Assert.assertEquals(ClickHouseTestOption.STR1.getEffectiveDefaultValue(), |
| 111 | + ClickHouseTestOption.STR1.getDefaultValue()); |
| 112 | + Assert.assertEquals(ClickHouseTestOption.INT1.getEffectiveDefaultValue(), |
| 113 | + ClickHouseTestOption.INT1.getDefaultValue()); |
| 114 | + Assert.assertEquals(ClickHouseTestOption.BOOL1.getEffectiveDefaultValue(), |
| 115 | + ClickHouseTestOption.BOOL1.getDefaultValue()); |
42 | 116 | } |
43 | | - |
44 | | - @Override |
45 | | - public Class<?> getValueType() { |
46 | | - return clazz; |
47 | | - } |
48 | | - |
49 | | - @Override |
50 | | - public String getDescription() { |
51 | | - return description; |
52 | | - } |
53 | | - } |
54 | | - |
55 | | - @Test(groups = { "unit" }) |
56 | | - public void testFromString() { |
57 | | - Assert.assertThrows(IllegalArgumentException.class, |
58 | | - () -> ClickHouseOption.fromString(null, String.class)); |
59 | | - Assert.assertEquals(ClickHouseOption.fromString("", String.class), ""); |
60 | | - |
61 | | - Assert.assertEquals(ClickHouseOption.fromString("", Boolean.class), Boolean.FALSE); |
62 | | - Assert.assertEquals(ClickHouseOption.fromString("Yes", Boolean.class), Boolean.FALSE); |
63 | | - Assert.assertEquals(ClickHouseOption.fromString("1", Boolean.class), Boolean.TRUE); |
64 | | - Assert.assertEquals(ClickHouseOption.fromString("true", Boolean.class), Boolean.TRUE); |
65 | | - Assert.assertEquals(ClickHouseOption.fromString("True", Boolean.class), Boolean.TRUE); |
66 | | - |
67 | | - Assert.assertEquals(ClickHouseOption.fromString("", Integer.class), Integer.valueOf(0)); |
68 | | - Assert.assertEquals(ClickHouseOption.fromString("0", Integer.class), Integer.valueOf(0)); |
69 | | - Assert.assertThrows(IllegalArgumentException.class, |
70 | | - () -> ClickHouseOption.fromString(null, Integer.class)); |
71 | | - |
72 | | - Assert.assertEquals(ClickHouseOption.fromString("0.1", Float.class), Float.valueOf(0.1F)); |
73 | | - Assert.assertEquals(ClickHouseOption.fromString("NaN", Float.class), Float.valueOf(Float.NaN)); |
74 | | - |
75 | | - Assert.assertEquals(ClickHouseOption.fromString("Map", ClickHouseDataType.class), ClickHouseDataType.Map); |
76 | | - Assert.assertEquals(ClickHouseOption.fromString("RowBinary", ClickHouseFormat.class), |
77 | | - ClickHouseFormat.RowBinary); |
78 | | - Assert.assertThrows(IllegalArgumentException.class, |
79 | | - () -> ClickHouseOption.fromString("NonExistFormat", ClickHouseFormat.class)); |
80 | | - } |
81 | | - |
82 | | - @Test(groups = { "unit" }) |
83 | | - public void testGetEffectiveDefaultValue() { |
84 | | - // environment variables are set in pom.xml |
85 | | - Assert.assertEquals(ClickHouseTestOption.STR.getEffectiveDefaultValue(), |
86 | | - ClickHouseTestOption.STR.getDefaultValueFromEnvVar().get()); |
87 | | - Assert.assertEquals(ClickHouseTestOption.INT.getEffectiveDefaultValue(), |
88 | | - Integer.parseInt(ClickHouseTestOption.INT.getDefaultValueFromEnvVar().get())); |
89 | | - Assert.assertEquals(ClickHouseTestOption.BOOL.getEffectiveDefaultValue(), |
90 | | - Boolean.valueOf(ClickHouseTestOption.BOOL.getDefaultValueFromEnvVar().get())); |
91 | | - |
92 | | - String sv = "system.property"; |
93 | | - int iv = 12345; |
94 | | - boolean bv = true; |
95 | | - System.setProperty(ClickHouseTestOption.STR0.getPrefix().toLowerCase() + "_" |
96 | | - + ClickHouseTestOption.STR0.name().toLowerCase(), sv); |
97 | | - System.setProperty(ClickHouseTestOption.INT0.getPrefix().toLowerCase() + "_" |
98 | | - + ClickHouseTestOption.INT0.name().toLowerCase(), String.valueOf(iv)); |
99 | | - System.setProperty(ClickHouseTestOption.BOOL0.getPrefix().toLowerCase() + "_" |
100 | | - + ClickHouseTestOption.BOOL0.name().toLowerCase(), String.valueOf(bv)); |
101 | | - |
102 | | - Assert.assertEquals(ClickHouseTestOption.STR0.getEffectiveDefaultValue(), sv); |
103 | | - Assert.assertEquals(ClickHouseTestOption.INT0.getEffectiveDefaultValue(), iv); |
104 | | - Assert.assertEquals(ClickHouseTestOption.BOOL0.getEffectiveDefaultValue(), bv); |
105 | | - |
106 | | - Assert.assertEquals(ClickHouseTestOption.STR1.getEffectiveDefaultValue(), |
107 | | - ClickHouseTestOption.STR1.getDefaultValue()); |
108 | | - Assert.assertEquals(ClickHouseTestOption.INT1.getEffectiveDefaultValue(), |
109 | | - ClickHouseTestOption.INT1.getDefaultValue()); |
110 | | - Assert.assertEquals(ClickHouseTestOption.BOOL1.getEffectiveDefaultValue(), |
111 | | - ClickHouseTestOption.BOOL1.getDefaultValue()); |
112 | | - } |
113 | 117 | } |
0 commit comments