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 org.xml.sax.ContentHandler;
20  import org.xml.sax.SAXException;
21  
22  import junit.framework.TestCase;
23  
24  /**
25   * Unit tests for the {@link SafeContentHandler} class.
26   */
27  public class SafeContentHandlerTest extends TestCase {
28  
29      private ContentHandler output;
30  
31      private ContentHandler safe;
32  
33      protected void setUp() {
34          output = new WriteOutContentHandler();
35          safe = new SafeContentHandler(output);
36      }
37  
38      public void testEmptyInput() throws SAXException {
39          safe.characters(new char[0], 0, 0);
40          safe.ignorableWhitespace(new char[0], 0, 0);
41          assertEquals("", output.toString());
42      }
43  
44      public void testNormalCharacters() throws SAXException {
45          safe.characters("abc".toCharArray(), 0, 3);
46          assertEquals("abc", output.toString());
47      }
48  
49      public void testNormalWhitespace() throws SAXException {
50          safe.ignorableWhitespace("abc".toCharArray(), 0, 3);
51          assertEquals("abc", output.toString());
52      }
53  
54      public void testInvalidCharacters() throws SAXException {
55          safe.characters("ab\u0007".toCharArray(), 0, 3);
56          safe.characters("a\u000Bc".toCharArray(), 0, 3);
57          safe.characters("\u0019bc".toCharArray(), 0, 3);
58          assertEquals("ab a c bc", output.toString());
59      }
60  
61      public void testInvalidWhitespace() throws SAXException {
62          safe.ignorableWhitespace("ab\u0000".toCharArray(), 0, 3);
63          safe.ignorableWhitespace("a\u0001c".toCharArray(), 0, 3);
64          safe.ignorableWhitespace("\u0002bc".toCharArray(), 0, 3);
65          assertEquals("ab a c bc", output.toString());
66      }
67  
68  }