/*
 * 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.OSCByteArrayToJavaConverter;
import java.util.ArrayList;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

/**
 * @author Chandrasekhar Ramakrishnan
 * @see OSCBundle
 */
public class OSCBundleTest extends junit.framework.TestCase {

	private void sendBundleTimestampTestHelper(OSCBundle bundle, Date expectedTimestamp) {
		byte[] byteArray = bundle.getByteArray();
		OSCByteArrayToJavaConverter converter = new OSCByteArrayToJavaConverter();
		OSCBundle packet = (OSCBundle) converter.convert(byteArray, byteArray.length);
		if (!packet.getTimestamp().equals(expectedTimestamp)) {
			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")) {
			fail("Send Bundle's message did not receive the correct address");
		}
	}

	public void testSendBundle() {
		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);
	}

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

@@ -1914,11 +1914,816 @@
 ATE);%0A%09%7D
+%0A%0A%09public void testSendBundleImmediateExplicit() %7B%0A%09%09Date timestampNow = GregorianCalendar.getInstance().getTime();%0A%09%09List%3COSCPacket%3E packetsSent = new ArrayList%3COSCPacket%3E(1);%0A%09%09packetsSent.add(new OSCMessage(%22/dummy%22));%0A%09%09OSCBundle bundle = new OSCBundle(packetsSent, timestampNow);%0A%09%09bundle.setTimestamp(OSCBundle.TIMESTAMP_IMMEDIATE);%0A%09%09sendBundleTimestampTestHelper(bundle, OSCBundle.TIMESTAMP_IMMEDIATE);%0A%09%7D%0A%0A%09public void testSendBundleImmediateExplicitNull() %7B%0A%09%09Date timestampNow = GregorianCalendar.getInstance().getTime();%0A%09%09List%3COSCPacket%3E packetsSent = new ArrayList%3COSCPacket%3E(1);%0A%09%09packetsSent.add(new OSCMessage(%22/dummy%22));%0A%09%09OSCBundle bundle = new OSCBundle(packetsSent, timestampNow);%0A%09%09bundle.setTimestamp(null);%0A%09%09sendBundleTimestampTestHelper(bundle, OSCBundle.TIMESTAMP_IMMEDIATE);%0A%09%7D
 %0A%7D%0A

add two new tests in OSCBundleTest, that check special timestamp handling