Skip to content

Commit fb63038

Browse files
committed
SiteParserTest: add unit tests.
Addressed to #685
1 parent 0c9bb14 commit fb63038

File tree

2 files changed

+103
-0
lines changed

2 files changed

+103
-0
lines changed

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -946,6 +946,7 @@
946946
<includes>
947947
<include>**/mystamps/web/service/*</include>
948948
<include>**/mystamps/web/util/*</include>
949+
<include>**/mystamps/web/util/extractor/SiteParser*</include>
949950
<include>**/mystamps/web/support/spring/security/ContentSecurityPolicyHeaderWriter*</include>
950951
</includes>
951952
</configuration>
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
package ru.mystamps.web.util.extractor;
2+
3+
import org.junit.Before;
4+
import org.junit.Rule;
5+
import org.junit.Test;
6+
import org.junit.rules.ExpectedException;
7+
8+
import ru.mystamps.web.tests.Random;
9+
10+
import static io.qala.datagen.RandomShortApi.nullOr;
11+
import static io.qala.datagen.RandomShortApi.nullOrBlank;
12+
import static io.qala.datagen.RandomValue.between;
13+
import static org.hamcrest.MatcherAssert.assertThat;
14+
import static org.hamcrest.Matchers.equalTo;
15+
16+
public class SiteParserTest {
17+
18+
@Rule
19+
public ExpectedException thrown = ExpectedException.none();
20+
21+
private SiteParser parser;
22+
23+
@Before
24+
public void init() {
25+
parser = new SiteParser();
26+
}
27+
28+
//
29+
// Tests for setField()
30+
//
31+
32+
@Test
33+
public void setFieldShouldRequireNonBlankName() {
34+
thrown.expect(IllegalStateException.class);
35+
thrown.expectMessage("Field name must be non-blank");
36+
37+
// @todo #685 SiteParserTest: introduce a method for generating random valid locator
38+
String anyValidLocator = "#id";
39+
40+
parser.setField(nullOrBlank(), anyValidLocator);
41+
}
42+
43+
@Test
44+
public void setFieldShouldRequireNonBlankValue() {
45+
thrown.expect(IllegalStateException.class);
46+
thrown.expectMessage("Field value must be non-blank");
47+
48+
// @todo #685 SiteParserTest: introduce a method for generating random valid field name
49+
String anyValidFieldName = "name";
50+
51+
parser.setField(anyValidFieldName, nullOrBlank());
52+
}
53+
54+
//
55+
// Tests for canParse()
56+
//
57+
58+
@Test
59+
public void canParseShouldRequireNonNullUrl() {
60+
thrown.expect(IllegalStateException.class);
61+
thrown.expectMessage("Site URL must be non-null");
62+
63+
parser.canParse(null);
64+
}
65+
66+
@Test
67+
public void canParseShouldRequireNonNullMatchedUrl() {
68+
parser.setMatchedUrl(null);
69+
70+
thrown.expect(IllegalStateException.class);
71+
thrown.expectMessage("Matched URL must be set");
72+
73+
parser.canParse(Random.url());
74+
}
75+
76+
//
77+
// Tests for parse()
78+
//
79+
80+
@Test
81+
public void parseShouldRequireNonBlankPageContent() {
82+
thrown.expect(IllegalArgumentException.class);
83+
thrown.expectMessage("Page content must be non-blank");
84+
85+
parser.parse(nullOrBlank());
86+
}
87+
88+
//
89+
// Tests for toString()
90+
//
91+
92+
@SuppressWarnings("checkstyle:magicnumber")
93+
@Test
94+
public void toStringShouldReturnName() {
95+
// @todo #685 SiteParserTest: introduce a method for generating random string of arbitrary length
96+
String expectedName = nullOr(between(1, 15).unicode());
97+
parser.setName(expectedName);
98+
99+
assertThat(parser.toString(), equalTo(expectedName));
100+
}
101+
102+
}

0 commit comments

Comments
 (0)