Skip to content

[SHIRO-811] fix toggling test through Mockito conversion. #282

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,17 @@
import org.apache.shiro.subject.support.SubjectRunnable;
import org.apache.shiro.test.SecurityManagerTestSupport;
import org.junit.Test;
import org.mockito.ArgumentCaptor;

import java.util.concurrent.*;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

import static org.easymock.EasyMock.*;
import static org.junit.Assert.assertNotNull;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

/**
* Test cases for the {@link SubjectAwareExecutorService} implementation.
Expand All @@ -34,40 +41,42 @@ public class SubjectAwareExecutorServiceTest extends SecurityManagerTestSupport
@SuppressWarnings({"unchecked"})
@Test
public void testSubmitRunnable() {
ExecutorService mockExecutorService = createNiceMock(ExecutorService.class);
expect(mockExecutorService.submit(isA(SubjectRunnable.class))).andReturn(new DummyFuture());
replay(mockExecutorService);
ExecutorService mockExecutorService = mock(ExecutorService.class);
ArgumentCaptor<SubjectRunnable> captor = ArgumentCaptor.forClass(SubjectRunnable.class);
when(mockExecutorService.submit(captor.capture())).thenReturn(new DummyFuture<>());

final SubjectAwareExecutorService executor = new SubjectAwareExecutorService(mockExecutorService);

Runnable testRunnable = new Runnable() {
public void run() {
System.out.println("Hello World");
}
};
Runnable testRunnable = () -> System.out.println("Hello World");

executor.submit(testRunnable);
verify(mockExecutorService);
SubjectRunnable subjectRunnable = captor.getValue();
assertNotNull(subjectRunnable);
}

private class DummyFuture<V> implements Future<V> {

@Override
public boolean cancel(boolean b) {
return false;
}

@Override
public boolean isCancelled() {
return false;
}

@Override
public boolean isDone() {
return true;
}

@Override
public V get() throws InterruptedException, ExecutionException {
return null;
}

@Override
public V get(long l, TimeUnit timeUnit) throws InterruptedException, ExecutionException, TimeoutException {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,13 @@
import org.apache.shiro.subject.support.SubjectRunnable;
import org.apache.shiro.test.SecurityManagerTestSupport;
import org.junit.Test;
import org.mockito.ArgumentCaptor;

import java.util.concurrent.Executor;

import static org.easymock.EasyMock.*;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;

/**
* Test cases for the {@link SubjectAwareExecutor} implementation.
Expand All @@ -35,21 +38,17 @@ public class SubjectAwareExecutorTest extends SecurityManagerTestSupport {

@Test
public void testExecute() {
Executor targetMockExecutor = createNiceMock(Executor.class);
//* ensure the target Executor receives a SubjectRunnable instance that retains the subject identity:
//(this is what verifies the test is valid):
targetMockExecutor.execute(isA(SubjectRunnable.class));
replay(targetMockExecutor);

Executor targetMockExecutor = mock(Executor.class);
final SubjectAwareExecutor executor = new SubjectAwareExecutor(targetMockExecutor);

Runnable work = new Runnable() {
public void run() {
System.out.println("Hello World");
}
};
Runnable work = () -> System.out.println("Hello World");
executor.execute(work);

verify(targetMockExecutor);
//* ensure the target Executor receives a SubjectRunnable instance that retains the subject identity:
//(this is what verifies the test is valid):
ArgumentCaptor<SubjectRunnable> subjectRunnableArgumentCaptor = ArgumentCaptor.forClass(SubjectRunnable.class);
verify(targetMockExecutor).execute(subjectRunnableArgumentCaptor.capture());
SubjectRunnable subjectRunnable = subjectRunnableArgumentCaptor.getValue();
assertNotNull(subjectRunnable);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,10 @@ public BasicHttpAuthenticationFilter() {
* </ol>
*
* @param request incoming ServletRequest
* @param response outgoing ServletResponse
* @param response outgoing ServletResponse (never used)
* @return the AuthenticationToken used to execute the login attempt
*/
@Override
protected AuthenticationToken createToken(ServletRequest request, ServletResponse response) {
String authorizationHeader = getAuthzHeader(request);
if (authorizationHeader == null || authorizationHeader.length() == 0) {
Expand Down Expand Up @@ -125,6 +126,7 @@ protected AuthenticationToken createToken(ServletRequest request, ServletRespons
* @param encoded the Base64-encoded username:password value found after the scheme in the header
* @return the username (index 0)/password (index 1) pair obtained from the encoded header data.
*/
@Override
protected String[] getPrincipalsAndCredentials(String scheme, String encoded) {
String decoded = Base64.decodeToString(encoded);
return decoded.split(":", 2);
Expand Down
12 changes: 6 additions & 6 deletions web/src/test/java/org/apache/shiro/web/WebTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,21 @@
*/
package org.apache.shiro.web;

import static org.easymock.EasyMock.createNiceMock;
import static org.easymock.EasyMock.expect;

import javax.servlet.FilterConfig;
import javax.servlet.ServletContext;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

/**
* @since 1.0
*/
public abstract class WebTest {

protected FilterConfig createNiceMockFilterConfig() {
FilterConfig mock = createNiceMock(FilterConfig.class);
ServletContext mockServletContext = createNiceMock(ServletContext.class);
expect(mock.getServletContext()).andReturn(mockServletContext);
FilterConfig mock = mock(FilterConfig.class);
ServletContext mockServletContext = mock(ServletContext.class);
when(mock.getServletContext()).thenReturn(mockServletContext);
return mock;
}

Expand Down
Loading