This repository has been archived by the owner on Dec 3, 2023. It is now read-only.
/
StringEnumTest.java
128 lines (109 loc) · 3.93 KB
/
StringEnumTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*
* Copyright 2017 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.cloud;
import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import com.google.api.core.ApiFunction;
import com.google.common.testing.EqualsTester;
import java.util.Arrays;
import org.junit.Assert;
import org.junit.Test;
public class StringEnumTest {
public static class Letter extends StringEnumValue {
private static final long serialVersionUID = -1717976087182628526L;
private Letter(String constant) {
super(constant);
}
private static final ApiFunction<String, Letter> CONSTRUCTOR =
new ApiFunction<String, Letter>() {
@Override
public Letter apply(String constant) {
return new Letter(constant);
}
};
private static final StringEnumType<Letter> type =
new StringEnumType(Letter.class, CONSTRUCTOR);
public static final Letter A = type.createAndRegister("A");
public static final Letter B = type.createAndRegister("B");
public static final Letter C = type.createAndRegister("C");
public static Letter valueOfStrict(String constant) {
return type.valueOfStrict(constant);
}
/** Get the StorageClass for the given String constant, and allow unrecognized values. */
public static Letter valueOf(String constant) {
return type.valueOf(constant);
}
/** Return the known values for StorageClass. */
public static Letter[] values() {
return type.values();
}
}
@Test(expected = NullPointerException.class)
public void testNullClass() {
new StringEnumType<Letter>(null, Letter.CONSTRUCTOR);
}
@Test
public void testNullConstructor() {
try {
new StringEnumType<Letter>(Letter.class, null);
Assert.fail();
} catch (NullPointerException ex) {
assertNull(ex.getMessage());
}
}
@Test
public void testEnumInstances() {
assertThat(Letter.A.toString()).isEqualTo("A");
}
@Test
public void testValueOf() {
assertThat(Letter.valueOf("A")).isSameInstanceAs(Letter.A);
assertThat(Letter.valueOf("B")).isSameInstanceAs(Letter.B);
assertThat(Letter.valueOf("C")).isSameInstanceAs(Letter.C);
assertThat(Letter.valueOf("NonExistentLetter").toString()).isEqualTo("NonExistentLetter");
}
@Test
public void testValueOfStrict() {
assertThat(Letter.valueOfStrict("A")).isSameInstanceAs(Letter.A);
assertThat(Letter.valueOfStrict("B")).isSameInstanceAs(Letter.B);
assertThat(Letter.valueOfStrict("C")).isSameInstanceAs(Letter.C);
}
@Test
public void testEquals() {
new EqualsTester()
.addEqualityGroup(Letter.A, Letter.valueOf("A"), Letter.valueOfStrict("A"))
.addEqualityGroup(Letter.B, Letter.valueOf("B"), Letter.valueOfStrict("B"))
.addEqualityGroup(Letter.C, Letter.valueOf("C"), Letter.valueOfStrict("C"))
.addEqualityGroup(Letter.valueOf("NonExistentLetter"), Letter.valueOf("NonExistentLetter"))
.testEquals();
}
@Test
public void testValueOfStrict_invalid() {
try {
Letter.valueOfStrict("NonExistentLetter");
Assert.fail();
} catch (IllegalArgumentException ex) {
assertNotNull(ex.getMessage());
}
}
@Test
public void testValues() {
assertThat(
Arrays.asList(Letter.values()).containsAll(Arrays.asList(Letter.A, Letter.B, Letter.C)))
.isTrue();
}
}