Skip to content

Commit a927030

Browse files
Update Junit from 5.13.4 to 6.0.0 (#472)
Requires a minimum of Java 17 for compilation. The release target remains Java 11. This change also enabled Java compiler linting and fails on linting errors. To address lint failures in the existing codebase, the bahviour of the org.hyperledger.fabric.Logger constructor has changed to avoid a potential 'this' escape. Any subclasses must now ensure that the parent logger is set explicitly after construction by calling setParent(Logger). No changes are required for code obtaining logger instances using the static Logger.getLogger(String) method. Signed-off-by: Mark S. Lewis <Mark.S.Lewis@outlook.com>
1 parent 658983f commit a927030

File tree

10 files changed

+57
-36
lines changed

10 files changed

+57
-36
lines changed

build.gradle

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,22 +46,16 @@ subprojects {
4646
group = 'org.hyperledger.fabric-chaincode-java'
4747
version = rootProject.version
4848

49-
java {
50-
sourceCompatibility = JavaVersion.VERSION_11
51-
targetCompatibility = JavaVersion.VERSION_11
52-
}
53-
5449
compileJava {
55-
if (javaCompiler.get().metadata.languageVersion.canCompileOrRun(10)) {
56-
options.release = 11
57-
}
50+
options.release = 11
51+
options.compilerArgs += ['-Werror', '-Xlint:all']
5852
}
5953

6054
dependencies {
6155
implementation 'commons-cli:commons-cli:1.10.0'
6256
implementation 'commons-logging:commons-logging:1.3.5'
6357

64-
testImplementation platform('org.junit:junit-bom:5.13.4')
58+
testImplementation platform('org.junit:junit-bom:6.0.0')
6559
testImplementation 'org.junit.jupiter:junit-jupiter'
6660
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
6761
testImplementation 'org.assertj:assertj-core:3.27.6'

examples/fabric-contract-example-as-service/build.gradle

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ repositories {
1515
dependencies {
1616
implementation 'org.hyperledger.fabric-chaincode-java:fabric-chaincode-shim:2.5.7'
1717
implementation 'org.json:json:20250517'
18-
testImplementation 'org.junit.jupiter:junit-jupiter:5.13.4'
18+
testImplementation platform('org.junit:junit-bom:6.0.0')
19+
testImplementation 'org.junit.jupiter:junit-jupiter'
1920
testImplementation 'org.assertj:assertj-core:3.27.6'
2021
testImplementation 'org.mockito:mockito-core:5.20.0'
2122
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'

examples/fabric-contract-example-gradle/build.gradle

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@ repositories {
1515
dependencies {
1616
implementation 'org.hyperledger.fabric-chaincode-java:fabric-chaincode-shim:2.5.7'
1717
implementation 'org.json:json:20250517'
18-
testImplementation 'org.junit.jupiter:junit-jupiter:5.13.4'
18+
testImplementation platform('org.junit:junit-bom:6.0.0')
19+
testImplementation 'org.junit.jupiter:junit-jupiter'
1920
testImplementation 'org.assertj:assertj-core:3.27.6'
2021
testImplementation 'org.mockito:mockito-core:5.20.0'
22+
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
2123
}
2224

2325
shadowJar {

examples/fabric-contract-example-maven/pom.xml

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,6 @@
1818
<logback.version>1.5.19</logback.version>
1919
<slf4j.version>2.0.17</slf4j.version>
2020

21-
<!-- Test -->
22-
<junit.jupiter.version>5.13.4</junit.jupiter.version>
23-
<junit.platform.version>1.13.1</junit.platform.version>
24-
2521
</properties>
2622

2723
<repositories>
@@ -31,7 +27,19 @@
3127
</repository>
3228
</repositories>
3329

34-
<dependencies>
30+
<dependencyManagement>
31+
<dependencies>
32+
<dependency>
33+
<groupId>org.junit</groupId>
34+
<artifactId>junit-bom</artifactId>
35+
<version>6.0.0</version>
36+
<type>pom</type>
37+
<scope>import</scope>
38+
</dependency>
39+
</dependencies>
40+
</dependencyManagement>
41+
42+
<dependencies>
3543

3644
<!-- fabric-chaincode-java -->
3745
<dependency>
@@ -69,19 +77,16 @@
6977
<dependency>
7078
<groupId>org.junit.jupiter</groupId>
7179
<artifactId>junit-jupiter-api</artifactId>
72-
<version>${junit.jupiter.version}</version>
7380
<scope>compile</scope>
7481
</dependency>
7582
<dependency>
7683
<groupId>org.junit.jupiter</groupId>
7784
<artifactId>junit-jupiter-params</artifactId>
78-
<version>${junit.jupiter.version}</version>
7985
<scope>test</scope>
8086
</dependency>
8187
<dependency>
8288
<groupId>org.junit.jupiter</groupId>
8389
<artifactId>junit-jupiter-engine</artifactId>
84-
<version>${junit.jupiter.version}</version>
8590
<scope>test</scope>
8691
</dependency>
8792
<!-- https://mvnrepository.com/artifact/org.mockito/mockito-core -->
@@ -102,7 +107,6 @@
102107
<build>
103108
<sourceDirectory>src</sourceDirectory>
104109
<plugins>
105-
<!-- JUnit 5 requires Surefire version 2.22.0 or higher -->
106110
<plugin>
107111
<artifactId>maven-surefire-plugin</artifactId>
108112
<version>3.5.4</version>

examples/ledger-api/build.gradle

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@ repositories {
1515
dependencies {
1616
implementation 'org.hyperledger.fabric-chaincode-java:fabric-chaincode-shim:2.5.7'
1717
implementation 'org.json:json:20250517'
18-
testImplementation 'org.junit.jupiter:junit-jupiter:5.13.4'
18+
testImplementation platform('org.junit:junit-bom:6.0.0')
19+
testImplementation 'org.junit.jupiter:junit-jupiter'
1920
testImplementation 'org.assertj:assertj-core:3.27.6'
2021
testImplementation 'org.mockito:mockito-core:5.20.0'
22+
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
2123
}
2224

2325
shadowJar {

fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/Logger.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,25 @@
1414
/** Logger class to use throughout the Contract Implementation. */
1515
public class Logger extends java.util.logging.Logger {
1616

17+
/**
18+
* Subclasses must ensure that a parent logger is set appropriately, for example:
19+
*
20+
* <p>{@code logger.setParent(java.util.logging.Logger.getLogger("org.hyperledger.fabric"))}
21+
*
22+
* @param name A name for the logger.
23+
*/
1724
protected Logger(final String name) {
1825
super(name, null);
19-
20-
// ensure that the parent logger is set
21-
super.setParent(java.util.logging.Logger.getLogger("org.hyperledger.fabric"));
2226
}
2327

2428
/**
2529
* @param name
2630
* @return Logger
2731
*/
2832
public static Logger getLogger(final String name) {
29-
return new Logger(name);
33+
Logger result = new Logger(name);
34+
result.setParent(java.util.logging.Logger.getLogger("org.hyperledger.fabric"));
35+
return result;
3036
}
3137

3238
/** @param msgSupplier */

fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/contract/routing/impl/SerializerRegistryImpl.java

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import io.github.classgraph.ClassGraph;
99
import io.github.classgraph.ClassInfo;
1010
import io.github.classgraph.ScanResult;
11+
import java.lang.reflect.InvocationTargetException;
1112
import java.util.HashMap;
1213
import java.util.HashSet;
1314
import java.util.Map;
@@ -41,15 +42,20 @@ public SerializerInterface getSerializer(final String name, final Serializer.TAR
4142
return contents.get(key);
4243
}
4344

44-
private SerializerInterface add(
45-
final String name, final Serializer.TARGET target, final Class<SerializerInterface> clazz)
45+
private void add(final String name, final Serializer.TARGET target, final Class<?> clazz)
4646
throws InstantiationException, IllegalAccessException {
4747
LOGGER.debug(() -> "Adding new Class " + clazz.getCanonicalName() + " for " + target);
4848
final String key = name + ":" + target;
49-
final SerializerInterface newObj = clazz.newInstance();
50-
this.contents.put(key, newObj);
51-
52-
return newObj;
49+
try {
50+
final SerializerInterface newObj =
51+
(SerializerInterface) clazz.getDeclaredConstructor().newInstance();
52+
this.contents.put(key, newObj);
53+
} catch (InvocationTargetException | NoSuchMethodException e) {
54+
InstantiationException wrapper = new InstantiationException(
55+
"Exception constructing " + clazz.getCanonicalName() + ": " + e.getMessage());
56+
wrapper.addSuppressed(e);
57+
throw wrapper;
58+
}
5359
}
5460

5561
/**
@@ -67,10 +73,10 @@ public void findAndSetContents() throws InstantiationException, IllegalAccessExc
6773
final Set<String> seenClass = new HashSet<>();
6874

6975
try (ScanResult scanResult = classGraph.scan()) {
70-
for (final ClassInfo classInfo :
71-
scanResult.getClassesWithAnnotation(this.ANNOTATION_CLASS.getCanonicalName())) {
76+
for (final ClassInfo classInfo : scanResult.getClassesWithAnnotation(ANNOTATION_CLASS.getCanonicalName())) {
7277
LOGGER.debug(() -> "Found class with contract annotation: " + classInfo.getName());
73-
final Class<SerializerInterface> cls = (Class<SerializerInterface>) classInfo.loadClass();
78+
79+
final Class<?> cls = classInfo.loadClass();
7480
LOGGER.debug("Loaded class");
7581

7682
final String className = cls.getCanonicalName();

fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/shim/impl/QueryResultsIteratorImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ public T next() {
107107
}
108108

109109
@Override
110-
public void close() throws Exception {
110+
public void close() {
111111

112112
final ByteString requestPayload = QueryStateClose.newBuilder()
113113
.setId(currentQueryResponse.getId())

fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/shim/ledger/QueryResultsIterator.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,7 @@
1212
*
1313
* @param <T> the type of elements returned by the iterator
1414
*/
15-
public interface QueryResultsIterator<T> extends Iterable<T>, AutoCloseable {}
15+
public interface QueryResultsIterator<T> extends Iterable<T>, AutoCloseable {
16+
@Override
17+
void close();
18+
}

fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/shim/ledger/QueryResultsIteratorWithMetadata.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,7 @@
1818
public interface QueryResultsIteratorWithMetadata<T> extends Iterable<T>, AutoCloseable {
1919
/** @return Query Metadata */
2020
QueryResponseMetadata getMetadata();
21+
22+
@Override
23+
void close();
2124
}

0 commit comments

Comments
 (0)