Home | History | Annotate | Download | only in base
      1 /*
      2  * Copyright (C) 2011 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 com.google.common.annotations.GwtCompatible;
     20 import com.google.common.annotations.GwtIncompatible;
     21 import com.google.common.testing.EqualsTester;
     22 import com.google.common.testing.NullPointerTester;
     23 import com.google.common.testing.SerializableTester;
     24 
     25 import junit.framework.TestCase;
     26 
     27 /**
     28  * Tests for {@link Enums}.
     29  *
     30  * @author Steve McKay
     31  */
     32 @GwtCompatible(emulated = true)
     33 public class EnumsTest extends TestCase {
     34 
     35   private enum TestEnum {
     36     CHEETO,
     37     HONDA,
     38     POODLE,
     39   }
     40 
     41   private enum OtherEnum {}
     42 
     43   public void testValueOfFunction() {
     44     Function<String, TestEnum> function = Enums.valueOfFunction(TestEnum.class);
     45     assertEquals(TestEnum.CHEETO, function.apply("CHEETO"));
     46     assertEquals(TestEnum.HONDA, function.apply("HONDA"));
     47     assertEquals(TestEnum.POODLE, function.apply("POODLE"));
     48   }
     49 
     50   public void testValueOfFunction_caseSensitive() {
     51     Function<String, TestEnum> function = Enums.valueOfFunction(TestEnum.class);
     52     assertNull(function.apply("cHEETO"));
     53     assertNull(function.apply("Honda"));
     54     assertNull(function.apply("poodlE"));
     55   }
     56 
     57   public void testValueOfFunction_nullWhenNotMatchingConstant() {
     58     Function<String, TestEnum> function = Enums.valueOfFunction(TestEnum.class);
     59     assertNull(function.apply("WOMBAT"));
     60   }
     61 
     62   public void testValueOfFunction_equals() {
     63     new EqualsTester()
     64         .addEqualityGroup(
     65             Enums.valueOfFunction(TestEnum.class), Enums.valueOfFunction(TestEnum.class))
     66         .addEqualityGroup(Enums.valueOfFunction(OtherEnum.class))
     67         .testEquals();
     68   }
     69 
     70   @GwtIncompatible("SerializableTester")
     71   public void testValueOfFunction_serialization() {
     72     Function<String, TestEnum> function = Enums.valueOfFunction(TestEnum.class);
     73     SerializableTester.reserializeAndAssert(function);
     74   }
     75 
     76   @GwtIncompatible("NullPointerTester")
     77   public void testNullPointerExceptions() throws Exception {
     78     NullPointerTester tester = new NullPointerTester();
     79     tester.testAllPublicStaticMethods(Enums.class);
     80   }
     81 }
     82