From: Felix Dörre Date: Sun, 14 Aug 2016 17:20:58 +0000 (+0200) Subject: add: testCase for CountryCode Multiton X-Git-Url: https://code.wpia.club/?p=gigi.git;a=commitdiff_plain;h=d3f2bc843552d3011a341555776fc7a77905ab55 add: testCase for CountryCode Multiton Change-Id: I8cd0088f8b007da41615cfb86c700c62ac7ae927 --- diff --git a/tests/org/cacert/gigi/dbObjects/TestCountryCode.java b/tests/org/cacert/gigi/dbObjects/TestCountryCode.java new file mode 100644 index 00000000..92b169ad --- /dev/null +++ b/tests/org/cacert/gigi/dbObjects/TestCountryCode.java @@ -0,0 +1,97 @@ +package org.cacert.gigi.dbObjects; + +import static org.junit.Assert.*; + +import java.util.Arrays; + +import org.cacert.gigi.GigiApiException; +import org.cacert.gigi.dbObjects.CountryCode.CountryCodeType; +import org.cacert.gigi.testUtils.BusinessTest; +import org.hamcrest.BaseMatcher; +import org.hamcrest.CoreMatchers; +import org.hamcrest.Description; +import org.hamcrest.Matcher; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; +import org.junit.runners.Parameterized.Parameter; +import org.junit.runners.Parameterized.Parameters; + +@RunWith(Parameterized.class) +public class TestCountryCode extends BusinessTest { + + @Parameter(0) + public CountryCodeType type; + + @Parameters(name = "Type: {0}") + public static Iterable getParameters() { + return Arrays.asList(new Object[] { + CountryCodeType.CODE_2_CHARS + }, new Object[] { + CountryCodeType.CODE_3_CHARS + }); + } + + @Test + public void testList() throws GigiApiException { + CountryCode[] ccs = CountryCode.getCountryCodes(type); + for (CountryCode cc : ccs) { + assertSame(type, cc.getCountryCodeType()); + assertThat(cc.getCountryCode(), stringLength(type.getLen())); + } + } + + @Test + public void testFetch() throws GigiApiException { + String ref = type == CountryCodeType.CODE_2_CHARS ? "DE" : "DEU"; + CountryCode cc = CountryCode.getCountryCode(ref, type); + assertEquals(ref, cc.getCountryCode()); + assertEquals("Germany", cc.getCountry()); + } + + @Test + public void testCheck() throws GigiApiException { + String ref = type == CountryCodeType.CODE_2_CHARS ? "DE" : "DEU"; + String reff = type == CountryCodeType.CODE_2_CHARS ? "DF" : "DFU"; + + CountryCode.checkCountryCode(ref, type); + try { + CountryCode.checkCountryCode(reff, type); + } catch (GigiApiException e) { + assertThat(e.getMessage(), CoreMatchers.containsString("was wrong")); + } + + CountryCode.getCountryCode(ref, type); + try { + CountryCode.getCountryCode(reff, type); + } catch (GigiApiException e) { + assertThat(e.getMessage(), CoreMatchers.containsString("was wrong")); + } + } + + @Test + public void testSingleInstance() throws GigiApiException { + String ref = type == CountryCodeType.CODE_2_CHARS ? "DE" : "DEU"; + assertSame(CountryCode.getCountryCode(ref, type), CountryCode.getCountryCode(ref, type)); + } + + private Matcher stringLength(final int len) { + return new BaseMatcher() { + + @Override + public boolean matches(Object s) { + if (s instanceof String) { + return ((String) s).length() == len; + } + return false; + } + + @Override + public void describeTo(Description arg0) { + arg0.appendText("String of length " + len); + } + + }; + } + +}