Skip to content

Commit abb1162

Browse files
committed
Update to java 17 + fix for accessibility issue (Exception and Enum classes)
1 parent 2ece04b commit abb1162

File tree

3 files changed

+77
-30
lines changed

3 files changed

+77
-30
lines changed

pom.xml

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<groupId>net.lonzak.common</groupId>
66
<artifactId>unittest-utilities</artifactId>
77
<name>Automating junit tests</name>
8-
<version>2.0.13</version>
8+
<version>3.0.0</version>
99

1010
<properties>
1111
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
@@ -31,18 +31,17 @@
3131
<plugin>
3232
<groupId>org.apache.maven.plugins</groupId>
3333
<artifactId>maven-compiler-plugin</artifactId>
34-
<version>3.5.1</version>
34+
<version>3.13.0</version>
3535
<configuration>
36-
<source>1.8</source>
37-
<target>1.8</target>
38-
<fork>true</fork>
39-
<compilerVersion>1.8</compilerVersion>
36+
<source>17</source>
37+
<target>17</target>
38+
<release>17</release>
4039
</configuration>
4140
</plugin>
4241
<plugin>
4342
<groupId>org.apache.maven.plugins</groupId>
4443
<artifactId>maven-jar-plugin</artifactId>
45-
<version>3.0.2</version>
44+
<version>3.4.2</version>
4645
<configuration>
4746
<archive>
4847
<manifest>
@@ -54,7 +53,7 @@
5453
<plugin>
5554
<groupId>org.apache.maven.plugins</groupId>
5655
<artifactId>maven-source-plugin</artifactId>
57-
<version>3.0.1</version>
56+
<version>3.3.1</version>
5857
<executions>
5958
<execution>
6059
<id>attach-sources</id>
@@ -67,7 +66,7 @@
6766
<plugin>
6867
<groupId>org.apache.maven.plugins</groupId>
6968
<artifactId>maven-javadoc-plugin</artifactId>
70-
<version>3.0.1</version>
69+
<version>3.11.1</version>
7170
<executions>
7271
<execution>
7372
<id>attach-javadocs</id>
@@ -86,7 +85,7 @@
8685
<dependency>
8786
<groupId>org.apache.commons</groupId>
8887
<artifactId>commons-lang3</artifactId>
89-
<version>3.13.0</version>
88+
<version>3.17.0</version>
9089
<scope>compile</scope>
9190
</dependency>
9291
<dependency>
@@ -95,10 +94,15 @@
9594
<version>4.13.2</version>
9695
<scope>compile</scope>
9796
</dependency>
97+
<dependency>
98+
<groupId>jakarta.activation</groupId>
99+
<artifactId>jakarta.activation-api</artifactId>
100+
<version>2.1.3</version>
101+
</dependency>
98102
<dependency>
99103
<groupId>commons-io</groupId>
100104
<artifactId>commons-io</artifactId>
101-
<version>2.13.0</version>
105+
<version>2.18.0</version>
102106
<scope>test</scope>
103107
</dependency>
104108
</dependencies>

src/main/java/net/lonzak/common/unittest/AutoTester.java

Lines changed: 62 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import java.lang.reflect.Array;
3434
import java.lang.reflect.Constructor;
3535
import java.lang.reflect.Field;
36+
import java.lang.reflect.InaccessibleObjectException;
3637
import java.lang.reflect.InvocationTargetException;
3738
import java.lang.reflect.Method;
3839
import java.lang.reflect.Modifier;
@@ -99,9 +100,10 @@
99100
import java.util.concurrent.atomic.AtomicLong;
100101
import java.util.concurrent.atomic.AtomicLongArray;
101102
import java.util.regex.Pattern;
103+
import java.util.stream.Collectors;
102104

