Skip to content

Commit

Permalink
Fix spring junit integration test runner. spring application context …
Browse files Browse the repository at this point in the history
…will be created if the test class is annotated with any spring annotation
  • Loading branch information
artpar committed Jan 11, 2024
1 parent 1b578c4 commit ffa42ec
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 56 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

<artifactId>unlogged-sdk</artifactId>
<groupId>video.bug</groupId>
<version>0.1.41</version>
<version>0.1.42</version>

<properties>
<maven.compiler.source>8</maven.compiler.source>
Expand Down
87 changes: 32 additions & 55 deletions src/main/java/io/unlogged/AgentCommandExecutorImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import net.bytebuddy.ByteBuddy;
import net.bytebuddy.NamingStrategy;
import net.bytebuddy.description.annotation.AnnotationDescription;
import net.bytebuddy.description.type.TypeDefinition;
import net.bytebuddy.dynamic.DynamicType;
import net.bytebuddy.dynamic.loading.ClassInjector;
import net.bytebuddy.dynamic.loading.ClassLoadingStrategy;
Expand Down Expand Up @@ -162,11 +161,12 @@ public AgentCommandRawResponse executeCommandRaw(AgentCommandRequest agentComman

String targetClassName = agentCommandRequest.getClassName();
if (applicationContext != null && getBeanMethod != null) {
try {
objectInstanceByClass = getBeanMethod.invoke(applicationContext, Class.forName(targetClassName));
} catch (Exception e) {
try {
objectInstanceByClass = getBeanMethod.invoke(applicationContext,
Class.forName(targetClassName));
} catch (Exception e) {

}
}
}
if (objectInstanceByClass == null) {
objectInstanceByClass = logger.getObjectByClassName(targetClassName);
Expand Down Expand Up @@ -228,33 +228,6 @@ public AgentCommandRawResponse executeCommandRaw(AgentCommandRequest agentComman
// instance of SpringApplicationContext


if (this.springTestContextManager == null) {


TestBaseImplementation baseImplementation = new TestBaseImplementation();

DynamicType.Builder<Object> builder = byteBuddyInstance
.subclass(Object.class)
.name("UnloggedTestAnnotationCarrierClass");
try {
AnnotationDescription withSecurityContextAnnotation = getAnnotationDescription(
"org.springframework.security.test.context.support.WithSecurityContext");
builder = builder.annotateType(withSecurityContextAnnotation);
} catch (Exception e) {

}
DynamicType.Unloaded<Object> dynamicallyCreatedTestClassMaker = builder
.defineMethod("basicTest", Void.class)
.withParameters(new ArrayList<TypeDefinition>())
.intercept(MethodDelegation.to(baseImplementation))
.make();

DynamicType.Loaded<Object> dynamicallyCreatedTestClassInstance = dynamicallyCreatedTestClassMaker.load(
targetClassLoader);

trySpringIntegration(dynamicallyCreatedTestClassInstance.getLoaded());
}

if (this.springTestContextManager != null) {
RequestAuthentication authRequest = requestAuthentication;

Expand Down Expand Up @@ -1070,16 +1043,15 @@ private Object createParameterUsingMocking(String methodParameter, Class<?> para

if (methodName.startsWith("get") || methodName.startsWith("is")) {
ArrayList<ThenParameter> thenParameterList = new ArrayList<>();

ReturnValue returnParameter;
if (checkCanClassBeExtended(valueType)) {
returnParameter = new ReturnValue(
providedValue.toString(), valueType.getCanonicalName(), ReturnValueType.MOCK);
}
else {
returnParameter = new ReturnValue(
providedValue.toString(), valueType.getCanonicalName(), ReturnValueType.REAL);
}

ReturnValue returnParameter;
if (checkCanClassBeExtended(valueType)) {
returnParameter = new ReturnValue(
providedValue.toString(), valueType.getCanonicalName(), ReturnValueType.MOCK);
} else {
returnParameter = new ReturnValue(
providedValue.toString(), valueType.getCanonicalName(), ReturnValueType.REAL);
}

DeclaredMock returnParamCallMock = new DeclaredMock();
returnParameter.addDeclaredMock(returnParamCallMock);
Expand Down Expand Up @@ -1111,19 +1083,19 @@ private Object createParameterUsingMocking(String methodParameter, Class<?> para
return parameterObject.getMockedFieldInstance();
}

private boolean checkCanClassBeExtended(Class<?> fieldType) {
if (fieldType.isPrimitive()) {
return false;
}
if (fieldType.isArray()) {
return false;
}
if ((fieldType.getModifiers() & java.lang.reflect.Modifier.FINAL) != 0) {
return false;
}
return true;
}
private boolean checkCanClassBeExtended(Class<?> fieldType) {
if (fieldType.isPrimitive()) {
return false;
}
if (fieldType.isArray()) {
return false;
}
if ((fieldType.getModifiers() & java.lang.reflect.Modifier.FINAL) != 0) {
return false;
}

return true;
}

private Object getValueToSet(TypeFactory typeFactory, JsonNode fieldValueInNodeByName, Class<?> type) throws JsonProcessingException {
Object valueToSet = null;
Expand Down Expand Up @@ -1443,4 +1415,9 @@ private Object setSpringApplicationContextAndLoadBeanFactory(Object applicationC
}


public void enableSpringIntegration(Class<?> testClass) {
if (this.springTestContextManager == null) {
trySpringIntegration(testClass);
}
}
}
8 changes: 8 additions & 0 deletions src/main/java/io/unlogged/runner/UnloggedTestRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ public class UnloggedTestRunner extends Runner {
final private AtomicRecordService atomicRecordService = new AtomicRecordService();
private final AgentCommandExecutorImpl commandExecutor;
private final AtomicInteger testCounter = new AtomicInteger();
private final Class<?> testClass;
private boolean isSpringPresent;
private Method getBeanMethod;
private Object applicationContext;
Expand All @@ -99,9 +100,11 @@ public Object getObjectByClassName(String className) {

public UnloggedTestRunner(Class<?> testClass) {
super();
this.testClass = testClass;
this.commandExecutor = new AgentCommandExecutorImpl(objectMapper, eventLogger);
this.testDescription = Description.createTestDescription(testClass, "Unlogged test runner");

commandExecutor.enableSpringIntegration(this.testClass);
collectTests();
Runtime.getInstance("format=discard");
}
Expand Down Expand Up @@ -373,6 +376,10 @@ public void collectTests() {
Map<String, List<StoredCandidate>> storedCandidateMap = classRecords.getStoredCandidateMap();
// System.err.println("running the tests from unlogged: " + classFileResource);

int candidateCount = storedCandidateMap.values().stream().mapToInt(Collection::size).sum();
if (candidateCount < 1) {
continue;
}
Description suiteDescription = Description.createSuiteDescription(Class.forName(className));
// System.err.println(className );
testDescription.addChild(suiteDescription);
Expand All @@ -392,6 +399,7 @@ public void collectTests() {

Description testDescription = getTestDescription(targetClassTypeInstance, candidateId,
candidate.getCandidateId());

suiteDescription.addChild(testDescription);
// this.testDescription.addChild(testDescription);
descriptionStoredCandidateMap.put(testDescription, candidate);
Expand Down

0 comments on commit ffa42ec

Please sign in to comment.