Skip to content

Commit

Permalink
Allow zero in length validation. return boolean for checks.
Browse files Browse the repository at this point in the history
  • Loading branch information
melowe committed Nov 16, 2018
1 parent c1b4413 commit dfe7845
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -144,18 +144,18 @@ static PartyInfoParser create() {
}


static void checkLength(long value) {
Optional.of(value)
.filter(v -> v > 0)
static boolean checkLength(long value) {
return Optional.of(value)
.filter(v -> v >= 0)
.filter(v -> v < Long.MAX_VALUE - 1)
.orElseThrow(() -> new PartyInfoParserException("Invalid length "+ value));
.isPresent();
}

static void checkLength(int value) {
Optional.of(value)
.filter(v -> v > 0)
static boolean checkLength(int value) {
return Optional.of(value)
.filter(v -> v >= 0)
.filter(v -> v < Integer.MAX_VALUE - 1)
.orElseThrow(() -> new PartyInfoParserException("Invalid length "+ value));
.isPresent();
}

}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import java.nio.ByteBuffer;
import java.util.Base64;
import java.util.Random;

import static org.assertj.core.api.Assertions.assertThat;

Expand Down Expand Up @@ -164,34 +165,64 @@ public void toUsingSameInfoFromFixture() {

}

@Test(expected = PartyInfoParserException.class)
public void checkLengthZero() {
PartyInfoParser.checkLength(0);
@Test
public void zeroAllows() {
assertThat(PartyInfoParser.checkLength(0)).isTrue();
}

@Test(expected = PartyInfoParserException.class)
public void checkLengthMinusValue() {
PartyInfoParser.checkLength(-1);
@Test
public void negativeValuesNotAllowed() {

new Random()
.ints(10)
.forEach(n -> {
int input = Integer.signum(n) == 1 ? Math.negateExact(n) : n;
boolean result = PartyInfoParser.checkLength(input);
assertThat(result).isFalse();

});
}

@Test(expected = PartyInfoParserException.class)
public void checkLengthZeroLong() {
PartyInfoParser.checkLength((long) 0);
@Test
public void positiveValuesAllowed() {

new Random()
.ints(10)
.map(Math::abs)
.forEach(n -> {

assertThat(PartyInfoParser.checkLength(n))
.isTrue();

});

}

@Test(expected = PartyInfoParserException.class)
@Test
public void checkLengthMaxValue() {
PartyInfoParser.checkLength(Integer.MAX_VALUE);
assertThat(PartyInfoParser.checkLength(Integer.MAX_VALUE)).isFalse();

}

@Test(expected = PartyInfoParserException.class)
public void checkLengthMinusValueLong() {
PartyInfoParser.checkLength((long) -1);
@Test
public void negativeLOngValuesNotAllows() {

new Random()
.ints(10)
.map(Math::negateExact)
.asLongStream()
.forEach(n -> {

long input = Long.signum(n) == 1 ? Math.negateExact(n) : n;
boolean result = PartyInfoParser.checkLength(input);
assertThat(result).isFalse();
});
}

@Test(expected = PartyInfoParserException.class)
@Test
public void checkLengthMaxValueLong() {
PartyInfoParser.checkLength(Long.MAX_VALUE);
assertThat(PartyInfoParser.checkLength(Long.MAX_VALUE)).isFalse();

}

}

0 comments on commit dfe7845

Please sign in to comment.