View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      https://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.text;
18  
19  import static org.junit.jupiter.api.Assertions.assertEquals;
20  import static org.junit.jupiter.api.Assertions.assertFalse;
21  import static org.junit.jupiter.api.Assertions.assertNotNull;
22  import static org.junit.jupiter.api.Assertions.assertNull;
23  import static org.junit.jupiter.api.Assertions.assertTrue;
24  
25  import java.lang.reflect.Constructor;
26  import java.lang.reflect.Modifier;
27  
28  import org.junit.jupiter.api.Test;
29  
30  /**
31   * Tests {@link CaseUtils} class.
32   */
33  class CaseUtilsTest {
34  
35      @Test
36      void testConstructor() {
37          assertNotNull(new CaseUtils());
38          final Constructor<?>[] cons = CaseUtils.class.getDeclaredConstructors();
39          assertEquals(1, cons.length);
40          assertTrue(Modifier.isPublic(cons[0].getModifiers()));
41          assertTrue(Modifier.isPublic(CaseUtils.class.getModifiers()));
42          assertFalse(Modifier.isFinal(CaseUtils.class.getModifiers()));
43      }
44  
45      @Test
46      void testToCamelCase() {
47          assertNull(CaseUtils.toCamelCase(null, false, null));
48          assertEquals("", CaseUtils.toCamelCase("", true, null));
49          assertEquals("", CaseUtils.toCamelCase("  ", false, null));
50          assertEquals("aBC@def", CaseUtils.toCamelCase("a  b  c  @def", false, null));
51          assertEquals("ABC@def", CaseUtils.toCamelCase("a b c @def", true));
52          assertEquals("ABC@def", CaseUtils.toCamelCase("a b c @def", true, '-'));
53          assertEquals("ABC@def", CaseUtils.toCamelCase("a b c @def", true, '-'));
54  
55          final char[] chars = { '-', '+', ' ', '@' };
56          assertEquals("", CaseUtils.toCamelCase("-+@ ", true, chars));
57          assertEquals("toCamelCase", CaseUtils.toCamelCase("   to-CAMEL-cASE", false, chars));
58          assertEquals("ToCamelCase", CaseUtils.toCamelCase("@@@@   to+CAMEL@cASE ", true, chars));
59          assertEquals("ToCaMeLCase", CaseUtils.toCamelCase("To+CA+ME L@cASE", true, chars));
60  
61          assertEquals("toCamelCase", CaseUtils.toCamelCase("To.Camel.Case", false, '.'));
62          assertEquals("toCamelCase", CaseUtils.toCamelCase("To.Camel-Case", false, '-', '.'));
63          assertEquals("toCamelCase", CaseUtils.toCamelCase(" to @ Camel case", false, '-', '@'));
64          assertEquals("ToCamelCase", CaseUtils.toCamelCase(" @to @ Camel case", true, '-', '@'));
65  
66          assertEquals("ToCamelCase", CaseUtils.toCamelCase("TO CAMEL CASE", true, null));
67          assertEquals("toCamelCase", CaseUtils.toCamelCase("TO CAMEL CASE", false, null));
68          assertEquals("toCamelCase", CaseUtils.toCamelCase("TO CAMEL CASE", false, null));
69          assertEquals("tocamelcase", CaseUtils.toCamelCase("tocamelcase", false, null));
70          assertEquals("Tocamelcase", CaseUtils.toCamelCase("tocamelcase", true, null));
71          assertEquals("tocamelcase", CaseUtils.toCamelCase("Tocamelcase", false, null));
72  
73          assertEquals("Tocamelcase", CaseUtils.toCamelCase("tocamelcase", true));
74          assertEquals("tocamelcase", CaseUtils.toCamelCase("tocamelcase", false));
75  
76          assertEquals("\uD800\uDF00\uD800\uDF02", CaseUtils.toCamelCase("\uD800\uDF00 \uD800\uDF02", true));
77          assertEquals("\uD800\uDF00\uD800\uDF01\uD800\uDF02\uD800\uDF03",
78                  CaseUtils.toCamelCase("\uD800\uDF00\uD800\uDF01\uD800\uDF14\uD800\uDF02\uD800\uDF03", true, '\uD800', '\uDF14'));
79      }
80  }