]> WPIA git - gigi.git/blob - tests/org/cacert/gigi/dbObjects/TestCountryCode.java
92b169ad3ef42441a48c402af1a25c459e4c84d3
[gigi.git] / tests / org / cacert / gigi / dbObjects / TestCountryCode.java
1 package org.cacert.gigi.dbObjects;
2
3 import static org.junit.Assert.*;
4
5 import java.util.Arrays;
6
7 import org.cacert.gigi.GigiApiException;
8 import org.cacert.gigi.dbObjects.CountryCode.CountryCodeType;
9 import org.cacert.gigi.testUtils.BusinessTest;
10 import org.hamcrest.BaseMatcher;
11 import org.hamcrest.CoreMatchers;
12 import org.hamcrest.Description;
13 import org.hamcrest.Matcher;
14 import org.junit.Test;
15 import org.junit.runner.RunWith;
16 import org.junit.runners.Parameterized;
17 import org.junit.runners.Parameterized.Parameter;
18 import org.junit.runners.Parameterized.Parameters;
19
20 @RunWith(Parameterized.class)
21 public class TestCountryCode extends BusinessTest {
22
23     @Parameter(0)
24     public CountryCodeType type;
25
26     @Parameters(name = "Type: {0}")
27     public static Iterable<Object[]> getParameters() {
28         return Arrays.<Object[]>asList(new Object[] {
29                 CountryCodeType.CODE_2_CHARS
30         }, new Object[] {
31                 CountryCodeType.CODE_3_CHARS
32         });
33     }
34
35     @Test
36     public void testList() throws GigiApiException {
37         CountryCode[] ccs = CountryCode.getCountryCodes(type);
38         for (CountryCode cc : ccs) {
39             assertSame(type, cc.getCountryCodeType());
40             assertThat(cc.getCountryCode(), stringLength(type.getLen()));
41         }
42     }
43
44     @Test
45     public void testFetch() throws GigiApiException {
46         String ref = type == CountryCodeType.CODE_2_CHARS ? "DE" : "DEU";
47         CountryCode cc = CountryCode.getCountryCode(ref, type);
48         assertEquals(ref, cc.getCountryCode());
49         assertEquals("Germany", cc.getCountry());
50     }
51
52     @Test
53     public void testCheck() throws GigiApiException {
54         String ref = type == CountryCodeType.CODE_2_CHARS ? "DE" : "DEU";
55         String reff = type == CountryCodeType.CODE_2_CHARS ? "DF" : "DFU";
56
57         CountryCode.checkCountryCode(ref, type);
58         try {
59             CountryCode.checkCountryCode(reff, type);
60         } catch (GigiApiException e) {
61             assertThat(e.getMessage(), CoreMatchers.containsString("was wrong"));
62         }
63
64         CountryCode.getCountryCode(ref, type);
65         try {
66             CountryCode.getCountryCode(reff, type);
67         } catch (GigiApiException e) {
68             assertThat(e.getMessage(), CoreMatchers.containsString("was wrong"));
69         }
70     }
71
72     @Test
73     public void testSingleInstance() throws GigiApiException {
74         String ref = type == CountryCodeType.CODE_2_CHARS ? "DE" : "DEU";
75         assertSame(CountryCode.getCountryCode(ref, type), CountryCode.getCountryCode(ref, type));
76     }
77
78     private Matcher<String> stringLength(final int len) {
79         return new BaseMatcher<String>() {
80
81             @Override
82             public boolean matches(Object s) {
83                 if (s instanceof String) {
84                     return ((String) s).length() == len;
85                 }
86                 return false;
87             }
88
89             @Override
90             public void describeTo(Description arg0) {
91                 arg0.appendText("String of length " + len);
92             }
93
94         };
95     }
96
97 }