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.testing.SerializableTester; 21 22 import junit.framework.TestCase; 23 24 import java.lang.annotation.Retention; 25 import java.lang.annotation.RetentionPolicy; 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 testGetIfPresent() { 44 assertEquals(Optional.of(TestEnum.CHEETO), Enums.getIfPresent(TestEnum.class, "CHEETO")); 45 assertEquals(Optional.of(TestEnum.HONDA), Enums.getIfPresent(TestEnum.class, "HONDA")); 46 assertEquals(Optional.of(TestEnum.POODLE), Enums.getIfPresent(TestEnum.class, "POODLE")); 47 48 assertTrue(Enums.getIfPresent(TestEnum.class, "CHEETO").isPresent()); 49 assertTrue(Enums.getIfPresent(TestEnum.class, "HONDA").isPresent()); 50 assertTrue(Enums.getIfPresent(TestEnum.class, "POODLE").isPresent()); 51 52 assertEquals(TestEnum.CHEETO, Enums.getIfPresent(TestEnum.class, "CHEETO").get()); 53 assertEquals(TestEnum.HONDA, Enums.getIfPresent(TestEnum.class, "HONDA").get()); 54 assertEquals(TestEnum.POODLE, Enums.getIfPresent(TestEnum.class, "POODLE").get()); 55 } 56 57 public void testGetIfPresent_caseSensitive() { 58 assertFalse(Enums.getIfPresent(TestEnum.class, "cHEETO").isPresent()); 59 assertFalse(Enums.getIfPresent(TestEnum.class, "Honda").isPresent()); 60 assertFalse(Enums.getIfPresent(TestEnum.class, "poodlE").isPresent()); 61 } 62 63 public void testGetIfPresent_whenNoMatchingConstant() { 64 assertEquals(Optional.absent(), Enums.getIfPresent(TestEnum.class, "WOMBAT")); 65 } 66 67 // Create a second ClassLoader and use it to get a second version of the TestEnum class. 68 // Run Enums.getIfPresent on that other TestEnum and then return a WeakReference containing the 69 // new ClassLoader. If Enums.getIfPresent does caching that prevents the shadow TestEnum 70 // (and therefore its ClassLoader) from being unloaded, then this WeakReference will never be 71 // cleared. 72 73 public void testStringConverter_convert() { 74 Converter<String, TestEnum> converter = Enums.stringConverter(TestEnum.class); 75 assertEquals(TestEnum.CHEETO, converter.convert("CHEETO")); 76 assertEquals(TestEnum.HONDA, converter.convert("HONDA")); 77 assertEquals(TestEnum.POODLE, converter.convert("POODLE")); 78 assertNull(converter.convert(null)); 79 assertNull(converter.reverse().convert(null)); 80 } 81 82 public void testStringConverter_convertError() { 83 Converter<String, TestEnum> converter = Enums.stringConverter(TestEnum.class); 84 try { 85 converter.convert("xxx"); 86 fail(); 87 } catch (IllegalArgumentException expected) { 88 } 89 } 90 91 public void testStringConverter_reverse() { 92 Converter<String, TestEnum> converter = Enums.stringConverter(TestEnum.class); 93 assertEquals("CHEETO", converter.reverse().convert(TestEnum.CHEETO)); 94 assertEquals("HONDA", converter.reverse().convert(TestEnum.HONDA)); 95 assertEquals("POODLE", converter.reverse().convert(TestEnum.POODLE)); 96 } 97 98 public void testStringConverter_nullConversions() { 99 Converter<String, TestEnum> converter = Enums.stringConverter(TestEnum.class); 100 assertNull(converter.convert(null)); 101 assertNull(converter.reverse().convert(null)); 102 } 103 104 public void testStringConverter_serialization() { 105 SerializableTester.reserializeAndAssert(Enums.stringConverter(TestEnum.class)); 106 } 107 108 @Retention(RetentionPolicy.RUNTIME) 109 private @interface ExampleAnnotation {} 110 111 private enum AnEnum { 112 @ExampleAnnotation FOO, 113 BAR 114 } 115 } 116 117