package nl.hsac.fitnesse.util;

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

import static org.junit.Assert.assertTrue;

/**
 * Tests RandomPostalCodeGenerator.
 * The postal codes are checked on length only to validate the switch
 */
public class RandomPostalCodeGeneratorTest {
    private final RandomPostalCodeGenerator postalCode = new RandomPostalCodeGenerator();

    @Rule
    public final ExpectedException exception = ExpectedException.none();

    private boolean resultEqualsLength(String result, int length) {
        return result.length() == length;
    }

    @Test
    public void testRandomPostalCodeAustralia() {
        for (int i = 0; i < 100; i++) {
            testCountryCodeToLength("AU", 4);
        }
    }

    @Test
    public void testRandomPostalCodeBelgium() {
        for (int i = 0; i < 100; i++) {
            testCountryCodeToLength("BE", 4);
        }
    }

    @Test
    public void testRandomPostalCodeBrazil() {
        for (int i = 0; i < 100; i++) {
            testCountryCodeToTwoLengths("BR", 8, 9);
        }
    }

    @Test
    public void testRandomPostalCanada() {
        for (int i = 0; i < 100; i++) {
            testCountryCodeToTwoLengths("CA", 6, 7);
        }
    }

    @Test
    public void testRandomPostalCodeFrance() {
        for (int i = 0; i < 100; i++) {
            testCountryCodeToLength("FR", 5);
        }
    }

    @Test
    public void testRandomPostalCodeGermany() {
        for (int i = 0; i < 100; i++) {
            testCountryCodeToLength("DE", 5);
        }
    }

    @Test
    public void testRandomPostalCodeGreenland() {
        for (int i = 0; i < 100; i++) {
            testCountryCodeToLength("GL", 4);
        }
    }

    @Test
    public void testRandomPostalCodeItaly() {
        for (int i = 0; i < 100; i++) {
            testCountryCodeToLength("IT", 5);
        }
    }

    @Test
    public void testRandomPostalCodeNetherlands() {
        for (int i = 0; i < 100; i++) {
            testCountryCodeToLength("NL", 6);
        }
    }

    @Test
    public void testRandomPostalCodeNorway() {
        for (int i = 0; i < 100; i++) {
            testCountryCodeToLength("NO", 4);
        }
    }

    @Test
    public void testRandomPostalCodeUnitedStates() {
        for (int i = 0; i < 100; i++) {
            testCountryCodeToTwoLengths("US", 5, 10);
        }
    }

    private void testCountryCodeToLength(String countryCode, int length) {
        String result = postalCode.getRandomPostalCodeString(countryCode);
        assertTrue("Got: " + result, resultEqualsLength(result, length));
    }

    private void testCountryCodeToTwoLengths(String countryCode, int length1, int length2) {
        String result = postalCode.getRandomPostalCodeString(countryCode);
        assertTrue("Got: " + result, resultEqualsLength(result, length1) || resultEqualsLength(result, length2));
    }
}
@@ -1002,72 +1002,17 @@
     
-for (int i = 0; i %3C 100; i++) %7B%0A            testCountryCodeToTwo
+checkCode
 Leng
@@ -1028,26 +1028,16 @@
  8, 9);%0A
-        %7D%0A
     %7D%0A%0A 
@@ -1161,27 +1161,20 @@
 deTo
-Two
 Length
-s
 (%22CA%22,
- 6,
  7);
@@ -2234,72 +2234,17 @@
     
-for (int i = 0; i %3C 100; i++) %7B%0A            testCountryCodeToTwo
+checkCode
 Leng
@@ -2261,26 +2261,16 @@
 5, 10);%0A
-        %7D%0A
     %7D%0A%0A 
@@ -2508,32 +2508,645 @@
    private void 
+checkCodeLengths(String countryCode, int length1, int length2) %7B%0A        int length1Count = 0;%0A        int length2Count = 0;%0A        for (int i = 0; i %3C 100; i++) %7B%0A            int length = testCountryCodeToTwoLengths(countryCode, length1, length2);%0A            if (length == length1) %7B%0A                length1Count++;%0A            %7D else %7B%0A                length2Count++;%0A            %7D%0A        %7D%0A        assertTrue(countryCode + %22: too few length1 codes: %22 + length1Count, length1Count %3E 30);%0A        assertTrue(countryCode + %22: too few length2 codes: %22 + length2Count, length2Count %3E 30);%0A    %7D%0A%0A    private int 
 testCountryCodeT
@@ -3292,36 +3292,28 @@
     
-assertTrue(%22Got: %22 + result,
+boolean hasLength1 =
  res
@@ -3348,11 +3348,38 @@
 th1)
- %7C%7C
+;%0A        boolean hasLength2 =
  res
@@ -3410,17 +3410,127 @@
 length2)
-)
+;%0A        assertTrue(%22Got: %22 + result, hasLength1 %7C%7C hasLength2);%0A        return hasLength1 ? length1 : length2
 ;%0A    %7D%0A

Add check that not all codes have same lengths, if two lengths are permitted (all CA codes have length 7)