1 package com.atlassian.marketplace.client.api;
2
3 import com.atlassian.marketplace.client.api.EnumWithKey;
4
5 import org.junit.Test;
6
7 import static com.atlassian.fugue.Option.none;
8 import static com.atlassian.fugue.Option.some;
9 import static org.hamcrest.MatcherAssert.assertThat;
10 import static org.hamcrest.Matchers.equalTo;
11
12 public class EnumWithKeyTest
13 {
14 @Test
15 public void canGetAllValues()
16 {
17 assertThat(EnumWithKey.Parser.forType(MyEnum.class).getValues(),
18 equalTo(new MyEnum[] { MyEnum.LITTLE, MyEnum.BIG }));
19 }
20
21 @Test
22 public void valueForKeyMatchesKey()
23 {
24 assertThat(EnumWithKey.Parser.forType(MyEnum.class).valueForKey("big"),
25 equalTo(some(MyEnum.BIG)));
26 }
27
28 @Test
29 public void valueForKeyMatchesKeyCaseInsensitively()
30 {
31 assertThat(EnumWithKey.Parser.forType(MyEnum.class).valueForKey("BiG"),
32 equalTo(some(MyEnum.BIG)));
33 }
34
35 @Test
36 public void valueForKeyReturnsNoneIfNotFound()
37 {
38 assertThat(EnumWithKey.Parser.forType(MyEnum.class).valueForKey("medium"),
39 equalTo(none(MyEnum.class)));
40 }
41
42 @SuppressWarnings("unchecked")
43 @Test(expected = IllegalStateException.class)
44 public void cannotGetParserForUnsupportedEnum()
45 {
46 Class<?> badClassObscuredByTypeErasure = EnumWithoutKey.class;
47 EnumWithKey.Parser.forType((Class<? extends EnumWithKey>) badClassObscuredByTypeErasure);
48 }
49
50 private static enum MyEnum implements EnumWithKey
51 {
52 LITTLE("little"),
53 BIG("big");
54
55 private final String key;
56
57 private MyEnum(String key)
58 {
59 this.key = key;
60 }
61
62 public String getKey()
63 {
64 return key;
65 }
66 }
67
68 private static enum EnumWithoutKey
69 {
70 RED,
71 GREEN;
72 }
73 }