-
Notifications
You must be signed in to change notification settings - Fork 988
/
Copy pathStringUtilsTest.java
107 lines (91 loc) · 4.45 KB
/
StringUtilsTest.java
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/*
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.elasticsearch.hadoop.util;
import org.junit.Test;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import static org.junit.Assert.*;
public class StringUtilsTest {
@Test
public void testJsonEncodingQuote() {
String value = "foo\"bar";
String jsonEscaped = "foo\\\"bar";
assertEquals(jsonEscaped, StringUtils.jsonEncoding(value));
}
@Test
public void testParseIpInEs1x() {
assertEquals("1.2.3.4", StringUtils.parseIpAddress("inet[/1.2.3.4:9200]").ip);
}
@Test
public void testParseIpInEs1xWithHostName() {
assertEquals("11.22.33.44", StringUtils.parseIpAddress("inet[foobar/11.22.33.44:9200]").ip);
}
@Test
public void testParseIpInEs2x() {
assertEquals("111.222.333.444", StringUtils.parseIpAddress("111.222.333.444:9200").ip);
}
@Test
public void testParseIpInEs2xWithHostName() {
assertEquals("11.222.3.4", StringUtils.parseIpAddress("foobar/11.222.3.4:9200").ip);
}
@Test
public void testSingularIndexNames() {
assertTrue(StringUtils.isValidSingularIndexName("abcdefg"));
assertTrue(StringUtils.isValidSingularIndexName("\uD840\uDE13")); // HTTP Encode: %F0%A0%88%93
assertFalse(StringUtils.isValidSingularIndexName("+abcdefg"));
assertFalse(StringUtils.isValidSingularIndexName("-abcdefg"));
assertFalse(StringUtils.isValidSingularIndexName("_abcdefg"));
assertFalse(StringUtils.isValidSingularIndexName("*abcdefg"));
assertFalse(StringUtils.isValidSingularIndexName("abc*defg"));
assertFalse(StringUtils.isValidSingularIndexName("abcd/efg"));
assertFalse(StringUtils.isValidSingularIndexName("abcd\\efg"));
assertFalse(StringUtils.isValidSingularIndexName("abc|defg"));
assertFalse(StringUtils.isValidSingularIndexName("abcdef,g"));
assertFalse(StringUtils.isValidSingularIndexName("abcdef<em>g"));
assertFalse(StringUtils.isValidSingularIndexName("abcde\"fg"));
assertFalse(StringUtils.isValidSingularIndexName("a bcdefg"));
assertFalse(StringUtils.isValidSingularIndexName("abcdefg?"));
// Multi-Index Format is not a legal singular index name, but it should be resolved to one at runtime
assertFalse(StringUtils.isValidSingularIndexName("abc{date|yyyy-MM-dd}defg"));
}
@Test
public void testTokenize() {
List<String> test1 = Arrays.asList(new String[]{"this", "is a", "test"});
String concatenatedString = StringUtils.concatenate(test1);
List<String> tokens = StringUtils.tokenize(concatenatedString, ",", true, true);
assertEquals(test1, tokens);
List<String> test2 = Arrays.asList(new String[]{"this", " is a", " test ", " "});
concatenatedString = StringUtils.concatenate(test2);
tokens = StringUtils.tokenize(concatenatedString, ",", false, false);
assertEquals(test2, tokens);
List<String> test3 = Arrays.asList(new String[]{"this", "is, a", "test"});
concatenatedString = StringUtils.concatenate(test3);
tokens = StringUtils.tokenize(concatenatedString, ",", true, true);
assertEquals(test3, tokens);
Object[] test4 = new String[]{"this", "is, a", "test"};
concatenatedString = StringUtils.concatenate(test4, ";");
tokens = StringUtils.tokenize(concatenatedString, ";", true, true);
assertEquals(Arrays.asList(test4), tokens);
List<String> test5 = Arrays.asList(new String[]{"this", "is, a", "test"});
concatenatedString = StringUtils.concatenate(test5, ",");
tokens = StringUtils.tokenize(concatenatedString, ";,", true, true);
assertEquals(test5, tokens);
}
}