|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the "Elastic License |
| 4 | + * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side |
| 5 | + * Public License v 1"; you may not use this file except in compliance with, at |
| 6 | + * your election, the "Elastic License 2.0", the "GNU Affero General Public |
| 7 | + * License v3.0 only", or the "Server Side Public License, v 1". |
| 8 | + */ |
| 9 | + |
| 10 | +package org.elasticsearch.xcontent; |
| 11 | + |
| 12 | +import org.elasticsearch.test.ESTestCase; |
| 13 | + |
| 14 | +import java.nio.charset.StandardCharsets; |
| 15 | + |
| 16 | +public class TextTests extends ESTestCase { |
| 17 | + public void testConvertToBytes() { |
| 18 | + String value = randomUnicodeOfLength(randomInt(128)); |
| 19 | + byte[] encodedArr = value.getBytes(StandardCharsets.UTF_8); |
| 20 | + var encoded = new XContentString.EncodedBytes(encodedArr); |
| 21 | + |
| 22 | + var text = new Text(value); |
| 23 | + assertTrue(text.hasString()); |
| 24 | + assertFalse(text.hasBytes()); |
| 25 | + |
| 26 | + assertEquals(value, text.string()); |
| 27 | + assertEquals(encoded, text.bytes()); |
| 28 | + |
| 29 | + assertTrue(text.hasString()); |
| 30 | + assertTrue(text.hasBytes()); |
| 31 | + |
| 32 | + // Ensure the conversion didn't mess up subsequent calls |
| 33 | + assertEquals(value, text.string()); |
| 34 | + assertEquals(encoded, text.bytes()); |
| 35 | + |
| 36 | + assertSame(text.bytes(), text.bytes()); |
| 37 | + } |
| 38 | + |
| 39 | + public void testConvertToString() { |
| 40 | + String value = randomUnicodeOfLength(randomInt(128)); |
| 41 | + byte[] encodedArr = value.getBytes(StandardCharsets.UTF_8); |
| 42 | + var encoded = new XContentString.EncodedBytes(encodedArr); |
| 43 | + |
| 44 | + var text = new Text(encoded); |
| 45 | + assertFalse(text.hasString()); |
| 46 | + assertTrue(text.hasBytes()); |
| 47 | + |
| 48 | + assertEquals(value, text.string()); |
| 49 | + assertEquals(encoded, text.bytes()); |
| 50 | + |
| 51 | + assertTrue(text.hasString()); |
| 52 | + assertTrue(text.hasBytes()); |
| 53 | + |
| 54 | + // Ensure the conversion didn't mess up subsequent calls |
| 55 | + assertEquals(value, text.string()); |
| 56 | + assertEquals(encoded, text.bytes()); |
| 57 | + |
| 58 | + assertSame(encoded, text.bytes()); |
| 59 | + } |
| 60 | + |
| 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 | + |
| 88 | + public void testEquals() { |
| 89 | + String value = randomUnicodeOfLength(randomInt(128)); |
| 90 | + byte[] encodedArr = value.getBytes(StandardCharsets.UTF_8); |
| 91 | + var encoded = new XContentString.EncodedBytes(encodedArr); |
| 92 | + |
| 93 | + { |
| 94 | + var text1 = new Text(value); |
| 95 | + var text2 = new Text(value); |
| 96 | + assertTrue(text1.equals(text2)); |
| 97 | + } |
| 98 | + |
| 99 | + { |
| 100 | + var text1 = new Text(value); |
| 101 | + var text2 = new Text(encoded); |
| 102 | + assertTrue(text1.equals(text2)); |
| 103 | + } |
| 104 | + |
| 105 | + { |
| 106 | + var text1 = new Text(encoded); |
| 107 | + var text2 = new Text(encoded); |
| 108 | + assertTrue(text1.equals(text2)); |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + public void testCompareTo() { |
| 113 | + String value1 = randomUnicodeOfLength(randomInt(128)); |
| 114 | + byte[] encodedArr1 = value1.getBytes(StandardCharsets.UTF_8); |
| 115 | + var encoded1 = new XContentString.EncodedBytes(encodedArr1); |
| 116 | + |
| 117 | + { |
| 118 | + var text1 = new Text(value1); |
| 119 | + var text2 = new Text(value1); |
| 120 | + assertEquals(0, text1.compareTo(text2)); |
| 121 | + } |
| 122 | + |
| 123 | + { |
| 124 | + var text1 = new Text(value1); |
| 125 | + var text2 = new Text(encoded1); |
| 126 | + assertEquals(0, text1.compareTo(text2)); |
| 127 | + } |
| 128 | + |
| 129 | + { |
| 130 | + var text1 = new Text(encoded1); |
| 131 | + var text2 = new Text(encoded1); |
| 132 | + assertEquals(0, text1.compareTo(text2)); |
| 133 | + } |
| 134 | + |
| 135 | + String value2 = randomUnicodeOfLength(randomInt(128)); |
| 136 | + byte[] encodedArr2 = value2.getBytes(StandardCharsets.UTF_8); |
| 137 | + var encoded2 = new XContentString.EncodedBytes(encodedArr2); |
| 138 | + |
| 139 | + int compSign = (int) Math.signum(encoded1.compareTo(encoded2)); |
| 140 | + |
| 141 | + { |
| 142 | + var text1 = new Text(value1); |
| 143 | + var text2 = new Text(value2); |
| 144 | + assertEquals(compSign, (int) Math.signum(text1.compareTo(text2))); |
| 145 | + } |
| 146 | + |
| 147 | + { |
| 148 | + var text1 = new Text(value1); |
| 149 | + var text2 = new Text(encoded2); |
| 150 | + assertEquals(compSign, (int) Math.signum(text1.compareTo(text2))); |
| 151 | + } |
| 152 | + |
| 153 | + { |
| 154 | + var text1 = new Text(encoded1); |
| 155 | + var text2 = new Text(value2); |
| 156 | + assertEquals(compSign, (int) Math.signum(text1.compareTo(text2))); |
| 157 | + } |
| 158 | + |
| 159 | + { |
| 160 | + var text1 = new Text(encoded1); |
| 161 | + var text2 = new Text(encoded2); |
| 162 | + assertEquals(compSign, (int) Math.signum(text1.compareTo(text2))); |
| 163 | + } |
| 164 | + } |
| 165 | + |
| 166 | +} |
0 commit comments