|
| 1 | +/* |
| 2 | + * Copyright 2017 Google Inc. All Rights Reserved. |
| 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 | +package com.google.cloud; |
| 17 | + |
| 18 | +import static com.google.common.truth.Truth.assertThat; |
| 19 | + |
| 20 | +import com.google.api.core.ApiFunction; |
| 21 | +import com.google.common.testing.EqualsTester; |
| 22 | +import java.util.Arrays; |
| 23 | +import org.junit.Rule; |
| 24 | +import org.junit.Test; |
| 25 | +import org.junit.rules.ExpectedException; |
| 26 | + |
| 27 | +public class StringEnumTest { |
| 28 | + |
| 29 | + @Rule |
| 30 | + public ExpectedException expectedException = ExpectedException.none(); |
| 31 | + |
| 32 | + public static class Letter extends StringEnumValue { |
| 33 | + private Letter(String constant) { |
| 34 | + super(constant); |
| 35 | + } |
| 36 | + |
| 37 | + private static ApiFunction<String, Letter> CONSTRUCTOR = |
| 38 | + new ApiFunction<String, Letter>() { |
| 39 | + @Override |
| 40 | + public Letter apply(String constant) { |
| 41 | + return new Letter(constant); |
| 42 | + } |
| 43 | + }; |
| 44 | + |
| 45 | + private static StringEnumType<Letter> type = new StringEnumType( |
| 46 | + Letter.class, |
| 47 | + CONSTRUCTOR); |
| 48 | + |
| 49 | + public static Letter A = type.createAndRegister("A"); |
| 50 | + public static Letter B = type.createAndRegister("B"); |
| 51 | + public static Letter C = type.createAndRegister("C"); |
| 52 | + |
| 53 | + public static Letter valueOfStrict(String constant) { |
| 54 | + return type.valueOfStrict(constant); |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Get the StorageClass for the given String constant, and allow unrecognized values. |
| 59 | + */ |
| 60 | + public static Letter valueOf(String constant) { |
| 61 | + return type.valueOf(constant); |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Return the known values for StorageClass. |
| 66 | + */ |
| 67 | + public static Letter[] values() { |
| 68 | + return type.values(); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + public void testNullClass() { |
| 74 | + expectedException.expect(NullPointerException.class); |
| 75 | + new StringEnumType<Letter>(null, Letter.CONSTRUCTOR); |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + public void testNullConstructor() { |
| 80 | + expectedException.expect(NullPointerException.class); |
| 81 | + new StringEnumType<Letter>(Letter.class, null); |
| 82 | + } |
| 83 | + |
| 84 | + @Test |
| 85 | + public void testEnumInstances() { |
| 86 | + assertThat(Letter.A.toString()).isEqualTo("A"); |
| 87 | + } |
| 88 | + |
| 89 | + @Test |
| 90 | + public void testValueOf() { |
| 91 | + assertThat(Letter.valueOf("A")).isSameAs(Letter.A); |
| 92 | + assertThat(Letter.valueOf("B")).isSameAs(Letter.B); |
| 93 | + assertThat(Letter.valueOf("C")).isSameAs(Letter.C); |
| 94 | + assertThat(Letter.valueOf("NonExistentLetter").toString()).isEqualTo("NonExistentLetter"); |
| 95 | + } |
| 96 | + |
| 97 | + @Test |
| 98 | + public void testValueOfStrict() { |
| 99 | + assertThat(Letter.valueOfStrict("A")).isSameAs(Letter.A); |
| 100 | + assertThat(Letter.valueOfStrict("B")).isSameAs(Letter.B); |
| 101 | + assertThat(Letter.valueOfStrict("C")).isSameAs(Letter.C); |
| 102 | + } |
| 103 | + |
| 104 | + @Test |
| 105 | + public void testEquals() { |
| 106 | + EqualsTester tester = new EqualsTester(); |
| 107 | + |
| 108 | + tester.addEqualityGroup(Letter.A, Letter.valueOf("A"), Letter.valueOfStrict("A")); |
| 109 | + tester.addEqualityGroup(Letter.B, Letter.valueOf("B"), Letter.valueOfStrict("B")); |
| 110 | + tester.addEqualityGroup(Letter.C, Letter.valueOf("C"), Letter.valueOfStrict("C")); |
| 111 | + tester |
| 112 | + .addEqualityGroup(Letter.valueOf("NonExistentLetter"), Letter.valueOf("NonExistentLetter")); |
| 113 | + } |
| 114 | + |
| 115 | + @Test |
| 116 | + public void testValueOfStrict_invalid() { |
| 117 | + expectedException.expect(IllegalArgumentException.class); |
| 118 | + Letter.valueOfStrict("NonExistentLetter"); |
| 119 | + } |
| 120 | + |
| 121 | + @Test |
| 122 | + public void testValues() { |
| 123 | + assertThat( |
| 124 | + Arrays.asList(Letter.values()).containsAll(Arrays.asList(Letter.A, Letter.B, Letter.C))); |
| 125 | + } |
| 126 | +} |
0 commit comments