1 package org.cacert.gigi.dbObjects;
3 import static org.junit.Assert.*;
5 import java.util.Arrays;
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;
21 @RunWith(Parameterized.class)
22 public class TestCountryCode extends BusinessTest {
25 public CountryCodeType type;
27 @Parameters(name = "Type: {0}")
28 public static Iterable<Object[]> getParameters() {
29 return Arrays.<Object[]>asList(new Object[] {
30 CountryCodeType.CODE_2_CHARS
32 CountryCodeType.CODE_3_CHARS
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()));
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());
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";
57 Country.checkCountryCode(ref, type);
59 Country.checkCountryCode(reff, type);
60 } catch (GigiApiException e) {
61 assertThat(e.getMessage(), CoreMatchers.containsString("was wrong"));
64 Country.getCountryByCode(ref, type);
66 Country.getCountryByCode(reff, type);
67 } catch (GigiApiException e) {
68 assertThat(e.getMessage(), CoreMatchers.containsString("was wrong"));
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));
78 private Matcher<String> stringLength(final int len) {
79 return new BaseMatcher<String>() {
82 public boolean matches(Object s) {
83 if (s instanceof String) {
84 return ((String) s).length() == len;
90 public void describeTo(Description arg0) {
91 arg0.appendText("String of length " + len);