1 2 4 5 33 34 package org.xmlpull.v1.tests; 35 36 import java.io.FileInputStream ; 37 import java.io.IOException ; 38 import java.io.InputStream ; 39 import java.io.StringReader ; 40 import org.xmlpull.v1.XmlPullParser; 41 import org.xmlpull.v1.XmlPullParserException; 42 import org.xmlpull.v1.XmlPullParserFactory; 43 45 47 public class XmlTestCase extends UtilTestCase { 48 private static final String NS_URI = "http://xmlpull.org/v1/tests/2002-08.xsd"; 49 private static final boolean verbose = false; 51 52 56 public XmlTestCase(String name) { 57 super(name); 58 } 60 61 protected void verbose(String msg) 62 { 63 if(verbose) { 64 if(PackageTests.runnigAllTests()) { 65 System.out.println(msg); 67 } else { 68 System.out.println(msg); 69 } 70 } 71 } 72 73 protected void info(String msg) 74 { 75 if(PackageTests.runnigAllTests()) { 76 PackageTests.addNote(msg+"\n"); 77 } else { 78 System.out.println(msg); 79 } 80 } 81 82 private final static String RESOURCE_PREFIX = "/org/xmlpull/v1/tests/xml/"; 83 private InputStream openStream(String name) throws IOException { 84 InputStream is = null; 86 if(is == null) { 87 try { 88 is = new FileInputStream (name); 89 } catch(Exception ex) { 90 } 91 } 92 if(is == null) { 93 try { 94 is = getClass().getResourceAsStream (RESOURCE_PREFIX+name); 95 } catch(Exception ex) { 96 } 97 } 98 if(is == null) { 99 try { 100 is = getClass().getResourceAsStream ("/"+name); 101 } catch(Exception ex) { 102 } 103 } 104 110 111 112 if (is == null) { 113 throw new IOException ("could not open XML test for '"+name+"'"); 114 } 115 return is; 116 } 117 120 public void testXml(String testName) 121 throws IOException , XmlPullParserException 122 { 123 124 XmlPullParserFactory factory = factoryNewInstance(); 125 factory.setNamespaceAware(true); 126 128 129 XmlPullParser pp = factory.newPullParser(); 130 132 133 InputStream is = openStream(testName); 134 verbose("> LOADING TESTS '"+testName+"'"); 135 pp.setInput(is, null); 136 executeTests(pp); 137 is.close(); 138 verbose("< FINISHED TESTS '"+testName+"'"); 139 } 140 141 142 protected void executeTests(XmlPullParser pp) throws IOException , XmlPullParserException 143 { 144 145 pp.nextTag(); 147 pp.require(pp.START_TAG, NS_URI, "tests"); 148 while(pp.nextTag() == pp.START_TAG) { 149 executeTestParser(pp); 150 } 151 pp.require(pp.END_TAG, NS_URI, "tests"); 152 } 153 154 protected void executeTestParser(XmlPullParser pp) throws IOException , XmlPullParserException 155 { 156 pp.require(pp.START_TAG, NS_URI, "test-parser"); 157 String testName = pp.getAttributeValue("", "name"); 158 verbose(">> START TEST '"+testName+"'"); 159 XmlPullParserFactory testFactory = factoryNewInstance(); 161 verbose(">> using testFactory='"+testFactory.getClass()+"'"); 162 XmlPullParser testParser = null; 163 boolean testInputReady = false; 164 while(pp.nextTag() == pp.START_TAG) { 165 String action = pp.getName(); 166 if("create-parser".equals(action)) { 167 testParser = testFactory.newPullParser(); 168 verbose(">> using testParser='"+testParser.getClass()+"'"); 169 pp.nextText(); 170 } else if("input-inline".equals(action)) { 171 if(testInputReady) { 172 throw new RuntimeException ("input already set"+pp.getPositionDescription()); 173 } 174 testInputReady = true; 175 String input = pp.nextText(); 176 verbose(">>> "+action+" to '"+printable(input)+"'"); 177 verbose(">>> "+action+" to '"+input+"'"); 178 testParser.setInput(new StringReader ( input )); 179 } else if("expect".equals(action)) { 180 String type = pp.getAttributeValue("", "type"); 181 String namespace = pp.getAttributeValue("", "namespace"); 182 String name = pp.getAttributeValue("", "name"); 183 String empty = pp.getAttributeValue("", "empty"); 184 verbose(">>> "+action 185 +" type="+type 186 +(namespace != null ? " namespace="+namespace : "") 187 +(name != null ? " name="+name : "") 188 +(empty != null ? " empty="+empty : "")); 189 if(type != null) { 190 int event = convertToEventType(pp,type); 191 assertEquals(pp.TYPES[ event ], pp.TYPES[ testParser.getEventType() ]); 193 assertEquals(event, testParser.getEventType()); 195 } 196 if(namespace != null) assertEquals(namespace, testParser.getNamespace()); 197 if(name != null) assertEquals(name, testParser.getName()); 198 if(empty != null) { 199 boolean isEmpty = Boolean.valueOf( empty ).booleanValue(); 200 assertEquals(isEmpty, testParser.isEmptyElementTag()); 201 } 202 pp.nextText(); 203 } else if("next".equals(action)) { 204 verbose(">>> "+action); 205 testParser.next(); 206 pp.nextText(); 207 } else if("next-tag".equals(action)) { 208 verbose(">>> "+action); 209 testParser.nextTag(); 210 pp.nextText(); 211 } else if("next-text".equals(action)) { 212 verbose(">>> "+action); String expected = pp.getAttributeValue("", "text"); 214 String testText = testParser.nextText(); 215 if(expected != null) { 216 verbose(">>> "+action+" testParser="+testParser.getPositionDescription() 217 +" excpectecd='"+printable(expected)+"' test='"+printable(testText)+"'"); 218 assertEquals(testParser.getPositionDescription(), printable(expected), printable(testText)); 219 assertEquals(expected, testText); 220 } 221 pp.nextText(); 222 } else if("set-feature".equals(action)) { 223 String feature = pp.nextText(); 224 verbose(">>> "+action+" "+feature); 225 testParser.setFeature(feature, true); 226 } else { 227 verbose(">>> UNKNONW ACTION '"+action+"' ("+pp.getPositionDescription()+")"); 228 String content = pp.nextText(); 229 if(content.length() > 0) { 230 if(verbose) System.err.println(">>> CONTENT='"+printable(content)+"'"); 231 } 232 233 } 234 } 235 236 pp.require(pp.END_TAG, NS_URI, "test-parser"); 237 verbose(">> END TEST '"+testName+"'"); 238 } 239 240 241 private int convertToEventType(XmlPullParser pp, String eventName) { 242 for (int i = 0; i < pp.TYPES.length; i++) 243 { 244 if( eventName.equals(pp.TYPES[ i ]) ) { 245 return i; 246 } 247 } 248 throw new RuntimeException ("unknown event type "+eventName+pp.getPositionDescription()); 249 } 250 251 } 252 253 | Popular Tags |