Skip to content

Commit

Permalink
chore: improve test coverage.
Browse files Browse the repository at this point in the history
  • Loading branch information
overcat committed Aug 5, 2024
1 parent 8d5ce3e commit cae9e99
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 10 deletions.
23 changes: 17 additions & 6 deletions src/main/java/org/stellar/sdk/LedgerBounds.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.stellar.sdk;

import lombok.Builder;
import lombok.Value;
import org.stellar.sdk.xdr.Uint32;
import org.stellar.sdk.xdr.XdrUnsignedInteger;
Expand All @@ -14,25 +13,37 @@
* target="_blank">LedgerBounds</a>
*/
@Value
@Builder
public class LedgerBounds {
/** Minimum ledger sequence number of the transaction. */
long minLedger;

/** Maximum ledger sequence number of the transaction. */
long maxLedger;

public LedgerBounds(long minLedger, long maxLedger) {
if (minLedger < XdrUnsignedInteger.MIN_VALUE || minLedger > XdrUnsignedInteger.MAX_VALUE) {
throw new IllegalArgumentException("minLedger must be between 0 and 2^32-1");
}
if (maxLedger < XdrUnsignedInteger.MIN_VALUE || maxLedger > XdrUnsignedInteger.MAX_VALUE) {
throw new IllegalArgumentException("maxLedger must be between 0 and 2^32-1");
}
if (maxLedger > 0 && minLedger > maxLedger) {
throw new IllegalArgumentException("minLedger can not be greater than maxLedger");
}
this.minLedger = minLedger;
this.maxLedger = maxLedger;
}

/**
* Creates a new LedgerBounds object.
*
* @param xdrLedgerBounds XDR LedgerBounds object to convert.
* @return LedgerBounds
*/
public static LedgerBounds fromXdr(org.stellar.sdk.xdr.LedgerBounds xdrLedgerBounds) {
return new LedgerBoundsBuilder()
.minLedger(xdrLedgerBounds.getMinLedger().getUint32().getNumber())
.maxLedger(xdrLedgerBounds.getMaxLedger().getUint32().getNumber())
.build();
return new LedgerBounds(
xdrLedgerBounds.getMinLedger().getUint32().getNumber(),
xdrLedgerBounds.getMaxLedger().getUint32().getNumber());
}

/**
Expand Down
74 changes: 74 additions & 0 deletions src/test/java/org/stellar/sdk/LedgerBoundsTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package org.stellar.sdk;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

import org.junit.Test;

public class LedgerBoundsTest {
@Test
public void testSetLedgerBoundsNegativeMinLedger() {
try {
new LedgerBounds(-1, 300);
fail();
} catch (IllegalArgumentException e) {
assertEquals("minLedger must be between 0 and 2^32-1", e.getMessage());
}
}

@Test
public void testSetLedgerBoundsNegativeMaxLedger() {
try {
new LedgerBounds(1, -300);
fail();
} catch (IllegalArgumentException e) {
assertEquals("maxLedger must be between 0 and 2^32-1", e.getMessage());
}
}

@Test
public void testSetLedgerBoundsMinLedgerGreaterThanUint32MaxValue() {
try {
new LedgerBounds(4294967296L, 300);
fail();
} catch (IllegalArgumentException e) {
assertEquals("minLedger must be between 0 and 2^32-1", e.getMessage());
}
}

@Test
public void testSetLedgerBoundsMaxLedgerGreaterThanUint32MaxValue() {
try {
new LedgerBounds(1, 4294967296L);
fail();
} catch (IllegalArgumentException e) {
assertEquals("maxLedger must be between 0 and 2^32-1", e.getMessage());
}
}

@Test
public void testSetLedgerBoundsMinLedgerGreaterThanMaxLedger() {
try {
new LedgerBounds(300, 1);
fail();
} catch (IllegalArgumentException e) {
assertEquals("minLedger can not be greater than maxLedger", e.getMessage());
}
}

@Test
public void testSetLedgerBoundsMinLedgerGreaterThanMaxLedgerButMaxLedgerIsZero() {
LedgerBounds ledgerBounds = new LedgerBounds(300, 0);
assertEquals(300, ledgerBounds.getMinLedger());
assertEquals(0, ledgerBounds.getMaxLedger());
assertEquals(LedgerBounds.fromXdr(ledgerBounds.toXdr()), ledgerBounds);
}

@Test
public void testSetLedgerBounds() {
LedgerBounds ledgerBounds = new LedgerBounds(300, 400);
assertEquals(300, ledgerBounds.getMinLedger());
assertEquals(400, ledgerBounds.getMaxLedger());
assertEquals(LedgerBounds.fromXdr(ledgerBounds.toXdr()), ledgerBounds);
}
}
6 changes: 2 additions & 4 deletions src/test/java/org/stellar/sdk/TransactionBuilderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ public void testBuilderSetsLedgerBounds() throws IOException {
TransactionPreconditions.builder()
.timeBounds(
new TimeBounds(BigInteger.ZERO, TransactionPreconditions.TIMEOUT_INFINITE))
.ledgerBounds(LedgerBounds.builder().minLedger(1).maxLedger(2).build())
.ledgerBounds(new LedgerBounds(1, 2))
.build())
.setBaseFee(Transaction.MIN_BASE_FEE)
.build();
Expand Down Expand Up @@ -654,9 +654,7 @@ public void testBuilderWhenTimeoutSetAndLedgerBoundsSet() throws IOException {
.build())
.setBaseFee(Transaction.MIN_BASE_FEE)
.addPreconditions(
TransactionPreconditions.builder()
.ledgerBounds(LedgerBounds.builder().minLedger(123).maxLedger(456).build())
.build())
TransactionPreconditions.builder().ledgerBounds(new LedgerBounds(123, 456)).build())
.setTimeout(10)
.build();

Expand Down

0 comments on commit cae9e99

Please sign in to comment.