Skip to content

Commit

Permalink
Tests for double negation
Browse files Browse the repository at this point in the history
  • Loading branch information
njr-11 committed Dec 6, 2024
1 parent 5240da6 commit f5d3bb9
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
35 changes: 35 additions & 0 deletions api/src/test/java/jakarta/data/BasicRestrictionRecordTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,41 @@ void shouldNegateLTERestriction() {
});
}

@Test
void shouldNegateNegatedRestriction() {
Restriction<Book> titleRestriction =
Restrict.equalTo("A Developer's Guide to Jakarta Data", "title");
BasicRestriction<Book> titleRestrictionBasic =
(BasicRestriction<Book>) titleRestriction;
BasicRestriction<Book> negatedTitleRestrictionBasic =
(BasicRestriction<Book>) titleRestriction.negate();
BasicRestriction<Book> negatedNegatedTitleRestrictionBasic =
(BasicRestriction<Book>) negatedTitleRestrictionBasic.negate();

SoftAssertions.assertSoftly(soft -> {
soft.assertThat(titleRestrictionBasic.comparison())
.isEqualTo(Operator.EQUAL);
soft.assertThat(titleRestrictionBasic.value())
.isEqualTo("A Developer's Guide to Jakarta Data");
soft.assertThat(titleRestrictionBasic.isNegated())
.isEqualTo(false);

soft.assertThat(negatedTitleRestrictionBasic.comparison())
.isEqualTo(Operator.EQUAL);
soft.assertThat(negatedTitleRestrictionBasic.value())
.isEqualTo("A Developer's Guide to Jakarta Data");
soft.assertThat(negatedTitleRestrictionBasic.isNegated())
.isEqualTo(true);

soft.assertThat(negatedNegatedTitleRestrictionBasic.comparison())
.isEqualTo(Operator.EQUAL);
soft.assertThat(negatedNegatedTitleRestrictionBasic.value())
.isEqualTo("A Developer's Guide to Jakarta Data");
soft.assertThat(negatedNegatedTitleRestrictionBasic.isNegated())
.isEqualTo(false);
});
}

@Test
void shouldSupportNegatedRestrictionUsingDefaultConstructor() {
BasicRestrictionRecord<String> restriction = new BasicRestrictionRecord<>("author", Operator.EQUAL, "Unknown");
Expand Down
29 changes: 29 additions & 0 deletions api/src/test/java/jakarta/data/CompositeRestrictionRecordTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,35 @@ void shouldNegateCompositeRestriction() {
});
}

@Test
void shouldNegateNegatedCompositeRestriction() {
Restriction<Person> ageBetween20and30 = Restrict.between(20, 30, "age");
Restriction<Person> nameContainsDuke = Restrict.contains("Duke", "name");
Restriction<Person> any = Restrict.any(ageBetween20and30, nameContainsDuke);
Restriction<Person> anyNegated = any.negate();
Restriction<Person> anyNotNegated = Restrict.not(anyNegated);

SoftAssertions.assertSoftly(soft -> {
soft.assertThat(any.isNegated()).isEqualTo(false);
soft.assertThat(((CompositeRestriction<Person>) any).restrictions()
.get(0).isNegated()).isEqualTo(false);
soft.assertThat(((CompositeRestriction<Person>) any).restrictions()
.get(1).isNegated()).isEqualTo(false);

soft.assertThat(anyNegated.isNegated()).isEqualTo(true);
soft.assertThat(((CompositeRestriction<Person>) anyNegated).restrictions()
.get(0).isNegated()).isEqualTo(false);
soft.assertThat(((CompositeRestriction<Person>) anyNegated).restrictions()
.get(1).isNegated()).isEqualTo(false);

soft.assertThat(anyNotNegated.isNegated()).isEqualTo(false);
soft.assertThat(((CompositeRestriction<Person>) anyNotNegated).restrictions()
.get(0).isNegated()).isEqualTo(false);
soft.assertThat(((CompositeRestriction<Person>) anyNotNegated).restrictions()
.get(1).isNegated()).isEqualTo(false);
});
}

@Test
void shouldPreserveRestrictionsOrder() {
Restriction<String> restriction1 = new BasicRestrictionRecord<>("title", Operator.EQUAL, "Java Guide");
Expand Down

0 comments on commit f5d3bb9

Please sign in to comment.