/*
* 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.common;
import org.elasticsearch.test.ESTestCase;
public class StringsTests extends ESTestCase {
public void testToCamelCase() {
assertEquals("foo", Strings.toCamelCase("foo"));
assertEquals("fooBar", Strings.toCamelCase("fooBar"));
assertEquals("FooBar", Strings.toCamelCase("FooBar"));
assertEquals("fooBar", Strings.toCamelCase("foo_bar"));
assertEquals("fooBarFooBar", Strings.toCamelCase("foo_bar_foo_bar"));
assertEquals("fooBar", Strings.toCamelCase("foo_bar_"));
assertEquals("_foo", Strings.toCamelCase("_foo"));
assertEquals("_fooBar", Strings.toCamelCase("_foo_bar_"));
}
public void testSubstring() {
assertEquals(null, Strings.substring(null, 0, 1000));
assertEquals("foo", Strings.substring("foo", 0, 1000));
assertEquals("foo", Strings.substring("foo", 0, 3));
assertEquals("oo", Strings.substring("foo", 1, 3));
assertEquals("oo", Strings.substring("foo", 1, 100));
assertEquals("f", Strings.substring("foo", 0, 1));
}
public void testCleanTruncate() {
assertEquals(null, Strings.cleanTruncate(null, 10));
assertEquals("foo", Strings.cleanTruncate("foo", 10));
assertEquals("foo", Strings.cleanTruncate("foo", 3));
// Throws out high surrogates
assertEquals("foo", Strings.cleanTruncate("foo\uD83D\uDEAB", 4));
// But will keep the whole character
assertEquals("foo\uD83D\uDEAB", Strings.cleanTruncate("foo\uD83D\uDEAB", 5));
/*
* Doesn't take care around combining marks. This example has its
* meaning changed because that last codepoint is supposed to combine
* backwards into the find "o" and be represented as the "o" with a
* circle around it with a slash through it. As in "no 'o's allowed
* here.
*/
assertEquals("o", Strings.cleanTruncate("o\uD83D\uDEAB", 1));
assertEquals("", Strings.cleanTruncate("foo", 0));
}
}