/*
 * Java Genetic Algorithm Library (@__identifier__@).
 * Copyright (c) @__year__@ Franz Wilhelmstötter
 *
 * 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.
 *
 * Author:
 *    Franz Wilhelmstötter (franz.wilhelmstoetter@gmx.at)
 */
package org.jenetics.xml;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;

import javax.xml.stream.XMLStreamException;

import org.testng.Assert;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import org.jenetics.BitChromosome;
import org.jenetics.CharacterChromosome;
import org.jenetics.PermutationChromosome;
import org.jenetics.xml.stream.AutoCloseableXMLStreamReader;
import org.jenetics.xml.stream.AutoCloseableXMLStreamWriter;
import org.jenetics.xml.stream.Reader;
import org.jenetics.xml.stream.Writer;
import org.jenetics.xml.stream.XML;

/**
 * @author <a href="mailto:franz.wilhelmstoetter@gmx.at">Franz Wilhelmstötter</a>
 */
public class WritersReadersTests {

	static <T> byte[] toBytes(final T value, final Writer<T> writer)
		throws XMLStreamException
	{
		final ByteArrayOutputStream out = new ByteArrayOutputStream();
		try (AutoCloseableXMLStreamWriter xml = XML.writer(out)) {
			writer.write(value, xml);
		}

		return out.toByteArray();
	}

	static <T> T fromBytes(final byte[] bytes, final Reader<T> reader)
		throws XMLStreamException
	{
		final ByteArrayInputStream in = new ByteArrayInputStream(bytes);
		try (AutoCloseableXMLStreamReader xml = XML.reader(in)) {
			xml.next();
			return reader.read(xml);
		}
	}

	@Test(dataProvider = "marshallings")
	public <T> void marshalling(
		final T expected,
		final Writer<T> writer,
		final Reader<T> reader
	)
		throws XMLStreamException
	{
		final byte[] bytes = toBytes(expected, writer);
		final T actual = fromBytes(bytes, reader);

		Assert.assertEquals(actual, expected);
	}

	@DataProvider
	public Object[][] marshallings() {
		return new Object[][] {
			{
				BitChromosome.of(10),
				Writers.BitChromosome.writer(),
				Readers.BitChromosome.reader()
			},
			{
				CharacterChromosome.of(15),
				Writers.CharacterChromosome.writer(),
				Readers.CharacterChromosome.reader()
			},
			{
				PermutationChromosome.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10),
				Writers.PermutationChromosome.writer(),
				Readers.PermutationChromosome.reader(Reader.of("allele").map(Integer::parseInt))
			}
		};
	}

}

@@ -1057,32 +1057,145 @@
 cterChromosome;%0A
+import org.jenetics.DoubleChromosome;%0Aimport org.jenetics.IntegerChromosome;%0Aimport org.jenetics.LongChromosome;%0A
 import org.jenet
@@ -2373,16 +2373,59 @@
 riter);%0A
+%09%09//System.out.println(new String(bytes));%0A
 %09%09final 
@@ -2953,32 +2953,38 @@
 tationChromosome
+%0A%09%09%09%09%09
 .reader(Reader.o
@@ -3019,16 +3019,406 @@
 seInt))%0A
+%09%09%09%7D,%0A%09%09%09%7B%0A%09%09%09%09IntegerChromosome.of(0, 1_000_000, 15),%0A%09%09%09%09Writers.IntegerChromosome.writer(),%0A%09%09%09%09Readers.IntegerChromosome.reader()%0A%09%09%09%7D,%0A%09%09%09%7B%0A%09%09%09%09LongChromosome.of(0, 1_000_000, 15),%0A%09%09%09%09Writers.LongChromosome.writer(),%0A%09%09%09%09Readers.LongChromosome.reader()%0A%09%09%09%7D,%0A%09%09%09%7B%0A%09%09%09%09DoubleChromosome.of(0, 1_000_000, 15),%0A%09%09%09%09Writers.DoubleChromosome.writer(),%0A%09%09%09%09Readers.DoubleChromosome.reader()%0A
 %09%09%09%7D%0A%09%09%7D

Implement chromosome marshaling tests.