Gridarta Editor
StringUtilsTest.java
Go to the documentation of this file.
1 /*
2  * Gridarta MMORPG map editor for Crossfire, Daimonin and similar games.
3  * Copyright (C) 2000-2015 The Gridarta Developers.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 
20 package net.sf.gridarta.utils;
21 
22 import org.jetbrains.annotations.NotNull;
23 import org.jetbrains.annotations.Nullable;
24 import org.junit.Assert;
25 import org.junit.Test;
26 
31 public class StringUtilsTest {
32 
36  @Test
40  testRemoveTrailingWhitespace(" abc def ", " abc def");
41  testRemoveTrailingWhitespace(" abc \n def ", " abc \n def");
42  }
43 
49  private static void testRemoveTrailingWhitespace(final CharSequence input, final String output) {
50  Assert.assertEquals(output, StringUtils.removeTrailingWhitespace(input));
51  }
52 
56  @Test
60  testRemoveTrailingWhitespaceFromLines(" abc def ", " abc def");
62  testRemoveTrailingWhitespaceFromLines("abc\n" + "def\n" + "ghi\n", "abc\n" + "def\n" + "ghi\n");
63  testRemoveTrailingWhitespaceFromLines(" abc\n" + "def\n" + "ghi\n", " abc\n" + "def\n" + "ghi\n");
64  testRemoveTrailingWhitespaceFromLines("abc \n" + "def\n" + "ghi\n", "abc\n" + "def\n" + "ghi\n");
65  testRemoveTrailingWhitespaceFromLines(" abc \n d e f \n g h i \n ", " abc\n d e f\n g h i\n");
66  }
67 
73  private static void testRemoveTrailingWhitespaceFromLines(@NotNull final CharSequence input, @NotNull final String output) {
74  Assert.assertEquals(output, StringUtils.removeTrailingWhitespaceFromLines(input));
75  }
76 
80  @Test
81  public void testEnsureTrailingNewline() {
83  testEnsureTrailingNewline("\n", "\n");
84  testEnsureTrailingNewline(" abc def ", " abc def \n");
85  testEnsureTrailingNewline("\n\n\n", "\n\n\n");
86  testEnsureTrailingNewline("abc\n\n" + "def", "abc\n\n" + "def\n");
87  }
88 
94  private static void testEnsureTrailingNewline(@NotNull final String input, @NotNull final String output) {
95  Assert.assertEquals(output, StringUtils.ensureTrailingNewline(input));
96  }
97 
102  @Test
103  public void testDiffTextString() {
104  testDiffTextString("", "abc", null, null);
105 
106  testDiffTextString("abc", "abc", "abc", "abc");
107  testDiffTextString("abc\n" + "def", "abc", "abc", "abc");
108  testDiffTextString("def\n" + "abc\n" + "def", "abc", "abc", "abc");
109  testDiffTextString("def\n" + "abc", "abc", "abc", "abc");
110 
111  testDiffTextString("abc def", "abc", null, "abc def");
112  testDiffTextString("def abc\n" + "abc def", "abc", null, "abc def");
113  testDiffTextString("abc def\n" + "def abc", "abc", null, "abc def");
114  testDiffTextString("def abc\n" + "abc def\n" + "def abc", "abc", null, "abc def");
115  }
116 
127  private static void testDiffTextString(@NotNull final CharSequence base, @NotNull final String str, @Nullable final String expectedFalse, @Nullable final String expectedTrue) {
128  Assert.assertEquals(expectedFalse, StringUtils.diffTextString(base, str, false));
129  Assert.assertEquals(expectedTrue, StringUtils.diffTextString(base, str, true));
130  }
131 
135  @Test
136  public void testRemoveAttribute() {
137  testGetAttribute("", "abc", null);
138  testGetAttribute("abc def", "abc", "def");
139  testGetAttribute("abc def", "ab", null);
140  testGetAttribute("abc def", "def", null);
141  testGetAttribute("abc def\n" + "ghi jkl\n" + "mno pqr\n", "abc", "def");
142  testGetAttribute("abc def\n" + "ghi jkl\n" + "mno pqr\n", "ghi", "jkl");
143  testGetAttribute("abc def\n" + "ghi jkl\n" + "mno pqr\n", "mno", "pqr");
144  testGetAttribute("abc def\n" + "ghi jkl\n" + "mno pqr\n", "xxx", null);
145  }
146 
154  private static void testGetAttribute(@NotNull final CharSequence attributes, @NotNull final String attributeName, @Nullable final String expectedAttributeValue) {
155  final String attributeValue = StringUtils.getAttribute(attributes, attributeName);
156  Assert.assertEquals(expectedAttributeValue, attributeValue);
157  }
158 
159 }
static void testEnsureTrailingNewline(@NotNull final String input, @NotNull final String output)
Checks one invocation of StringUtils#ensureTrailingNewline(String).
Utility class for string manipulation.
static String removeTrailingWhitespace(@NotNull final CharSequence str)
Removes trailing whitespace from a string.
void testRemoveAttribute()
Test case for StringUtils#getAttribute(CharSequence, String).
static CharSequence ensureTrailingNewline(@NotNull final String str)
Returns a given string which ends with a trailing newline character; empty strings remain empty...
static String getAttribute(@NotNull final CharSequence attributes, @NotNull final String attributeName)
Returns an attribute line from a set of attribute definitions.
static String removeTrailingWhitespaceFromLines(@NotNull final CharSequence str)
Removes trailing whitespace from all lines of a string.
static CharSequence diffTextString(@NotNull final CharSequence base, @NotNull final String str, final boolean ignoreValues)
Helper function for 'diffArchText()': Looks for occurrence of the attribute 'str' in 'base' and if fo...
static void testRemoveTrailingWhitespace(final CharSequence input, final String output)
Checks one invocation of StringUtils#removeTrailingWhitespace(CharSequence).
static void testGetAttribute(@NotNull final CharSequence attributes, @NotNull final String attributeName, @Nullable final String expectedAttributeValue)
Checks that StringUtils#getAttribute(CharSequence, String) does work.
void testRemoveTrailingWhitespaceFromLines()
Test case for StringUtils#removeTrailingWhitespaceFromLines(CharSequence).
void testRemoveTrailingWhitespace()
Test case for StringUtils#removeTrailingWhitespace(CharSequence).
void testEnsureTrailingNewline()
Test case for StringUtils#ensureTrailingNewline(String).
static void testDiffTextString(@NotNull final CharSequence base, @NotNull final String str, @Nullable final String expectedFalse, @Nullable final String expectedTrue)
Checks two invocations of String, boolean).
static void testRemoveTrailingWhitespaceFromLines(@NotNull final CharSequence input, @NotNull final String output)
Checks one invocation of StringUtils#removeTrailingWhitespaceFromLines(CharSequence).
void testDiffTextString()
Test case for String, boolean).