77 * License v3.0 only", or the "Server Side Public License, v 1".
88 */
99
10- package org .elasticsearch .common . text ;
10+ package org .elasticsearch .xcontent ;
1111
12- import org .elasticsearch .common .bytes .BytesArray ;
1312import org .elasticsearch .test .ESTestCase ;
1413
1514import java .nio .charset .StandardCharsets ;
1615
1716public class TextTests extends ESTestCase {
1817 public void testConvertToBytes () {
1918 String value = randomUnicodeOfLength (randomInt (128 ));
20- var encoded = new BytesArray (value );
19+ byte [] encodedArr = value .getBytes (StandardCharsets .UTF_8 );
20+ var encoded = new XContentString .EncodedBytes (encodedArr );
2121
2222 var text = new Text (value );
2323 assertTrue (text .hasString ());
@@ -38,7 +38,8 @@ public void testConvertToBytes() {
3838
3939 public void testConvertToString () {
4040 String value = randomUnicodeOfLength (randomInt (128 ));
41- var encoded = new BytesArray (value );
41+ byte [] encodedArr = value .getBytes (StandardCharsets .UTF_8 );
42+ var encoded = new XContentString .EncodedBytes (encodedArr );
4243
4344 var text = new Text (encoded );
4445 assertFalse (text .hasString ());
@@ -47,7 +48,7 @@ public void testConvertToString() {
4748 assertEquals (value , text .string ());
4849 assertEquals (encoded , text .bytes ());
4950
50- assertFalse (text .hasString ());
51+ assertTrue (text .hasString ());
5152 assertTrue (text .hasBytes ());
5253
5354 // Ensure the conversion didn't mess up subsequent calls
@@ -57,9 +58,37 @@ public void testConvertToString() {
5758 assertSame (encoded , text .bytes ());
5859 }
5960
61+ public void testStringLength () {
62+ int stringLength = randomInt (128 );
63+ String value = randomUnicodeOfLength (stringLength );
64+ byte [] encodedArr = value .getBytes (StandardCharsets .UTF_8 );
65+ var encoded = new XContentString .EncodedBytes (encodedArr );
66+
67+ {
68+ var text = new Text (value );
69+ assertTrue (text .hasString ());
70+ assertEquals (stringLength , text .stringLength ());
71+ }
72+
73+ {
74+ var text = new Text (encoded );
75+ assertFalse (text .hasString ());
76+ assertEquals (stringLength , text .stringLength ());
77+ assertTrue (text .hasString ());
78+ }
79+
80+ {
81+ var text = new Text (encoded , stringLength );
82+ assertFalse (text .hasString ());
83+ assertEquals (stringLength , text .stringLength ());
84+ assertFalse (text .hasString ());
85+ }
86+ }
87+
6088 public void testEquals () {
6189 String value = randomUnicodeOfLength (randomInt (128 ));
62- var encoded = new BytesArray (value );
90+ byte [] encodedArr = value .getBytes (StandardCharsets .UTF_8 );
91+ var encoded = new XContentString .EncodedBytes (encodedArr );
6392
6493 {
6594 var text1 = new Text (value );
@@ -82,7 +111,8 @@ public void testEquals() {
82111
83112 public void testCompareTo () {
84113 String value1 = randomUnicodeOfLength (randomInt (128 ));
85- var encoded1 = new BytesArray (value1 );
114+ byte [] encodedArr1 = value1 .getBytes (StandardCharsets .UTF_8 );
115+ var encoded1 = new XContentString .EncodedBytes (encodedArr1 );
86116
87117 {
88118 var text1 = new Text (value1 );
@@ -103,7 +133,8 @@ public void testCompareTo() {
103133 }
104134
105135 String value2 = randomUnicodeOfLength (randomInt (128 ));
106- var encoded2 = new BytesArray (value2 );
136+ byte [] encodedArr2 = value2 .getBytes (StandardCharsets .UTF_8 );
137+ var encoded2 = new XContentString .EncodedBytes (encodedArr2 );
107138
108139 int compSign = (int ) Math .signum (encoded1 .compareTo (encoded2 ));
109140
0 commit comments