/*
 * Copyright (C) 2003, C. Ramakrishnan / Illposed Software.
 * All rights reserved.
 *
 * This code is licensed under the BSD 3-Clause license.
 * See file LICENSE (or LICENSE.html) for more information.
 */

package com.illposed.osc;

import com.illposed.osc.utility.OSCParser;
import com.illposed.osc.utility.OSCSerializer;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import org.junit.Assert;
import org.junit.Test;

/**
 * @author Chandrasekhar Ramakrishnan
 * @see OSCBundle
 */
public class OSCBundleTest {

	private void sendBundleTimestampTestHelper(OSCBundle bundle, Date expectedTimestamp) throws IOException {
		final ByteArrayOutputStream buffer = new ByteArrayOutputStream();
		final OSCSerializer stream = new OSCSerializer(buffer);
		stream.write(bundle);
		final byte[] byteArray = buffer.toByteArray();
		OSCParser converter = new OSCParser();
		OSCBundle packet = (OSCBundle) converter.convert(byteArray, byteArray.length);
		if (!packet.getTimestamp().equals(expectedTimestamp)) {
			Assert.fail("Send Bundle did not receive the correct timestamp " + packet.getTimestamp()
				+ "(" + packet.getTimestamp().getTime() +
				") (should be " + expectedTimestamp +"( " + expectedTimestamp.getTime() + ")) ");
		}
		List<OSCPacket> packets = packet.getPackets();
		OSCMessage msg = (OSCMessage) packets.get(0);
		if (!msg.getAddress().equals("/dummy")) {
			Assert.fail("Send Bundle's message did not receive the correct address");
		}
	}

	@Test
	public void testSendBundle() throws IOException {
		Date timestampNow = GregorianCalendar.getInstance().getTime();
		List<OSCPacket> packetsSent = new ArrayList<OSCPacket>(1);
		packetsSent.add(new OSCMessage("/dummy"));
		OSCBundle bundle = new OSCBundle(packetsSent, timestampNow);
		sendBundleTimestampTestHelper(bundle, timestampNow);
	}

	@Test
	public void testSendBundleImmediate() throws IOException {
		List<OSCPacket> packetsSent = new ArrayList<OSCPacket>(1);
		packetsSent.add(new OSCMessage("/dummy"));
		OSCBundle bundle = new OSCBundle(packetsSent);
		sendBundleTimestampTestHelper(bundle, OSCBundle.TIMESTAMP_IMMEDIATE);
	}

	@Test
	public void testSendBundleImmediateExplicit() throws IOException {
		Date timestampNow = GregorianCalendar.getInstance().getTime();
		List<OSCPacket> packetsSent = new ArrayList<OSCPacket>(1);
		packetsSent.add(new OSCMessage("/dummy"));
		OSCBundle bundle = new OSCBundle(packetsSent, timestampNow);
		bundle.setTimestamp(OSCBundle.TIMESTAMP_IMMEDIATE);
		sendBundleTimestampTestHelper(bundle, OSCBundle.TIMESTAMP_IMMEDIATE);
	}

	@Test
	public void testSendBundleImmediateExplicitNull() throws IOException {
		Date timestampNow = GregorianCalendar.getInstance().getTime();
		List<OSCPacket> packetsSent = new ArrayList<OSCPacket>(1);
		packetsSent.add(new OSCMessage("/dummy"));
		OSCBundle bundle = new OSCBundle(packetsSent, timestampNow);
		bundle.setTimestamp(null);
		sendBundleTimestampTestHelper(bundle, OSCBundle.TIMESTAMP_IMMEDIATE);
	}
}

@@ -271,99 +271,20 @@
 .OSC
-P
+Rep
 arser
-;%0Aimport com.illposed.osc.utility.OSCSerializer;%0Aimport java.io.ByteArrayOutputStream
+Test
 ;%0Aim
@@ -681,139 +681,57 @@
 nal 
-ByteArrayOutputStream buffer = new ByteArrayOutputStream();%0A%09%09final OSCSerializer stream = new OSCSerializer(buffer);%0A%09%09
+OSCBundle reparsedBundle = OSCReparserTe
 st
+.
 re
-am.writ
+pars
 e(bu
@@ -743,190 +743,27 @@
 ;%0A%09%09
-final byte%5B%5D byteArray = buffer.toByteArray();%0A%09%09OSCParser converter = new OSCParser();%0A%09%09OSCBundle packet = (OSCBundle) converter.convert(byteArray, byteArray.length);%0A%09%09if (!packet
+if (!reparsedBundle
 .get
@@ -869,30 +869,38 @@
 mestamp %22 + 
-packet
+reparsedBundle
 .getTimestam
@@ -915,22 +915,30 @@
 + %22(%22 + 
-packet
+reparsedBundle
 .getTime
@@ -1075,22 +1075,30 @@
 ckets = 
-packet
+reparsedBundle
 .getPack

use `OSCReparserTest.reparse()` [test]