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