1 /* 2 * Copyright (C) 2006 The Guava Authors 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.google.common.base; 18 19 import static com.google.common.base.CaseFormat.LOWER_CAMEL; 20 import static com.google.common.base.CaseFormat.LOWER_HYPHEN; 21 import static com.google.common.base.CaseFormat.LOWER_UNDERSCORE; 22 import static com.google.common.base.CaseFormat.UPPER_CAMEL; 23 import static com.google.common.base.CaseFormat.UPPER_UNDERSCORE; 24 25 import com.google.common.annotations.GwtCompatible; 26 import com.google.common.annotations.GwtIncompatible; 27 import com.google.common.testing.NullPointerTester; 28 import com.google.common.testing.SerializableTester; 29 import junit.framework.TestCase; 30 31 /** 32 * Unit test for {@link CaseFormat}. 33 * 34 * @author Mike Bostock 35 */ 36 @GwtCompatible(emulated = true) 37 public class CaseFormatTest extends TestCase { 38 testIdentity()39 public void testIdentity() { 40 for (CaseFormat from : CaseFormat.values()) { 41 assertSame(from + " to " + from, "foo", from.to(from, "foo")); 42 for (CaseFormat to : CaseFormat.values()) { 43 assertEquals(from + " to " + to, "", from.to(to, "")); 44 assertEquals(from + " to " + to, " ", from.to(to, " ")); 45 } 46 } 47 } 48 49 @GwtIncompatible // NullPointerTester testNullArguments()50 public void testNullArguments() { 51 NullPointerTester tester = new NullPointerTester(); 52 tester.testAllPublicStaticMethods(CaseFormat.class); 53 for (CaseFormat format : CaseFormat.values()) { 54 tester.testAllPublicInstanceMethods(format); 55 } 56 } 57 testLowerHyphenToLowerHyphen()58 public void testLowerHyphenToLowerHyphen() { 59 assertEquals("foo", LOWER_HYPHEN.to(LOWER_HYPHEN, "foo")); 60 assertEquals("foo-bar", LOWER_HYPHEN.to(LOWER_HYPHEN, "foo-bar")); 61 } 62 testLowerHyphenToLowerUnderscore()63 public void testLowerHyphenToLowerUnderscore() { 64 assertEquals("foo", LOWER_HYPHEN.to(LOWER_UNDERSCORE, "foo")); 65 assertEquals("foo_bar", LOWER_HYPHEN.to(LOWER_UNDERSCORE, "foo-bar")); 66 } 67 testLowerHyphenToLowerCamel()68 public void testLowerHyphenToLowerCamel() { 69 assertEquals("foo", LOWER_HYPHEN.to(LOWER_CAMEL, "foo")); 70 assertEquals("fooBar", LOWER_HYPHEN.to(LOWER_CAMEL, "foo-bar")); 71 } 72 testLowerHyphenToUpperCamel()73 public void testLowerHyphenToUpperCamel() { 74 assertEquals("Foo", LOWER_HYPHEN.to(UPPER_CAMEL, "foo")); 75 assertEquals("FooBar", LOWER_HYPHEN.to(UPPER_CAMEL, "foo-bar")); 76 } 77 testLowerHyphenToUpperUnderscore()78 public void testLowerHyphenToUpperUnderscore() { 79 assertEquals("FOO", LOWER_HYPHEN.to(UPPER_UNDERSCORE, "foo")); 80 assertEquals("FOO_BAR", LOWER_HYPHEN.to(UPPER_UNDERSCORE, "foo-bar")); 81 } 82 testLowerUnderscoreToLowerHyphen()83 public void testLowerUnderscoreToLowerHyphen() { 84 assertEquals("foo", LOWER_UNDERSCORE.to(LOWER_HYPHEN, "foo")); 85 assertEquals("foo-bar", LOWER_UNDERSCORE.to(LOWER_HYPHEN, "foo_bar")); 86 } 87 testLowerUnderscoreToLowerUnderscore()88 public void testLowerUnderscoreToLowerUnderscore() { 89 assertEquals("foo", LOWER_UNDERSCORE.to(LOWER_UNDERSCORE, "foo")); 90 assertEquals("foo_bar", LOWER_UNDERSCORE.to(LOWER_UNDERSCORE, "foo_bar")); 91 } 92 testLowerUnderscoreToLowerCamel()93 public void testLowerUnderscoreToLowerCamel() { 94 assertEquals("foo", LOWER_UNDERSCORE.to(LOWER_CAMEL, "foo")); 95 assertEquals("fooBar", LOWER_UNDERSCORE.to(LOWER_CAMEL, "foo_bar")); 96 } 97 testLowerUnderscoreToUpperCamel()98 public void testLowerUnderscoreToUpperCamel() { 99 assertEquals("Foo", LOWER_UNDERSCORE.to(UPPER_CAMEL, "foo")); 100 assertEquals("FooBar", LOWER_UNDERSCORE.to(UPPER_CAMEL, "foo_bar")); 101 } 102 testLowerUnderscoreToUpperUnderscore()103 public void testLowerUnderscoreToUpperUnderscore() { 104 assertEquals("FOO", LOWER_UNDERSCORE.to(UPPER_UNDERSCORE, "foo")); 105 assertEquals("FOO_BAR", LOWER_UNDERSCORE.to(UPPER_UNDERSCORE, "foo_bar")); 106 } 107 testLowerCamelToLowerHyphen()108 public void testLowerCamelToLowerHyphen() { 109 assertEquals("foo", LOWER_CAMEL.to(LOWER_HYPHEN, "foo")); 110 assertEquals("foo-bar", LOWER_CAMEL.to(LOWER_HYPHEN, "fooBar")); 111 assertEquals("h-t-t-p", LOWER_CAMEL.to(LOWER_HYPHEN, "HTTP")); 112 } 113 testLowerCamelToLowerUnderscore()114 public void testLowerCamelToLowerUnderscore() { 115 assertEquals("foo", LOWER_CAMEL.to(LOWER_UNDERSCORE, "foo")); 116 assertEquals("foo_bar", LOWER_CAMEL.to(LOWER_UNDERSCORE, "fooBar")); 117 assertEquals("h_t_t_p", LOWER_CAMEL.to(LOWER_UNDERSCORE, "hTTP")); 118 } 119 testLowerCamelToLowerCamel()120 public void testLowerCamelToLowerCamel() { 121 assertEquals("foo", LOWER_CAMEL.to(LOWER_CAMEL, "foo")); 122 assertEquals("fooBar", LOWER_CAMEL.to(LOWER_CAMEL, "fooBar")); 123 } 124 testLowerCamelToUpperCamel()125 public void testLowerCamelToUpperCamel() { 126 assertEquals("Foo", LOWER_CAMEL.to(UPPER_CAMEL, "foo")); 127 assertEquals("FooBar", LOWER_CAMEL.to(UPPER_CAMEL, "fooBar")); 128 assertEquals("HTTP", LOWER_CAMEL.to(UPPER_CAMEL, "hTTP")); 129 } 130 testLowerCamelToUpperUnderscore()131 public void testLowerCamelToUpperUnderscore() { 132 assertEquals("FOO", LOWER_CAMEL.to(UPPER_UNDERSCORE, "foo")); 133 assertEquals("FOO_BAR", LOWER_CAMEL.to(UPPER_UNDERSCORE, "fooBar")); 134 } 135 testUpperCamelToLowerHyphen()136 public void testUpperCamelToLowerHyphen() { 137 assertEquals("foo", UPPER_CAMEL.to(LOWER_HYPHEN, "Foo")); 138 assertEquals("foo-bar", UPPER_CAMEL.to(LOWER_HYPHEN, "FooBar")); 139 } 140 testUpperCamelToLowerUnderscore()141 public void testUpperCamelToLowerUnderscore() { 142 assertEquals("foo", UPPER_CAMEL.to(LOWER_UNDERSCORE, "Foo")); 143 assertEquals("foo_bar", UPPER_CAMEL.to(LOWER_UNDERSCORE, "FooBar")); 144 } 145 testUpperCamelToLowerCamel()146 public void testUpperCamelToLowerCamel() { 147 assertEquals("foo", UPPER_CAMEL.to(LOWER_CAMEL, "Foo")); 148 assertEquals("fooBar", UPPER_CAMEL.to(LOWER_CAMEL, "FooBar")); 149 assertEquals("hTTP", UPPER_CAMEL.to(LOWER_CAMEL, "HTTP")); 150 } 151 testUpperCamelToUpperCamel()152 public void testUpperCamelToUpperCamel() { 153 assertEquals("Foo", UPPER_CAMEL.to(UPPER_CAMEL, "Foo")); 154 assertEquals("FooBar", UPPER_CAMEL.to(UPPER_CAMEL, "FooBar")); 155 } 156 testUpperCamelToUpperUnderscore()157 public void testUpperCamelToUpperUnderscore() { 158 assertEquals("FOO", UPPER_CAMEL.to(UPPER_UNDERSCORE, "Foo")); 159 assertEquals("FOO_BAR", UPPER_CAMEL.to(UPPER_UNDERSCORE, "FooBar")); 160 assertEquals("H_T_T_P", UPPER_CAMEL.to(UPPER_UNDERSCORE, "HTTP")); 161 assertEquals("H__T__T__P", UPPER_CAMEL.to(UPPER_UNDERSCORE, "H_T_T_P")); 162 } 163 testUpperUnderscoreToLowerHyphen()164 public void testUpperUnderscoreToLowerHyphen() { 165 assertEquals("foo", UPPER_UNDERSCORE.to(LOWER_HYPHEN, "FOO")); 166 assertEquals("foo-bar", UPPER_UNDERSCORE.to(LOWER_HYPHEN, "FOO_BAR")); 167 } 168 testUpperUnderscoreToLowerUnderscore()169 public void testUpperUnderscoreToLowerUnderscore() { 170 assertEquals("foo", UPPER_UNDERSCORE.to(LOWER_UNDERSCORE, "FOO")); 171 assertEquals("foo_bar", UPPER_UNDERSCORE.to(LOWER_UNDERSCORE, "FOO_BAR")); 172 } 173 testUpperUnderscoreToLowerCamel()174 public void testUpperUnderscoreToLowerCamel() { 175 assertEquals("foo", UPPER_UNDERSCORE.to(LOWER_CAMEL, "FOO")); 176 assertEquals("fooBar", UPPER_UNDERSCORE.to(LOWER_CAMEL, "FOO_BAR")); 177 } 178 testUpperUnderscoreToUpperCamel()179 public void testUpperUnderscoreToUpperCamel() { 180 assertEquals("Foo", UPPER_UNDERSCORE.to(UPPER_CAMEL, "FOO")); 181 assertEquals("FooBar", UPPER_UNDERSCORE.to(UPPER_CAMEL, "FOO_BAR")); 182 assertEquals("HTTP", UPPER_UNDERSCORE.to(UPPER_CAMEL, "H_T_T_P")); 183 } 184 testUpperUnderscoreToUpperUnderscore()185 public void testUpperUnderscoreToUpperUnderscore() { 186 assertEquals("FOO", UPPER_UNDERSCORE.to(UPPER_UNDERSCORE, "FOO")); 187 assertEquals("FOO_BAR", UPPER_UNDERSCORE.to(UPPER_UNDERSCORE, "FOO_BAR")); 188 } 189 testConverterToForward()190 public void testConverterToForward() { 191 assertEquals("FooBar", UPPER_UNDERSCORE.converterTo(UPPER_CAMEL).convert("FOO_BAR")); 192 assertEquals("fooBar", UPPER_UNDERSCORE.converterTo(LOWER_CAMEL).convert("FOO_BAR")); 193 assertEquals("FOO_BAR", UPPER_CAMEL.converterTo(UPPER_UNDERSCORE).convert("FooBar")); 194 assertEquals("FOO_BAR", LOWER_CAMEL.converterTo(UPPER_UNDERSCORE).convert("fooBar")); 195 } 196 testConverterToBackward()197 public void testConverterToBackward() { 198 assertEquals("FOO_BAR", UPPER_UNDERSCORE.converterTo(UPPER_CAMEL).reverse().convert("FooBar")); 199 assertEquals("FOO_BAR", UPPER_UNDERSCORE.converterTo(LOWER_CAMEL).reverse().convert("fooBar")); 200 assertEquals("FooBar", UPPER_CAMEL.converterTo(UPPER_UNDERSCORE).reverse().convert("FOO_BAR")); 201 assertEquals("fooBar", LOWER_CAMEL.converterTo(UPPER_UNDERSCORE).reverse().convert("FOO_BAR")); 202 } 203 testConverter_nullConversions()204 public void testConverter_nullConversions() { 205 for (CaseFormat outer : CaseFormat.values()) { 206 for (CaseFormat inner : CaseFormat.values()) { 207 assertNull(outer.converterTo(inner).convert(null)); 208 assertNull(outer.converterTo(inner).reverse().convert(null)); 209 } 210 } 211 } 212 testConverter_toString()213 public void testConverter_toString() { 214 assertEquals( 215 "LOWER_HYPHEN.converterTo(UPPER_CAMEL)", LOWER_HYPHEN.converterTo(UPPER_CAMEL).toString()); 216 } 217 testConverter_serialization()218 public void testConverter_serialization() { 219 for (CaseFormat outer : CaseFormat.values()) { 220 for (CaseFormat inner : CaseFormat.values()) { 221 SerializableTester.reserializeAndAssert(outer.converterTo(inner)); 222 } 223 } 224 } 225 } 226