Skip to content

Commit

Permalink
test: added tests for MatcherChainBuilder and CustomElementMatchers
Browse files Browse the repository at this point in the history
  • Loading branch information
levinkerschberger committed Sep 9, 2024
1 parent a8bbf46 commit 9f09115
Show file tree
Hide file tree
Showing 6 changed files with 184 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ public class CustomElementMatchers {
* the name settings are available.
*/
public static <T extends NamedElement> ElementMatcher.Junction<T> nameIs(String methodName) {

if(methodName.isEmpty()){
return null;
}

return new NameMatcher<>(new StringMatcher(methodName, StringMatcher.Mode.EQUALS_FULLY));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,21 @@
import net.bytebuddy.matcher.ElementMatcher;

/**
* This class is a copy InspectIT Ocelot: <a
* href="https://github.com/inspectIT/inspectit-ocelot/blob/master/inspectit-ocelot-core/src/main/java/rocks/inspectit/ocelot/core/instrumentation/config/matcher/MatcherChainBuilder.java">
* MatcherChainBuilder </a>
*
* <p>Original Author: The InspectIT Ocelot team
*
* <p>Date Copied: 09.09.2024
* Helper class for building and linking {@link ElementMatcher}s. All methods are null-safe. Adding
* a null matcher will not have an effect on the matcher in construction.
*
* @param <T> The matchers generic type. Most of the time it will be TypeDescription or
* MethodDescription.
*/
public class MethodMatcherChainBuilder<T> {
public class MatcherChainBuilder<T> {

private ElementMatcher.Junction<T> matcher = null;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import rocks.inspectit.gepard.agent.internal.configuration.model.instrumentation.Scope;
import rocks.inspectit.gepard.agent.resolver.ConfigurationHolder;
import rocks.inspectit.gepard.agent.resolver.matcher.CustomElementMatchers;
import rocks.inspectit.gepard.agent.resolver.matcher.MethodMatcherChainBuilder;
import rocks.inspectit.gepard.agent.resolver.matcher.MatcherChainBuilder;

/**
* This class is used to resolve the {@link Scope} based on the {@link Scope} List, contained in the
Expand Down Expand Up @@ -100,8 +100,8 @@ private List<String> collectMethodNames(List<Scope> scopes) {
*/
private ElementMatcher.Junction<MethodDescription> buildMatcherForMethods(
List<String> methodNames) {
MethodMatcherChainBuilder<MethodDescription> matcherChainBuilder =
new MethodMatcherChainBuilder<>();
MatcherChainBuilder<MethodDescription> matcherChainBuilder =
new MatcherChainBuilder<>();
methodNames.forEach(
methodName -> matcherChainBuilder.or(CustomElementMatchers.nameIs(methodName)));
return matcherChainBuilder.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ private InspectitConfiguration createConfiguration(List<Scope> scopes) {
*
* @param enabled the status of the scope for this test class
* @param methodNames the method names to be instrumented
* @return the scope with the current class as scope
* @return the scope with the current class as fqn
*/
private Scope createScope(boolean enabled, List<String> methodNames) {
return new Scope(TEST_TYPE.getName(), methodNames, enabled);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package rocks.inspectit.gepard.agent.resolver.matcher;

import net.bytebuddy.description.NamedElement;
import net.bytebuddy.matcher.ElementMatcher;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;

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

class CustomElementMatchersTest {
@Nested
public class NameIs {

@Test
public void emptyNameReturnsNull() {
String method = "";
ElementMatcher.Junction<NamedElement> result = CustomElementMatchers.nameIs(method);
assertNull(result);
}

@Test
public void validNameMatcherIsReturned () {
String method = "testMethod";
ElementMatcher.Junction<NamedElement> result = CustomElementMatchers.nameIs(method);
assertNotNull(result);
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
package rocks.inspectit.gepard.agent.resolver.matcher;

import net.bytebuddy.matcher.ElementMatcher;
import net.bytebuddy.matcher.ElementMatchers;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;

import static net.bytebuddy.matcher.ElementMatchers.not;
import static org.junit.jupiter.api.Assertions.*;

class MatcherChainBuilderTest {
private MatcherChainBuilder<Object> builder;

private ElementMatcher.Junction<Object> anyMatcher = ElementMatchers.any();

@BeforeEach
public void beforeEach() {
builder = new MatcherChainBuilder<>();
}

@Nested
public class Or {

@Test
public void nullMatcher() {
builder.or(null);

assertNull(builder.build());
assertTrue(builder.isEmpty());
}

@Test
public void singleMatcher() {
builder.or(anyMatcher);

Assertions.assertEquals(builder.build(), anyMatcher);
assertFalse(builder.isEmpty());
}

@Test
public void multipleMatcher() {
builder.or(anyMatcher);
builder.or(anyMatcher);

assertEquals(builder.build(), anyMatcher.or(anyMatcher));
}

@Test
public void nullOnNotEmpty() {
builder.or(anyMatcher);
builder.or(null);

assertEquals(builder.build(), anyMatcher);
}
}

@Nested
public class And {

@Test
public void nullMatcher() {
builder.and(null);

assertNull(builder.build());
assertTrue(builder.isEmpty());
}

@Test
public void singleMatcher() {
builder.and(anyMatcher);

assertEquals(builder.build(), anyMatcher);
assertFalse(builder.isEmpty());
}

@Test
public void multipleMatcher() {
builder.and(anyMatcher);
builder.and(anyMatcher);

assertEquals(builder.build(), anyMatcher.and(anyMatcher));
}

@Test
public void nullOnNotEmpty() {
builder.and(anyMatcher);
builder.and(null);

assertEquals(builder.build(), anyMatcher);
}

@Test
public void conditionalTrue() {
builder.and(true, anyMatcher);

assertEquals(builder.build(), anyMatcher);
}

@Test
public void conditionalFalse() {
builder.and(false, anyMatcher);

assertEquals(builder.build(), not(anyMatcher));
}

@Test
public void conditionalFalseOnNotEmpty() {
builder.and(anyMatcher);
builder.and(false, anyMatcher);

assertEquals(builder.build(), anyMatcher.and(not(anyMatcher)));
}
}

@Nested
public class IsEmpty {

@Test
public void empty() {
boolean result = builder.isEmpty();

assertTrue(result);
}

@Test
public void notEmpty() {
builder.and(ElementMatchers.any());

boolean result = builder.isEmpty();

assertFalse(result);
}
}

}

0 comments on commit 9f09115

Please sign in to comment.