103-
import javax.activation.DataSource;
104-
import javax.activation.FileDataSource;
105+
import jakarta.activation.DataSource;
106+
import jakarta.activation.FileDataSource;
105107
import javax.sql.rowset.serial.SerialBlob;
106108
import javax.sql.rowset.serial.SerialException;
107109
import javax.xml.datatype.DatatypeConfigurationException;
@@ -556,8 +558,10 @@ private static void checkGettersAndSetters(ArrayList<Class<?>> constructedClasse
556558
throws ClassNotFoundException, InstantiationException, IllegalAccessException, InvocationTargetException {
557559

558560
// all public methods are relevant
559-
List<Method> publicMethods = Arrays.asList(dtoClass.getMethods());
561+
List<Method> publicMethods = Arrays.stream(dtoClass.getMethods()).collect(Collectors.toCollection(ArrayList::new));
560562

563+
clearMethods(publicMethods);
564+
561565
// all protected methods are relevant
562566
ArrayList<Method> allMethods = getInheritedProtectedMethods(dtoClass);
563567

@@ -590,11 +594,11 @@ else if (current.getName().startsWith("is")) {
590594
}
591595
}
592596
}
593-
597+
594598
for (Method method : toRemove) {
595599
allMethods.remove(method);
596600
}
597-
601+
598602
try {
599603
constructSetMethods(constructedClasses, dtoClass, constructedObjects, allMethods, implOfAbstractClasses,
600604
specialValues);
@@ -2188,12 +2192,19 @@ private static ExtractionValue extractValueFromField(Class<?> dtoClass, Method m
21882192

21892193
// if no value is found in the direct class, check super classes
21902194
List<Field> inheritedFields = getInheritedFields(dtoClass);
2191-
2195+
21922196
for (Field field : inheritedFields) {
2193-
if (StringUtils.uncapitalize(method.getName().substring(3)).equals(field.getName())) {
2194-
field.setAccessible(true);
2195-
return new ExtractionValue(true, field.get(constructedObject));
2196-
}
2197+
if (StringUtils.uncapitalize(method.getName().substring(3)).equals(field.getName())) {
2198+
try {
2199+
field.setAccessible(true);
2200+
return new ExtractionValue(true, field.get(constructedObject));
2201+
}
2202+
catch(InaccessibleObjectException ioe) {
2203+
//ignore accessibility problems due to java module system
2204+
System.err.println("Can not fully test class "+dtoClass.getName()+" due to accessability problems of "+field);
2205+
return new ExtractionValue(false, field.get(constructedObject));
2206+
}
2207+
}
21972208
}
21982209

21992210
return new ExtractionValue(false, null);
@@ -2410,6 +2421,19 @@ private static List<Field> getInheritedFields(Class<?> clazz) {
24102421
fields.addAll(Arrays.asList(classToCheck.getSuperclass().getDeclaredFields()));
24112422
classToCheck = classToCheck.getSuperclass();
24122423
}
2424+
2425+
//exclude special problematic fields because access is restricted
2426+
ArrayList<Field> toBeRemoved = new ArrayList<>();
2427+
for (Field field : fields) {
2428+
if(field.getName().equals("stackTrace")) {
2429+
toBeRemoved.add(field);
2430+
} else if(field.getName().equals("finalize")) {
2431+
toBeRemoved.add(field);
2432+
} else if(field.getName().equals("clone")) {
2433+
toBeRemoved.add(field);
2434+
}
2435+
}
2436+
fields.removeAll(toBeRemoved);
24132437

24142438
return fields;
24152439
}
@@ -2433,12 +2457,20 @@ private static ArrayList<Method> getInheritedProtectedMethods(Class<?> clazz) {
24332457
classToCheck = classToCheck.getSuperclass();
24342458
}
24352459

2460+
clearMethods(methods);
2461+
24362462
// clear public and private methods
24372463
for (Method method : methods) {
2438-
if (Modifier.isProtected(method.getModifiers())) {
2439-
method.setAccessible(true);
2440-
protectedMethods.add(method);
2441-
}
2464+
if (Modifier.isProtected(method.getModifiers())) {
2465+
try {
2466+
method.setAccessible(true);
2467+
protectedMethods.add(method);
2468+
}
2469+
catch(InaccessibleObjectException ioe) {
2470+
//ignore accessibility problems due to java module system
2471+
System.err.println("Can not fully test class "+clazz.getName()+" due to accessability problems: "+method);
2472+
}
2473+
}
24422474
}
24432475

24442476
return protectedMethods;
@@ -2505,8 +2537,7 @@ private static void specialValuesValid(Constructor<?>[] constructors, SpecialVal
25052537
}
25062538
}
25072539
if (!foundMatch && value.getNumberOfArguments() != 0) {
2508-
System.err.println("The following special value " + specialValues
2509-
+ " could not be matched to any constructor argument. Check the index and the data type.");
2540+
System.err.println("The special value can not be matched to a constructor argument. Check the index and the data type. Special values: " + specialValues);
25102541
}
25112542
}
25122543
}
@@ -2545,6 +2576,21 @@ private static X509Certificate readCertificate() {
25452576

25462577
return cert;
25472578
}
2579+
2580+
private static void clearMethods(List<Method> methods) {
2581+
//exclude special problematic methods because access is restricted
2582+
ArrayList<Method> toBeRemoved = new ArrayList<>();
2583+
for (Method method : methods) {
2584+
if((method.getName().equals("getStackTrace") || method.getName().equals("setStackTrace"))) {
2585+
toBeRemoved.add(method);
2586+
} else if(method.getName().equals("finalize")) {
2587+
toBeRemoved.add(method);
2588+
} else if(method.getName().equals("clone")) {
2589+
toBeRemoved.add(method);
2590+
}
2591+
}
2592+
methods.removeAll(toBeRemoved);
2593+
}
25482594

25492595
static Byte getRandomByte() {
25502596
byte[] b = new byte[] {0};

src/test/java/net/lonzak/common/unittest/AutoTesterTest.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,6 @@
3131

3232
import org.junit.Assert;
3333
import org.junit.Test;
34-
import net.lonzak.common.unittest.AutoTester;
35-
import net.lonzak.common.unittest.PotentialErrorDetected;
36-
import net.lonzak.common.unittest.SpecialValueLocator;
3734
import net.lonzak.common.unittest.SpecialValueLocator.ConstructorValue;
3835
import net.lonzak.common.unittest.examples.classes.ArrayObject;
3936
import net.lonzak.common.unittest.examples.classes.Constructor;

0 commit comments

Comments
 (0)