KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xmlpull > v1 > tests > TestEolNormalization


1 /* -*- c-basic-offset: 4; indent-tabs-mode: nil; -*- //------100-columns-wide------>|*/
2 // for license see accompanying LICENSE_TESTS.txt file (available also at http://www.xmlpull.org)
3

4 package org.xmlpull.v1.tests;
5
6 //import junit.framework.Test;
7
import junit.framework.TestCase;
8 import junit.framework.TestSuite;
9
10 import java.io.StringReader JavaDoc;
11 import java.io.IOException JavaDoc;
12
13 import org.xmlpull.v1.XmlPullParser;
14 import org.xmlpull.v1.XmlPullParserFactory;
15 import org.xmlpull.v1.XmlPullParserException;
16
17 /**
18  * Test end-of-line normalization
19  *
20  * @author <a HREF="http://www.extreme.indiana.edu/~aslom/">Aleksander Slominski</a>
21  */

22 public class TestEolNormalization extends UtilTestCase {
23     private XmlPullParserFactory factory;
24
25     public TestEolNormalization(String JavaDoc name) {
26         super(name);
27     }
28
29     protected void setUp() throws XmlPullParserException {
30         factory = factoryNewInstance();
31         //factory.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true);
32
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 JavaDoc, XmlPullParserException
42     {
43
44         XmlPullParser pp = factory.newPullParser();
45
46         //-----------------------
47
// ---- simple tests for end of line normalization
48

49         final String JavaDoc simpleR = "-\n-\r-\r\n-\n\r-";
50
51         // element content EOL normalizaton
52

53         final String JavaDoc tagSimpleR = "<test>"+simpleR+"</test>";
54
55         final String JavaDoc 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         // attribute content normalization
62

63         final String JavaDoc attrSimpleR = "<test a=\""+simpleR+"\"/>";
64
65         final String JavaDoc normalizedSimpleN = "- - - - -";
66
67         parseOneElement(pp, attrSimpleR, true);
68         String JavaDoc attrVal = pp.getAttributeValue("","a");
69
70         //TODO Xerces2
71
assertEquals(printable(normalizedSimpleN), printable(attrVal));
72
73         //-----------------------
74
// --- more complex example with more line engins together
75

76         final String JavaDoc 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         // element content
81

82         final String JavaDoc tagR =
83             "<m:test xmlns:m='Some-Namespace-URI'>"+
84             firstR+
85             "</m:test>\r\n";
86
87         final String JavaDoc 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         // attribute value
95

96         final String JavaDoc attrR =
97             "<m:test xmlns:m='Some-Namespace-URI' fifi='"+firstR+"'/>";
98
99         final String JavaDoc normalizedN =
100             " ";
101
102         parseOneElement(pp, attrR, true);
103         attrVal = pp.getAttributeValue("","fifi");
104         //System.err.println("attrNormalized.len="+normalizedN.length());
105
//System.err.println("attrVal.len="+attrVal.length());
106

107         //TODO Xerces2
108
assertEquals(printable(normalizedN), printable(attrVal));
109
110
111         //-----------------------
112
// --- even more complex
113

114         final String JavaDoc manyLineBreaks =
115             "fifi\r&amp;\r&amp;\r\n foo &amp;\r bar \n\r\n&quot;"+
116             firstR;
117
118         final String JavaDoc manyTag =
119             "<m:test xmlns:m='Some-Namespace-URI'>"+
120             manyLineBreaks+
121             "</m:test>\r\n";
122
123         final String JavaDoc manyExpected =
124             "fifi\n&\n&\n foo &\n bar \n\n\""+
125             expectedN;
126         //"\r \r\n \n\r \n\n \r\n\r \r\r \r\n\n \n\r\r\n\r";
127

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         // having \r\n as last characters is the hardest case
137
//assertEquals(XmlPullParser.CONTENT, pp.next());
138
//assertEquals("\n", pp.readContent());
139
assertEquals(pp.next(), XmlPullParser.END_DOCUMENT);
140
141
142         final String JavaDoc manyAttr =
143             "<m:test xmlns:m='Some-Namespace-URI' fifi='"+manyLineBreaks+"'/>";
144
145         final String JavaDoc manyNormalized =
146             "fifi & & foo & bar \""+
147             normalizedN;
148
149         parseOneElement(pp, manyAttr, true);
150         attrVal = pp.getAttributeValue("","fifi");
151         //TODO Xerces2
152
assertEquals(printable(manyNormalized), printable(attrVal));
153
154     }
155
156     private void parseOneElement(
157         final XmlPullParser pp,
158         final String JavaDoc buf,
159         final boolean supportNamespaces)
160         throws IOException JavaDoc, XmlPullParserException
161     {
162         //pp.setInput(buf.toCharArray());
163
pp.setInput(new StringReader JavaDoc(buf));
164         //pp.setNamespaceAware(supportNamespaces);
165
pp.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, supportNamespaces);
166         //pp.setAllowedMixedContent(false);
167
pp.next();
168         //pp.readStartTag(stag);
169
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 JavaDoc[] args) {
178         junit.textui.TestRunner.run (new TestSuite(TestEolNormalization.class));
179     }
180
181 }
182
183
Popular Tags