Skip to content

Commit 666e0c1

Browse files
nosanmhalbritter
authored andcommitted
Added Arch Rules for String.toLowerCase and String.toUpperCase without Locale
1 parent 4a4af0a commit 666e0c1

File tree

6 files changed

+161
-2
lines changed

6 files changed

+161
-2
lines changed

buildSrc/src/main/java/org/springframework/boot/build/architecture/ArchitectureCheck.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2022-2024 the original author or authors.
2+
* Copyright 2012-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -81,7 +81,8 @@ public ArchitectureCheck() {
8181
allBeanFactoryPostProcessorBeanMethodsShouldBeStaticAndHaveNoParameters(),
8282
noClassesShouldCallStepVerifierStepVerifyComplete(),
8383
noClassesShouldConfigureDefaultStepVerifierTimeout(), noClassesShouldCallCollectorsToList(),
84-
noClassesShouldCallURLEncoderWithStringEncoding(), noClassesShouldCallURLDecoderWithStringEncoding());
84+
noClassesShouldCallURLEncoderWithStringEncoding(), noClassesShouldCallURLDecoderWithStringEncoding(),
85+
noClassesShouldCallStringToUpperCaseWithoutLocale(), noClassesShouldCallStringToLowerCaseWithoutLocale());
8586
getRules().addAll(getProhibitObjectsRequireNonNull()
8687
.map((prohibit) -> prohibit ? noClassesShouldCallObjectsRequireNonNull() : Collections.emptyList()));
8788
getRuleDescriptions().set(getRules().map((rules) -> rules.stream().map(ArchRule::getDescription).toList()));
@@ -181,6 +182,20 @@ public void check(JavaMethod item, ConditionEvents events) {
181182
};
182183
}
183184

185+
private ArchRule noClassesShouldCallStringToLowerCaseWithoutLocale() {
186+
return ArchRuleDefinition.noClasses()
187+
.should()
188+
.callMethod(String.class, "toLowerCase")
189+
.because("String.toLowerCase(Locale.ROOT) should be used instead");
190+
}
191+
192+
private ArchRule noClassesShouldCallStringToUpperCaseWithoutLocale() {
193+
return ArchRuleDefinition.noClasses()
194+
.should()
195+
.callMethod(String.class, "toUpperCase")
196+
.because("String.toUpperCase(Locale.ROOT) should be used instead");
197+
}
198+
184199
private ArchRule noClassesShouldCallStepVerifierStepVerifyComplete() {
185200
return ArchRuleDefinition.noClasses()
186201
.should()

buildSrc/src/test/java/org/springframework/boot/build/architecture/ArchitectureCheckTests.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,42 @@ void whenClassCallsObjectsRequireNonNullWithSupplierTaskFailsAndWritesReport() t
146146
});
147147
}
148148

149+
@Test
150+
void whenClassCallsStringToUpperCaseWithoutLocaleFailsAndWritesReport() throws Exception {
151+
prepareTask("string/toUpperCase", (architectureCheck) -> {
152+
assertThatExceptionOfType(GradleException.class).isThrownBy(architectureCheck::checkArchitecture);
153+
assertThat(failureReport(architectureCheck)).isNotEmpty()
154+
.content()
155+
.contains("because String.toUpperCase(Locale.ROOT) should be used instead");
156+
});
157+
}
158+
159+
@Test
160+
void whenClassCallsStringToLowerCaseWithoutLocaleFailsAndWritesReport() throws Exception {
161+
prepareTask("string/toLowerCase", (architectureCheck) -> {
162+
assertThatExceptionOfType(GradleException.class).isThrownBy(architectureCheck::checkArchitecture);
163+
assertThat(failureReport(architectureCheck)).isNotEmpty()
164+
.content()
165+
.contains("because String.toLowerCase(Locale.ROOT) should be used instead");
166+
});
167+
}
168+
169+
@Test
170+
void whenClassCallsStringToLowerCaseWithLocaleShouldNotFail() throws Exception {
171+
prepareTask("string/toLowerCaseWithLocale", (architectureCheck) -> {
172+
architectureCheck.checkArchitecture();
173+
assertThat(failureReport(architectureCheck)).isEmpty();
174+
});
175+
}
176+
177+
@Test
178+
void whenClassCallsStringToUpperCaseWithLocaleShouldNotFail() throws Exception {
179+
prepareTask("string/toUpperCaseWithLocale", (architectureCheck) -> {
180+
architectureCheck.checkArchitecture();
181+
assertThat(failureReport(architectureCheck)).isEmpty();
182+
});
183+
}
184+
149185
private void prepareTask(String classes, Callback<ArchitectureCheck> callback) throws Exception {
150186
File projectDir = new File(this.temp, "project");
151187
projectDir.mkdirs();
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Copyright 2012-2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.build.architecture.string.toLowerCase;
18+
19+
class ToLowerCase {
20+
21+
void exampleMethod() {
22+
String test = "Object must not be null";
23+
System.out.println(test.toLowerCase());
24+
}
25+
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Copyright 2012-2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.build.architecture.string.toLowerCaseWithLocale;
18+
19+
import java.util.Locale;
20+
21+
class ToLowerCaseWithLocale {
22+
23+
void exampleMethod() {
24+
String test = "Object must not be null";
25+
System.out.println(test.toLowerCase(Locale.ENGLISH));
26+
}
27+
28+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Copyright 2012-2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.build.architecture.string.toUpperCase;
18+
19+
class ToUpperCase {
20+
21+
void exampleMethod() {
22+
String test = "Object must not be null";
23+
System.out.println(test.toUpperCase());
24+
}
25+
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Copyright 2012-2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.build.architecture.string.toUpperCaseWithLocale;
18+
19+
import java.util.Locale;
20+
21+
class ToUpperCaseWithLocale {
22+
23+
void exampleMethod() {
24+
String test = "Object must not be null";
25+
System.out.println(test.toUpperCase(Locale.ROOT));
26+
}
27+
28+
}

0 commit comments

Comments
 (0)