Skip to content

Commit 68ebb9c

Browse files
committed
SiteParser.extractCountry(): add unit tests.
Addressed to #685 No functional changes.
1 parent 35cd3ef commit 68ebb9c

File tree

2 files changed

+55
-1
lines changed

2 files changed

+55
-1
lines changed

src/main/java/ru/mystamps/web/util/extractor/SiteParser.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ protected String extractCategory(Element body) {
167167
return category;
168168
}
169169

170-
private String extractCountry(Element body) {
170+
protected String extractCountry(Element body) {
171171
String locator = ObjectUtils.firstNonNull(countryLocator, shortDescriptionLocator);
172172
if (locator == null) {
173173
return null;

src/test/java/ru/mystamps/web/util/extractor/SiteParserTest.java

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,60 @@ public void extractCategoryShouldReturnTextOfShortDescriptionLocator() {
345345
assertThat(msg, category, equalTo(expectedName));
346346
}
347347

348+
//
349+
// Tests for extractCountry()
350+
//
351+
352+
@Test
353+
public void extractCountryShouldReturnNullWhenLocatorsAreNotSet() {
354+
parser.setCountryLocator(null);
355+
parser.setShortDescriptionLocator(null);
356+
Element doc = createEmptyDocument();
357+
358+
String country = parser.extractCountry(doc);
359+
360+
assertThat(country, is(nullValue()));
361+
}
362+
363+
@Test
364+
public void extractCountryShouldReturnNullWhenElementNotFound() {
365+
parser.setCountryLocator(Random.jsoupLocator());
366+
Element doc = createEmptyDocument();
367+
368+
String country = parser.extractCountry(doc);
369+
370+
assertThat(country, is(nullValue()));
371+
}
372+
373+
@Test
374+
public void extractCountryShouldReturnTextOfCountryLocator() {
375+
parser.setCountryLocator("#country");
376+
377+
String expectedName = Random.countryName();
378+
String html = String.format("<div id='country'>%s</div>", expectedName);
379+
Element doc = createDocumentFromText(html);
380+
381+
String country = parser.extractCountry(doc);
382+
383+
String msg = String.format("couldn't extract a country from '%s'", doc);
384+
assertThat(msg, country, equalTo(expectedName));
385+
}
386+
387+
@Test
388+
public void extractCountryShouldReturnTextOfShortDescriptionLocator() {
389+
parser.setCountryLocator(null);
390+
parser.setShortDescriptionLocator("#desc");
391+
392+
String expectedName = Random.countryName();
393+
String html = String.format("<div id='desc'>%s</div>", expectedName);
394+
Element doc = createDocumentFromText(html);
395+
396+
String country = parser.extractCountry(doc);
397+
398+
String msg = String.format("couldn't extract a country from '%s'", doc);
399+
assertThat(msg, country, equalTo(expectedName));
400+
}
401+
348402
private static String describe(SiteParser parser) {
349403
StringBuilder sb = new StringBuilder();
350404
sb.append("SiteParser[name=")

0 commit comments

Comments
 (0)