View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.io.input.compatibility;
18  
19  import java.io.IOException;
20  
21  import org.apache.commons.io.input.CharSequenceInputStream;
22  import org.apache.commons.io.input.XmlStreamReaderUtilitiesTest;
23  
24  /**
25   * Test compatibility of the original XmlStreamReader (before all the refactoring).
26   */
27  public class XmlStreamReaderUtilitiesCompatibilityTest extends XmlStreamReaderUtilitiesTest {
28  
29      /** Mock {@link XmlStreamReader} implementation */
30      private static final class MockXmlStreamReader extends XmlStreamReader {
31          MockXmlStreamReader(final String defaultEncoding) throws IOException {
32              super(CharSequenceInputStream.builder().setCharSequence("").get(), null, true, defaultEncoding);
33          }
34      }
35      @Override
36      protected String calculateHttpEncoding(final String httpContentType, final String bomEnc, final String xmlGuessEnc,
37              final String xmlEnc, final boolean lenient, final String defaultEncoding) throws IOException {
38          try (MockXmlStreamReader mock = new MockXmlStreamReader(defaultEncoding)) {
39                  return mock.calculateHttpEncoding(
40                          XmlStreamReader.getContentTypeMime(httpContentType),
41                          XmlStreamReader.getContentTypeEncoding(httpContentType),
42                          bomEnc, xmlGuessEnc, xmlEnc, null, lenient);
43          }
44      }
45  
46      @Override
47      protected String calculateRawEncoding(final String bomEnc, final String xmlGuessEnc, final String xmlEnc,
48              final String defaultEncoding) throws IOException {
49          try (MockXmlStreamReader mock = new MockXmlStreamReader(defaultEncoding)) {
50              return mock.calculateRawEncoding(bomEnc, xmlGuessEnc, xmlEnc, null);
51          }
52      }
53  }