📄 stringutilstests.java
字号:
/*
* Copyright 2002-2006 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.util;
import java.util.Arrays;
import java.util.Properties;
import junit.framework.TestCase;
/**
* @author Rod Johnson
* @author Juergen Hoeller
*/
public class StringUtilsTests extends TestCase {
public void testHasTextBlank() throws Exception {
String blank = " ";
assertEquals(false, StringUtils.hasText(blank));
}
public void testHasTextNullEmpty() throws Exception {
assertEquals(false, StringUtils.hasText(null));
assertEquals(false, StringUtils.hasText(""));
}
public void testHasTextValid() throws Exception {
assertEquals(true, StringUtils.hasText("t"));
}
public void testTrimLeadingWhitespace() throws Exception {
assertEquals("", StringUtils.trimLeadingWhitespace(""));
assertEquals("", StringUtils.trimLeadingWhitespace(" "));
assertEquals("", StringUtils.trimLeadingWhitespace("\t"));
assertEquals("a", StringUtils.trimLeadingWhitespace(" a"));
assertEquals("a ", StringUtils.trimLeadingWhitespace("a "));
assertEquals("a ", StringUtils.trimLeadingWhitespace(" a "));
}
public void testTrimTrailingWhitespace() throws Exception {
assertEquals("", StringUtils.trimTrailingWhitespace(""));
assertEquals("", StringUtils.trimTrailingWhitespace(" "));
assertEquals("", StringUtils.trimTrailingWhitespace("\t"));
assertEquals("a", StringUtils.trimTrailingWhitespace("a "));
assertEquals(" a", StringUtils.trimTrailingWhitespace(" a"));
assertEquals(" a", StringUtils.trimTrailingWhitespace(" a "));
}
public void testCountOccurrencesOf() {
assertTrue("nullx2 = 0",
StringUtils.countOccurrencesOf(null, null) == 0);
assertTrue("null string = 0",
StringUtils.countOccurrencesOf("s", null) == 0);
assertTrue("null substring = 0",
StringUtils.countOccurrencesOf(null, "s") == 0);
String s = "erowoiueoiur";
assertTrue("not found = 0",
StringUtils.countOccurrencesOf(s, "WERWER") == 0);
assertTrue("not found char = 0",
StringUtils.countOccurrencesOf(s, "x") == 0);
assertTrue("not found ws = 0",
StringUtils.countOccurrencesOf(s, " ") == 0);
assertTrue("not found empty string = 0",
StringUtils.countOccurrencesOf(s, "") == 0);
assertTrue("found char=2", StringUtils.countOccurrencesOf(s, "e") == 2);
assertTrue("found substring=2",
StringUtils.countOccurrencesOf(s, "oi") == 2);
assertTrue("found substring=2",
StringUtils.countOccurrencesOf(s, "oiu") == 2);
assertTrue("found substring=3",
StringUtils.countOccurrencesOf(s, "oiur") == 1);
assertTrue("test last", StringUtils.countOccurrencesOf(s, "r") == 2);
}
public void testReplace() throws Exception {
String inString = "a6AazAaa77abaa";
String oldPattern = "aa";
String newPattern = "foo";
// Simple replace
String s = StringUtils.replace(inString, oldPattern, newPattern);
assertTrue("Replace 1 worked", s.equals("a6AazAfoo77abfoo"));
// Non match: no change
s = StringUtils.replace(inString, "qwoeiruqopwieurpoqwieur", newPattern);
assertTrue("Replace non matched is equal", s.equals(inString));
// Null new pattern: should ignore
s = StringUtils.replace(inString, oldPattern, null);
assertTrue("Replace non matched is equal", s.equals(inString));
// Null old pattern: should ignore
s = StringUtils.replace(inString, null, newPattern);
assertTrue("Replace non matched is equal", s.equals(inString));
}
public void testDelete() throws Exception {
String inString = "The quick brown fox jumped over the lazy dog";
String noThe = StringUtils.delete(inString, "the");
assertTrue("Result has no the [" + noThe + "]",
noThe.equals("The quick brown fox jumped over lazy dog"));
String nohe = StringUtils.delete(inString, "he");
assertTrue("Result has no he [" + nohe + "]",
nohe.equals("T quick brown fox jumped over t lazy dog"));
String nosp = StringUtils.delete(inString, " ");
assertTrue("Result has no spaces",
nosp.equals("Thequickbrownfoxjumpedoverthelazydog"));
String killEnd = StringUtils.delete(inString, "dog");
assertTrue("Result has no dog",
killEnd.equals("The quick brown fox jumped over the lazy "));
String mismatch = StringUtils.delete(inString, "dxxcxcxog");
assertTrue("Result is unchanged", mismatch.equals(inString));
}
public void testDeleteAny() throws Exception {
String inString = "Able was I ere I saw Elba";
String res = StringUtils.deleteAny(inString, "I");
assertTrue("Result has no Is [" + res + "]",
res.equals("Able was ere saw Elba"));
res = StringUtils.deleteAny(inString, "AeEba!");
assertTrue("Result has no Is [" + res + "]",
res.equals("l ws I r I sw l"));
String mismatch = StringUtils.deleteAny(inString, "#@$#$^");
assertTrue("Result is unchanged", mismatch.equals(inString));
String whitespace =
"This is\n\n\n \t a messagy string with whitespace\n";
assertTrue("Has CR", whitespace.indexOf("\n") != -1);
assertTrue("Has tab", whitespace.indexOf("\t") != -1);
assertTrue("Has sp", whitespace.indexOf(" ") != -1);
String cleaned = StringUtils.deleteAny(whitespace, "\n\t ");
assertTrue("Has no CR", cleaned.indexOf("\n") == -1);
assertTrue("Has no tab", cleaned.indexOf("\t") == -1);
assertTrue("Has no sp", cleaned.indexOf(" ") == -1);
assertTrue("Still has chars", cleaned.length() > 10);
}
public void testQuote() {
assertEquals("'myString'", StringUtils.quote("myString"));
assertEquals("''", StringUtils.quote(""));
assertNull(StringUtils.quote(null));
}
public void testQuoteIfString() {
assertEquals("'myString'", StringUtils.quoteIfString("myString"));
assertEquals("''", StringUtils.quoteIfString(""));
assertEquals(new Integer(5), StringUtils.quoteIfString(new Integer(5)));
assertNull(StringUtils.quoteIfString(null));
}
public void testUnqualify() {
String qualified = "i.am.not.unqualified";
assertEquals("unqualified", StringUtils.unqualify(qualified));
}
public void testCapitalize() {
String capitalized = "i am not capitalized";
assertEquals("I am not capitalized", StringUtils.capitalize(capitalized));
}
public void testUncapitalize() {
String capitalized = "I am capitalized";
assertEquals("i am capitalized", StringUtils.uncapitalize(capitalized));
}
public void testGetFilename() {
assertEquals(null, StringUtils.getFilename(null));
assertEquals("", StringUtils.getFilename(""));
assertEquals("myfile", StringUtils.getFilename("myfile"));
assertEquals("myfile", StringUtils.getFilename("mypath/myfile"));
assertEquals("myfile.", StringUtils.getFilename("myfile."));
assertEquals("myfile.", StringUtils.getFilename("mypath/myfile."));
assertEquals("myfile.txt", StringUtils.getFilename("myfile.txt"));
assertEquals("myfile.txt", StringUtils.getFilename("mypath/myfile.txt"));
}
public void testGetFilenameExtension() {
assertEquals(null, StringUtils.getFilenameExtension(null));
assertEquals(null, StringUtils.getFilenameExtension(""));
assertEquals(null, StringUtils.getFilenameExtension("myfile"));
assertEquals(null, StringUtils.getFilenameExtension("myPath/myfile"));
assertEquals("", StringUtils.getFilenameExtension("myfile."));
assertEquals("", StringUtils.getFilenameExtension("myPath/myfile."));
assertEquals("txt", StringUtils.getFilenameExtension("myfile.txt"));
assertEquals("txt", StringUtils.getFilenameExtension("mypath/myfile.txt"));
}
public void testStripFilenameExtension() {
assertEquals(null, StringUtils.stripFilenameExtension(null));
assertEquals("", StringUtils.stripFilenameExtension(""));
assertEquals("myfile", StringUtils.stripFilenameExtension("myfile"));
assertEquals("mypath/myfile", StringUtils.stripFilenameExtension("mypath/myfile"));
assertEquals("myfile", StringUtils.stripFilenameExtension("myfile."));
assertEquals("mypath/myfile", StringUtils.stripFilenameExtension("mypath/myfile."));
assertEquals("myfile", StringUtils.stripFilenameExtension("myfile.txt"));
assertEquals("mypath/myfile", StringUtils.stripFilenameExtension("mypath/myfile.txt"));
}
public void testCleanPath() {
assertEquals("mypath/myfile", StringUtils.cleanPath("mypath/myfile"));
assertEquals("mypath/myfile", StringUtils.cleanPath("mypath\\myfile"));
assertEquals("mypath/myfile", StringUtils.cleanPath("mypath/../mypath/myfile"));
assertEquals("mypath/myfile", StringUtils.cleanPath("mypath/myfile/../../mypath/myfile"));
assertEquals("../mypath/myfile", StringUtils.cleanPath("../mypath/myfile"));
assertEquals("../mypath/myfile", StringUtils.cleanPath("../mypath/../mypath/myfile"));
assertEquals("../mypath/myfile", StringUtils.cleanPath("mypath/../../mypath/myfile"));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -