Skip to content

Commit

Permalink
wip: finished tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lprimak committed Apr 21, 2024
1 parent 3369263 commit db3cb88
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 0 deletions.
19 changes: 19 additions & 0 deletions core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,18 @@

<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<annotationProcessorPaths>
<annotationProcessorPath>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
</annotationProcessorPath>
</annotationProcessorPaths>
</configuration>
</plugin>
<!-- collect the test classes so they can be referenced by other modules -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
Expand Down Expand Up @@ -175,6 +187,13 @@
<version>${mockito.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
<scope>provided</scope>
<optional>true</optional>
</dependency>

<!-- JDBC Realm tests: -->
<dependency>
Expand Down
84 changes: 84 additions & 0 deletions core/src/test/java/org/apache/shiro/SecurityUtilsUnwrapTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,14 @@
*/
package org.apache.shiro;

import lombok.RequiredArgsConstructor;
import lombok.experimental.Delegate;
import org.apache.shiro.mgt.DefaultSecurityManager;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.mgt.SessionsSecurityManager;
import org.apache.shiro.mgt.WrappedSecurityManager;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.subject.SubjectContext;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
Expand All @@ -29,11 +35,42 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.when;

@ExtendWith(MockitoExtension.class)
class SecurityUtilsUnwrapTest {
@Mock
SecurityManager securityManager;
@Mock
DefaultSecurityManager defaultSecurityManager;
@Mock
Subject subject;
@Mock
SubjectContext subjectContext;

@RequiredArgsConstructor
static class Wrapped implements WrappedSecurityManager, SecurityManager {
private final @Delegate SecurityManager securityManager;

@Override
@SuppressWarnings("unchecked")
public <SM extends SecurityManager> SM unwrap() {
return (SM) securityManager;
}
}

@RequiredArgsConstructor
static class InvalidWrapped implements WrappedSecurityManager, SecurityManager {
private final @Delegate SecurityManager securityManager;

@Override
@SuppressWarnings("unchecked")
public <SM extends SecurityManager> SM unwrap() {
return (SM) this;
}
}

@Test
void basicUnwrap() {
Expand All @@ -52,4 +89,51 @@ void failedTypeUnwrap() {
SessionsSecurityManager ssm = unwrapSecurityManager(securityManager, SessionsSecurityManager.class);
});
}

@Test
void defaultSecurityManager() {
var dsm = unwrapSecurityManager(defaultSecurityManager, DefaultSecurityManager.class);
assertEquals(defaultSecurityManager, dsm);
when(defaultSecurityManager.createSubject(subjectContext)).thenReturn(subject);
Subject subject = dsm.createSubject(subjectContext);
assertEquals(this.subject, subject);
verify(defaultSecurityManager).createSubject(subjectContext);
verifyNoMoreInteractions(defaultSecurityManager, this.subject, subjectContext);
}

@Test
void invalidCast() {
SecurityManager wrapped = new Wrapped(defaultSecurityManager);
assertThrows(ClassCastException.class, () -> {
DefaultSecurityManager sm = (DefaultSecurityManager) wrapped;
});
}

@Test
void unwrapOne() {
SecurityManager wrapped = new Wrapped(defaultSecurityManager);
assertEquals(defaultSecurityManager, unwrapSecurityManager(wrapped, DefaultSecurityManager.class));
}

@Test
void unwrapTwo() {
SecurityManager wrapped = new Wrapped(new Wrapped(defaultSecurityManager));
assertEquals(defaultSecurityManager, unwrapSecurityManager(wrapped, DefaultSecurityManager.class));
}

@Test
void invalidWrap() {
SecurityManager wrapped = new Wrapped(new InvalidWrapped(defaultSecurityManager));
assertThrows(IllegalStateException.class, () -> {
assertEquals(defaultSecurityManager, unwrapSecurityManager(wrapped, DefaultSecurityManager.class));
});
}

@Test
void invalidWrapInverted() {
SecurityManager wrapped = new InvalidWrapped(new Wrapped(defaultSecurityManager));
assertThrows(IllegalStateException.class, () -> {
assertEquals(defaultSecurityManager, unwrapSecurityManager(wrapped, DefaultSecurityManager.class));
});
}
}

0 comments on commit db3cb88

Please sign in to comment.