001/*
002 * acme4j - Java ACME client
003 *
004 * Copyright (C) 2018 Richard "Shred" Körber
005 *   http://acme4j.shredzone.org
006 *
007 * Licensed under the Apache License, Version 2.0 (the "License");
008 * you may not use this file except in compliance with the License.
009 *
010 * This program is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
013 */
014package org.shredzone.acme4j.connector;
015
016import static org.assertj.core.api.Assertions.assertThat;
017
018import java.io.ByteArrayInputStream;
019import java.io.IOException;
020import java.nio.charset.StandardCharsets;
021
022import org.junit.jupiter.api.Test;
023
024/**
025 * Unit tests for {@link TrimmingInputStream}.
026 */
027public class TrimmingInputStreamTest {
028    private final static String FULL_TEXT =
029              "Gallia est omnis divisa in partes tres,\r\n\r\n\r\n"
030            + "quarum unam incolunt Belgae, aliam Aquitani,\r\r\r\n\n"
031            + "tertiam, qui ipsorum lingua Celtae, nostra Galli appellantur.";
032
033    private final static String TRIMMED_TEXT =
034              "Gallia est omnis divisa in partes tres,\n"
035            + "quarum unam incolunt Belgae, aliam Aquitani,\n"
036            + "tertiam, qui ipsorum lingua Celtae, nostra Galli appellantur.";
037
038    @Test
039    public void testEmpty() throws IOException {
040        var out = trimByStream("");
041        assertThat(out).isEqualTo("");
042    }
043
044    @Test
045    public void testLineBreakOnly() throws IOException {
046        var out1 = trimByStream("\n");
047        assertThat(out1).isEqualTo("");
048
049        var out2 = trimByStream("\r");
050        assertThat(out2).isEqualTo("");
051
052        var out3 = trimByStream("\r\n");
053        assertThat(out3).isEqualTo("");
054    }
055
056    @Test
057    public void testTrim() throws IOException {
058        var out = trimByStream(FULL_TEXT);
059        assertThat(out).isEqualTo(TRIMMED_TEXT);
060    }
061
062    @Test
063    public void testTrimEndOnly() throws IOException {
064        var out = trimByStream(FULL_TEXT + "\r\n\r\n");
065        assertThat(out).isEqualTo(TRIMMED_TEXT + "\n");
066    }
067
068    @Test
069    public void testTrimStartOnly() throws IOException {
070        var out = trimByStream("\n\n" + FULL_TEXT);
071        assertThat(out).isEqualTo(TRIMMED_TEXT);
072    }
073
074    @Test
075    public void testTrimFull() throws IOException {
076        var out = trimByStream("\n\n" + FULL_TEXT + "\r\n\r\n");
077        assertThat(out).isEqualTo(TRIMMED_TEXT + "\n");
078    }
079
080    @Test
081    public void testAvailable() throws IOException {
082        try (var in = new TrimmingInputStream(
083                new ByteArrayInputStream("Test".getBytes(StandardCharsets.US_ASCII)))) {
084            assertThat(in.available()).isNotEqualTo(0);
085        }
086    }
087
088    /**
089     * Trims a string by running it through the {@link TrimmingInputStream}.
090     */
091    private String trimByStream(String str) throws IOException {
092        var out = new StringBuilder();
093
094        try (var in = new TrimmingInputStream(
095                        new ByteArrayInputStream(str.getBytes(StandardCharsets.US_ASCII)))) {
096            int ch;
097            while ((ch = in.read()) >= 0) {
098                out.append((char) ch);
099            }
100        }
101
102        return out.toString();
103    }
104
105}