Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
EddeCCC committed Aug 19, 2024
1 parent e8a5a02 commit dc3e5a4
Show file tree
Hide file tree
Showing 6 changed files with 142 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,6 @@ Set<Class<?>> getNextBatch(int batchSize) {
checkedClassesCount++;

try {
if ("com.example.demo.GreetingController".equals(clazz.getName()))
System.out.println("GREET");

boolean shouldInstrument = configurationResolver.shouldInstrument(clazz);
boolean isInstrumented = instrumentationState.isInstrumented(clazz);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,26 @@ public static InstrumentationState create() {
* Checks, if the provided class is already instrumented.
*
* @param clazz the class object
* @return true, if the provided type is already instrumented.
* @return true, if the provided class is already instrumented.
*/
public boolean isInstrumented(Class<?> clazz) {
return activeInstrumentations.asMap().entrySet().stream()
.filter(entry -> entry.getKey().isEqualTo(clazz))
.map(Map.Entry::getValue)
.map(ClassInstrumentationConfiguration::isInstrumented)
.findAny()
.orElse(false);
ClassInstrumentationConfiguration activeConfig =
activeInstrumentations.asMap().entrySet().stream()
.filter(entry -> entry.getKey().isEqualTo(clazz))
.map(Map.Entry::getValue)
.findAny()
.orElse(null);

if (Objects.nonNull(activeConfig)) return activeConfig.isInstrumented();
return false;
}

/**
* Checks, if the provided type is already instrumented.
*
* @param instrumentedType the class type
* @return true, if the provided type is already instrumented.
*/
public boolean isInstrumented(InstrumentedType instrumentedType) {
ClassInstrumentationConfiguration activeConfig =
activeInstrumentations.getIfPresent(instrumentedType);
Expand All @@ -59,7 +68,6 @@ public void addInstrumentedType(InstrumentedType type) {
* @param type the uninstrumented type
*/
public void invalidateInstrumentedType(InstrumentedType type) {
System.out.println("INVALIDATED");
activeInstrumentations.invalidate(type);
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package rocks.inspectit.gepard.agent.internal.instrumentation;

import net.bytebuddy.agent.builder.AgentBuilder;

import java.util.Objects;
import javax.annotation.Nullable;
import net.bytebuddy.agent.builder.AgentBuilder;

/**
* Stores the full name as well as the class loader of a specific type.<br>
Expand All @@ -16,11 +16,15 @@
*/
public class InstrumentedType {

/** Fully qualified name of the type */
private final String typeName;

/**
* The classLoader to load the type. Might be {@code null} to represent the bootstrap classLoader
*/
private final ClassLoader classLoader;

public InstrumentedType(final String typeName, final ClassLoader classLoader) {
public InstrumentedType(String typeName, @Nullable ClassLoader classLoader) {
this.typeName = typeName;
this.classLoader = classLoader;
}
Expand All @@ -32,13 +36,18 @@ public InstrumentedType(final String typeName, final ClassLoader classLoader) {
* @return true, if the provided class objects references this type
*/
public boolean isEqualTo(Class<?> clazz) {
if (classLoader == null)
return typeName.equals(clazz.getName()) && Objects.isNull(clazz.getClassLoader());
return typeName.equals(clazz.getName()) && classLoader.equals(clazz.getClassLoader());
}

@Override
public boolean equals(Object other) {
if (other instanceof InstrumentedType otherType)
if (other instanceof InstrumentedType otherType) {
if (classLoader == null)
return typeName.equals(otherType.typeName) && Objects.isNull(otherType.classLoader);
return typeName.equals(otherType.typeName) && classLoader.equals(otherType.classLoader);
}
return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import java.lang.instrument.UnmodifiableClassException;
import java.util.HashSet;
import java.util.Set;
import net.bytebuddy.description.type.TypeDescription;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,14 @@
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

import net.bytebuddy.description.type.TypeDescription;
import org.junit.jupiter.api.Test;

// TODO Write more tests

class InstrumentationStateTest {

private static final Class<?> TEST_CLASS = InstrumentationStateTest.class;

private static final InstrumentedType TEST_TYPE = new InstrumentedType(TEST_CLASS.getName(), TEST_CLASS.getClassLoader());
private static final InstrumentedType TEST_TYPE =
new InstrumentedType(TEST_CLASS.getName(), TEST_CLASS.getClassLoader());

@Test
void classIsNotInstrumented() {
Expand All @@ -23,6 +21,27 @@ void classIsNotInstrumented() {
assertFalse(isInstrumented);
}

@Test
void classIsInstrumented() {
InstrumentationState state = InstrumentationState.create();

state.addInstrumentedType(TEST_TYPE);
boolean isInstrumented = state.isInstrumented(TEST_CLASS);

assertTrue(isInstrumented);
}

@Test
void classIsDeinstrumented() {
InstrumentationState state = InstrumentationState.create();

state.addInstrumentedType(TEST_TYPE);
state.invalidateInstrumentedType(TEST_TYPE);
boolean isInstrumented = state.isInstrumented(TEST_CLASS);

assertFalse(isInstrumented);
}

@Test
void typeIsNotInstrumented() {
InstrumentationState state = InstrumentationState.create();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package rocks.inspectit.gepard.agent.internal.instrumentation;

import static org.junit.jupiter.api.Assertions.*;

import org.junit.jupiter.api.Test;

class InstrumentedTypeTest {

private static final Class<?> TEST_CLASS = InstrumentedTypeTest.class;

@Test
void typeIsEqualToClass() {
String typeName = TEST_CLASS.getName();
ClassLoader typeClassLoader = TEST_CLASS.getClassLoader();
InstrumentedType type = new InstrumentedType(typeName, typeClassLoader);

assertTrue(type.isEqualTo(TEST_CLASS));
}

@Test
void typeWithDifferentNameIsNotEqualToClass() {
String typeName = TEST_CLASS.getSimpleName();
ClassLoader typeClassLoader = TEST_CLASS.getClassLoader();
InstrumentedType type = new InstrumentedType(typeName, typeClassLoader);

assertFalse(type.isEqualTo(TEST_CLASS));
}

@Test
void typeWithDifferentClassLoaderIsNotEqualToClass() {
String typeName = TEST_CLASS.getName();
InstrumentedType type = new InstrumentedType(typeName, null);

assertFalse(type.isEqualTo(TEST_CLASS));
}

@Test
void typeEqualsOtherType() {
String typeName = TEST_CLASS.getName();
ClassLoader typeClassLoader = TEST_CLASS.getClassLoader();

InstrumentedType type1 = new InstrumentedType(typeName, typeClassLoader);
InstrumentedType type2 = new InstrumentedType(typeName, typeClassLoader);

assertEquals(type2, type1);
}

@Test
void typeDoesNotEqualOtherType() {
String typeName = TEST_CLASS.getName();
ClassLoader typeClassLoader = TEST_CLASS.getClassLoader();

InstrumentedType type1 = new InstrumentedType(typeName, typeClassLoader);
InstrumentedType type2 = new InstrumentedType(typeName, null);

assertNotEquals(type2, type1);
}

@Test
void typeDoesNotEqualNull() {
String typeName = TEST_CLASS.getName();
ClassLoader typeClassLoader = TEST_CLASS.getClassLoader();

InstrumentedType type1 = new InstrumentedType(typeName, typeClassLoader);

assertNotEquals(null, type1);
}

@Test
void typesHaveSameHashCode() {
String typeName = TEST_CLASS.getName();
ClassLoader typeClassLoader = TEST_CLASS.getClassLoader();

InstrumentedType type1 = new InstrumentedType(typeName, typeClassLoader);
InstrumentedType type2 = new InstrumentedType(typeName, typeClassLoader);

assertEquals(type2.hashCode(), type1.hashCode());
}

@Test
void typesHaveDifferentHashCode() {
String typeName = TEST_CLASS.getName();
ClassLoader typeClassLoader = TEST_CLASS.getClassLoader();

InstrumentedType type1 = new InstrumentedType(typeName, typeClassLoader);
InstrumentedType type2 = new InstrumentedType(typeName, null);

assertNotEquals(type2.hashCode(), type1.hashCode());
}
}

0 comments on commit dc3e5a4

Please sign in to comment.