|
| 1 | +/* |
| 2 | + * Copyright 2008-present MongoDB, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.mongodb.client.model.expressions; |
| 18 | + |
| 19 | +import org.junit.jupiter.api.Test; |
| 20 | + |
| 21 | +import java.nio.charset.StandardCharsets; |
| 22 | +import java.util.Arrays; |
| 23 | + |
| 24 | +import static com.mongodb.client.model.expressions.Expressions.of; |
| 25 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 26 | + |
| 27 | +@SuppressWarnings({"ConstantConditions"}) |
| 28 | +class StringExpressionsFunctionalTest extends AbstractExpressionsFunctionalTest { |
| 29 | + // https://www.mongodb.com/docs/manual/reference/operator/aggregation/#string-expression-operators |
| 30 | + |
| 31 | + private final String jalapeno = "jalape\u00F1o"; |
| 32 | + private final String sushi = "\u5BFF\u53F8"; |
| 33 | + private final String fish = "\uD83D\uDC1F"; |
| 34 | + |
| 35 | + @Test |
| 36 | + public void literalsTest() { |
| 37 | + assertExpression("", of(""), "''"); |
| 38 | + assertExpression("abc", of("abc"), "'abc'"); |
| 39 | + assertThrows(IllegalArgumentException.class, () -> of((String) null)); |
| 40 | + assertExpression(fish, of(fish), "'" + fish + "'"); |
| 41 | + } |
| 42 | + |
| 43 | + @Test |
| 44 | + public void concatTest() { |
| 45 | + // https://www.mongodb.com/docs/manual/reference/operator/aggregation/concat/ |
| 46 | + assertExpression( |
| 47 | + "abc".concat("de"), |
| 48 | + of("abc").concat(of("de")), |
| 49 | + "{'$concat': ['abc', 'de']}"); |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + public void toLowerTest() { |
| 54 | + // https://www.mongodb.com/docs/manual/reference/operator/aggregation/toLower/ |
| 55 | + assertExpression( |
| 56 | + "ABC".toLowerCase(), |
| 57 | + of("ABC").toLower(), |
| 58 | + "{'$toLower': 'ABC'}"); |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + public void toUpperTest() { |
| 63 | + // https://www.mongodb.com/docs/manual/reference/operator/aggregation/toUpper/ (?) |
| 64 | + assertExpression( |
| 65 | + "abc".toUpperCase(), |
| 66 | + of("abc").toUpper(), |
| 67 | + "{'$toUpper': 'abc'}"); |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + public void strLenTest() { |
| 72 | + // https://www.mongodb.com/docs/manual/reference/operator/aggregation/strLenCP/ (?) |
| 73 | + assertExpression( |
| 74 | + "abc".codePointCount(0, 3), |
| 75 | + of("abc").strLen(), |
| 76 | + "{'$strLenCP': 'abc'}"); |
| 77 | + |
| 78 | + // unicode |
| 79 | + assertExpression( |
| 80 | + jalapeno.codePointCount(0, jalapeno.length()), |
| 81 | + of(jalapeno).strLen(), |
| 82 | + "{'$strLenCP': '" + jalapeno + "'}"); |
| 83 | + assertExpression( |
| 84 | + sushi.codePointCount(0, sushi.length()), |
| 85 | + of(sushi).strLen(), |
| 86 | + "{'$strLenCP': '" + sushi + "'}"); |
| 87 | + assertExpression( |
| 88 | + fish.codePointCount(0, fish.length()), |
| 89 | + of(fish).strLen(), |
| 90 | + "{'$strLenCP': '" + fish + "'}"); |
| 91 | + } |
| 92 | + |
| 93 | + @Test |
| 94 | + public void strLenBytesTest() { |
| 95 | + // https://www.mongodb.com/docs/manual/reference/operator/aggregation/strLenBytes/ (?) |
| 96 | + assertExpression( |
| 97 | + "abc".getBytes(StandardCharsets.UTF_8).length, |
| 98 | + of("abc").strLenBytes(), |
| 99 | + "{'$strLenBytes': 'abc'}"); |
| 100 | + |
| 101 | + // unicode |
| 102 | + assertExpression( |
| 103 | + jalapeno.getBytes(StandardCharsets.UTF_8).length, |
| 104 | + of(jalapeno).strLenBytes(), |
| 105 | + "{'$strLenBytes': '" + jalapeno + "'}"); |
| 106 | + assertExpression( |
| 107 | + sushi.getBytes(StandardCharsets.UTF_8).length, |
| 108 | + of(sushi).strLenBytes(), |
| 109 | + "{'$strLenBytes': '" + sushi + "'}"); |
| 110 | + assertExpression( |
| 111 | + fish.getBytes(StandardCharsets.UTF_8).length, |
| 112 | + of(fish).strLenBytes(), |
| 113 | + "{'$strLenBytes': '" + fish + "'}"); |
| 114 | + |
| 115 | + // comparison |
| 116 | + assertExpression(8, of(jalapeno).strLen()); |
| 117 | + assertExpression(9, of(jalapeno).strLenBytes()); |
| 118 | + assertExpression(2, of(sushi).strLen()); |
| 119 | + assertExpression(6, of(sushi).strLenBytes()); |
| 120 | + assertExpression(1, of(fish).strLen()); |
| 121 | + assertExpression(4, of(fish).strLenBytes()); |
| 122 | + } |
| 123 | + |
| 124 | + @Test |
| 125 | + public void substrTest() { |
| 126 | + // https://www.mongodb.com/docs/manual/reference/operator/aggregation/substr/ |
| 127 | + // https://www.mongodb.com/docs/manual/reference/operator/aggregation/substrCP/ (?) |
| 128 | + // substr is deprecated, an alias for bytes |
| 129 | + assertExpression( |
| 130 | + "abc".substring(1, 1 + 1), |
| 131 | + of("abc").substr(of(1), of(1)), |
| 132 | + "{'$substrCP': ['abc', 1, 1]}"); |
| 133 | + |
| 134 | + // unicode |
| 135 | + assertExpression( |
| 136 | + jalapeno.substring(5, 5 + 3), |
| 137 | + of(jalapeno).substr(of(5), of(3)), |
| 138 | + "{'$substrCP': ['" + jalapeno + "', 5, 3]}"); |
| 139 | + assertExpression( |
| 140 | + "e\u00F1o", |
| 141 | + of(jalapeno).substr(of(5), of(3))); |
| 142 | + |
| 143 | + // bounds; convenience |
| 144 | + assertExpression("abc", of("abc").substr(0, 99)); |
| 145 | + assertExpression("ab", of("abc").substr(0, 2)); |
| 146 | + assertExpression("b", of("abc").substr(1, 1)); |
| 147 | + assertExpression("", of("abc").substr(1, 0)); |
| 148 | + } |
| 149 | + |
| 150 | + @Test |
| 151 | + public void substrBytesTest() { |
| 152 | + // https://www.mongodb.com/docs/manual/reference/operator/aggregation/substrBytes/ (?) |
| 153 | + assertExpression( |
| 154 | + "b", |
| 155 | + of("abc").substrBytes(of(1), of(1)), |
| 156 | + "{'$substrBytes': ['abc', 1, 1]}"); |
| 157 | + |
| 158 | + // unicode |
| 159 | + byte[] bytes = Arrays.copyOfRange(sushi.getBytes(StandardCharsets.UTF_8), 0, 3); |
| 160 | + String expected = new String(bytes, StandardCharsets.UTF_8); |
| 161 | + assertExpression(expected, |
| 162 | + of(sushi).substrBytes(of(0), of(3))); |
| 163 | + // server returns "starting index is a UTF-8 continuation byte" error when substrBytes(1, 1) |
| 164 | + |
| 165 | + // convenience |
| 166 | + assertExpression("b", of("abc").substrBytes(1, 1)); |
| 167 | + } |
| 168 | +} |
0 commit comments