/*
 * 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.messageselector.OSCPatternAddressMessageSelector;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

/**
 * @see OSCPacketDispatcher
 */
public class OSCPacketDispatcherTest {

	private OSCPacketDispatcher dispatcher;
	private SimpleOSCMessageListener listener1;
	private SimpleOSCMessageListener listener2;

	@Before
	public void setUp() {
		dispatcher = new OSCPacketDispatcher(OSCSerializerFactory.createDefaultFactory());
		listener1 = new SimpleOSCMessageListener();
		dispatcher.addListener(new OSCPatternAddressMessageSelector("/listener1"), listener1);
		listener2 = new SimpleOSCMessageListener();
		dispatcher.addListener(new OSCPatternAddressMessageSelector("/listener2"), listener2);
	}

	@After
	public void tearDown() {

	}

	@Test
	public void testDispatchToListener1() {
		OSCMessage message = new OSCMessage("/listener1");
		dispatcher.dispatchPacket(message);
		if (!listener1.isMessageReceived()) {
			Assert.fail("Message to listener1 didn't get sent to listener1");
		}
		if (listener2.isMessageReceived()) {
			Assert.fail("Message to listener1 got sent to listener2");
		}
	}

	@Test
	public void testDispatchToListener2() {
		OSCMessage message = new OSCMessage("/listener2");
		dispatcher.dispatchPacket(message);
		if (listener1.isMessageReceived()) {
			Assert.fail("Message to listener2 got sent to listener1");
		}
		if (!listener2.isMessageReceived()) {
			Assert.fail("Message to listener2 didn't get sent to listener2");
		}
	}

	@Test
	public void testDispatchToNobody() {
		OSCMessage message = new OSCMessage("/nobody");
		dispatcher.dispatchPacket(message);
		if (listener1.isMessageReceived() || listener2.isMessageReceived()) {
			Assert.fail("Message to nobody got dispatched incorrectly");
		}
	}

	@Test
	public void testDispatchBundle() {
		OSCBundle bundle = new OSCBundle();
		bundle.addPacket(new OSCMessage("/listener1"));
		bundle.addPacket(new OSCMessage("/listener2"));
		dispatcher.dispatchPacket(bundle);
		if (!listener1.isMessageReceived()) {
			Assert.fail("Bundle didn't dispatch message to listener 1");
		}
		if (!listener2.isMessageReceived()) {
			Assert.fail("Bundle didn't dispatch message to listener 2");
		}
	}
}

@@ -296,16 +296,67 @@
 lector;%0A
+import java.util.ArrayList;%0Aimport java.util.List;%0A
 import o
@@ -2518,11 +2518,459 @@
 ;%0A%09%09%7D%0A%09%7D
+%0A%0A%09@Test%0A%09public void testDispatchManyArguments() %7B%0A%0A%09%09final int numArguments = OSCPacketDispatcher.MAX_ARGUMENTS + 1;%0A%09%09final List%3CObject%3E arguments = new ArrayList%3CObject%3E(numArguments);%0A%09%09for (int ai = 0; ai %3C numArguments; ai++) %7B%0A%09%09%09arguments.add(ai);%0A%09%09%7D%0A%09%09OSCMessage message = new OSCMessage(%22/listener1%22, arguments);%0A%09%09dispatcher.dispatchPacket(message);%0A%09%09Assert.assertEquals(numArguments, listener1.getMessage().getArguments().size());%0A%09%7D
 %0A%7D%0A

test dispatching many arguments [test]