Skip to content

Commit

Permalink
test protected method
Browse files Browse the repository at this point in the history
  • Loading branch information
laurit committed Nov 10, 2023
1 parent 4c25c19 commit 52d5ffb
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import muzzle.TestClasses.LdcAdvice;
import muzzle.TestClasses.MethodBodyAdvice;
import muzzle.TestClasses.Nested;
import muzzle.other.OtherTestClasses;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
Expand Down Expand Up @@ -71,16 +72,15 @@ public void methodBodyCreatesReferences() {
refB,
"method",
"(Ljava/lang/String;)Ljava/lang/String;",
PROTECTED_OR_HIGHER,
PACKAGE_OR_HIGHER,
OwnershipFlag.NON_STATIC);
assertMethod(
refB, "methodWithPrimitives", "(Z)V", PROTECTED_OR_HIGHER, OwnershipFlag.NON_STATIC);
assertMethod(refB, "staticMethod", "()V", PROTECTED_OR_HIGHER, OwnershipFlag.STATIC);
assertMethod(refB, "methodWithPrimitives", "(Z)V", PACKAGE_OR_HIGHER, OwnershipFlag.NON_STATIC);
assertMethod(refB, "staticMethod", "()V", PACKAGE_OR_HIGHER, OwnershipFlag.STATIC);
assertMethod(
refB,
"methodWithArrays",
"([Ljava/lang/String;)[Ljava/lang/Object;",
PROTECTED_OR_HIGHER,
PACKAGE_OR_HIGHER,
OwnershipFlag.NON_STATIC);

// field refs
Expand All @@ -93,7 +93,7 @@ public void methodBodyCreatesReferences() {
@Test
public void protectedRefTest() {
ReferenceCollector collector = new ReferenceCollector(s -> false);
collector.collectReferencesFromAdvice(Nested.B2.class.getName());
collector.collectReferencesFromAdvice(OtherTestClasses.Nested.B2.class.getName());
collector.prune();
Map<String, ClassRef> references = collector.getReferences();

Expand Down Expand Up @@ -137,21 +137,21 @@ public void invokedynamicCreatesReferences() {
references.get("muzzle.TestClasses$Nested$SomeImplementation"),
"someMethod",
"()V",
PROTECTED_OR_HIGHER,
PACKAGE_OR_HIGHER,
OwnershipFlag.NON_STATIC);
assertThat(references).containsKey("muzzle.TestClasses$Nested$B");
assertMethod(
references.get("muzzle.TestClasses$Nested$B"),
"staticMethod",
"()V",
PROTECTED_OR_HIGHER,
PACKAGE_OR_HIGHER,
OwnershipFlag.STATIC);
assertThat(references).containsKey("muzzle.TestClasses$Nested$A");
assertMethod(
references.get("muzzle.TestClasses$Nested$A"),
"<init>",
"()V",
PROTECTED_OR_HIGHER,
PACKAGE_OR_HIGHER,
OwnershipFlag.NON_STATIC);
}

Expand Down
26 changes: 26 additions & 0 deletions muzzle/src/test/java/muzzle/other/OtherTestClasses.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package muzzle.other;

import muzzle.TestClasses;

// to testing protected methods we need to have a test classes in different packages
@SuppressWarnings("unused")
public class OtherTestClasses {

@SuppressWarnings("ClassNamedLikeTypeParameter")
public static class Nested {
public static class B2 extends TestClasses.Nested.B {
public void stuff() {
super.protectedMethod();
}
}

private Nested() {}
}

private OtherTestClasses() {}
}

0 comments on commit 52d5ffb

Please sign in to comment.