forked from eugenp/tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
unit tests for nest based access control
- Loading branch information
Showing
2 changed files
with
74 additions
and
0 deletions.
There are no files selected for viewing
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,24 @@ | ||
package com.baeldung; | ||
|
||
import java.lang.reflect.Method; | ||
|
||
public class Outer { | ||
|
||
public void outerPublic() { | ||
} | ||
|
||
private void outerPrivate() { | ||
} | ||
|
||
class Inner { | ||
|
||
public void innerPublic() { | ||
outerPrivate(); | ||
} | ||
|
||
public void innerPublicReflection(Outer ob) throws Exception { | ||
Method method = ob.getClass().getDeclaredMethod("outerPrivate"); | ||
method.invoke(ob); | ||
} | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
core-java-11/src/test/java/com/baeldung/OuterUnitTest.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,50 @@ | ||
package com.baeldung; | ||
|
||
import static org.hamcrest.CoreMatchers.is; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
import org.junit.Test; | ||
|
||
public class OuterUnitTest { | ||
|
||
private static final String NEST_HOST_NAME = "com.baeldung.Outer"; | ||
|
||
@Test | ||
public void whenGetNestHostFromOuter_thenGetNestHost() { | ||
is(Outer.class.getNestHost().getName()).equals(NEST_HOST_NAME); | ||
} | ||
|
||
@Test | ||
public void whenGetNestHostFromInner_thenGetNestHost() { | ||
is(Outer.Inner.class.getNestHost().getName()).equals(NEST_HOST_NAME); | ||
} | ||
|
||
@Test | ||
public void whenCheckNestmatesForNestedClasses_thenGetTrue() { | ||
is(Outer.Inner.class.isNestmateOf(Outer.class)).equals(true); | ||
} | ||
|
||
@Test | ||
public void whenCheckNestmatesForUnrelatedClasses_thenGetFalse() { | ||
is(Outer.Inner.class.isNestmateOf(Outer.class)).equals(false); | ||
} | ||
|
||
@Test | ||
public void whenGetNestMembersForNestedClasses_thenGetAllNestedClasses() { | ||
List<String> nestMembers = Arrays.stream(Outer.Inner.class.getNestMembers()) | ||
.map(Class::getName) | ||
.collect(Collectors.toList()); | ||
|
||
is(nestMembers.size()).equals(2); | ||
|
||
boolean containsOuter = nestMembers.stream() | ||
.anyMatch("com.baeldung.Outer"::equals); | ||
is(containsOuter).equals(true); | ||
|
||
boolean containsInner = nestMembers.stream() | ||
.anyMatch("com.baeldung.Outer$Inner"::equals); | ||
is(containsInner).equals(true); | ||
} | ||
} |