/*
 * JBoss, Home of Professional Open Source.
 * Copyright 2015, Red Hat, Inc., and individual contributors
 * as indicated by the @author tags. See the copyright.txt file in the
 * distribution for a full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */

package org.wildfly.clustering.marshalling.spi;

import static org.junit.Assert.*;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.function.BiConsumer;

import org.wildfly.clustering.marshalling.Externalizer;

/**
 * @author Paul Ferraro
 */
public class ExternalizerTestUtil {

    public static <T> void test(Externalizer<T> externalizer, T subject) throws IOException, ClassNotFoundException {
        test(externalizer, subject, (expected, actual) -> assertEquals(expected, actual));
    }

    public static <T> void test(Externalizer<T> externalizer, T subject, BiConsumer<T, T> assertion) throws IOException, ClassNotFoundException {
        assertTrue(externalizer.getTargetClass().isInstance(subject));

        ByteArrayOutputStream out = new ByteArrayOutputStream();
        try (ObjectOutputStream output = new ObjectOutputStream(out)) {
            externalizer.writeObject(output, subject);
        }

        try (ObjectInputStream input = new ObjectInputStream(new ByteArrayInputStream(out.toByteArray()))) {
            T result = externalizer.readObject(input);
            assertion.accept(subject, result);
            assertTrue(externalizer.getTargetClass().isInstance(result));
        }
    }
}

@@ -1312,16 +1312,41 @@
 sumer;%0A%0A
+import org.junit.Assert;%0A
 import o
@@ -1927,17 +1927,32 @@
 tStream 
-o
+externalizedOutp
 ut = new
@@ -2041,17 +2041,32 @@
 tStream(
-o
+externalizedOutp
 ut)) %7B%0A 
@@ -2126,24 +2126,94 @@
 %0A        %7D%0A%0A
+        byte%5B%5D externalizedBytes = externalizedOutput.toByteArray();%0A%0A
         try 
@@ -2290,25 +2290,25 @@
 eam(
-out.toByteArray()
+externalizedBytes
 ))) 
@@ -2494,16 +2494,509 @@
        %7D
+%0A%0A        // If object is serializable, make sure we've actually improved upon default serialization size%0A        if (subject instanceof java.io.Serializable) %7B%0A            ByteArrayOutputStream out = new ByteArrayOutputStream();%0A            try (ObjectOutputStream output = new ObjectOutputStream(out)) %7B%0A                output.writeObject(subject);%0A            %7D%0A%0A            byte%5B%5D bytes = out.toByteArray();%0A            Assert.assertTrue(externalizedBytes.length %3C bytes.length);%0A        %7D
 %0A    %7D%0A%7D

Validate that our externalizers actually reduce the marshalling payload.