Skip to content

Commit

Permalink
Update to java 17 + fix for accessibility issue (Exception and Enum c…
Browse files Browse the repository at this point in the history
…lasses)
  • Loading branch information
Lonzak committed Jan 30, 2025
1 parent 2ece04b commit abb1162
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 30 deletions.
26 changes: 15 additions & 11 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<groupId>net.lonzak.common</groupId>
<artifactId>unittest-utilities</artifactId>
<name>Automating junit tests</name>
<version>2.0.13</version>
<version>3.0.0</version>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
Expand All @@ -31,18 +31,17 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<version>3.13.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<fork>true</fork>
<compilerVersion>1.8</compilerVersion>
<source>17</source>
<target>17</target>
<release>17</release>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
<version>3.4.2</version>
<configuration>
<archive>
<manifest>
Expand All @@ -54,7 +53,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>3.0.1</version>
<version>3.3.1</version>
<executions>
<execution>
<id>attach-sources</id>
Expand All @@ -67,7 +66,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>3.0.1</version>
<version>3.11.1</version>
<executions>
<execution>
<id>attach-javadocs</id>
Expand All @@ -86,7 +85,7 @@
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.13.0</version>
<version>3.17.0</version>
<scope>compile</scope>
</dependency>
<dependency>
Expand All @@ -95,10 +94,15 @@
<version>4.13.2</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>jakarta.activation</groupId>
<artifactId>jakarta.activation-api</artifactId>
<version>2.1.3</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.13.0</version>
<version>2.18.0</version>
<scope>test</scope>
</dependency>
</dependencies>
Expand Down
78 changes: 62 additions & 16 deletions src/main/java/net/lonzak/common/unittest/AutoTester.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import java.lang.reflect.Array;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InaccessibleObjectException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
Expand Down Expand Up @@ -99,9 +100,10 @@
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.AtomicLongArray;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import javax.activation.DataSource;
import javax.activation.FileDataSource;
import jakarta.activation.DataSource;
import jakarta.activation.FileDataSource;
import javax.sql.rowset.serial.SerialBlob;
import javax.sql.rowset.serial.SerialException;
import javax.xml.datatype.DatatypeConfigurationException;
Expand Down Expand Up @@ -556,8 +558,10 @@ private static void checkGettersAndSetters(ArrayList<Class<?>> constructedClasse
throws ClassNotFoundException, InstantiationException, IllegalAccessException, InvocationTargetException {

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

clearMethods(publicMethods);

// all protected methods are relevant
ArrayList<Method> allMethods = getInheritedProtectedMethods(dtoClass);

Expand Down Expand Up @@ -590,11 +594,11 @@ else if (current.getName().startsWith("is")) {
}
}
}

for (Method method : toRemove) {
allMethods.remove(method);
}

try {
constructSetMethods(constructedClasses, dtoClass, constructedObjects, allMethods, implOfAbstractClasses,
specialValues);
Expand Down Expand Up @@ -2188,12 +2192,19 @@ private static ExtractionValue extractValueFromField(Class<?> dtoClass, Method m

// if no value is found in the direct class, check super classes
List<Field> inheritedFields = getInheritedFields(dtoClass);

for (Field field : inheritedFields) {
if (StringUtils.uncapitalize(method.getName().substring(3)).equals(field.getName())) {
field.setAccessible(true);
return new ExtractionValue(true, field.get(constructedObject));
}
if (StringUtils.uncapitalize(method.getName().substring(3)).equals(field.getName())) {
try {
field.setAccessible(true);
return new ExtractionValue(true, field.get(constructedObject));
}
catch(InaccessibleObjectException ioe) {
//ignore accessibility problems due to java module system
System.err.println("Can not fully test class "+dtoClass.getName()+" due to accessability problems of "+field);
return new ExtractionValue(false, field.get(constructedObject));
}
}
}

return new ExtractionValue(false, null);
Expand Down Expand Up @@ -2410,6 +2421,19 @@ private static List<Field> getInheritedFields(Class<?> clazz) {
fields.addAll(Arrays.asList(classToCheck.getSuperclass().getDeclaredFields()));
classToCheck = classToCheck.getSuperclass();
}

//exclude special problematic fields because access is restricted
ArrayList<Field> toBeRemoved = new ArrayList<>();
for (Field field : fields) {
if(field.getName().equals("stackTrace")) {
toBeRemoved.add(field);
} else if(field.getName().equals("finalize")) {
toBeRemoved.add(field);
} else if(field.getName().equals("clone")) {
toBeRemoved.add(field);
}
}
fields.removeAll(toBeRemoved);

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

clearMethods(methods);

// clear public and private methods
for (Method method : methods) {
if (Modifier.isProtected(method.getModifiers())) {
method.setAccessible(true);
protectedMethods.add(method);
}
if (Modifier.isProtected(method.getModifiers())) {
try {
method.setAccessible(true);
protectedMethods.add(method);
}
catch(InaccessibleObjectException ioe) {
//ignore accessibility problems due to java module system
System.err.println("Can not fully test class "+clazz.getName()+" due to accessability problems: "+method);
}
}
}

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

return cert;
}

private static void clearMethods(List<Method> methods) {
//exclude special problematic methods because access is restricted
ArrayList<Method> toBeRemoved = new ArrayList<>();
for (Method method : methods) {
if((method.getName().equals("getStackTrace") || method.getName().equals("setStackTrace"))) {
toBeRemoved.add(method);
} else if(method.getName().equals("finalize")) {
toBeRemoved.add(method);
} else if(method.getName().equals("clone")) {
toBeRemoved.add(method);
}
}
methods.removeAll(toBeRemoved);
}

static Byte getRandomByte() {
byte[] b = new byte[] {0};
Expand Down
3 changes: 0 additions & 3 deletions src/test/java/net/lonzak/common/unittest/AutoTesterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,6 @@

import org.junit.Assert;
import org.junit.Test;
import net.lonzak.common.unittest.AutoTester;
import net.lonzak.common.unittest.PotentialErrorDetected;
import net.lonzak.common.unittest.SpecialValueLocator;
import net.lonzak.common.unittest.SpecialValueLocator.ConstructorValue;
import net.lonzak.common.unittest.examples.classes.ArrayObject;
import net.lonzak.common.unittest.examples.classes.Constructor;
Expand Down

0 comments on commit abb1162

Please sign in to comment.