Skip to content

Commit

Permalink
add test class
Browse files Browse the repository at this point in the history
  • Loading branch information
mariofusco committed Aug 22, 2023
1 parent fb855cb commit 6d09085
Showing 1 changed file with 64 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package com.fasterxml.jackson.core.write;

import com.fasterxml.jackson.core.BaseTest;
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.util.BufferRecyclerPool;

import java.io.IOException;
import java.io.OutputStream;

public class BufferRecyclerPoolTest extends BaseTest {

public void testNoOp() {
checkBufferRecyclerPoolImpl(BufferRecyclerPool.PoolStrategy.NO_OP);
}

public void testThreadLocal() {
checkBufferRecyclerPoolImpl(BufferRecyclerPool.PoolStrategy.THREAD_LOCAL);
}

public void testLockFree() {
checkBufferRecyclerPoolImpl(BufferRecyclerPool.PoolStrategy.LOCK_FREE);
}

public void testConcurrentDequeue() {
checkBufferRecyclerPoolImpl(BufferRecyclerPool.PoolStrategy.CONCURRENT_DEQUEUE);
}

private void checkBufferRecyclerPoolImpl(BufferRecyclerPool.PoolStrategy poolStrategy) {
JsonFactory jsonFactory = new JsonFactory().setBufferRecyclerPool(poolStrategy.getPool());
assertEquals(6, write("test", jsonFactory));
}

protected final int write(Object value, JsonFactory jsonFactory) {
NopOutputStream out = new NopOutputStream();
try (JsonGenerator gen = jsonFactory.createGenerator(out)) {
gen.writeObject(value);
} catch (IOException e) {
throw new RuntimeException(e);
}
return out.size();
}

public class NopOutputStream extends OutputStream {
protected int size = 0;

public NopOutputStream() { }

@Override
public void write(int b) throws IOException { ++size; }

@Override
public void write(byte[] b) throws IOException { size += b.length; }

@Override
public void write(byte[] b, int offset, int len) throws IOException { size += len; }

public NopOutputStream reset() {
size = 0;
return this;
}
public int size() { return size; }
}
}

0 comments on commit 6d09085

Please sign in to comment.