From ec4c542dadc5919f07716c6cfa61ba1515f8458b Mon Sep 17 00:00:00 2001 From: Xiao Date: Tue, 21 Sep 2021 23:39:10 -0400 Subject: [PATCH] CODEC-303: Refactor NoOpBaseNCodec to improve test logic --- pom.xml | 6 +++ .../commons/codec/binary/BaseNCodecTest.java | 44 +++++++------------ 2 files changed, 23 insertions(+), 27 deletions(-) diff --git a/pom.xml b/pom.xml index 73f8074319..95ae29cb4f 100644 --- a/pom.xml +++ b/pom.xml @@ -243,6 +243,12 @@ limitations under the License. 3.12.0 test + + org.mockito + mockito-core + 3.12.4 + test + 1.8 diff --git a/src/test/java/org/apache/commons/codec/binary/BaseNCodecTest.java b/src/test/java/org/apache/commons/codec/binary/BaseNCodecTest.java index 3b89c4d35d..9de00fd2fc 100644 --- a/src/test/java/org/apache/commons/codec/binary/BaseNCodecTest.java +++ b/src/test/java/org/apache/commons/codec/binary/BaseNCodecTest.java @@ -28,9 +28,19 @@ import org.junit.Before; import org.junit.Test; +import static org.mockito.Mockito.CALLS_REAL_METHODS; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.withSettings; + public class BaseNCodecTest { - BaseNCodec codec; + public static BaseNCodec mockBaseNCodec1() { + BaseNCodec mockInstance = mock(BaseNCodec.class, + withSettings().useConstructor(0, 0, 0, 0).defaultAnswer(CALLS_REAL_METHODS)); + return mockInstance; + } + + BaseNCodec codec; @Before public void setUp() { @@ -223,7 +233,8 @@ void decode(final byte[] pArray, final int i, final int length, final Context co @Test public void testEnsureBufferSize() { - final BaseNCodec ncodec = new NoOpBaseNCodec(); + // Construct mock object + final BaseNCodec ncodec = BaseNCodecTest.mockBaseNCodec1(); final Context context = new Context(); Assert.assertNull("Initial buffer should be null", context.buffer); @@ -306,7 +317,8 @@ private static void assertEnsureBufferSizeExpandsToMaxBufferSize(final boolean e System.gc(); } - final BaseNCodec ncodec = new NoOpBaseNCodec(); + // Construct mock object + final BaseNCodec ncodec = BaseNCodecTest.mockBaseNCodec1(); final Context context = new Context(); // Allocate the initial buffer @@ -358,7 +370,8 @@ static long getPresumableFreeMemory() { @Test(expected = OutOfMemoryError.class) public void testEnsureBufferSizeThrowsOnOverflow() { - final BaseNCodec ncodec = new NoOpBaseNCodec(); + // Construct mock object + final BaseNCodec ncodec = BaseNCodecTest.mockBaseNCodec1(); final Context context = new Context(); final int length = 10; @@ -367,27 +380,4 @@ public void testEnsureBufferSizeThrowsOnOverflow() { final int extra = Integer.MAX_VALUE; ncodec.ensureBufferSize(extra, context); } - - /** - * Extend BaseNCodec without implementation (no operations = NoOp). - * Used for testing the memory allocation in {@link BaseNCodec#ensureBufferSize(int, Context)}. - */ - private static class NoOpBaseNCodec extends BaseNCodec { - NoOpBaseNCodec() { - super(0, 0, 0, 0); - } - - @Override - void encode(final byte[] pArray, final int i, final int length, final Context context) { - } - - @Override - void decode(final byte[] pArray, final int i, final int length, final Context context) { - } - - @Override - protected boolean isInAlphabet(final byte value) { - return false; - } - } }