Skip to content

Commit

Permalink
Fix NPE while call getLastMessageId. (apache#6562)
Browse files Browse the repository at this point in the history
### Motivation

Fixes apache#6561

### Modifications

Initialize `BatchMessageAckerDisabled` with a `new BitSet()` Object.
  • Loading branch information
murong00 authored Mar 19, 2020
1 parent 9634eb7 commit 2007de6
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
class BatchMessageAcker {

private BatchMessageAcker() {
this.bitSet = null;
this.bitSet = new BitSet();
this.batchSize = 0;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@
*/
package org.apache.pulsar.client.impl;

import java.util.BitSet;

class BatchMessageAckerDisabled extends BatchMessageAcker {

static final BatchMessageAckerDisabled INSTANCE = new BatchMessageAckerDisabled();

private BatchMessageAckerDisabled() {
super(null, 0);
super(new BitSet(), 0);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNotEquals;
import static org.testng.Assert.assertTrue;
import static org.testng.Assert.fail;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectWriter;
import org.apache.pulsar.common.util.ObjectMapperFactory;
import org.testng.annotations.Test;

public class BatchMessageIdImplTest {
Expand Down Expand Up @@ -68,4 +72,30 @@ public void equalsTest() {
assertEquals(msgId, batchMsgId4);
}

@Test
public void deserializationTest() {
// initialize BitSet with null
BatchMessageAcker ackerDisabled = new BatchMessageAcker(null, 0);
BatchMessageIdImpl batchMsgId = new BatchMessageIdImpl(0, 0, 0, 0, ackerDisabled);

ObjectWriter writer = ObjectMapperFactory.create().writerWithDefaultPrettyPrinter();

try {
writer.writeValueAsString(batchMsgId);
fail("Shouldn't be deserialized");
} catch (JsonProcessingException e) {
// expected
assertTrue(e.getCause() instanceof NullPointerException);
}

// use the default BatchMessageAckerDisabled
BatchMessageIdImpl batchMsgIdToDeserialize = new BatchMessageIdImpl(0, 0, 0, 0);

try {
writer.writeValueAsString(batchMsgIdToDeserialize);
} catch (JsonProcessingException e) {
fail("Should be successful");
}
}

}

0 comments on commit 2007de6

Please sign in to comment.