Skip to content

Commit d7a3fc3

Browse files
committed
Adopt naming conventions for constant fields
* public, protected and package-private constants must be upper case * private constants may have any case * except for logger which must be lowercase.
1 parent ffe6b73 commit d7a3fc3

8 files changed

Lines changed: 76 additions & 49 deletions

File tree

CONTRIBUTING.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ I hereby agree to the terms of the JUnit Contributor License Agreement.
5454

5555
### Naming Conventions
5656

57+
Acronyms are words.
58+
5759
Whenever an acronym is included as part of a type name or method name, keep the first
5860
letter of the acronym uppercase and use lowercase for the rest of the acronym. Otherwise,
5961
it becomes _impossible_ to perform camel-cased searches in IDEs, and it becomes
@@ -119,6 +121,17 @@ code -- class names, method names, variable names, etc.
119121
See [`ExtensionContext`](junit-jupiter-api/src/main/java/org/junit/jupiter/api/extension/ExtensionContext.java) and
120122
[`ParameterContext`](junit-jupiter-api/src/main/java/org/junit/jupiter/api/extension/ParameterContext.java) for example Javadoc.
121123

124+
### Constant fields
125+
126+
A constant field is a `static final` field whose value is immutable. If a static final
127+
field has a primitive type or an immutable reference type it is a constant field.
128+
129+
- To minimize accessibility and mutability for all non-private `static final` fields
130+
under `src/main` should be constant fields.
131+
- Constant fields should be named using uppercase words separated by underscores. For
132+
example `DEFAULT_HTTP_URL_PROVIDER`.
133+
134+
Note: `org.junit.platform.commons.logging.Logger` is considered mutable.
122135

123136
### Nullability
124137

