io.github.carlomicieli.footballdb.starter.pages.TableBuilderTests.java Source code

Java tutorial

Introduction

Here is the source code for io.github.carlomicieli.footballdb.starter.pages.TableBuilderTests.java

Source

/*
 * 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 io.github.carlomicieli.footballdb.starter.test.JsoupHelper;
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.Optional;

import static io.github.carlomicieli.footballdb.starter.test.MyProjectAssertions.assertThat;

/**
 * @author Carlo Micieli
 */
public class TableBuilderTests {
    @Test
    public void shouldReturnEmptyWhenTheProvidedElementIsNotATable() {
        Optional<Table> table = TableBuilder.fromElement(notTableElement());
        assertThat(table).isNotPresent();
    }

    @Test
    public void shouldBuildNewTablesFromElements() {
        Optional<Table> table = TableBuilder.fromElement(tableElement());

        Table tab = table.orElseThrow(RuntimeException::new);

        assertThat(tab.size()).isEqualTo(ImmutablePair.of(3, 3));
        assertThat(tab.rowValues(1)).contains(Arrays.asList("1", "one", "3"));
        assertThat(tab.rowValues(2)).contains(Arrays.asList("", "", ""));
        assertThat(tab.rowValues(3)).contains(Arrays.asList("2", "two", "1"));
    }

    private static Element notTableElement() {
        String html = "<div id=\"id\">ooo</div>";
        Document doc = JsoupHelper.fromHtml(html);
        return doc.getElementById("id");
    }

    private static Element tableElement() {
        String html = "<table id=\"my-table\">" + "<tr><td>1</td><td>one</td><td>3</td></tr>"
                + "<tr><td colspan=\"3\"></td></tr>" + "<tr><td>2</td><td>two</td><td>1</td></tr>" + "</table>";
        Document doc = JsoupHelper.fromHtml(html);
        return doc.getElementById("my-table");
    }
}