]> WPIA git - gigi.git/blobdiff - 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
diff --git a/tests/club/wpia/gigi/dbObjects/TestCountryCode.java b/tests/club/wpia/gigi/dbObjects/TestCountryCode.java
new file mode 100644 (file)
index 0000000..1a9b1ad
--- /dev/null
@@ -0,0 +1,99 @@
+package club.wpia.gigi.dbObjects;
+
+import static org.junit.Assert.*;
+
+import java.util.Arrays;
+import java.util.List;
+
+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;
+
+import club.wpia.gigi.GigiApiException;
+import club.wpia.gigi.dbObjects.Country;
+import club.wpia.gigi.dbObjects.Country.CountryCodeType;
+import club.wpia.gigi.testUtils.BusinessTest;
+
+@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 {
+        List<Country> ccs = Country.getCountries();
+        for (Country cc : ccs) {
+            assertThat(cc.getCode(type), stringLength(type.getLen()));
+        }
+    }
+
+    @Test
+    public void testFetch() throws GigiApiException {
+        String ref = type == CountryCodeType.CODE_2_CHARS ? "DE" : "DEU";
+        Country cc = Country.getCountryByCode(ref, type);
+        assertEquals(ref, cc.getCode(type));
+        assertEquals("Germany", cc.getName());
+    }
+
+    @Test
+    public void testCheck() throws GigiApiException {
+        String ref = type == CountryCodeType.CODE_2_CHARS ? "DE" : "DEU";
+        String reff = type == CountryCodeType.CODE_2_CHARS ? "DF" : "DFU";
+
+        Country.checkCountryCode(ref, type);
+        try {
+            Country.checkCountryCode(reff, type);
+        } catch (GigiApiException e) {
+            assertThat(e.getMessage(), CoreMatchers.containsString("was wrong"));
+        }
+
+        Country.getCountryByCode(ref, type);
+        try {
+            Country.getCountryByCode(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(Country.getCountryByCode(ref, type), Country.getCountryByCode(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);
+            }
+
+        };
+    }
+
+}