documentation/src/tools/java/org/junit/api/tools/ApiReportGenerator.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
*/
4949
class ApiReportGenerator {
5050

51-
private static final Logger LOGGER = LoggerFactory.getLogger(ApiReportGenerator.class);
51+
private static final Logger logger = LoggerFactory.getLogger(ApiReportGenerator.class);
5252
private static final String EOL = System.lineSeparator();
5353

5454
public static void main(String... args) {
@@ -160,7 +160,7 @@ private static SortedSet<ClassInfo> collectTypes(ScanResult scanResult) {
160160
.filter(it -> !it.getAnnotationInfo(API.class).isInherited()) //
161161
.collect(toCollection(TreeSet::new));
162162

163-
LOGGER.debug(() -> {
163+
logger.debug(() -> {
164164
var builder = new StringBuilder("Listing of all " + types.size() + " annotated types:");
165165
builder.append(EOL);
166166
types.forEach(e -> builder.append(e.getName()).append(EOL));

gradle/config/checkstyle/checkstyleMain.xml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,20 @@
4848
<module name="EqualsAvoidNull"/>
4949
<module name="EmptyStatement"/>
5050
<module name="MissingDeprecated"/>
51+
<!-- non-private static final fields must be constants and named accordingly.
52+
See CONTRIBUTING.md - Constant fields -->
53+
<module name="ConstantName">
54+
<property name="applyToPrivate" value="false"/>
55+
</module>
56+
<module name="ConstantName">
57+
<!-- private static final fields may be mutable or immutable.
58+
But logger is always mutable. See CONTRIBUTING.md - Constant fields -->
59+
<property name="format" value="^(?!^LOGGER$).*$"/>
60+
<property name="applyToPublic" value="false"/>
61+
<property name="applyToProtected" value="false"/>
62+
<property name="applyToPackage" value="false"/>
63+
</module>
64+
5165
</module>
5266

5367
<module name="JavadocPackage" />

junit-jupiter-api/src/main/java/org/junit/jupiter/api/io/TempDirDeletionStrategy.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ DeletionResult delete(Path tempDir, AnnotatedElementContext elementContext, Exte
9696
*/
9797
final class IgnoreFailures implements TempDirDeletionStrategy {
9898

99-
private static final Logger LOGGER = LoggerFactory.getLogger(IgnoreFailures.class);
99+
private static final Logger logger = LoggerFactory.getLogger(IgnoreFailures.class);
100100
private final TempDirDeletionStrategy delegate;
101101

102102
/**
@@ -123,7 +123,7 @@ public DeletionResult delete(Path tempDir, AnnotatedElementContext elementContex
123123
}
124124

125125
private void logWarning(AnnotatedElementContext elementContext, DeletionException exception) {
126-
LOGGER.warn(exception, () -> "Failed to delete all temporary files for %s".formatted(
126+
logger.warn(exception, () -> "Failed to delete all temporary files for %s".formatted(
127127
descriptionFor(elementContext.getAnnotatedElement())));
128128
}
129129

@@ -169,7 +169,7 @@ final class Standard implements TempDirDeletionStrategy {
169169
*/
170170
public static final Standard INSTANCE = new Standard();
171171

172-
private static final Logger LOGGER = LoggerFactory.getLogger(Standard.class);
172+
private static final Logger logger = LoggerFactory.getLogger(Standard.class);
173173

174174
private Standard() {
175175
}
@@ -201,7 +201,7 @@ private void delete(Path tempDir, FileOperations fileOperations, BiConsumer<Path
201201

202202
@Override
203203
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
204-
LOGGER.trace(() -> "preVisitDirectory: " + dir);
204+
logger.trace(() -> "preVisitDirectory: " + dir);
205205
if (isLinkWithTargetOutsideTempDir(dir)) {
206206
warnAboutLinkWithTargetOutsideTempDir("link", dir);
207207
delete(dir, fileOperations);
@@ -215,7 +215,7 @@ public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) th
215215

216216
@Override
217217
public FileVisitResult visitFileFailed(Path file, IOException exc) {
218-
LOGGER.trace(exc, () -> "visitFileFailed: " + file);
218+
logger.trace(exc, () -> "visitFileFailed: " + file);
219219
if (exc instanceof NoSuchFileException && !Files.exists(file, LinkOption.NOFOLLOW_LINKS)) {
220220
return CONTINUE;
221221
}
@@ -226,7 +226,7 @@ public FileVisitResult visitFileFailed(Path file, IOException exc) {
226226

227227
@Override
228228
public FileVisitResult visitFile(Path file, BasicFileAttributes attributes) throws IOException {
229-
LOGGER.trace(() -> "visitFile: " + file);
229+
logger.trace(() -> "visitFile: " + file);
230230
if (Files.isSymbolicLink(file) && isLinkWithTargetOutsideTempDir(file)) {
231231
warnAboutLinkWithTargetOutsideTempDir("symbolic link", file);
232232
}
@@ -236,7 +236,7 @@ public FileVisitResult visitFile(Path file, BasicFileAttributes attributes) thro
236236

237237
@Override
238238
public FileVisitResult postVisitDirectory(Path dir, @Nullable IOException exc) {
239-
LOGGER.trace(exc, () -> "postVisitDirectory: " + dir);
239+
logger.trace(exc, () -> "postVisitDirectory: " + dir);
240240
delete(dir, fileOperations);
241241
return CONTINUE;
242242
}
@@ -248,15 +248,15 @@ private boolean isLinkWithTargetOutsideTempDir(Path path) {
248248
return !path.toRealPath().startsWith(rootRealPath);
249249
}
250250
catch (IOException e) {
251-
LOGGER.trace(e,
251+
logger.trace(e,
252252
() -> "Failed to determine real path for " + path + "; assuming it is not a link");
253253
return false;
254254
}
255255
}
256256

257257
private void warnAboutLinkWithTargetOutsideTempDir(String linkType, Path file) throws IOException {
258258
Path realPath = file.toRealPath();
259-
LOGGER.warn(() -> """
259+
logger.warn(() -> """
260260
Deleting %s from location inside of temp dir (%s) \
261261
to location outside of temp dir (%s) but not the target file/directory""".formatted(
262262
linkType, file, realPath));
@@ -303,13 +303,13 @@ private void resetPermissionsAndTryToDeleteAgain(Path path, IOException exceptio
303303
}
304304

305305
private void deleteWithLogging(Path file, FileOperations fileOperations) throws IOException {
306-
LOGGER.trace(() -> "Attempting to delete " + file);
306+
logger.trace(() -> "Attempting to delete " + file);
307307
try {
308308
fileOperations.delete(file);
309-
LOGGER.trace(() -> "Successfully deleted " + file);
309+
logger.trace(() -> "Successfully deleted " + file);
310310
}
311311
catch (IOException e) {
312-
LOGGER.trace(e, () -> "Failed to delete " + file);
312+
logger.trace(e, () -> "Failed to delete " + file);
313313
throw e;
314314
}
315315
}

junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/descriptor/AbstractExtensionContext.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
*/
5555
abstract class AbstractExtensionContext<T extends TestDescriptor> implements ExtensionContextInternal, AutoCloseable {
5656

57-
private static final Logger LOGGER = LoggerFactory.getLogger(AbstractExtensionContext.class);
57+
private static final Logger logger = LoggerFactory.getLogger(AbstractExtensionContext.class);
5858
private static final Namespace CLOSEABLE_RESOURCE_LOGGING_NAMESPACE = Namespace.create(
5959
AbstractExtensionContext.class, "CloseableResourceLogging");
6060

@@ -113,7 +113,7 @@ private <N> NamespacedHierarchicalStore.CloseAction<N> createCloseAction() {
113113
if (value instanceof Store.CloseableResource resource) {
114114
if (isAutoCloseEnabled) {
115115
store.computeIfAbsent(value.getClass(), type -> {
116-
LOGGER.warn(() -> "Type implements CloseableResource but not AutoCloseable: " + type.getName());
116+
logger.warn(() -> "Type implements CloseableResource but not AutoCloseable: " + type.getName());
117117
return true;
118118
});
119119
}

junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/extension/TempDirectory.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -334,17 +334,17 @@ public void close() {
334334

335335
record Cleanup(CleanupMode cleanupMode, Supplier<TempDirDeletionStrategy> deletionStrategy) {
336336

337-
private static final Logger LOGGER = LoggerFactory.getLogger(Cleanup.class);
337+
private static final Logger logger = LoggerFactory.getLogger(Cleanup.class);
338338

339339
void run(Path dir, AnnotatedElementContext elementContext, ExtensionContext extensionContext)
340340
throws IOException {
341341
if (cleanupMode == NEVER || (cleanupMode == ON_SUCCESS && selfOrChildFailed(extensionContext))) {
342-
LOGGER.info(() -> "Skipping cleanup of temp dir %s for %s due to CleanupMode.%s.".formatted(dir,
342+
logger.info(() -> "Skipping cleanup of temp dir %s for %s due to CleanupMode.%s.".formatted(dir,
343343
descriptionFor(elementContext.getAnnotatedElement()), cleanupMode.name()));
344344
return;
345345
}
346346

347-
LOGGER.trace(() -> "Cleaning up temp dir " + dir);
347+
logger.trace(() -> "Cleaning up temp dir " + dir);
348348
if (Files.exists(dir)) {
349349
deletionStrategy.get().delete(dir, elementContext, extensionContext) //
350350
.toException() //

0 commit comments

Comments
 (0)