1 2 4 package org.xmlpull.v1.tests; 5 6 import junit.framework.TestCase; 8 import junit.framework.TestSuite; 9 10 import java.io.StringReader ; 11 import java.io.IOException ; 12 13 import org.xmlpull.v1.XmlPullParser; 14 import org.xmlpull.v1.XmlPullParserFactory; 15 import org.xmlpull.v1.XmlPullParserException; 16 17 22 public class TestEolNormalization extends UtilTestCase { 23 private XmlPullParserFactory factory; 24 25 public TestEolNormalization(String name) { 26 super(name); 27 } 28 29 protected void setUp() throws XmlPullParserException { 30 factory = factoryNewInstance(); 31 factory.setNamespaceAware(true); 33 assertEquals(true, factory.getFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES)); 34 assertEquals(false, factory.getFeature(XmlPullParser.FEATURE_VALIDATION)); 35 } 36 37 protected void tearDown() { 38 } 39 40 public void testNormalizeLine() 41 throws IOException , XmlPullParserException 42 { 43 44 XmlPullParser pp = factory.newPullParser(); 45 46 49 final String simpleR = "-\n-\r-\r\n-\n\r-"; 50 51 53 final String tagSimpleR = "<test>"+simpleR+"</test>"; 54 55 final String expectedSimpleN = "-\n-\n-\n-\n\n-"; 56 57 parseOneElement(pp, tagSimpleR, true); 58 assertEquals(XmlPullParser.TEXT, pp.next()); 59 assertEquals(printable(expectedSimpleN), printable(pp.getText())); 60 61 63 final String attrSimpleR = "<test a=\""+simpleR+"\"/>"; 64 65 final String normalizedSimpleN = "- - - - -"; 66 67 parseOneElement(pp, attrSimpleR, true); 68 String attrVal = pp.getAttributeValue("","a"); 69 70 assertEquals(printable(normalizedSimpleN), printable(attrVal)); 72 73 76 final String firstR = 77 "\r \r\n \n\r \n\n \r\n\r \r\r \r\n\n \n\r\r\n\r"+ 78 ""; 79 80 82 final String tagR = 83 "<m:test xmlns:m='Some-Namespace-URI'>"+ 84 firstR+ 85 "</m:test>\r\n"; 86 87 final String expectedN = 88 "\n \n \n\n \n\n \n\n \n\n \n\n \n\n\n\n"; 89 90 parseOneElement(pp, tagR, true); 91 assertEquals(XmlPullParser.TEXT, pp.next()); 92 assertEquals(printable(expectedN), printable(pp.getText())); 93 94 96 final String attrR = 97 "<m:test xmlns:m='Some-Namespace-URI' fifi='"+firstR+"'/>"; 98 99 final String normalizedN = 100 " "; 101 102 parseOneElement(pp, attrR, true); 103 attrVal = pp.getAttributeValue("","fifi"); 104 107 assertEquals(printable(normalizedN), printable(attrVal)); 109 110 111 114 final String manyLineBreaks = 115 "fifi\r&\r&\r\n foo &\r bar \n\r\n""+ 116 firstR; 117 118 final String manyTag = 119 "<m:test xmlns:m='Some-Namespace-URI'>"+ 120 manyLineBreaks+ 121 "</m:test>\r\n"; 122 123 final String manyExpected = 124 "fifi\n&\n&\n foo &\n bar \n\n\""+ 125 expectedN; 126 128 parseOneElement(pp, manyTag, true); 129 assertEquals(XmlPullParser.TEXT, pp.next()); 130 assertEquals(printable(manyExpected), printable(pp.getText())); 131 assertEquals(manyExpected, pp.getText()); 132 133 assertEquals(pp.next(), XmlPullParser.END_TAG); 134 assertEquals("test", pp.getName()); 135 136 assertEquals(pp.next(), XmlPullParser.END_DOCUMENT); 140 141 142 final String manyAttr = 143 "<m:test xmlns:m='Some-Namespace-URI' fifi='"+manyLineBreaks+"'/>"; 144 145 final String manyNormalized = 146 "fifi & & foo & bar \""+ 147 normalizedN; 148 149 parseOneElement(pp, manyAttr, true); 150 attrVal = pp.getAttributeValue("","fifi"); 151 assertEquals(printable(manyNormalized), printable(attrVal)); 153 154 } 155 156 private void parseOneElement( 157 final XmlPullParser pp, 158 final String buf, 159 final boolean supportNamespaces) 160 throws IOException , XmlPullParserException 161 { 162 pp.setInput(new StringReader (buf)); 164 pp.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, supportNamespaces); 166 pp.next(); 168 if(supportNamespaces) { 170 assertEquals("test", pp.getName()); 171 } else { 172 assertEquals("m:test", pp.getName()); 173 } 174 } 175 176 177 public static void main (String [] args) { 178 junit.textui.TestRunner.run (new TestSuite(TestEolNormalization.class)); 179 } 180 181 } 182 183 | Popular Tags |