1 package org.cacert.gigi.dbObjects;
3 import static org.junit.Assert.*;
5 import java.util.Arrays;
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;
20 @RunWith(Parameterized.class)
21 public class TestCountryCode extends BusinessTest {
24 public CountryCodeType type;
26 @Parameters(name = "Type: {0}")
27 public static Iterable<Object[]> getParameters() {
28 return Arrays.<Object[]>asList(new Object[] {
29 CountryCodeType.CODE_2_CHARS
31 CountryCodeType.CODE_3_CHARS
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()));
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());
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 CountryCode.checkCountryCode(ref, type);
59 CountryCode.checkCountryCode(reff, type);
60 } catch (GigiApiException e) {
61 assertThat(e.getMessage(), CoreMatchers.containsString("was wrong"));
64 CountryCode.getCountryCode(ref, type);
66 CountryCode.getCountryCode(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(CountryCode.getCountryCode(ref, type), CountryCode.getCountryCode(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);