Skip to content

Commit 1ae8ac8

Browse files
Add retrieval methods for the sections
1 parent 8aaedb2 commit 1ae8ac8

File tree

1 file changed

+52
-12
lines changed

1 file changed

+52
-12
lines changed

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

Lines changed: 52 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package org.javawebstack.orm.test.shared.verification;
22

33
import org.javawebstack.orm.query.Query;
4+
import org.javawebstack.orm.test.exception.SectionIndexOutOfBoundException;
45
import org.javawebstack.orm.test.shared.util.QueryStringUtil;
56

67
import java.util.HashSet;
8+
import java.util.List;
79

810
import static org.junit.jupiter.api.Assertions.assertTrue;
911
import static org.junit.jupiter.api.Assertions.fail;
@@ -51,24 +53,19 @@ public void assertOrderByContains(String containedSubstring) {
5153
*
5254
* @param topLevelKeyword The top level keyword that prefaces the section.
5355
* @param containedSubstring The substring which should be contained in the first section of the given type.
54-
* @param sectionIndex The index of the section to be checked, thusly 0 refers to the first occurrence etc.
56+
* @param sectionIndex The index of the section to be checked, thus 0 refers to the first occurrence etc.
5557
*/
5658
public void assertSectionContains(String topLevelKeyword, String containedSubstring, int sectionIndex) {
57-
String sectionString;
58-
59+
String sectionString = null;
5960
try {
60-
sectionString = new QueryStringUtil(this.query.getQueryString().getQuery())
61-
.getTopLevelSectionsByKeyword(topLevelKeyword)
62-
.get(sectionIndex);
63-
} catch (IndexOutOfBoundsException ignored) {
61+
sectionString = getSection(topLevelKeyword, sectionIndex);
62+
} catch (SectionIndexOutOfBoundException e) {
6463
fail(String.format(
6564
"A top level section of type %s and index %d was tested but only %d sections of that type existed.",
66-
sectionIndex,
67-
topLevelKeyword,
68-
sectionIndex + 1
65+
e.getTopLevelKeyword(),
66+
e.getAttemptedIndex(),
67+
e.getSectionCount()
6968
));
70-
71-
return;
7269
}
7370

7471
assertTrue(
@@ -77,4 +74,47 @@ public void assertSectionContains(String topLevelKeyword, String containedSubstr
7774
);
7875
}
7976

77+
/**
78+
* Retrieves the inner part of a section by its keyword. With multiple occurrences it will only retrieve the first
79+
* one. It does not include the keyword and one whitespaces at start and end.
80+
*
81+
* @param topLevelKeyword The top level keyword that prefaces the section.
82+
* @return The inner part of the first section as specified.
83+
* @throws SectionIndexOutOfBoundException if no section by that top level keyword exists.
84+
*/
85+
public String getSection(String topLevelKeyword) throws SectionIndexOutOfBoundException {
86+
return this.getSection(topLevelKeyword, 0);
87+
}
88+
89+
/**
90+
* Retrieves the inner part of a section by its keyword and index specifying which occurrence should be retrieved.
91+
* It does not include the keyword and one whitespaces at start and end.
92+
*
93+
* @param topLevelKeyword The top level keyword that prefaces the section.
94+
* @param sectionIndex The index of the section to be retrieved, thus 0 refers to the first occurrence etc.
95+
* @return The inner part of the first section as specified.
96+
* @throws SectionIndexOutOfBoundException if there are less than sectionIndex + 1 elements
97+
*/
98+
public String getSection(String topLevelKeyword, int sectionIndex) throws SectionIndexOutOfBoundException {
99+
List<String> sectionList = this.getSectionList(topLevelKeyword);
100+
try {
101+
return sectionList.get(sectionIndex);
102+
} catch (IndexOutOfBoundsException converted) {
103+
104+
SectionIndexOutOfBoundException exception = new SectionIndexOutOfBoundException();
105+
106+
exception.setSectionCount(sectionList.size());
107+
exception.setAttemptedIndex(sectionIndex);
108+
exception.setTopLevelKeyword(topLevelKeyword);
109+
110+
throw exception;
111+
}
112+
}
113+
114+
115+
public List<String> getSectionList(String topLevelKeyword) {
116+
return new QueryStringUtil(this.query.getQueryString().getQuery())
117+
.getTopLevelSectionsByKeyword(topLevelKeyword);
118+
}
119+
80120
}

0 commit comments

Comments
 (0)