/*
 * 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.OSCReparserTest;
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 OSCBundle reparsedBundle = OSCReparserTest.reparse(bundle);
		if (!reparsedBundle.getTimestamp().equals(expectedTimestamp)) {
			Assert.fail("Send Bundle did not receive the correct timestamp " + reparsedBundle.getTimestamp()
				+ "(" + reparsedBundle.getTimestamp().getTime() +
				") (should be " + expectedTimestamp +"( " + expectedTimestamp.getTime() + ")) ");
		}
		List<OSCPacket> packets = reparsedBundle.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);
	}
}

@@ -2785,11 +2785,1026 @@
 ATE);%0A%09%7D
+%0A%0A%09@Test%0A%09public void testSendMultiLevelBundle() throws IOException %7B%0A%0A%09%09// create this structure:%0A%09%09// bundle-0%0A%09%09//   %3E message-0%0A%09%09//   %3E bundle-1%0A%09%09//     %3E message-1%0A%09%09//     %3E bundle-2%0A%09%09//       %3E message-2%0A%09%09//       %3E message-3%0A%0A%09%09final List%3COSCPacket%3E packetsBundle2 = new ArrayList%3COSCPacket%3E(2);%0A%09%09packetsBundle2.add(new OSCMessage(%22/leaf/2%22));%0A%09%09packetsBundle2.add(new OSCMessage(%22/leaf/3%22));%0A%09%09final OSCBundle bundle2 = new OSCBundle(packetsBundle2);%0A%0A%09%09final List%3COSCPacket%3E packetsBundle1 = new ArrayList%3COSCPacket%3E(2);%0A%09%09packetsBundle1.add(new OSCMessage(%22/leaf/1%22));%0A%09%09packetsBundle1.add(bundle2);%0A%09%09final OSCBundle bundle1 = new OSCBundle(packetsBundle1);%0A%0A%09%09final List%3COSCPacket%3E packetsBundle0 = new ArrayList%3COSCPacket%3E(2);%0A%09%09packetsBundle0.add(new OSCMessage(%22/leaf/0%22));%0A%09%09packetsBundle0.add(bundle1);%0A%09%09final OSCBundle bundle0 = new OSCBundle(packetsBundle0);%0A%0A%09%09OSCReparserTest.reparse(bundle0);%0A%09%09// TODO make a smarter test, where we serialize, re-parse and then check the whole thing%0A%09%7D
 %0A%7D%0A

add `OSCBundleTest#testSendMultiLevelBundle()` [test]