package com.bubelich;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import javax.xml.bind.DatatypeConverter;
import java.security.SecureRandom;
import java.util.Random;

public class jBase64Test extends Assert{

    private static byte [] input_t1 = new byte[10*1024+1];
    private static byte [] input_t2 = new byte[10*1024+2];
    private static byte [] input_t3 = new byte[10*1024+3];

    @Before
    public void setUp(){

        Random rnd = new Random();

        rnd.nextBytes(input_t1);
        rnd.nextBytes(input_t2);
        rnd.nextBytes(input_t3);

    }

    @Test
    public void testEncode() throws Exception {

        // For one byte //
        assertEquals("Pw==",jBase64.encode(new byte[]{0x3f},jBase64.ALPHABET.BASE));

        // For two byte //
        assertEquals("P08=",jBase64.encode(new byte[]{0x3f,0x4f}, jBase64.ALPHABET.BASE));

        // For three bytes //
        assertEquals("P09f", jBase64.encode(new byte[]{0x3f, 0x4f, 0x5f}, jBase64.ALPHABET.BASE));

        // Text without padding //
        assertEquals("Encode input T1", DatatypeConverter.printBase64Binary(input_t1), jBase64.encode(input_t1, jBase64.ALPHABET.BASE));

        // Text with 1th padding //
        assertEquals("Encode input T2", DatatypeConverter.printBase64Binary(input_t2), jBase64.encode(input_t2, jBase64.ALPHABET.BASE));

        // Text with 2th padding //
        assertEquals("Encode input T3", DatatypeConverter.printBase64Binary(input_t3), jBase64.encode(input_t3, jBase64.ALPHABET.BASE));

        // For null //
        try { jBase64.encode(null, jBase64.ALPHABET.BASE); }
        catch (NullPointerException ex){ /* OK */}

        // Empty byte //
        try { jBase64.encode(new byte[]{}, jBase64.ALPHABET.BASE); }
        catch (RuntimeException ex){ /* OK */}

    }

    @Test
    public void testDecode() throws Exception {

        // With padding //
        assertEquals("any carnal pleasur",new String(jBase64.decode("YW55IGNhcm5hbCBwbGVhc3Vy", jBase64.ALPHABET.BASE,true)));
        assertEquals("any carnal pleasu",new String(jBase64.decode("YW55IGNhcm5hbCBwbGVhc3U=", jBase64.ALPHABET.BASE,true)));
        assertEquals("any carnal pleas",new String(jBase64.decode("YW55IGNhcm5hbCBwbGVhcw==", jBase64.ALPHABET.BASE,true)));


        // Without padding //
        assertEquals("any carnal pleasur",new String(jBase64.decode("YW55IGNhcm5hbCBwbGVhc3Vy", jBase64.ALPHABET.BASE,true)));
        assertEquals("any carnal pleasu",new String(jBase64.decode("YW55IGNhcm5hbCBwbGVhc3U", jBase64.ALPHABET.BASE,true)));
        assertEquals("any carnal pleas",new String(jBase64.decode("YW55IGNhcm5hbCBwbGVhcw", jBase64.ALPHABET.BASE,true)));

        // Normal //
        assertArrayEquals(input_t1,jBase64.decode(jBase64.encode(input_t1, jBase64.ALPHABET.BASE), jBase64.ALPHABET.BASE,true));
        assertArrayEquals(input_t2,jBase64.decode(jBase64.encode(input_t2, jBase64.ALPHABET.BASE), jBase64.ALPHABET.BASE,true));
        assertArrayEquals(input_t2,jBase64.decode(jBase64.encode(input_t2, jBase64.ALPHABET.BASE), jBase64.ALPHABET.BASE,true));

        // Safe //
        assertArrayEquals(input_t1,jBase64.decode(jBase64.encode(input_t1, jBase64.ALPHABET.SAFE), jBase64.ALPHABET.SAFE,true));
        assertArrayEquals(input_t2,jBase64.decode(jBase64.encode(input_t2, jBase64.ALPHABET.SAFE), jBase64.ALPHABET.SAFE,true));
        assertArrayEquals(input_t2,jBase64.decode(jBase64.encode(input_t2, jBase64.ALPHABET.SAFE), jBase64.ALPHABET.SAFE,true));

        // Exception expect //
        try { jBase64.decode("", jBase64.ALPHABET.BASE,true); }
        catch (IllegalArgumentException ex){ /* OK */}
        catch (Exception ex){ fail("Something wrong"); }

        try { jBase64.decode(null, jBase64.ALPHABET.BASE,true); }
        catch (NullPointerException ex){ /* OK */}

        // Wrong padding //
        try { jBase64.decode("YW55IGNhcm5hbCBwbGVhcw=x", jBase64.ALPHABET.BASE, true); }
        catch (IllegalArgumentException ex){ /* OK */}

        try { jBase64.decode("YW55IGNhcm5hbCBwbGVhcw=x", jBase64.ALPHABET.BASE,true); }
        catch (IllegalArgumentException ex){ /* OK */}

    }
}
@@ -285,17 +285,14 @@
 yte%5B
-10*
 1024+
-1
+0
 %5D;%0A 
@@ -341,17 +341,14 @@
 yte%5B
-10*
 1024+
-2
+1
 %5D;%0A 
@@ -397,17 +397,14 @@
 yte%5B
-10*
 1024+
-3
+2
 %5D;%0A%0A
@@ -997,32 +997,34 @@
 PHABET.BASE));%0A%0A
+//
         // Text 
@@ -1034,32 +1034,34 @@
 hout padding //%0A
+//
         assertEq
@@ -1173,33 +1173,37 @@
 LPHABET.BASE));%0A
-%0A
+//%0A//
         // Text 
@@ -1214,32 +1214,34 @@
  1th padding //%0A
+//
         assertEq
@@ -1353,33 +1353,37 @@
 LPHABET.BASE));%0A
-%0A
+//%0A//
         // Text 
@@ -1394,32 +1394,34 @@
  2th padding //%0A
+//
         assertEq
@@ -3593,32 +3593,33 @@
 4.ALPHABET.BASE,
+ 
 true); %7D%0A       
@@ -3669,65 +3669,8 @@
  */%7D
-%0A        catch (Exception ex)%7B fail(%22Something wrong%22); %7D
 %0A%0A  
@@ -3716,32 +3716,33 @@
 4.ALPHABET.BASE,
+ 
 true); %7D%0A       
@@ -4034,24 +4034,25 @@
 PHABET.BASE,
+ 
 true); %7D%0A   

Update JUnit test, bug fix for Travis. v2