package uk.co.ourfriendirony.medianotifier.general;


import org.junit.Test;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.GregorianCalendar;

import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.fail;

public class StringHandlerTest {
    private DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

    @Test
    public void cleanUrlEncodesSpaces() {
        assertEquals(
                "simple+url",
                StringHandler.cleanUrl("simple url")
        );
    }

    @Test
    public void cleanTitleReversesTheAndA() {
        assertEquals(
                "Simple Test, The",
                StringHandler.cleanTitle("The Simple Test")
        );
        assertEquals(
                "Simple Test, A",
                StringHandler.cleanTitle("A Simple Test")
        );
        assertEquals(
                "Simple Test",
                StringHandler.cleanTitle("Simple Test")
        );
    }

    @Test
    public void canConvertDateToString() {
        Date date = new GregorianCalendar(2001, 0, 1).getTime();
        assertEquals(
                "2001-01-01",
                StringHandler.dateToString(date)
        );
    }

    @Test
    public void canHandleNullDateToString() {
        assertEquals(
                "",
                StringHandler.dateToString(null)
        );
    }

    @Test
    public void canHandleStringToDate() {
        String date = "2001-01-01";
        try {
            Date actualDate = dateFormat.parse(date);
            assertEquals(
                    actualDate,
                    StringHandler.stringToDate(date)
            );
        } catch (ParseException e) {
            fail(e.getMessage());
        }
    }

    @Test
    public void canPadNumberWithLeadingZeros() {
        assertEquals(
                "0000003",
                StringHandler.pad(3, 7)
        );
        assertEquals(
                "00000000123",
                StringHandler.pad(123, 11)
        );
    }

    @Test
    public void canReplaceSingleTokens() {
        String token = "@WIBBLE@";
        String value = "expected";
        String initialString = "this is my @WIBBLE@ string. string values should be @WIBBLE@";

        assertEquals(
                "this is my expected string. string values should be expected",
                StringHandler.replaceTokens(initialString, token, value)
        );
    }

    @Test
    public void canReplaceAllTokens() {
        String[] tokens = {"@WIBBLE@", "@WOBBLE@"};
        String[] values = {"expected", "string"};
        String initialString = "this is my @WIBBLE@ @WOBBLE@. @WOBBLE@ values should be @WIBBLE@";

        assertEquals(
                "this is my expected string. string values should be expected",
                StringHandler.replaceTokens(initialString, tokens, values)
        );
    }

    @Test
    public void canGetStringValueOfInteger() {
        assertEquals(
                "1",
                StringHandler.getNotificationNumber(1)
        );
        assertEquals(
                "7",
                StringHandler.getNotificationNumber(7)
        );
        assertEquals(
                "9",
                StringHandler.getNotificationNumber(9)
        );
    }

    @Test
    public void canGetDefinedStringValueIfIntegerGreaterThanTen() {
        assertEquals(
                "9+",
                StringHandler.getNotificationNumber(10)
        );
        assertEquals(
                "9+",
                StringHandler.getNotificationNumber(20)
        );
    }
}

@@ -104,41 +104,8 @@
 at;%0A
-import java.text.ParseException;%0A
 impo
@@ -127,32 +127,32 @@
 mpleDateFormat;%0A
+
 import java.util
@@ -283,20 +283,26 @@
 .Assert.
-fail
+assertTrue
 ;%0A%0Apubli
@@ -1480,34 +1480,71 @@
     
-String date = %222001-01-01%22
+Date expectedDate = new GregorianCalendar(2001, 0, 1).getTime()
 ;%0A  
@@ -1553,72 +1553,42 @@
     
+S
 tr
-y %7B%0A            Date actualDate = dateFormat.parse(date);%0A    
+ing dateString = %222001-01-01%22;%0A
     
@@ -1625,28 +1625,22 @@
     
-    actual
+expected
 Date,%0A
-    
     
@@ -1678,24 +1678,30 @@
 gToDate(date
+String
 )%0A          
@@ -1702,95 +1702,321 @@
     
+);%0A
     
-);
+%7D%0A
 %0A    
-    %7D catch (ParseException e) %7B%0A            fail(e.getMessage());%0A        %7D
+@Test%0A    public void willDefaultToNowIfFailsToParseDate() %7B%0A        String dateString = %22wibble%22;%0A        long currentTime = new GregorianCalendar().getTime().getTime();%0A        long exceptionTime = StringHandler.stringToDate(dateString).getTime();%0A        assertTrue(currentTime %3C= exceptionTime);
 %0A   
@@ -2970,32 +2970,32 @@
   assertEquals(%0A
-
                 
@@ -3143,32 +3143,505 @@
       );%0A    %7D%0A%0A
+    @Test%0A    public void ignoreReplaceTokensWhenValueAndTokenLengthsMismatch() %7B%0A        String%5B%5D tokens = %7B%22@WIBBLE@%22, %22@WOBBLE@%22%7D;%0A        String%5B%5D values = %7B%22expected%22%7D;%0A        String initialString = %22this is my @WIBBLE@ @WOBBLE@. @WOBBLE@ values should be @WIBBLE@%22;%0A%0A        assertEquals(%0A                %22this is my @WIBBLE@ @WOBBLE@. @WOBBLE@ values should be @WIBBLE@%22,%0A                StringHandler.replaceTokens(initialString, tokens, values)%0A        );%0A    %7D%0A%0A
     @Test%0A    pu

extend string handler class tests[2]