/*
 * Copyright (C) 2003, C. Ramakrishnan / Auracle.
 * 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.Date;
import java.util.List;

/**
 * @author Chandrasekhar Ramakrishnan
 * @see OSCByteArrayToJavaConverter
 */
public class OSCByteArrayToJavaConverterTest extends junit.framework.TestCase {

	private OSCByteArrayToJavaConverter converter;

	@Override
	protected void setUp() throws Exception {
		converter = new OSCByteArrayToJavaConverter();
	}

	@Override
	protected void tearDown() throws Exception {

	}

	private static void checkAddress(final String expectedAddress, final String observedAddress) {
		if (!observedAddress.equals(expectedAddress)) {
			fail("Address should be " + expectedAddress + ", but is " + observedAddress);
		}
	}

	public void testReadSimplePacket() throws Exception {
		final byte[] bytes = {47, 115, 99, 47, 114, 117, 110, 0, 44, 0, 0, 0};
		final OSCMessage packet = (OSCMessage) converter.convert(bytes, bytes.length);
		checkAddress("/sc/run", packet.getAddress());
	}

	public void testReadComplexPacket() throws Exception {
		final byte[] bytes = {0x2F, 0x73, 0x5F, 0x6E, 0x65, 0x77, 0, 0, 0x2C, 0x69, 0x73, 0x66, 0, 0, 0, 0, 0, 0, 0x3, (byte) 0xE9, 0x66, 0x72, 0x65, 0x71, 0, 0, 0, 0, 0x43, (byte) 0xDC, 0, 0};

		final OSCMessage packet = (OSCMessage) converter.convert(bytes, bytes.length);
		checkAddress("/s_new", packet.getAddress());
		final List<Object> arguments = packet.getArguments();
		if (arguments.size() != 3) {
			fail("Num arguments should be 3, but is " + arguments.size());
		}
		if (!(Integer.valueOf(1001).equals(arguments.get(0)))) {
			fail("Argument 1 should be 1001, but is " + arguments.get(0));
		}
		if (!("freq".equals(arguments.get(1)))) {
			fail("Argument 2 should be freq, but is " + arguments.get(1));
		}
		if (!(Float.valueOf(440.0f).equals(arguments.get(2)))) {
			fail("Argument 3 should be 440.0, but is " + arguments.get(2));
		}
	}

	public void testReadBundle() throws Exception {
		final byte[] bytes = {0x23, 0x62, 0x75, 0x6E, 0x64, 0x6C, 0x65, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0x0C, 0X2F, 0x74, 0x65, 0x73, 0x74, 0, 0, 0, 0x2C, 0, 0, 0};

		final OSCBundle bundle = (OSCBundle) converter.convert(bytes, bytes.length);
		if (!bundle.getTimestamp().equals(new Date(0))) {
			fail("Timestamp should be 0, but is " + bundle.getTimestamp());
		}
		final List<OSCPacket> packets = bundle.getPackets();
		if (packets.size() != 1) {
			fail("Num packets should be 1, but is " + packets.size());
		}
		final OSCMessage message = (OSCMessage) packets.get(0);
		checkAddress("/test", message.getAddress());
	}
}

@@ -938,32 +938,983 @@
 dress);%0A%09%09%7D%0A%09%7D%0A%0A
+%09public void testReadShortestPacketWithoutArgumentsSeparator() throws Exception %7B%0A%09%09// This pakcet ommits the character (',') that separates address%0A%09%09// from parameters. This is supposed legacy practise,%0A%09%09// but still not explicitly forbidden by the OSC 1.0 specificiations,%0A%09%09// and should therefore be supported.%0A%09%09final byte%5B%5D bytes = %7B47, 0, 0, 0%7D;%0A%09%09final OSCMessage packet = (OSCMessage) converter.convert(bytes, bytes.length);%0A%09%09checkAddress(%22/%22, packet.getAddress());%0A%09%7D%0A%0A%09public void testReadShortPacket1() throws Exception %7B%0A%09%09final byte%5B%5D bytes = %7B47, 0, 0, 0, 44, 0, 0, 0%7D;%0A%09%09final OSCMessage packet = (OSCMessage) converter.convert(bytes, bytes.length);%0A%09%09checkAddress(%22/%22, packet.getAddress());%0A%09%7D%0A%0A%09public void testReadShortPacket2() throws Exception %7B%0A%09%09final byte%5B%5D bytes = %7B47, 115, 0, 0, 44, 0, 0, 0%7D;%0A%09%09final OSCMessage packet = (OSCMessage) converter.convert(bytes, bytes.length);%0A%09%09checkAddress(%22/s%22, packet.getAddress());%0A%09%7D%0A%0A
 %09public void tes

add 3 new tests (1 is currently failing)