diff --git a/activemq-client/src/main/java/org/apache/activemq/util/BitArrayBin.java b/activemq-client/src/main/java/org/apache/activemq/util/BitArrayBin.java index 592873b7e7..528144ee3d 100755 --- a/activemq-client/src/main/java/org/apache/activemq/util/BitArrayBin.java +++ b/activemq-client/src/main/java/org/apache/activemq/util/BitArrayBin.java @@ -21,21 +21,21 @@ import java.util.LinkedList; /** * Holder for many bitArrays - used for message audit - * - * + * + * */ public class BitArrayBin implements Serializable { private static final long serialVersionUID = 1L; - private LinkedList list; + private final LinkedList list; private int maxNumberOfArrays; - private int firstIndex = -1; + private long firstIndex = -1; private long lastInOrderBit=-1; /** * Create a BitArrayBin to a certain window size (number of messages to * keep) - * + * * @param windowSize */ public BitArrayBin(int windowSize) { @@ -49,7 +49,7 @@ public class BitArrayBin implements Serializable { /** * Set a bit - * + * * @param index * @param value * @return true if set @@ -65,7 +65,7 @@ public class BitArrayBin implements Serializable { } return answer; } - + /** * Test if in order * @param index @@ -85,7 +85,7 @@ public class BitArrayBin implements Serializable { /** * Get the boolean value at the index - * + * * @param index * @return true/false */ @@ -107,7 +107,7 @@ public class BitArrayBin implements Serializable { /** * Get the BitArray for the index - * + * * @param index * @return BitArray */ @@ -123,7 +123,7 @@ public class BitArrayBin implements Serializable { list.add(new BitArray()); overShoot--; } - + bin = maxNumberOfArrays - 1; } answer = list.get(bin); @@ -137,7 +137,7 @@ public class BitArrayBin implements Serializable { /** * Get the index of the bin from the total index - * + * * @param index * @return the index of the bin */ @@ -153,7 +153,7 @@ public class BitArrayBin implements Serializable { /** * Get the offset into a bin from the total index - * + * * @param index * @return the relative offset into a bin */ @@ -167,9 +167,9 @@ public class BitArrayBin implements Serializable { public long getLastSetIndex() { long result = -1; - + if (firstIndex >=0) { - result = firstIndex; + result = firstIndex; BitArray last = null; for (int lastBitArrayIndex = maxNumberOfArrays -1; lastBitArrayIndex >= 0; lastBitArrayIndex--) { last = list.get(lastBitArrayIndex); diff --git a/activemq-client/src/test/java/org/apache/activemq/util/BitArrayBinTest.java b/activemq-client/src/test/java/org/apache/activemq/util/BitArrayBinTest.java index e1276d88be..2b17d8fd60 100644 --- a/activemq-client/src/test/java/org/apache/activemq/util/BitArrayBinTest.java +++ b/activemq-client/src/test/java/org/apache/activemq/util/BitArrayBinTest.java @@ -16,16 +16,21 @@ */ package org.apache.activemq.util; -import junit.framework.TestCase; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; -public class BitArrayBinTest extends TestCase { - +import org.junit.Test; + +public class BitArrayBinTest { + + @Test public void testSetAroundWindow() throws Exception { doTestSetAroundWindow(500, 2000); doTestSetAroundWindow(512, 2000); doTestSetAroundWindow(128, 512); } + @Test public void testSetHiLo() throws Exception { BitArrayBin toTest = new BitArrayBin(50); toTest.setBit(0, true); @@ -34,127 +39,136 @@ public class BitArrayBinTest extends TestCase { assertTrue("set", toTest.getBit(0)); toTest.setBit(0, true); - assertTrue("set", toTest.getBit(0)); + assertTrue("set", toTest.getBit(0)); } - + private void doTestSetAroundWindow(int window, int dataSize) throws Exception { BitArrayBin toTest = new BitArrayBin(window); - - for (int i=0; i <= dataSize; i++) { + + for (int i = 0; i <= dataSize; i++) { assertTrue("not already set", !toTest.setBit(i, Boolean.TRUE)); assertEquals("current is max", i, toTest.getLastSetIndex()); } assertEquals("last is max", dataSize, toTest.getLastSetIndex()); - + int windowOfValidData = roundWindow(dataSize, window); - int i=dataSize; - for (; i >= dataSize -windowOfValidData; i--) { + int i = dataSize; + for (; i >= dataSize - windowOfValidData; i--) { assertTrue("was already set, id=" + i, toTest.setBit(i, Boolean.TRUE)); } assertEquals("last is still max", dataSize, toTest.getLastSetIndex()); - + for (; i >= 0; i--) { assertTrue("was not already set, id=" + i, !toTest.setBit(i, Boolean.TRUE)); } - - for (int j= dataSize +1; j<=(2*dataSize); j++) { + + for (int j = dataSize + 1; j <= (2 * dataSize); j++) { assertTrue("not already set: id=" + j, !toTest.setBit(j, Boolean.TRUE)); } - - assertEquals("last still max*2", 2*dataSize, toTest.getLastSetIndex()); + assertEquals("last still max*2", 2 * dataSize, toTest.getLastSetIndex()); } - + + @Test public void testSetUnsetAroundWindow() throws Exception { doTestSetUnSetAroundWindow(500, 2000); doTestSetUnSetAroundWindow(512, 2000); doTestSetUnSetAroundWindow(128, 512); } - + private void doTestSetUnSetAroundWindow(int dataSize, int window) throws Exception { BitArrayBin toTest = new BitArrayBin(window); - - for (int i=0; i <=dataSize; i++) { + + for (int i = 0; i <= dataSize; i++) { assertTrue("not already set", !toTest.setBit(i, Boolean.TRUE)); } - + int windowOfValidData = roundWindow(dataSize, window); - for (int i=dataSize; i >= 0 && i >=dataSize -windowOfValidData; i--) { + for (int i = dataSize; i >= 0 && i >= dataSize - windowOfValidData; i--) { assertTrue("was already set, id=" + i, toTest.setBit(i, Boolean.FALSE)); } - for (int i=0; i <=dataSize; i++) { + for (int i = 0; i <= dataSize; i++) { assertTrue("not already set, id:" + i, !toTest.setBit(i, Boolean.TRUE)); } - for (int j= 2*dataSize; j< 4*dataSize; j++) { + for (int j = 2 * dataSize; j < 4 * dataSize; j++) { assertTrue("not already set: id=" + j, !toTest.setBit(j, Boolean.TRUE)); } } - + + @Test public void testSetAroundLongSizeMultiplier() throws Exception { int window = 512; int dataSize = 1000; - for (int muliplier=1; muliplier <8; muliplier++) { - for (int value=0; value 0? 1:0); + validDataBin += (windowSize % BitArray.LONG_SIZE > 0 ? 1 : 0); int startOfValid = validDataBin * BitArray.LONG_SIZE; - - return dataSetEnd - startOfValid; + + return dataSetEnd - startOfValid; } + @Test + public void testLargeNumber() throws Exception { + BitArrayBin toTest = new BitArrayBin(1024); + toTest.setBit(1, true); + long largeNum = Integer.MAX_VALUE * 2L + 100L; + toTest.setBit(largeNum, true); + assertTrue("set", toTest.getBit(largeNum)); + } }