Java tutorial
/* * Copyright 2014 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package io.github.carlomicieli.footballdb.starter.pages; import static io.github.carlomicieli.footballdb.starter.pages.Page.*; import static io.github.carlomicieli.footballdb.starter.test.JsoupHelper.emptyDocument; import static io.github.carlomicieli.footballdb.starter.test.JsoupHelper.fromHtml; import static io.github.carlomicieli.footballdb.starter.test.MyProjectAssertions.assertThat; import org.apache.commons.lang3.tuple.ImmutablePair; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; import org.junit.Test; import java.util.Arrays; import java.util.NoSuchElementException; import java.util.Optional; import java.util.stream.Stream; public class PageTests { @Test public void shouldReturnAStreamForElementsWithTheSameClass() { String html = "<div class=\"one\">\n" + " <p>one</p>\n" + "</div>\n" + "<div class=\"one\">\n" + " <p>two</p>\n" + "</div>\n" + "<div class=\"one\">\n" + " <p>tree</p>\n" + "</div>"; Page page = page(fromHtml(html)); Stream<Element> streamOne = page.elementsWithClass("one"); Stream<Element> streamTwo = page.elementsWithClass("two"); assertThat(streamOne).isNotNull(); assertThat(streamOne.count()).isEqualTo(3); assertThat(streamTwo.count()).isEqualTo(0); } @Test public void shouldGetValuesContainedInThePage() { Page page = page(emptyDocument()); Optional<String> foundValue = page.getValueByName("found"); Optional<String> notFoundValue = page.getValueByName("not-found"); assertThat(foundValue).isPresent().contains("value"); assertThat(notFoundValue).isNotPresent(); } @Test public void shouldReturnStreamForChildrenElementsWithGivenTag() { String html = "<table id=\"my-table\">" + "<tr><td>1</td><td>one</td></tr>" + "<tr><td>2</td><td>two</td></tr>" + "<tr><td>3</td><td>three</td></tr>" + "</table>"; Page page = page(fromHtml(html)); Element table = page.elementWithId("my-table").orElseThrow(NoSuchElementException::new); Stream<Element> stream = Page.childrenWithTag(table, "tr"); Stream<Element> columns = stream.limit(1).flatMap(r -> Page.childrenWithTag(r, "td")); assertThat(columns.count()).isEqualTo(2); } @Test public void shouldReturnEmptyIfTheTableWithTheGivenIdIsNotFound() { Page page = page(emptyDocument()); Optional<Table> notFound = page.getTableById("not-found"); assertThat(notFound).isNotPresent(); } @Test public void shouldGetValuesFromTablesWithoutHeaderOrBody() { String html = "<table id=\"my-table\">" + "<tr><td>1</td><td>one</td></tr>" + "<tr><td>2</td><td>two</td></tr>" + "<tr><td>3</td><td>three</td></tr>" + "</table>"; Page page = page(fromHtml(html)); Table table = page.getTableById("my-table").orElse(null); assertThat(table).isNotNull(); assertThat(table.size()).isEqualTo(ImmutablePair.of(3, 2)); } @Test public void shouldGetValuesFromTablesWithHeaderAndWithoutBody() { String html = "<table id=\"my-table\">" + "<thead><tr><td>col1</td><td>col2</td></tr></thead>" + "<tr><td>1</td><td>one</td></tr>" + "<tr><td>2</td><td>two</td></tr>" + "<tr><td>3</td><td>three</td></tr>" + "</table>"; Page page = page(fromHtml(html)); Table table = page.getTableById("my-table").orElse(null); assertThat(table).isNotNull(); assertThat(table.header()).contains("col1", "col2"); assertThat(table.size()).isEqualTo(ImmutablePair.of(3, 2)); } @Test public void shouldGetValuesFromTablesWithMoreHeaders() { String html = "<table id=\"my-table\">" + "<thead>" + "<tr><td>cat1</td><td colspan=\"2\">cat2</td></tr>" + "<tr><td>col1</td><td>col2</td><td>col3</td></tr>" + "</thead>" + "<tr><td>1</td><td>one</td><td>1</td></tr>" + "<tr><td>2</td><td>two</td><td>1</td></tr>" + "</table>"; Page page = page(fromHtml(html)); Table table = page.getTableById("my-table").orElse(null); assertThat(table).isNotNull(); assertThat(table.header()).contains("cat1.col1", "cat2.col2", "cat2.col3"); assertThat(table.size()).isEqualTo(ImmutablePair.of(2, 3)); } @Test public void shouldGetValuesFromTablesWithEmptyRows() { String html = "<table id=\"my-table\">" + "<tr><td>1</td><td>one</td><td>1</td></tr>" + "<tr><td colspan=\"3\"></td></tr>" + "<tr><td>2</td><td>two</td><td>1</td></tr>" + "</table>"; Page page = page(fromHtml(html)); Table table = page.getTableById("my-table").orElse(null); assertThat(table).isNotNull(); assertThat(table.size()).isEqualTo(ImmutablePair.of(3, 3)); assertThat(table.rowValues(2)).contains(Arrays.asList("", "", "")); } @Test public void shouldFindElementByPredicateByTagName() { String html = "" + "<div summary=\"one\">one</div>" + "<div summary=\"two\">two</div>" + "<div summary=\"three\"><div>three</div></div>"; Page page = page(fromHtml(html)); Stream<Element> divs = page.selectElements(whereTagNameIs("div")); assertThat(divs.count()).isEqualTo(4); } @Test public void shouldFindElementByPredicateForAttributes() { String html = "" + "<div summary=\"one\">one</div>" + "<div summary=\"two\">two</div>" + "<div summary=\"three\">three</div>"; Page page = page(fromHtml(html)); Stream<Element> divsExact = page.selectElements(containsAttr("summary", "two")); assertThat(divsExact.count()).isEqualTo(1); Stream<Element> divsStartsWith = page.selectElements(containsAttrLike("summary", "t%")); assertThat(divsStartsWith.count()).isEqualTo(2); } @Test public void shouldFindElementByPredicateForClassNames() { String html = "" + "<div class=\"one\">one</div>" + "<div class=\"two\">two</div>" + "<div class=\"three\">three</div>"; Page page = page(fromHtml(html)); Stream<Element> divsExact = page.selectElements(classNameIs("one")); assertThat(divsExact.count()).isEqualTo(1); } private static Page page(final Document doc) { return new Page(doc) { @Override public Optional<String> getValueByName(String name) { return name.equals("found") ? Optional.ofNullable("value") : Optional.empty(); } }; } }