Crossfire JXClient, Trunk  R20561
PacketWatcherTest.java
Go to the documentation of this file.
1 /*
2  * This file is part of JXClient, the Fullscreen Java Crossfire Client.
3  *
4  * JXClient is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * JXClient is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with JXClient; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17  *
18  * Copyright (C) 2005-2008 Yann Chachkoff.
19  * Copyright (C) 2006-2011 Andreas Kirschbaum.
20  */
21 
22 package com.realtime.crossfire.jxclient.scripts;
23 
28 import java.util.ArrayList;
29 import java.util.Collection;
30 import org.jetbrains.annotations.NotNull;
31 import org.junit.Assert;
32 import org.junit.Test;
33 
38 public class PacketWatcherTest {
39 
43  @Test
44  public void test1() {
45  final Collection<ReceivedPacketListener> listeners = new ArrayList<>();
47 
48  @Override
49  public void addPacketWatcherListener(@NotNull final ReceivedPacketListener listener) {
50  listeners.add(listener);
51  }
52 
53  @Override
54  public void removePacketWatcherListener(@NotNull final ReceivedPacketListener listener) {
55  listeners.remove(listener);
56  }
57 
58  };
59  final StringBuilder sb = new StringBuilder();
60  final ScriptProcess scriptProcess = new TestScriptProcess() {
61 
62  @Override
63  public void commandSent(@NotNull final String cmd) {
64  sb.append(cmd).append('\n');
65  }
66 
67  };
68  final PacketWatcher packetWatcher = new PacketWatcher(connection, scriptProcess);
69 
70  sb.setLength(0);
71  for (final ReceivedPacketListener listener : listeners) {
72  listener.processEmpty("command");
73  }
74  Assert.assertEquals("", sb.toString());
75 
76  packetWatcher.addCommand("command");
77 
78  sb.setLength(0);
79  for (final ReceivedPacketListener listener : listeners) {
80  //noinspection SpellCheckingInspection
81  listener.processEmpty("comman");
82  listener.processEmpty("command");
83  //noinspection SpellCheckingInspection
84  listener.processEmpty("commandx");
85  }
86  //noinspection SpellCheckingInspection
87  Assert.assertEquals("watch command\n"+"watch commandx\n", sb.toString());
88  }
89 
90 }
void test1()
Checks that PacketWatcher#addCommand(String) does work.
void addCommand(@NotNull final String command)
Adds a command to watch for.
Interface for listeners interested in received packets.
Implements the "watch" function for client-sided scripts.
Implements ScriptProcess for regression tests.
An external command executed as a client-sided script.
Adds encoding/decoding of crossfire protocol packets to a ServerConnection.