Skip to content

Commit d6f19ab

Browse files
Adding unit tests
1 parent 6db00d3 commit d6f19ab

3 files changed

Lines changed: 148 additions & 9 deletions

File tree

google-cloud-core/src/main/java/com/google/cloud/StringEnumType.java

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@
1717

1818
import com.google.api.core.ApiFunction;
1919
import com.google.api.core.InternalApi;
20-
import java.util.HashMap;
20+
import com.google.common.base.Preconditions;
21+
import java.lang.reflect.Array;
22+
import java.util.Collection;
23+
import java.util.LinkedHashMap;
2124
import java.util.Map;
2225

2326
/**
@@ -27,13 +30,13 @@
2730
@InternalApi
2831
public class StringEnumType<EnumT> {
2932

30-
private final String enumName;
33+
private final Class<EnumT> clazz;
3134
private final ApiFunction<String, EnumT> constructor;
32-
private Map<String, EnumT> knownValues = new HashMap<>();
35+
private final Map<String, EnumT> knownValues = new LinkedHashMap<>();
3336

34-
public StringEnumType(String enumName, ApiFunction<String, EnumT> constructor) {
35-
this.enumName = enumName;
36-
this.constructor = constructor;
37+
public StringEnumType(Class<EnumT> clazz, ApiFunction<String, EnumT> constructor) {
38+
this.clazz = Preconditions.checkNotNull(clazz);
39+
this.constructor = Preconditions.checkNotNull(constructor);
3740
}
3841

3942
/**
@@ -55,7 +58,7 @@ public EnumT valueOfStrict(String constant) {
5558
return value;
5659
} else {
5760
throw new IllegalArgumentException(
58-
"Constant \"" + constant + "\" not found for enum \"" + enumName + "\"");
61+
"Constant \"" + constant + "\" not found for enum \"" + clazz.getName() + "\"");
5962
}
6063
}
6164

@@ -75,6 +78,16 @@ public EnumT valueOf(String constant) {
7578
* Return the known values of this enum type.
7679
*/
7780
public EnumT[] values() {
78-
return (EnumT[]) knownValues.values().toArray();
81+
Collection<EnumT> valueCollection = knownValues.values();
82+
83+
@SuppressWarnings("unchecked")
84+
final EnumT[] valueArray = (EnumT[]) Array.newInstance(clazz, valueCollection.size());
85+
int i = 0;
86+
for (EnumT enumV : valueCollection) {
87+
valueArray[i] = enumV;
88+
i++;
89+
}
90+
91+
return valueArray;
7992
}
8093
}
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
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+
}

google-cloud-storage/src/main/java/com/google/cloud/storage/StorageClass.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public StorageClass apply(String constant) {
3838
};
3939

4040
private static StringEnumType<StorageClass> type = new StringEnumType(
41-
StorageClass.class.getName(),
41+
StorageClass.class,
4242
CONSTRUCTOR);
4343

4444
/**

0 commit comments

Comments
 (0)