Skip to content
Closed
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
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,12 @@ limitations under the License.
<version>3.12.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>3.12.4</version>
<scope>test</scope>
</dependency>
</dependencies>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
Expand Down
44 changes: 17 additions & 27 deletions src/test/java/org/apache/commons/codec/binary/BaseNCodecTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand All @@ -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;
}
}
}