Fix possible exception in toCamelCase method

This commit is contained in:
Benjamin Devèze 2014-02-20 23:45:50 +01:00 committed by Adrien Grand
parent bb219eff8d
commit 57fcd761f2
2 changed files with 39 additions and 1 deletions

View File

@ -1425,7 +1425,9 @@ public class Strings {
} }
changed = true; changed = true;
} }
sb.append(Character.toUpperCase(value.charAt(++i))); if (i < value.length() - 1) {
sb.append(Character.toUpperCase(value.charAt(++i)));
}
} else { } else {
if (changed) { if (changed) {
sb.append(c); sb.append(c);

View File

@ -0,0 +1,36 @@
/*
* 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.ElasticsearchTestCase;
import org.junit.Test;
public class StringsTests extends ElasticsearchTestCase {
@Test
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_"));
}
}