Crossfire JXClient, Trunk
BufferTest.java
Go to the documentation of this file.
1 package com.realtime.crossfire.jxclient.gui.log;
2 
4 import java.awt.Color;
5 import org.jetbrains.annotations.NotNull;
6 import org.junit.Assert;
7 import org.junit.Test;
8 
12 @SuppressWarnings("JavaDoc")
13 public class BufferTest {
14 
15  @Test
16  public void addLine_lines_addsLines() {
17  final TestBuffer buffer = new TestBuffer(3, 200, 10);
18 
19  addLine(buffer, "abc");
20  Assert.assertEquals("[0,9,abc]\n", format(buffer));
21 
22  addLine(buffer, "def", "ghi");
23  Assert.assertEquals("""
24  [0,9,abc]
25  [0,9,def][15,9,ghi]
26  """, format(buffer));
27 
28  addLine(buffer, "jkl");
29  Assert.assertEquals("""
30  [0,9,abc]
31  [0,9,def][15,9,ghi]
32  [0,9,jkl]
33  """, format(buffer));
34 
35  addLine(buffer, "mno", "pqr");
36  Assert.assertEquals("""
37  [0,9,abc]
38  [0,9,def][15,9,ghi]
39  [0,9,jkl]
40  [0,9,mno][15,9,pqr]
41  """, format(buffer));
42  }
43 
44  @Test
46  final TestBuffer buffer = new TestBuffer(3, 145, 10);
47 
48  addLine(buffer, "a", "b", "c", "d", "e");
49  Assert.assertEquals("[0,9,a][5,9,b][10,9,c][15,9,d][20,9,e]\n", format(buffer));
50  }
51 
52  @Test
54  final TestBuffer buffer = new TestBuffer(3, 200, 10);
55 
56  addLine(buffer, "abc");
57  buffer.prune();
58  Assert.assertEquals("[0,9,abc]\n", format(buffer));
59 
60  addLine(buffer, "def", "ghi");
61  buffer.prune();
62  Assert.assertEquals("""
63  [0,9,abc]
64  [0,9,def][15,9,ghi]
65  """, format(buffer));
66 
67  addLine(buffer, "jkl");
68  buffer.prune();
69  Assert.assertEquals("""
70  [0,9,abc]
71  [0,9,def][15,9,ghi]
72  [0,9,jkl]
73  """, format(buffer));
74 
75  addLine(buffer, "mno", "pqr");
76  buffer.prune();
77  Assert.assertEquals("""
78  [0,9,def][15,9,ghi]
79  [0,9,jkl]
80  [0,9,mno][15,9,pqr]
81  """, format(buffer));
82  }
83 
84  @Test
86  final TestBuffer buffer = new TestBuffer(3, 250, 10);
87 
88  addLine(buffer, "abc");
89  Assert.assertEquals("0", formatLines(buffer));
90  buffer.prune();
91  Assert.assertEquals("0", formatLines(buffer));
92 
93  addLine(buffer, "def", "ghi");
94  Assert.assertEquals("0 13", formatLines(buffer));
95  buffer.prune();
96  Assert.assertEquals("0 13", formatLines(buffer));
97 
98  addLine(buffer, "jkl");
99  Assert.assertEquals("0 13 29", formatLines(buffer));
100  buffer.prune();
101  Assert.assertEquals("0 13 29", formatLines(buffer));
102 
103  addLine(buffer, "mno", "pqr");
104  Assert.assertEquals("0 13 29 42", formatLines(buffer));
105  buffer.prune();
106  Assert.assertEquals("0 16 29", formatLines(buffer));
107  }
108 
109  @Test
111  final TestBuffer buffer = new TestBuffer(3, 200, 10);
112 
114  addLineWithType(buffer, MessageType.MSG_TYPE_BOOK, MessageType.MSG_SUBTYPE_BOOK_CLASP_1, "2");
116  addLineWithType(buffer, MessageType.MSG_TYPE_BOOK, MessageType.MSG_SUBTYPE_BOOK_CLASP_1, "4");
118  Assert.assertEquals("""
119  [0,9,1]
120  [0,9,2]
121  [0,9,3]
122  [0,9,4]
123  [0,9,5]
124  """, format(buffer));
125  buffer.prune();
126  Assert.assertEquals("""
127  [0,9,3]
128  [0,9,4]
129  [0,9,5]
130  """, format(buffer));
131  }
132 
133  private static void addLine(@NotNull final TestBuffer buffer, @NotNull final String @NotNull ... text) {
134  addLineWithType(buffer, MessageType.MSG_TYPE_BOOK, MessageType.MSG_SUBTYPE_BOOK_CLASP_1, text);
135  }
136 
137  private static void addLineWithType(@NotNull final TestBuffer buffer, final int type, final int subtype, @NotNull final String @NotNull ... text) {
138  final Line line = new Line(type, subtype, buffer.newTextSegment("timestamp ", false, false, false, FontID.PRINT, Color.BLACK, Color.BLACK));
139  for (final String word : text) {
140  buffer.addTextSegment(line, word, false, false, false, FontID.PRINT, Color.BLACK, Color.BLACK);
141  }
142  buffer.addLine(line);
143  }
144 
145  @NotNull
146  private static String format(@NotNull final TestBuffer buffer) {
147  final StringBuilder sb = new StringBuilder();
148  synchronized (buffer.getSyncObject()) {
149  for (final Line line : buffer.lines(0)) {
150  for (final TextSegment segment : line.segments()) {
151  sb.append(segment.format());
152  }
153  sb.append("\n");
154  }
155  }
156  return sb.toString();
157  }
158 
159  @NotNull
160  private static String formatLines(@NotNull final TestBuffer buffer) {
161  final StringBuilder sb = new StringBuilder();
162  synchronized (buffer.getSyncObject()) {
163  for (final Line line : buffer.lines(0)) {
164  if (!sb.isEmpty()) {
165  sb.append(" ");
166  }
167  sb.append(line.getStartPosition());
168  }
169  }
170  return sb.toString();
171  }
172 
173 }
com.realtime.crossfire.jxclient
com.realtime.crossfire.jxclient.gui.log.BufferTest
Tests for Buffer.
Definition: BufferTest.java:13
com.realtime.crossfire.jxclient.protocol.MessageType.MSG_TYPE_JXCLIENT
static final int MSG_TYPE_JXCLIENT
Internally used drawextinfo message type: a message has been generated by the client.
Definition: MessageType.java:136
com.realtime.crossfire.jxclient.gui.log.BufferTest.addLine_lines_addsLines
void addLine_lines_addsLines()
Definition: BufferTest.java:16
com.realtime.crossfire.jxclient.gui.log.BufferTest.prune_lines_prunesExcessLines
void prune_lines_prunesExcessLines()
Definition: BufferTest.java:85
com.realtime.crossfire.jxclient.gui.log.FontID.PRINT
PRINT
The default font.
Definition: FontID.java:34
com.realtime.crossfire.jxclient.protocol.MessageType.MSG_TYPE_BOOK
static final int MSG_TYPE_BOOK
drawextinfo message type: character did read a book.
Definition: MessageType.java:38
com.realtime.crossfire.jxclient.protocol.MessageType
Encapsulates the message type numbers for drawextinfo messages.
Definition: MessageType.java:33
com.realtime.crossfire.jxclient.protocol
Definition: MagicMap.java:23
com.realtime.crossfire.jxclient.gui.log.TextSegment
One segment of a Line which should be displayed without changing attributes.
Definition: TextSegment.java:40
com.realtime.crossfire.jxclient.gui.log.Line
Manages the contents of one text line.
Definition: Line.java:39
com.realtime.crossfire.jxclient.gui.log.BufferTest.prune_hideSentCommands_prunesExcessLines
void prune_hideSentCommands_prunesExcessLines()
Definition: BufferTest.java:110
com.realtime.crossfire.jxclient.protocol.MessageType.MSG_SUBTYPE_BOOK_CLASP_1
static final int MSG_SUBTYPE_BOOK_CLASP_1
Definition: MessageType.java:145
com.realtime.crossfire.jxclient.gui.log.FontID
Available font types.
Definition: FontID.java:29
com.realtime.crossfire.jxclient.gui.log.BufferTest.format
static String format(@NotNull final TestBuffer buffer)
Definition: BufferTest.java:146
com.realtime.crossfire.jxclient.gui.log.BufferTest.addLine
static void addLine(@NotNull final TestBuffer buffer, @NotNull final String @NotNull ... text)
Definition: BufferTest.java:133
com.realtime.crossfire.jxclient.gui.log.BufferTest.addLine_longLine_wrapsCorrectly
void addLine_longLine_wrapsCorrectly()
Definition: BufferTest.java:45
com.realtime.crossfire.jxclient.gui.log.BufferTest.formatLines
static String formatLines(@NotNull final TestBuffer buffer)
Definition: BufferTest.java:160
com.realtime.crossfire.jxclient.gui.log.Buffer.prune
void prune()
Prunes excess lines.
Definition: Buffer.java:232
com.realtime.crossfire
com.realtime.crossfire.jxclient.protocol.MessageType.MSG_SUBTYPE_JXCLIENT_COMMAND
static final int MSG_SUBTYPE_JXCLIENT_COMMAND
A command that has been sent to the server.
Definition: MessageType.java:479
com.realtime
com.realtime.crossfire.jxclient.gui.log.BufferTest.prune_pos_prunesExcessLines
void prune_pos_prunesExcessLines()
Definition: BufferTest.java:53
com
com.realtime.crossfire.jxclient.gui.log.TestBuffer
A Buffer implementation for tests.
Definition: TestBuffer.java:11
com.realtime.crossfire.jxclient.gui.log.BufferTest.addLineWithType
static void addLineWithType(@NotNull final TestBuffer buffer, final int type, final int subtype, @NotNull final String @NotNull ... text)
Definition: BufferTest.java:137