-
Notifications
You must be signed in to change notification settings - Fork 891
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from laurit/duplicate-helper-test
Add test for duplicate helper class
- Loading branch information
Showing
5 changed files
with
95 additions
and
0 deletions.
There are no files selected for viewing
9 changes: 9 additions & 0 deletions
9
testing-common/integration-tests/src/main/java/helper/DuplicateHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package helper; | ||
|
||
public class DuplicateHelper { | ||
public static String addSuffix(String string, String suffix) { | ||
return string + suffix; | ||
} | ||
|
||
private DuplicateHelper() {} | ||
} |
29 changes: 29 additions & 0 deletions
29
testing-common/integration-tests/src/main/java/helper/DuplicateHelperInstrumentation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package helper; | ||
|
||
import static net.bytebuddy.matcher.ElementMatchers.named; | ||
|
||
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; | ||
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer; | ||
import net.bytebuddy.asm.Advice; | ||
import net.bytebuddy.description.type.TypeDescription; | ||
import net.bytebuddy.matcher.ElementMatcher; | ||
|
||
public class DuplicateHelperInstrumentation implements TypeInstrumentation { | ||
@Override | ||
public ElementMatcher<TypeDescription> typeMatcher() { | ||
return named("helper.DuplicateHelperTestClass"); | ||
} | ||
|
||
@Override | ||
public void transform(TypeTransformer transformer) { | ||
transformer.applyAdviceToMethod(named("transform"), this.getClass().getName() + "$TestAdvice"); | ||
} | ||
|
||
@SuppressWarnings("unused") | ||
public static class TestAdvice { | ||
@Advice.OnMethodExit(suppress = Throwable.class) | ||
static void addSuffix(@Advice.Return(readOnly = false) String string) { | ||
string = DuplicateHelper.addSuffix(string, " foo"); | ||
} | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
...g-common/integration-tests/src/main/java/helper/DuplicateHelperInstrumentationModule.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package helper; | ||
|
||
import static java.util.Collections.singletonList; | ||
|
||
import com.google.auto.service.AutoService; | ||
import io.opentelemetry.javaagent.extension.instrumentation.InstrumentationModule; | ||
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
@AutoService(InstrumentationModule.class) | ||
public class DuplicateHelperInstrumentationModule extends InstrumentationModule { | ||
public DuplicateHelperInstrumentationModule() { | ||
super("duplicate-helper"); | ||
} | ||
|
||
@Override | ||
public List<String> getAdditionalHelperClassNames() { | ||
// muzzle adds the same class as helper, listing it twice to ensure it doesn't fail | ||
return Arrays.asList("helper.DuplicateHelper", "helper.DuplicateHelper"); | ||
} | ||
|
||
@Override | ||
public boolean isHelperClass(String className) { | ||
return "helper.DuplicateHelper".equals(className); | ||
} | ||
|
||
@Override | ||
public List<TypeInstrumentation> typeInstrumentations() { | ||
return singletonList(new DuplicateHelperInstrumentation()); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
testing-common/integration-tests/src/test/java/helper/DuplicateHelperTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package helper; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
class DuplicateHelperTest { | ||
|
||
@Test | ||
void duplicateHelper() { | ||
String string = DuplicateHelperTestClass.transform("test"); | ||
assertThat(string).isEqualTo("test foo"); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
testing-common/integration-tests/src/test/java/helper/DuplicateHelperTestClass.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package helper; | ||
|
||
class DuplicateHelperTestClass { | ||
|
||
// this method is transformed by instrumentation | ||
public static String transform(String string) { | ||
return string; | ||
} | ||
|
||
private DuplicateHelperTestClass() {} | ||
} |