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.tika.sax;
18  
19  import java.io.StringReader;
20  import java.net.ConnectException;
21  
22  import javax.xml.parsers.SAXParser;
23  import javax.xml.parsers.SAXParserFactory;
24  
25  import junit.framework.TestCase;
26  
27  import org.xml.sax.InputSource;
28  import org.xml.sax.helpers.DefaultHandler;
29  
30  /**
31   * Unit tests for the {@link OfflineContentHandler} class.
32   */
33  public class OfflineContentHandlerTest extends TestCase {
34  
35      private SAXParser parser;
36  
37      private DefaultHandler offline;
38  
39      protected void setUp() throws Exception {
40          parser = SAXParserFactory.newInstance().newSAXParser();
41          offline = new OfflineContentHandler(new DefaultHandler());
42      }
43  
44      public void testExternalDTD() throws Exception {
45          String xml =
46              "<!DOCTYPE foo SYSTEM \"http://127.234.172.38:7845/bar\"><foo/>";
47          try {
48              parser.parse(new InputSource(new StringReader(xml)), offline);
49          } catch (ConnectException e) {
50              fail("Parser tried to access the external DTD:" + e);
51          }
52      }
53  
54      public void testExternalEntity() throws Exception {
55          String xml =
56              "<!DOCTYPE foo ["
57              + " <!ENTITY bar SYSTEM \"http://127.234.172.38:7845/bar\">"
58              + " ]><foo>&bar;</foo>";
59          try {
60              parser.parse(new InputSource(new StringReader(xml)), offline);
61          } catch (ConnectException e) {
62              fail("Parser tried to access the external DTD:" + e);
63          }
64      }
65  
66  }