Skip to content

Commit

Permalink
Remove unnecessary usage of power mock
Browse files Browse the repository at this point in the history
  • Loading branch information
peisun1115 committed Jan 12, 2017
1 parent d752ecf commit 3c6d032
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,20 @@
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

@RunWith(PowerMockRunner.class)
@PrepareForTest({BlockWorker.class, LocalFileBlockReader.class})
@PrepareForTest({LocalFileBlockReader.class})
public final class DataServerBlockReadHandlerTest extends DataServerReadHandlerTest {
private BlockWorker mBlockWorker;
private BlockReader mBlockReader;

@Before
public void before() throws Exception {
ResourceLeakDetector.setLevel(ResourceLeakDetector.Level.ADVANCED);
mBlockWorker = PowerMockito.mock(BlockWorker.class);
PowerMockito.doNothing().when(mBlockWorker).accessBlock(Mockito.anyLong(), Mockito.anyLong());
mBlockWorker = Mockito.mock(BlockWorker.class);
Mockito.doNothing().when(mBlockWorker).accessBlock(Mockito.anyLong(), Mockito.anyLong());
mChannel = new EmbeddedChannel(
new DataServerBlockReadHandler(NettyExecutors.BLOCK_READER_EXECUTOR, mBlockWorker,
FileTransferType.MAPPED));
Expand All @@ -56,10 +55,10 @@ public void transferType() throws Exception {
long fileSize = PACKET_SIZE * 2;
long checksumExpected = populateInputFile(fileSize, 0, fileSize - 1);

BlockReader blockReader = PowerMockito.spy(mBlockReader);
BlockReader blockReader = Mockito.spy(mBlockReader);
// Do not call close here so that we can check result. It will be closed explicitly.
PowerMockito.doNothing().when(blockReader).close();
PowerMockito
Mockito.doNothing().when(blockReader).close();
Mockito
.when(mBlockWorker.readBlockRemote(Mockito.anyLong(), Mockito.anyLong(), Mockito.anyLong()))
.thenReturn(blockReader);
mChannel.writeInbound(buildReadRequest(0, fileSize));
Expand All @@ -80,16 +79,16 @@ public void readFailure() throws Exception {
@Override
protected void mockReader(long start) throws Exception {
mBlockReader = new LocalFileBlockReader(mFile);
PowerMockito
Mockito
.when(mBlockWorker.readBlockRemote(Mockito.anyLong(), Mockito.anyLong(), Mockito.anyLong()))
.thenReturn(mBlockReader);
}

@Override
protected RPCProtoMessage buildReadRequest(long offset, long len) {
Protocol.ReadRequest readRequest =
Protocol.ReadRequest.newBuilder().setId(1L).setOffset(offset).setLength(len)
.setLockId(1L).setType(Protocol.RequestType.ALLUXIO_BLOCK).build();
Protocol.ReadRequest.newBuilder().setId(1L).setOffset(offset).setLength(len).setLockId(1L)
.setType(Protocol.RequestType.ALLUXIO_BLOCK).build();
return new RPCProtoMessage(readRequest, null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,10 @@
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

import java.util.Random;

@RunWith(PowerMockRunner.class)
@PrepareForTest({BlockWorker.class})
public final class DataServerBlockWriteHandlerTest extends DataServerWriteHandlerTest {
private final Random mRandom = new Random();

Expand All @@ -45,15 +39,15 @@ public final class DataServerBlockWriteHandlerTest extends DataServerWriteHandle

@Before
public void before() throws Exception {
mBlockWorker = PowerMockito.mock(BlockWorker.class);
PowerMockito.doNothing().when(mBlockWorker)
mBlockWorker = Mockito.mock(BlockWorker.class);
Mockito.doNothing().when(mBlockWorker)
.createBlockRemote(Mockito.anyLong(), Mockito.anyLong(), Mockito.anyString(),
Mockito.anyLong());
PowerMockito.doNothing().when(mBlockWorker)
Mockito.doNothing().when(mBlockWorker)
.requestSpace(Mockito.anyLong(), Mockito.anyLong(), Mockito.anyLong());
mFile = mTestFolder.newFile().getPath();
mBlockWriter = new LocalFileBlockWriter(mFile);
PowerMockito.when(mBlockWorker.getTempBlockWriterRemote(Mockito.anyLong(), Mockito.anyLong()))
Mockito.when(mBlockWorker.getTempBlockWriterRemote(Mockito.anyLong(), Mockito.anyLong()))
.thenReturn(mBlockWriter);
mChecksum = 0;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,19 @@
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.modules.junit4.PowerMockRunner;

import java.io.FileInputStream;
import java.io.InputStream;

@RunWith(PowerMockRunner.class)
public final class DataServerUFSFileReadHandlerTest extends DataServerReadHandlerTest {
private FileSystemWorker mFileSystemWorker;
private InputStream mInputStream;

@Before
public void before() throws Exception {
ResourceLeakDetector.setLevel(ResourceLeakDetector.Level.ADVANCED);
mFileSystemWorker = PowerMockito.mock(FileSystemWorker.class);
mFileSystemWorker = Mockito.mock(FileSystemWorker.class);
mChannel = new EmbeddedChannel(
new DataServerUFSFileReadHandler(NettyExecutors.FILE_READER_EXECUTOR, mFileSystemWorker));
mChannelNoException = new EmbeddedChannelNoException(
Expand All @@ -63,7 +59,7 @@ public void readFailure() throws Exception {
protected void mockReader(long start) throws Exception {
mInputStream = new FileInputStream(mFile);
mInputStream.skip(start);
PowerMockito.when(mFileSystemWorker.getUfsInputStream(Mockito.anyLong(), Mockito.anyLong()))
Mockito.when(mFileSystemWorker.getUfsInputStream(Mockito.anyLong(), Mockito.anyLong()))
.thenReturn(mInputStream);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,12 @@
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.modules.junit4.PowerMockRunner;

import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.Random;

@RunWith(PowerMockRunner.class)
public final class DataServerUFSFileWriteHandlerTest extends DataServerWriteHandlerTest {
private final Random mRandom = new Random();

Expand All @@ -43,10 +39,10 @@ public final class DataServerUFSFileWriteHandlerTest extends DataServerWriteHand

@Before
public void before() throws Exception {
mFileSystemWorker = PowerMockito.mock(FileSystemWorker.class);
mFileSystemWorker = Mockito.mock(FileSystemWorker.class);
mFile = mTestFolder.newFile().getPath();
mOutputStream = new FileOutputStream(mFile);
PowerMockito.when(mFileSystemWorker.getUfsOutputStream(Mockito.anyLong()))
Mockito.when(mFileSystemWorker.getUfsOutputStream(Mockito.anyLong()))
.thenReturn(mOutputStream);
mChecksum = 0;
mChannel = new EmbeddedChannel(
Expand Down

0 comments on commit 3c6d032

Please sign in to comment.