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 org.hamcrest.MatcherAssert;
8   import org.hamcrest.Matchers;
9   import org.junit.jupiter.api.Assertions;
10  import org.junit.jupiter.api.Test;
11  import org.w3c.dom.ls.LSInput;
12  
13  /**
14   * Test case for {@link ClasspathInput}.
15   * @since 0.17.3
16   */
17  @SuppressWarnings("PMD.UnnecessaryLocalRule")
18  final class ClasspathInputTest {
19      /**
20       * Path of an XML resource available on the classpath in tests.
21       */
22      private static final String RESOURCE = "com/jcabi/xml/simple.xml";
23  
24      @Test
25      void readsStringFromResourceSuccessfully() {
26          final LSInput input = new ClasspathInput(
27              "Id", ClasspathInputTest.RESOURCE
28          );
29          MatcherAssert.assertThat(
30              "Input XML does not contains expected string, but it should",
31              input.getStringData(),
32              Matchers.containsString("<root>")
33          );
34      }
35  
36      @Test
37      void roundTripsBaseUri() {
38          final LSInput input = new ClasspathInput(
39              "Id", ClasspathInputTest.RESOURCE
40          );
41          input.setBaseURI("http://example.com/base");
42          MatcherAssert.assertThat(
43              "BaseURI set on the input should be returned by getBaseURI()",
44              input.getBaseURI(),
45              Matchers.equalTo("http://example.com/base")
46          );
47      }
48  
49      @Test
50      void roundTripsEncoding() {
51          final LSInput input = new ClasspathInput(
52              "Id", ClasspathInputTest.RESOURCE
53          );
54          input.setEncoding("ISO-8859-1");
55          MatcherAssert.assertThat(
56              "Encoding set on the input should be returned by getEncoding()",
57              input.getEncoding(),
58              Matchers.equalTo("ISO-8859-1")
59          );
60      }
61  
62      @Test
63      void roundTripsCertifiedText() {
64          final LSInput input = new ClasspathInput(
65              "Id", ClasspathInputTest.RESOURCE
66          );
67          input.setCertifiedText(true);
68          MatcherAssert.assertThat(
69              "CertifiedText set to true should be returned by getCertifiedText()",
70              input.getCertifiedText(),
71              Matchers.is(true)
72          );
73      }
74  
75      @Test
76      void rejectsByteStreamMutation() {
77          final LSInput input = new ClasspathInput(
78              "Id", ClasspathInputTest.RESOURCE
79          );
80          Assertions.assertThrows(
81              UnsupportedOperationException.class,
82              () -> input.setByteStream(null),
83              "Setting a byte stream on a classpath-resolved input is meaningless"
84          );
85      }
86  
87      @Test
88      void rejectsCharacterStreamMutation() {
89          final LSInput input = new ClasspathInput(
90              "Id", ClasspathInputTest.RESOURCE
91          );
92          Assertions.assertThrows(
93              UnsupportedOperationException.class,
94              () -> input.setCharacterStream(null),
95              "Setting a character stream on a classpath-resolved input is meaningless"
96          );
97      }
98  
99      @Test
100     void rejectsStringDataMutation() {
101         final LSInput input = new ClasspathInput(
102             "Id", ClasspathInputTest.RESOURCE
103         );
104         Assertions.assertThrows(
105             UnsupportedOperationException.class,
106             () -> input.setStringData("anything"),
107             "Setting string data on a classpath-resolved input is meaningless"
108         );
109     }
110 }