/*
 * Copyright 2015 JBoss by Red Hat
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.drools.core.io.impl;

import org.apache.commons.lang3.ArrayUtils;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

@RunWith(Parameterized.class)
public class ByteArrayResourceToStringTest {

    @Parameterized.Parameters(name = "{index}: bytes={0}, encoding={1}")
    public static Collection<Object[]> data() {
        return new ArrayList<Object[]>(Arrays.asList(new Object[][]
                        {
                                {
                                        Arrays.asList(new Byte[]{10, 20, 30, 40}),
                                        null,
                                        "ByteArrayResource[bytes=[10, 20, 30, 40], encoding=null]"
                                },
                                {
                                        Arrays.asList(new Byte[]{10, 20, 30, 40, 50, 60, 70, 80, 90, 100}),
                                        null,
                                        "ByteArrayResource[bytes=[10, 20, 30, 40, 50, 60, 70, 80, 90, 100], encoding=null]"
                                },
                                {
                                        Arrays.asList(new Byte[]{10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120}),
                                        null,
                                        "ByteArrayResource[bytes=[10, 20, 30, 40, 50, 60, 70, 80, 90, 100, ...], encoding=null]"
                                },
                                // non-null encoding
                                {
                                        Arrays.asList(new Byte[]{10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120}),
                                        "UTF-8",
                                        "ByteArrayResource[bytes=[10, 20, 30, 40, 50, 60, 70, 80, 90, 100, ...], encoding=UTF-8]"
                                },
                        }
        ));
    }

    // using List<Byte> instead of directly byte[] to make sure the bytes are printed as part of the test name
    // see above ({index}: bytes[{0}], encoding[{1}]) -- Array.toString only return object id
    @Parameterized.Parameter(0)
    public List<Byte> bytes;

    @Parameterized.Parameter(1)
    public String encoding;

    @Parameterized.Parameter(2)
    public String expectedString;

    @Test
    public void testToString() {
        byte[] byteArray = ArrayUtils.toPrimitive(bytes.toArray(new Byte[0]));
        ByteArrayResource byteArrayResource = new ByteArrayResource(byteArray, encoding);
        Assert.assertEquals(expectedString, byteArrayResource.toString());
    }

}

@@ -1169,67 +1169,8 @@
 %5B%5D%5B%5D
-%0A                        %7B%0A                                
 %7B%0A  
@@ -1167,32 +1167,42 @@
 ct%5B%5D%5B%5D%7B%0A        
+        %7B%0A
                 
@@ -1205,32 +1205,24 @@
             
-        
 Arrays.asLis
@@ -1252,32 +1252,16 @@
 , 40%7D),%0A
-                
         
@@ -1306,32 +1306,16 @@
         
-                
 %22ByteArr
@@ -1385,77 +1385,29 @@
     
-        
-        %7D,%0A                                %7B%0A                
+%7D,%0A                %7B%0A
     
@@ -1522,46 +1522,14 @@
     
-        
-        null,%0A                
+null,%0A
     
@@ -1652,77 +1652,29 @@
     
-                %7D,%0A                                %7B%0A                
+%7D,%0A                %7B%0A
     
@@ -1799,39 +1799,14 @@
     
-                null,%0A         
+null,%0A
     
@@ -1817,39 +1817,32 @@
                 
-       
 %22ByteArrayResour
@@ -1934,32 +1934,11 @@
     
-                %7D,%0A     
+%7D,%0A
     
@@ -1949,27 +1949,16 @@
         
-           
 // non-n
@@ -1990,42 +1990,10 @@
     
-         
-       %7B%0A                
+%7B%0A
     
@@ -2118,42 +2118,17 @@
     
-                %22UTF-8%22,%0A         
+%22UTF-8%22,%0A
     
@@ -2139,39 +2139,32 @@
                 
-       
 %22ByteArrayResour
@@ -2257,61 +2257,20 @@
     
-                %7D,%0A                        %7D%0A        
+%7D,%0A        %7D
 ));%0A

Fix formatting in ByteArrayResourceToStringTest