View Javadoc
1   /*
2    * SPDX-FileCopyrightText: Copyright (c) 2012-2026 Yegor Bugayenko
3    * SPDX-License-Identifier: MIT
4    */
5   package com.jcabi.xml;
6   
7   import java.io.ByteArrayInputStream;
8   import java.io.File;
9   import java.io.InputStream;
10  import java.nio.charset.StandardCharsets;
11  import java.nio.file.Files;
12  import org.cactoos.io.TeeInput;
13  import org.cactoos.scalar.LengthOf;
14  import org.hamcrest.MatcherAssert;
15  import org.hamcrest.Matchers;
16  import org.junit.jupiter.api.Test;
17  
18  /**
19   * Test case for {@link TextResource}.
20   *
21   * @since 0.1
22   */
23  @SuppressWarnings("PMD.UnnecessaryLocalRule")
24  final class TextResourceTest {
25  
26      @Test
27      void readsStreamAsText() {
28          final String text = "Blah!\u20ac\u2122";
29          final InputStream stream = new ByteArrayInputStream(
30              text.getBytes(StandardCharsets.UTF_8)
31          );
32          MatcherAssert.assertThat(
33              "Text does not match, but it should",
34              new TextResource(stream).toString(),
35              Matchers.is(text)
36          );
37      }
38  
39      @Test
40      void readsFileAsText() throws Exception {
41          final String text = "<a xmlns='urn:foo'><b>\u0433!</b></a>";
42          final File file = Files.createTempDirectory("")
43              .resolve("dummy.xml").toFile();
44          new LengthOf(new TeeInput(text, file)).value();
45          MatcherAssert.assertThat(
46              "Text does not match, but it should",
47              new TextResource(file).toString(),
48              Matchers.is(text)
49          );
50      }
51  
52  }