]> WPIA git - gigi.git/blob - tests/org/cacert/gigi/dbObjects/TestCountryCode.java
fix: ResultSet.getDate is often wrong as it fetches day-precision times
[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 import java.util.List;
7
8 import org.cacert.gigi.GigiApiException;
9 import org.cacert.gigi.dbObjects.Country.CountryCodeType;
10 import org.cacert.gigi.testUtils.BusinessTest;
11 import org.hamcrest.BaseMatcher;
12 import org.hamcrest.CoreMatchers;
13 import org.hamcrest.Description;
14 import org.hamcrest.Matcher;
15 import org.junit.Test;
16 import org.junit.runner.RunWith;
17 import org.junit.runners.Parameterized;
18 import org.junit.runners.Parameterized.Parameter;
19 import org.junit.runners.Parameterized.Parameters;
20
21 @RunWith(Parameterized.class)
22 public class TestCountryCode extends BusinessTest {
23
24     @Parameter(0)
25     public CountryCodeType type;
26
27     @Parameters(name = "Type: {0}")
28     public static Iterable<Object[]> getParameters() {
29         return Arrays.<Object[]>asList(new Object[] {
30                 CountryCodeType.CODE_2_CHARS
31         }, new Object[] {
32                 CountryCodeType.CODE_3_CHARS
33         });
34     }
35
36     @Test
37     public void testList() throws GigiApiException {
38         List<Country> ccs = Country.getCountries();
39         for (Country cc : ccs) {
40             assertThat(cc.getCode(type), 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         Country cc = Country.getCountryByCode(ref, type);
48         assertEquals(ref, cc.getCode(type));
49         assertEquals("Germany", cc.getName());
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         Country.checkCountryCode(ref, type);
58         try {
59             Country.checkCountryCode(reff, type);
60         } catch (GigiApiException e) {
61             assertThat(e.getMessage(), CoreMatchers.containsString("was wrong"));
62         }
63
64         Country.getCountryByCode(ref, type);
65         try {
66             Country.getCountryByCode(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(Country.getCountryByCode(ref, type), Country.getCountryByCode(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 }