Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
爱吃血肠
spring-framework
提交
9a88ebde
S
spring-framework
项目概览
爱吃血肠
/
spring-framework
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
S
spring-framework
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
9a88ebde
编写于
10月 22, 2017
作者:
J
Juergen Hoeller
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Consistent hasText checks for CharSequence vs String
Issue: SPR-15540
上级
92889906
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
34 addition
and
34 deletion
+34
-34
spring-core/src/main/java/org/springframework/util/StringUtils.java
...e/src/main/java/org/springframework/util/StringUtils.java
+12
-12
spring-core/src/test/java/org/springframework/util/StringUtilsTests.java
.../test/java/org/springframework/util/StringUtilsTests.java
+22
-22
未找到文件。
spring-core/src/main/java/org/springframework/util/StringUtils.java
浏览文件 @
9a88ebde
...
...
@@ -139,17 +139,7 @@ public abstract class StringUtils {
* @see Character#isWhitespace
*/
public
static
boolean
hasText
(
@Nullable
CharSequence
str
)
{
if
(!
hasLength
(
str
))
{
return
false
;
}
int
strLen
=
str
.
length
();
for
(
int
i
=
0
;
i
<
strLen
;
i
++)
{
if
(!
Character
.
isWhitespace
(
str
.
charAt
(
i
)))
{
return
true
;
}
}
return
false
;
return
(
hasLength
(
str
)
&&
containsText
(
str
));
}
/**
...
...
@@ -163,9 +153,19 @@ public abstract class StringUtils {
* @see #hasText(CharSequence)
*/
public
static
boolean
hasText
(
@Nullable
String
str
)
{
return
(
str
!=
null
&&
!
str
.
isEmpty
()
&&
hasText
((
CharSequence
)
str
));
return
(
hasLength
(
str
)
&&
containsText
(
str
));
}
private
static
boolean
containsText
(
CharSequence
str
)
{
int
strLen
=
str
.
length
();
for
(
int
i
=
0
;
i
<
strLen
;
i
++)
{
if
(!
Character
.
isWhitespace
(
str
.
charAt
(
i
)))
{
return
true
;
}
}
return
false
;
}
/**
* Check whether the given {@code CharSequence} contains any whitespace characters.
* @param str the {@code CharSequence} to check (may be {@code null})
...
...
spring-core/src/test/java/org/springframework/util/StringUtilsTests.java
浏览文件 @
9a88ebde
...
...
@@ -32,24 +32,24 @@ import static org.junit.Assert.*;
public
class
StringUtilsTests
{
@Test
public
void
testHasTextBlank
()
throws
Exception
{
public
void
testHasTextBlank
()
{
String
blank
=
" "
;
assertEquals
(
false
,
StringUtils
.
hasText
(
blank
));
}
@Test
public
void
testHasTextNullEmpty
()
throws
Exception
{
public
void
testHasTextNullEmpty
()
{
assertEquals
(
false
,
StringUtils
.
hasText
(
null
));
assertEquals
(
false
,
StringUtils
.
hasText
(
""
));
}
@Test
public
void
testHasTextValid
()
throws
Exception
{
public
void
testHasTextValid
()
{
assertEquals
(
true
,
StringUtils
.
hasText
(
"t"
));
}
@Test
public
void
testContainsWhitespace
()
throws
Exception
{
public
void
testContainsWhitespace
()
{
assertFalse
(
StringUtils
.
containsWhitespace
(
null
));
assertFalse
(
StringUtils
.
containsWhitespace
(
""
));
assertFalse
(
StringUtils
.
containsWhitespace
(
"a"
));
...
...
@@ -62,7 +62,7 @@ public class StringUtilsTests {
}
@Test
public
void
testTrimWhitespace
()
throws
Exception
{
public
void
testTrimWhitespace
()
{
assertEquals
(
null
,
StringUtils
.
trimWhitespace
(
null
));
assertEquals
(
""
,
StringUtils
.
trimWhitespace
(
""
));
assertEquals
(
""
,
StringUtils
.
trimWhitespace
(
" "
));
...
...
@@ -75,7 +75,7 @@ public class StringUtilsTests {
}
@Test
public
void
testTrimAllWhitespace
()
throws
Exception
{
public
void
testTrimAllWhitespace
()
{
assertEquals
(
""
,
StringUtils
.
trimAllWhitespace
(
""
));
assertEquals
(
""
,
StringUtils
.
trimAllWhitespace
(
" "
));
assertEquals
(
""
,
StringUtils
.
trimAllWhitespace
(
"\t"
));
...
...
@@ -87,7 +87,7 @@ public class StringUtilsTests {
}
@Test
public
void
testTrimLeadingWhitespace
()
throws
Exception
{
public
void
testTrimLeadingWhitespace
()
{
assertEquals
(
null
,
StringUtils
.
trimLeadingWhitespace
(
null
));
assertEquals
(
""
,
StringUtils
.
trimLeadingWhitespace
(
""
));
assertEquals
(
""
,
StringUtils
.
trimLeadingWhitespace
(
" "
));
...
...
@@ -100,7 +100,7 @@ public class StringUtilsTests {
}
@Test
public
void
testTrimTrailingWhitespace
()
throws
Exception
{
public
void
testTrimTrailingWhitespace
()
{
assertEquals
(
null
,
StringUtils
.
trimTrailingWhitespace
(
null
));
assertEquals
(
""
,
StringUtils
.
trimTrailingWhitespace
(
""
));
assertEquals
(
""
,
StringUtils
.
trimTrailingWhitespace
(
" "
));
...
...
@@ -113,7 +113,7 @@ public class StringUtilsTests {
}
@Test
public
void
testTrimLeadingCharacter
()
throws
Exception
{
public
void
testTrimLeadingCharacter
()
{
assertEquals
(
null
,
StringUtils
.
trimLeadingCharacter
(
null
,
' '
));
assertEquals
(
""
,
StringUtils
.
trimLeadingCharacter
(
""
,
' '
));
assertEquals
(
""
,
StringUtils
.
trimLeadingCharacter
(
" "
,
' '
));
...
...
@@ -126,7 +126,7 @@ public class StringUtilsTests {
}
@Test
public
void
testTrimTrailingCharacter
()
throws
Exception
{
public
void
testTrimTrailingCharacter
()
{
assertEquals
(
null
,
StringUtils
.
trimTrailingCharacter
(
null
,
' '
));
assertEquals
(
""
,
StringUtils
.
trimTrailingCharacter
(
""
,
' '
));
assertEquals
(
""
,
StringUtils
.
trimTrailingCharacter
(
" "
,
' '
));
...
...
@@ -166,7 +166,7 @@ public class StringUtilsTests {
}
@Test
public
void
testReplace
()
throws
Exception
{
public
void
testReplace
()
{
String
inString
=
"a6AazAaa77abaa"
;
String
oldPattern
=
"aa"
;
String
newPattern
=
"foo"
;
...
...
@@ -189,7 +189,7 @@ public class StringUtilsTests {
}
@Test
public
void
testDelete
()
throws
Exception
{
public
void
testDelete
()
{
String
inString
=
"The quick brown fox jumped over the lazy dog"
;
String
noThe
=
StringUtils
.
delete
(
inString
,
"the"
);
...
...
@@ -216,7 +216,7 @@ public class StringUtilsTests {
}
@Test
public
void
testDeleteAny
()
throws
Exception
{
public
void
testDeleteAny
()
{
String
inString
=
"Able was I ere I saw Elba"
;
String
res
=
StringUtils
.
deleteAny
(
inString
,
"I"
);
...
...
@@ -598,7 +598,7 @@ public class StringUtilsTests {
@Test
public
void
testParseLocaleStringSunnyDay
()
throws
Exception
{
public
void
testParseLocaleStringSunnyDay
()
{
Locale
expectedLocale
=
Locale
.
UK
;
Locale
locale
=
StringUtils
.
parseLocaleString
(
expectedLocale
.
toString
());
assertNotNull
(
"When given a bona-fide Locale string, must not return null."
,
locale
);
...
...
@@ -606,19 +606,19 @@ public class StringUtilsTests {
}
@Test
public
void
testParseLocaleStringWithMalformedLocaleString
()
throws
Exception
{
public
void
testParseLocaleStringWithMalformedLocaleString
()
{
Locale
locale
=
StringUtils
.
parseLocaleString
(
"_banjo_on_my_knee"
);
assertNotNull
(
"When given a malformed Locale string, must not return null."
,
locale
);
}
@Test
public
void
testParseLocaleStringWithEmptyLocaleStringYieldsNullLocale
()
throws
Exception
{
public
void
testParseLocaleStringWithEmptyLocaleStringYieldsNullLocale
()
{
Locale
locale
=
StringUtils
.
parseLocaleString
(
""
);
assertNull
(
"When given an empty Locale string, must return null."
,
locale
);
}
@Test
// SPR-8637
public
void
testParseLocaleWithMultiSpecialCharactersInVariant
()
throws
Exception
{
public
void
testParseLocaleWithMultiSpecialCharactersInVariant
()
{
String
variant
=
"proper-northern"
;
String
localeString
=
"en_GB_"
+
variant
;
Locale
locale
=
StringUtils
.
parseLocaleString
(
localeString
);
...
...
@@ -626,7 +626,7 @@ public class StringUtilsTests {
}
@Test
// SPR-3671
public
void
testParseLocaleWithMultiValuedVariant
()
throws
Exception
{
public
void
testParseLocaleWithMultiValuedVariant
()
{
String
variant
=
"proper_northern"
;
String
localeString
=
"en_GB_"
+
variant
;
Locale
locale
=
StringUtils
.
parseLocaleString
(
localeString
);
...
...
@@ -634,7 +634,7 @@ public class StringUtilsTests {
}
@Test
// SPR-3671
public
void
testParseLocaleWithMultiValuedVariantUsingSpacesAsSeparators
()
throws
Exception
{
public
void
testParseLocaleWithMultiValuedVariantUsingSpacesAsSeparators
()
{
String
variant
=
"proper northern"
;
String
localeString
=
"en GB "
+
variant
;
Locale
locale
=
StringUtils
.
parseLocaleString
(
localeString
);
...
...
@@ -642,7 +642,7 @@ public class StringUtilsTests {
}
@Test
// SPR-3671
public
void
testParseLocaleWithMultiValuedVariantUsingMixtureOfUnderscoresAndSpacesAsSeparators
()
throws
Exception
{
public
void
testParseLocaleWithMultiValuedVariantUsingMixtureOfUnderscoresAndSpacesAsSeparators
()
{
String
variant
=
"proper northern"
;
String
localeString
=
"en_GB_"
+
variant
;
Locale
locale
=
StringUtils
.
parseLocaleString
(
localeString
);
...
...
@@ -650,7 +650,7 @@ public class StringUtilsTests {
}
@Test
// SPR-3671
public
void
testParseLocaleWithMultiValuedVariantUsingSpacesAsSeparatorsWithLotsOfLeadingWhitespace
()
throws
Exception
{
public
void
testParseLocaleWithMultiValuedVariantUsingSpacesAsSeparatorsWithLotsOfLeadingWhitespace
()
{
String
variant
=
"proper northern"
;
String
localeString
=
"en GB "
+
variant
;
// lots of whitespace
Locale
locale
=
StringUtils
.
parseLocaleString
(
localeString
);
...
...
@@ -658,7 +658,7 @@ public class StringUtilsTests {
}
@Test
// SPR-3671
public
void
testParseLocaleWithMultiValuedVariantUsingUnderscoresAsSeparatorsWithLotsOfLeadingWhitespace
()
throws
Exception
{
public
void
testParseLocaleWithMultiValuedVariantUsingUnderscoresAsSeparatorsWithLotsOfLeadingWhitespace
()
{
String
variant
=
"proper_northern"
;
String
localeString
=
"en_GB_____"
+
variant
;
// lots of underscores
Locale
locale
=
StringUtils
.
parseLocaleString
(
localeString
);
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录