]> WPIA git - gigi.git/commitdiff
add: testCase for CountryCode Multiton
authorFelix Dörre <felix@dogcraft.de>
Sun, 14 Aug 2016 17:20:58 +0000 (19:20 +0200)
committerFelix Dörre <felix@dogcraft.de>
Tue, 16 Aug 2016 18:23:08 +0000 (20:23 +0200)
Change-Id: I8cd0088f8b007da41615cfb86c700c62ac7ae927

tests/org/cacert/gigi/dbObjects/TestCountryCode.java [new file with mode: 0644]

diff --git a/tests/org/cacert/gigi/dbObjects/TestCountryCode.java b/tests/org/cacert/gigi/dbObjects/TestCountryCode.java
new file mode 100644 (file)
index 0000000..92b169a
--- /dev/null
@@ -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<Object[]> getParameters() {
+        return Arrays.<Object[]>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<String> stringLength(final int len) {
+        return new BaseMatcher<String>() {
+
+            @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);
+            }
+
+        };
+    }
+
+}