|
29 | 29 | import static io.qala.datagen.RandomValue.between;
|
30 | 30 | import static org.hamcrest.MatcherAssert.assertThat;
|
31 | 31 | import static org.hamcrest.Matchers.equalTo;
|
| 32 | +import static org.hamcrest.Matchers.is; |
32 | 33 |
|
33 | 34 | public class SiteParserTest {
|
34 | 35 |
|
@@ -65,6 +66,91 @@ public void setFieldShouldRequireNonBlankValue() {
|
65 | 66 | parser.setField(anyValidFieldName, nullOrBlank());
|
66 | 67 | }
|
67 | 68 |
|
| 69 | + // |
| 70 | + // Tests for isFullyInitialized() |
| 71 | + // |
| 72 | + |
| 73 | + @Test |
| 74 | + public void isFullyInitializedMayBeOnlyWhenNameIsSet() { |
| 75 | + parser.setMatchedUrl(Random.url()); |
| 76 | + parser.setCategoryLocator(Random.jsoupLocator()); |
| 77 | + parser.setCountryLocator(Random.jsoupLocator()); |
| 78 | + parser.setShortDescriptionLocator(Random.jsoupLocator()); |
| 79 | + parser.setImageUrlLocator(Random.jsoupLocator()); |
| 80 | + parser.setImageUrlAttribute(Random.tagAttributeName()); |
| 81 | + parser.setIssueDateLocator(Random.jsoupLocator()); |
| 82 | + |
| 83 | + // ensure that required field is null |
| 84 | + parser.setName(null); |
| 85 | + |
| 86 | + String msg = describe(parser) + " expected to be not fully initialized"; |
| 87 | + assertThat(msg, parser.isFullyInitialized(), is(false)); |
| 88 | + } |
| 89 | + |
| 90 | + @Test |
| 91 | + public void isFullyInitializedMayBeOnlyWhenMatchedUrlIsSet() { |
| 92 | + parser.setName(Random.name()); |
| 93 | + parser.setCategoryLocator(Random.jsoupLocator()); |
| 94 | + parser.setCountryLocator(Random.jsoupLocator()); |
| 95 | + parser.setShortDescriptionLocator(Random.jsoupLocator()); |
| 96 | + parser.setImageUrlLocator(Random.jsoupLocator()); |
| 97 | + parser.setImageUrlAttribute(Random.tagAttributeName()); |
| 98 | + parser.setIssueDateLocator(Random.jsoupLocator()); |
| 99 | + |
| 100 | + // ensure that required field is null |
| 101 | + parser.setMatchedUrl(null); |
| 102 | + |
| 103 | + String msg = describe(parser) + " expected to be not fully initialized"; |
| 104 | + assertThat(msg, parser.isFullyInitialized(), is(false)); |
| 105 | + } |
| 106 | + |
| 107 | + @Test |
| 108 | + public void isFullyInitializedMayBeOnlyWhenOneOfLocatorIsSet() { |
| 109 | + parser.setName(Random.name()); |
| 110 | + parser.setMatchedUrl(Random.url()); |
| 111 | + parser.setImageUrlAttribute(Random.tagAttributeName()); |
| 112 | + |
| 113 | + // ensure that required fields are null |
| 114 | + parser.setCategoryLocator(null); |
| 115 | + parser.setCountryLocator(null); |
| 116 | + parser.setShortDescriptionLocator(null); |
| 117 | + parser.setImageUrlLocator(null); |
| 118 | + parser.setIssueDateLocator(null); |
| 119 | + |
| 120 | + String msg = describe(parser) + " expected to be not fully initialized"; |
| 121 | + assertThat(msg, parser.isFullyInitialized(), is(false)); |
| 122 | + } |
| 123 | + |
| 124 | + @Test |
| 125 | + public void isFullyInitializedWhenAllMandatoryFieldsAreSet() { |
| 126 | + parser.setName(Random.name()); |
| 127 | + parser.setMatchedUrl(Random.url()); |
| 128 | + |
| 129 | + final int countOfFieldsWithLocator = 5; |
| 130 | + String[] locators = new String[countOfFieldsWithLocator]; |
| 131 | + |
| 132 | + for (int i = 0; i < locators.length; i++) { |
| 133 | + int guaranteedSetPosition = between(0, locators.length - 1).integer(); |
| 134 | + if (i == guaranteedSetPosition) { |
| 135 | + locators[i] = Random.jsoupLocator(); |
| 136 | + } else { |
| 137 | + locators[i] = nullOr(Random.jsoupLocator()); |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + parser.setCategoryLocator(locators[0]); |
| 142 | + parser.setCountryLocator(locators[1]); |
| 143 | + parser.setShortDescriptionLocator(locators[2]); |
| 144 | + // CheckStyle: ignore MagicNumber for next 2 lines |
| 145 | + parser.setImageUrlLocator(locators[3]); |
| 146 | + parser.setIssueDateLocator(locators[4]); |
| 147 | + |
| 148 | + parser.setImageUrlAttribute(nullOr(Random.tagAttributeName())); |
| 149 | + |
| 150 | + String msg = describe(parser) + " expected to be fully initialized"; |
| 151 | + assertThat(msg, parser.isFullyInitialized(), is(true)); |
| 152 | + } |
| 153 | + |
68 | 154 | //
|
69 | 155 | // Tests for canParse()
|
70 | 156 | //
|
@@ -112,4 +198,26 @@ public void toStringShouldReturnName() {
|
112 | 198 | assertThat(parser.toString(), equalTo(expectedName));
|
113 | 199 | }
|
114 | 200 |
|
| 201 | + private static String describe(SiteParser parser) { |
| 202 | + StringBuilder sb = new StringBuilder(); |
| 203 | + sb.append("SiteParser[name=") |
| 204 | + .append(parser.getName()) |
| 205 | + .append(", matchedUrl=") |
| 206 | + .append(parser.getMatchedUrl()) |
| 207 | + .append(", categoryLocator=") |
| 208 | + .append(parser.getCountryLocator()) |
| 209 | + .append(", countryLocator=") |
| 210 | + .append(parser.getCountryLocator()) |
| 211 | + .append(", shortDescriptionLocator=") |
| 212 | + .append(parser.getShortDescriptionLocator()) |
| 213 | + .append(", imageUrlLocator=") |
| 214 | + .append(parser.getImageUrlLocator()) |
| 215 | + .append(", imageUrlAttribute=") |
| 216 | + .append(parser.getImageUrlAttribute()) |
| 217 | + .append(", issueDateLocator=") |
| 218 | + .append(parser.getIssueDateLocator()) |
| 219 | + .append(']'); |
| 220 | + return sb.toString(); |
| 221 | + } |
| 222 | + |
115 | 223 | }
|
0 commit comments