Skip to content

Commit 5aaa95f

Browse files
committed
Add some fail tests for validators
1 parent d89bfd8 commit 5aaa95f

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

src/main/java/at/favre/lib/bytes/BytesValidator.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,12 +147,12 @@ enum Operator {
147147
private final Operator operator;
148148

149149
public Logical(List<BytesValidator> validatorList, Operator operator) {
150-
if (validatorList.isEmpty()) throw new IllegalArgumentException("must contain at least 1 element");
150+
if (validatorList.isEmpty())
151+
throw new IllegalArgumentException("must contain at least 1 element");
151152
if (operator == NOT && validatorList.size() != 1)
152153
throw new IllegalArgumentException("not operator can only be applied to single element");
153154
this.validatorList = validatorList;
154155
this.operator = operator;
155-
156156
}
157157

158158
@Override

src/test/java/at/favre/lib/bytes/BytesValidatorTest.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@
2323

2424
import org.junit.Test;
2525

26+
import java.util.Arrays;
27+
import java.util.Collections;
28+
2629
import static at.favre.lib.bytes.BytesValidators.*;
2730
import static org.junit.Assert.*;
2831

@@ -85,6 +88,18 @@ public void testNotValidation() throws Exception {
8588
assertFalse(Bytes.allocate(2).validate(not(atMost(16))));
8689
}
8790

91+
@Test(expected = IllegalArgumentException.class)
92+
public void testLogicalNoElements() throws Exception {
93+
Bytes.allocate(2).validate(new BytesValidator.Logical(Collections.<BytesValidator>emptyList(), BytesValidator.Logical.Operator.AND));
94+
}
95+
96+
@Test(expected = IllegalArgumentException.class)
97+
public void testLogicalTooManyElements() throws Exception {
98+
Bytes.allocate(2).validate(new BytesValidator.Logical(
99+
Arrays.<BytesValidator>asList(new BytesValidator.Length(2, BytesValidator.Length.Mode.GREATER_OR_EQ_THAN), new BytesValidator.Length(2, BytesValidator.Length.Mode.EXACT))
100+
, BytesValidator.Logical.Operator.NOT));
101+
}
102+
88103
@Test
89104
public void testNestedValidation() throws Exception {
90105
assertTrue(Bytes.allocate(16).validate(

0 commit comments

Comments
 (0)