00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 package com.realtime.crossfire.jxclient.scripts;
00023
00024 import com.realtime.crossfire.jxclient.server.crossfire.CrossfireServerConnection;
00025 import com.realtime.crossfire.jxclient.server.crossfire.TestCrossfireServerConnection;
00026 import com.realtime.crossfire.jxclient.server.server.ReceivedPacketListener;
00027 import com.realtime.crossfire.jxclient.test.TestScriptProcess;
00028 import java.util.ArrayList;
00029 import java.util.Collection;
00030 import org.jetbrains.annotations.NotNull;
00031 import org.junit.Assert;
00032 import org.junit.Test;
00033
00038 public class PacketWatcherTest {
00039
00043 @Test
00044 public void test1() {
00045 final Collection<ReceivedPacketListener> listeners = new ArrayList<ReceivedPacketListener>();
00046 final CrossfireServerConnection connection = new TestCrossfireServerConnection() {
00047
00048 @Override
00049 public void addPacketWatcherListener(@NotNull final ReceivedPacketListener listener) {
00050 listeners.add(listener);
00051 }
00052
00053 @Override
00054 public void removePacketWatcherListener(@NotNull final ReceivedPacketListener listener) {
00055 listeners.remove(listener);
00056 }
00057
00058 };
00059 final StringBuilder sb = new StringBuilder();
00060 final ScriptProcess scriptProcess = new TestScriptProcess() {
00061
00062 @Override
00063 public void commandSent(@NotNull final String cmd) {
00064 sb.append(cmd).append('\n');
00065 }
00066
00067 };
00068 final PacketWatcher packetWatcher = new PacketWatcher(connection, scriptProcess);
00069
00070 sb.setLength(0);
00071 for (final ReceivedPacketListener listener : listeners) {
00072 listener.processEmpty("command");
00073 }
00074 Assert.assertEquals("", sb.toString());
00075
00076 packetWatcher.addCommand("command");
00077
00078 sb.setLength(0);
00079 for (final ReceivedPacketListener listener : listeners) {
00080 listener.processEmpty("comman");
00081 listener.processEmpty("command");
00082 listener.processEmpty("commandx");
00083 }
00084 Assert.assertEquals("watch command\n"+"watch commandx\n", sb.toString());
00085 }
00086
00087 }