1 /* 2 * Copyright (C) 2009 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package org.kxml2.io; 18 19 import junit.framework.TestCase; 20 21 import java.io.ByteArrayOutputStream; 22 import java.io.IOException; 23 import java.io.StringWriter; 24 import org.w3c.dom.Document; 25 import org.w3c.dom.NodeList; 26 import org.xmlpull.v1.XmlSerializer; 27 28 import static tests.support.Support_Xml.*; 29 30 public class KXmlSerializerTest extends TestCase { 31 private static final String NAMESPACE = null; 32 33 private static boolean isValidXmlCodePoint(int c) { 34 // http://www.w3.org/TR/REC-xml/#charsets 35 return (c >= 0x20 && c <= 0xd7ff) || (c == 0x9) || (c == 0xa) || (c == 0xd) || 36 (c >= 0xe000 && c <= 0xfffd) || (c >= 0x10000 && c <= 0x10ffff); 37 } 38 39 private static XmlSerializer newSerializer() throws IOException { 40 ByteArrayOutputStream bytesOut = new ByteArrayOutputStream(); 41 XmlSerializer serializer = new KXmlSerializer(); 42 serializer.setOutput(bytesOut, "UTF-8"); 43 serializer.startDocument("UTF-8", null); 44 return serializer; 45 } 46 47 public void testInvalidCharactersInText() throws IOException { 48 XmlSerializer serializer = newSerializer(); 49 serializer.startTag(NAMESPACE, "root"); 50 for (int ch = 0; ch <= 0xffff; ++ch) { 51 final String s = Character.toString((char) ch); 52 if (isValidXmlCodePoint(ch)) { 53 serializer.text("a" + s + "b"); 54 } else { 55 try { 56 serializer.text("a" + s + "b"); 57 fail(s); 58 } catch (IllegalArgumentException expected) { 59 } 60 } 61 } 62 serializer.endTag(NAMESPACE, "root"); 63 } 64 65 public void testInvalidCharactersInAttributeValues() throws IOException { 66 XmlSerializer serializer = newSerializer(); 67 serializer.startTag(NAMESPACE, "root"); 68 for (int ch = 0; ch <= 0xffff; ++ch) { 69 final String s = Character.toString((char) ch); 70 if (isValidXmlCodePoint(ch)) { 71 serializer.attribute(NAMESPACE, "a", "a" + s + "b"); 72 } else { 73 try { 74 serializer.attribute(NAMESPACE, "a", "a" + s + "b"); 75 fail(s); 76 } catch (IllegalArgumentException expected) { 77 } 78 } 79 } 80 serializer.endTag(NAMESPACE, "root"); 81 } 82 83 public void testInvalidCharactersInCdataSections() throws IOException { 84 XmlSerializer serializer = newSerializer(); 85 serializer.startTag(NAMESPACE, "root"); 86 for (int ch = 0; ch <= 0xffff; ++ch) { 87 final String s = Character.toString((char) ch); 88 if (isValidXmlCodePoint(ch)) { 89 serializer.cdsect("a" + s + "b"); 90 } else { 91 try { 92 serializer.cdsect("a" + s + "b"); 93 fail(s); 94 } catch (IllegalArgumentException expected) { 95 } 96 } 97 } 98 serializer.endTag(NAMESPACE, "root"); 99 } 100 101 public void testCdataWithTerminatorInside() throws Exception { 102 StringWriter writer = new StringWriter(); 103 XmlSerializer serializer = new KXmlSerializer(); 104 serializer.setOutput(writer); 105 serializer.startDocument("UTF-8", null); 106 serializer.startTag(NAMESPACE, "p"); 107 serializer.cdsect("a]]>b"); 108 serializer.endTag(NAMESPACE, "p"); 109 serializer.endDocument(); 110 // Adjacent CDATA sections aren't merged, so let's stick them together ourselves... 111 Document doc = domOf(writer.toString()); 112 NodeList children = doc.getFirstChild().getChildNodes(); 113 String text = ""; 114 for (int i = 0; i < children.getLength(); ++i) { 115 text += children.item(i).getNodeValue(); 116 } 117 assertEquals("a]]>b", text); 118 } 119 } 120