### Question:
AlternativeTextForLinkedImageRule extends AbstractRule { @Override protected void applyTo(Page page, Violations violations) { List<HtmlImage> images = page.findAllImageTags(); for (HtmlImage image : images) { if (isLinkedImage(image) && !hasNonEmptyAttribute(image, "alt")) { violations.add(createViolation(image, page, RULE_MESSAGE)); } } }  @Override String getName();  }### Answer:
@Test public void linkedImageWithoutAlternativeTextPresent() throws Exception { String content = "<html><body><a href=\"page.html\"><img src=\"contact.gif\" /></a></body></html>"; Violations violations = rule.applyTo(toHtmlPage(content)); assertViolations(violations, rule, 1); }
@Test public void linkedImageWithEmptyAlternativeTextPresent() throws Exception { String content = "<html><body><a href=\"page.html\"><img src=\"contact.gif\" alt=\"\" /></a></body></html>"; Violations violations = rule.applyTo(toHtmlPage(content)); assertViolations(violations, rule, 1); }
@Test public void linkedImageWithAlternativeTextPresent() throws Exception { String content = "<html><body><a href=\"page.html\"><img src=\"contact.gif\" alt=\"Contact\" /></a></body></html>"; Violations violations = rule.applyTo(toHtmlPage(content)); assertNoViolations(violations, rule); }
@Test public void nonLinkedImageWithMissingAlternativeText() throws Exception { String content = "<html><body><img src=\"contact.gif\" alt=\"\" /></body></html>"; Violations violations = rule.applyTo(toHtmlPage(content)); assertNoViolations(violations, rule); }