Skip to content

Commit ac7140e

Browse files
Add assertSectionEquals method
1 parent 51907a4 commit ac7140e

File tree

1 file changed

+43
-6
lines changed

1 file changed

+43
-6
lines changed

src/test/java/org/javawebstack/orm/test/shared/verification/QueryVerification.java

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,7 @@ public void assertSectionContains(String topLevelKeyword, String containedSubstr
5050
try {
5151
sectionString = getSection(topLevelKeyword, sectionIndex);
5252
} catch (SectionIndexOutOfBoundException e) {
53-
fail(String.format(
54-
"A top level section of type %s and index %d was tested but only %d sections of that type existed.",
55-
e.getTopLevelKeyword(),
56-
e.getAttemptedIndex(),
57-
e.getSectionCount()
58-
));
53+
this.failDueToSectionIndexOutOfBounds(e);
5954
}
6055

6156
assertTrue(
@@ -74,6 +69,39 @@ public void assertOrderByContains(String containedSubstring) {
7469
this.assertSectionContains("ORDER BY", containedSubstring);
7570
}
7671

72+
/**
73+
* Asserts that the first occurring section of a given top level keyword is equal to the given string.
74+
* This method uses the String.equals method internally and is therefore case sensitive.
75+
*
76+
* @param topLevelKeyword The top level keyword that prefaces the section.
77+
* @param expectedString The substring which the first section of the given type should be equal to.
78+
*/
79+
public void assertSectionEquals(String topLevelKeyword, String expectedString) {
80+
this.assertSectionEquals(topLevelKeyword, expectedString, 0);
81+
}
82+
83+
/**
84+
* Asserts that the i-th occurring section of a given top level keyword equal to the given string.
85+
* This method uses the String.equals method internally and is therefore case sensitive.
86+
*
87+
* @param topLevelKeyword The top level keyword that prefaces the section.
88+
* @param expectedString The substring which the specified section of the given type should be equal to.
89+
* @param sectionIndex The index of the section to be checked, thus 0 refers to the first occurrence etc.
90+
*/
91+
public void assertSectionEquals(String topLevelKeyword, String expectedString, int sectionIndex) {
92+
String sectionString = null;
93+
try {
94+
sectionString = getSection(topLevelKeyword, sectionIndex);
95+
} catch (SectionIndexOutOfBoundException e) {
96+
this.failDueToSectionIndexOutOfBounds(e);
97+
}
98+
99+
assertTrue(
100+
sectionString.equals(expectedString),
101+
String.format("The occurrence of index %d of %s section of the query was not equal to the string %s but looked like this: %s. Note that the match is case-sensitive.", sectionIndex, topLevelKeyword, expectedString, sectionString)
102+
);
103+
}
104+
77105
/**
78106
* Retrieves the inner part of a section by its keyword. With multiple occurrences it will only retrieve the first
79107
* one. It does not include the keyword and one whitespaces at start and end.
@@ -123,4 +151,13 @@ public List<String> getSectionList(String topLevelKeyword) {
123151
.getTopLevelSectionsByKeyword(topLevelKeyword);
124152
}
125153

154+
private void failDueToSectionIndexOutOfBounds(SectionIndexOutOfBoundException exception) {
155+
fail(String.format(
156+
"A top level section of type %s and index %d was tested but only %d sections of that type existed.",
157+
exception.getTopLevelKeyword(),
158+
exception.getAttemptedIndex(),
159+
exception.getSectionCount()
160+
));
161+
}
162+
126163
}

0 commit comments

Comments
 (0)