Skip to content

Commit

Permalink
Check missing title element and throw Exception to clarify this inste…
Browse files Browse the repository at this point in the history
…ad of NPE. Part of parisjug#6.

Signed-off-by: Sun Tan <[email protected]>
  • Loading branch information
sunix committed Mar 18, 2021
1 parent 3061e44 commit 43bbe4c
Show file tree
Hide file tree
Showing 4 changed files with 699 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.parisjug.eventpublisher.eventpage;

public class EventPageCheckException extends RuntimeException {

private static final long serialVersionUID = 1L;

public EventPageCheckException(String message) {
super(message);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ public class HtmlEventPage implements EventPage {

@Override
public String getTitle() {
return doc.select("#title").first().text();
Elements titleElements = doc.select("#title");
if(titleElements.isEmpty()){
throw new EventPageCheckException("The page should contain an element with the id \"title\". For instance: <div id=\"title\">Quarkus World Tour</div>.");
}
return titleElements.first().text();
}

protected void loadFromLocalHtmlFile(File htmlFile) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import java.io.File;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import io.quarkus.test.junit.QuarkusTest;
Expand All @@ -15,11 +16,12 @@
public class EventPageTest {

@Test
public void test() {
public void test_working_page() {
File file = new File(this.getClass().getResource("parisjug-20201208.html").getFile());

EventPage page = EventPage.fromHtmlLocalFile(file);
// EventPage page = EventPage.fromUrl("https://www.parisjug.org/xwiki/wiki/oldversion/view/Meeting/20201208");
// EventPage page =
// EventPage.fromUrl("https://www.parisjug.org/xwiki/wiki/oldversion/view/Meeting/20201208");
assertNotNull(page, "should be able to load from a xwiki html file");

// title
Expand All @@ -28,7 +30,10 @@ public void test() {
// details
assertTrue(page.getDetails().contains("<strong>18h45 à 19h00 : Accueil</strong>"));
assertFalse(page.getDetails().contains("Code de Conduite"));
assertTrue(page.getDetails().contains("https://www.parisjug.org/xwiki/wiki/oldversion/view/Speaker/DoudouxJeanMichel"), "should contain link https://www.parisjug.org/xwiki/wiki/oldversion/view/Speaker/DoudouxJeanMichel");
assertTrue(
page.getDetails()
.contains("https://www.parisjug.org/xwiki/wiki/oldversion/view/Speaker/DoudouxJeanMichel"),
"should contain link https://www.parisjug.org/xwiki/wiki/oldversion/view/Speaker/DoudouxJeanMichel");

// date time
assertEquals("Mardi 8 décembre 2020 à 19h00", page.getDateTime(), "Date and time");
Expand All @@ -40,11 +45,24 @@ public void test() {
assertEquals("20201208T191500Z", page.getEndTime(), "end time");

// long title
assertEquals("Paris JUG - Soirée Virtuelle: Le Java nouveau est arrivé : Java SE 15 (2020/12/08)", page.getLongTitle(), "Long title");
assertEquals("Paris JUG - Soirée Virtuelle: Le Java nouveau est arrivé : Java SE 15 (2020/12/08)",
page.getLongTitle(), "Long title");

// location
assertEquals("https://www.twitch.tv/parisjug", page.getLocation(), "location");

}

@Test
public void should_provide_clear_error_if_missing_title() {
File file = new File(this.getClass().getResource("parisjug-20201208_missing_title.html").getFile());
EventPage page = EventPage.fromHtmlLocalFile(file);

EventPageCheckException ex = Assertions.assertThrows(EventPageCheckException.class, () -> {
page.getTitle();
});
assertEquals(
"The page should contain an element with the id \"title\". For instance: <div id=\"title\">Quarkus World Tour</div>.",
ex.getMessage());

}
}
Loading

0 comments on commit 43bbe4c

Please sign in to comment.