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