Crossfire JXClient, Trunk  R20561
ParserTest.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.gui.log;
23 
24 import java.awt.Font;
25 import java.awt.FontFormatException;
26 import java.awt.Graphics2D;
27 import java.awt.GraphicsConfiguration;
28 import java.awt.GraphicsDevice;
29 import java.awt.GraphicsEnvironment;
30 import java.awt.Transparency;
31 import java.awt.image.BufferedImage;
32 import java.io.IOException;
33 import java.io.InputStream;
34 import java.util.Iterator;
35 import org.jetbrains.annotations.NotNull;
36 import org.junit.Assert;
37 import org.junit.Test;
38 
43 public class ParserTest {
44 
48  @Test
49  public void testEmpty() {
50  if (GraphicsEnvironment.isHeadless()) {
51  return;
52  }
53 
54  final Rec rec = new Rec();
55  rec.parser.parse("", null, rec.buffer);
56  rec.checkResult(""+"buffer:\n"+"line:\n");
57  }
58 
62  @Test
63  public void testAttributesIgnore() {
64  if (GraphicsEnvironment.isHeadless()) {
65  return;
66  }
67 
68  final Rec rec = new Rec();
69  rec.parser.parse("a[a]b[]c[[]d[]]e", null, rec.buffer);
70  rec.checkResult(""+"buffer:\n"+"line:\n"+"segment:abc[]d]e\n");
71  }
72 
76  @Test
77  public void testAttributes1() {
78  if (GraphicsEnvironment.isHeadless()) {
79  return;
80  }
81 
82  final Rec rec = new Rec();
83  rec.parser.parse("a b c", null, rec.buffer);
84  rec.parser.parse("[b]a b c", null, rec.buffer);
85  rec.parser.parse("[i]a b c", null, rec.buffer);
86  rec.parser.parse("[ul]a b c", null, rec.buffer);
87  rec.parser.parse("[fixed]a b c", null, rec.buffer);
88  rec.parser.parse("[arcane]a b c", null, rec.buffer);
89  rec.parser.parse("[hand]a b c", null, rec.buffer);
90  rec.parser.parse("[strange]a b c", null, rec.buffer);
91  rec.parser.parse("[print]a b c", null, rec.buffer);
92  rec.parser.parse("[color=red]a b c", null, rec.buffer);
93  rec.checkResult(""+"buffer:\n"+"line:\n"+"segment:a \n"+"segment:b \n"+"segment:c\n"+"line:\n"+"segment:(bold)a \n"+"segment:(bold)b \n"+"segment:(bold)c\n"+"line:\n"+"segment:(italic)a \n"+"segment:(italic)b \n"+"segment:(italic)c\n"+"line:\n"+"segment:(underline)a \n"+"segment:(underline)b \n"+"segment:(underline)c\n"+"line:\n"+"segment:(fixed)a \n"+"segment:(fixed)b \n"+"segment:(fixed)c\n"+"line:\n"+"segment:(arcane)a \n"+"segment:(arcane)b \n"+"segment:(arcane)c\n"+"line:\n"+"segment:(hand)a \n"+"segment:(hand)b \n"+"segment:(hand)c\n"+"line:\n"+"segment:(strange)a \n"+"segment:(strange)b \n"+"segment:(strange)c\n"+"line:\n"+"segment:a \n"+"segment:b \n"+"segment:c\n"+"line:\n"+"segment:(red)a \n"+"segment:(red)b \n"+"segment:(red)c\n");
94  }
95 
99  @Test
100  public void testAttributes2() {
101  if (GraphicsEnvironment.isHeadless()) {
102  return;
103  }
104 
105  final Rec rec = new Rec();
106  rec.parser.parse("a[b]b[i]c[ul]d[/b]e[/i]f[/ul]g", null, rec.buffer);
107  rec.parser.parse("Hello [b] all [b]crossfire[/b] members [/b]", null, rec.buffer);
108  rec.checkResult(""+"buffer:\n"+"line:\n"+"segment:a\n"+"segment:(bold)b\n"+"segment:(bold)(italic)c\n"+"segment:(bold)(italic)(underline)d\n"+"segment:(italic)(underline)e\n"+"segment:(underline)f\n"+"segment:g\n"+"line:\n"+"segment:Hello \n"+"segment:(bold) \n"+"segment:(bold)all \n"+"segment:(bold)crossfire\n"+"segment: \n"+"segment:members \n");
109  }
110 
114  @Test
115  public void testAttributesFont1() {
116  if (GraphicsEnvironment.isHeadless()) {
117  return;
118  }
119 
120  final Rec rec = new Rec();
121  rec.parser.parse("[b]a[fixed]b[arcane]c[hand]d[strange]e[print]f", null, rec.buffer);
122  rec.checkResult(""+"buffer:\n"+"line:\n"+"segment:(bold)a\n"+"segment:(bold)(fixed)b\n"+"segment:(bold)(arcane)c\n"+"segment:(bold)(hand)d\n"+"segment:(bold)(strange)e\n"+"segment:(bold)f\n");
123  }
124 
129  @Test
130  public void testAttributesFont2() {
131  if (GraphicsEnvironment.isHeadless()) {
132  return;
133  }
134 
135  final Rec rec = new Rec();
136  rec.parser.parse("a[fixed]b[/fixed]c", null, rec.buffer);
137  rec.checkResult(""+"buffer:\n"+"line:\n"+"segment:a\n"+"segment:(fixed)bc\n");
138  }
139 
143  @Test
144  public void testAttributesColor() {
145  if (GraphicsEnvironment.isHeadless()) {
146  return;
147  }
148 
149  final Rec rec = new Rec();
150  rec.parser.parse("[b]a[color=red]b[color=blue]c[color=green]d[/color]e[color=#01AB4F]f[/color]", null, rec.buffer);
151  rec.checkResult(""+"buffer:\n"+"line:\n"+"segment:(bold)a\n"+"segment:(bold)(red)b\n"+"segment:(bold)(blue)c\n"+"segment:(bold)(green)d\n"+"segment:(bold)e\n" + "segment:(bold)(java.awt.Color[r=1,g=171,b=79])f\n");
152  }
153 
157  @Test
158  public void testAttributesReset() {
159  if (GraphicsEnvironment.isHeadless()) {
160  return;
161  }
162 
163  final Rec rec = new Rec();
164  rec.parser.parse("[b][i][ul][hand][color=red]first", null, rec.buffer);
165  rec.parser.parse("second", null, rec.buffer);
166  rec.checkResult(""+"buffer:\n"+"line:\n"+"segment:(bold)(italic)(underline)(hand)(red)first\n"+"line:\n"+"segment:second\n");
167  }
168 
172  @Test
173  public void testMultiLine() {
174  if (GraphicsEnvironment.isHeadless()) {
175  return;
176  }
177 
178  final Rec rec = new Rec();
179  rec.parser.parse("first\n[b]second\nth[/b]ird[i]\n"+"fourth", null, rec.buffer);
180  rec.checkResult(""+"buffer:\n"+"line:\n"+"segment:first\n"+"line:\n"+"segment:(bold)second\n"+"line:\n"+"segment:(bold)th\n"+"segment:ird\n"+"line:\n"+"segment:(italic)fourth\n");
181  }
182 
186  @Test
187  public void testDropUnClosedTag() {
188  if (GraphicsEnvironment.isHeadless()) {
189  return;
190  }
191 
192  final Rec rec = new Rec();
193  rec.parser.parse("abc[fixed", null, rec.buffer);
194  rec.checkResult(""+"buffer:\n"+"line:\n"+"segment:abc\n");
195  }
196 
200  private static class Rec {
201 
205  @NotNull
206  private final Parser parser;
207 
211  @NotNull
212  private final Buffer buffer;
213 
217  private Rec() {
218  final GraphicsEnvironment graphicsEnvironment = GraphicsEnvironment.getLocalGraphicsEnvironment();
219  final GraphicsDevice graphicsDevice = graphicsEnvironment.getDefaultScreenDevice();
220  final GraphicsConfiguration graphicsConfiguration = graphicsDevice.getDefaultConfiguration();
221  final BufferedImage image = graphicsConfiguration.createCompatibleImage(1, 1, Transparency.TRANSLUCENT);
222  final Graphics2D g = image.createGraphics();
223  parser = new Parser();
224  final Font font;
225  try {
226  try (final InputStream fis = getClass().getClassLoader().getResourceAsStream("com/realtime/crossfire/jxclient/skins/ragnorok/fonts/regular.ttf")) {
227  try {
228  font = Font.createFont(Font.TRUETYPE_FONT, fis);
229  } catch (final FontFormatException ex) {
230  Assert.fail(ex.getMessage());
231  throw new AssertionError(ex);
232  }
233  }
234  } catch (final IOException ex) {
235  Assert.fail(ex.getMessage());
236  throw new AssertionError(ex);
237  }
238  buffer = new Buffer(new Fonts(font, font, font, font), g.getFontRenderContext(), 100);
239  g.dispose();
240  }
241 
246  private void checkResult(@NotNull final String expected) {
247  Assert.assertEquals(expected, dumpBuffer());
248  }
249 
254  @NotNull
255  private String dumpBuffer() {
256  final StringBuilder sb = new StringBuilder();
257  dumpBuffer(sb, buffer);
258  return sb.toString();
259  }
260 
266  private static void dumpBuffer(@NotNull final StringBuilder sb, @NotNull final Buffer buffer) {
267  sb.append("buffer:\n");
268  synchronized (buffer.getSyncObject()) {
269  final Iterator<Line> it = buffer.iterator();
270  while (it.hasNext()) {
271  final Iterable<Segment> line = it.next();
272  dumpLine(sb, line);
273  }
274  }
275  }
276 
282  private static void dumpLine(@NotNull final StringBuilder sb, @NotNull final Iterable<Segment> line) {
283  sb.append("line:\n");
284  for (final Segment segment : line) {
285  sb.append(segment);
286  }
287  }
288 
289  }
290 
291 }
static void dumpLine(@NotNull final StringBuilder sb, @NotNull final Iterable< Segment > line)
Appends the contents of a Line to a StringBuilder.
Parser for parsing drawextinfo messages received from a Crossfire server to update a Buffer instance...
Definition: Parser.java:37
void testAttributesColor()
Checks that color attributes are correctly parsed.
void checkResult(@NotNull final String expected)
Checks for expected contents of buffer.
void testMultiLine()
Checks that multi-line messages are correctly parsed.
void testAttributesReset()
Checks that attributes are reset for each message.
void testAttributes2()
Checks that attributes are correctly parsed.
static void dumpBuffer(@NotNull final StringBuilder sb, @NotNull final Buffer buffer)
Appends the contents of a Buffer to a StringBuilder.
One segment of a Line which should be displayed without changing text attributes. ...
Definition: Segment.java:34
String dumpBuffer()
Returns a string representation of buffer.
void testAttributes1()
Checks that attributes are correctly parsed.
Definition: ParserTest.java:77
void testDropUnClosedTag()
Checks that an un-closed tag is dropped.
Manages the contents of the contents of a log window.
Definition: Buffer.java:41
void testAttributesIgnore()
Checks that unknown attributes are ignored.
Definition: ParserTest.java:63
void testAttributesFont1()
Checks that font attributes are correctly parsed.
void parse(@NotNull final CharSequence text, @Nullable final Color defaultColor, @NotNull final Buffer buffer)
Parses a text message.
Definition: Parser.java:111
void testEmpty()
Checks that an empty string does not add anything.
Definition: ParserTest.java:49
void testAttributesFont2()
Checks that font attributes are correctly parsed: [/fixed] is undefined/does not end [fixed] block...