001/* 002 * Gridarta MMORPG map editor for Crossfire, Daimonin and similar games. 003 * Copyright (C) 2000-2010 The Gridarta Developers. 004 * 005 * This program is free software; you can redistribute it and/or modify 006 * it under the terms of the GNU General Public License as published by 007 * the Free Software Foundation; either version 2 of the License, or 008 * (at your option) any later version. 009 * 010 * This program is distributed in the hope that it will be useful, 011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 013 * GNU General Public License for more details. 014 * 015 * You should have received a copy of the GNU General Public License along 016 * with this program; if not, write to the Free Software Foundation, Inc., 017 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 018 */ 019 020package net.sf.gridarta.utils; 021 022import org.jetbrains.annotations.NotNull; 023import org.jetbrains.annotations.Nullable; 024import org.junit.Assert; 025import org.junit.Test; 026 027/** 028 * Test for {@link StringUtils}. 029 * @author Andreas Kirschbaum 030 */ 031public class StringUtilsTest { 032 033 /** 034 * Test case for {@link StringUtils#removeTrailingWhitespace(CharSequence)}. 035 */ 036 @Test 037 public void testRemoveTrailingWhitespace() { 038 testRemoveTrailingWhitespace("", ""); 039 testRemoveTrailingWhitespace(" ", ""); 040 testRemoveTrailingWhitespace(" abc def ", " abc def"); 041 testRemoveTrailingWhitespace(" abc \n def ", " abc \n def"); 042 } 043 044 /** 045 * Checks one invocation of {@link StringUtils#removeTrailingWhitespace(CharSequence)}. 046 * @param input the input string 047 * @param output the expected output string 048 */ 049 private static void testRemoveTrailingWhitespace(final CharSequence input, final String output) { 050 Assert.assertEquals(output, StringUtils.removeTrailingWhitespace(input)); 051 } 052 053 /** 054 * Test case for {@link StringUtils#removeTrailingWhitespaceFromLines(CharSequence)}. 055 */ 056 @Test 057 public void testRemoveTrailingWhitespaceFromLines() { 058 testRemoveTrailingWhitespaceFromLines("", ""); 059 testRemoveTrailingWhitespaceFromLines(" ", ""); 060 testRemoveTrailingWhitespaceFromLines(" abc def ", " abc def"); 061 testRemoveTrailingWhitespaceFromLines("\n", "\n"); 062 testRemoveTrailingWhitespaceFromLines("abc\n" + "def\n" + "ghi\n", "abc\n" + "def\n" + "ghi\n"); 063 testRemoveTrailingWhitespaceFromLines(" abc\n" + "def\n" + "ghi\n", " abc\n" + "def\n" + "ghi\n"); 064 testRemoveTrailingWhitespaceFromLines("abc \n" + "def\n" + "ghi\n", "abc\n" + "def\n" + "ghi\n"); 065 testRemoveTrailingWhitespaceFromLines(" abc \n d e f \n g h i \n ", " abc\n d e f\n g h i\n"); 066 } 067 068 /** 069 * Checks one invocation of {@link StringUtils#removeTrailingWhitespaceFromLines(CharSequence)}. 070 * @param input the input string 071 * @param output the expected output string 072 */ 073 private static void testRemoveTrailingWhitespaceFromLines(@NotNull final CharSequence input, @NotNull final String output) { 074 Assert.assertEquals(output, StringUtils.removeTrailingWhitespaceFromLines(input)); 075 } 076 077 /** 078 * Test case for {@link StringUtils#ensureTrailingNewline(String)}. 079 */ 080 @Test 081 public void testEnsureTrailingNewline() { 082 testEnsureTrailingNewline("", ""); 083 testEnsureTrailingNewline("\n", "\n"); 084 testEnsureTrailingNewline(" abc def ", " abc def \n"); 085 testEnsureTrailingNewline("\n\n\n", "\n\n\n"); 086 testEnsureTrailingNewline("abc\n\n" + "def", "abc\n\n" + "def\n"); 087 } 088 089 /** 090 * Checks one invocation of {@link StringUtils#ensureTrailingNewline(String)}. 091 * @param input the input string 092 * @param output the expected output string 093 */ 094 private static void testEnsureTrailingNewline(@NotNull final String input, @NotNull final String output) { 095 Assert.assertEquals(output, StringUtils.ensureTrailingNewline(input)); 096 } 097 098 /** 099 * Test case for {@link StringUtils#diffTextString(CharSequence, String, 100 * boolean)}. 101 */ 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 117 /** 118 * Checks two invocations of {@link StringUtils#diffTextString(CharSequence, 119 * String, boolean)}. 120 * @param base the 'base' parameter to use 121 * @param str the 'str' parameter to use 122 * @param expectedFalse the expected return value if 'ignoreValues' is 123 * <code>false</code> 124 * @param expectedTrue the expected return value if 'ignoreValues' is 125 * <code>true</code> 126 */ 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 132 /** 133 * Test case for {@link StringUtils#getAttribute(CharSequence, String)}. 134 */ 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 147 /** 148 * Checks that {@link StringUtils#getAttribute(CharSequence, String)} does 149 * work. 150 * @param attributes the attributes to search 151 * @param attributeName the attribute name to search for 152 * @param expectedAttributeValue the found attribute value 153 */ 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}