Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
  * Conditions Of Use
  *
  * This software was developed by employees of the National Institute of
  * Standards and Technology (NIST), and others.
  * This software is has been contributed to the public domain.
  * As a result, a formal license is not needed to use the software.
  *
  * This software is provided "AS IS."
 * NIST MAKES NO WARRANTY OF ANY KIND, EXPRESS, IMPLIED
 * OR STATUTORY, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTY OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT
 * AND DATA ACCURACY.  NIST does not warrant or make any representations
 * regarding the use of the software or the results thereof, including but
 * not limited to the correctness, accuracy, reliability or usefulness of
 * the software.
 *
 *
 */
Product of NIST/ITL Advanced Networking Technologies Division (ANTD). * Author: M. Ranganathan (mranga@nist.gov) * /
 
Implements Parser torture tests.

Author(s):
M. Ranganathan < mranga@nist.gov >
 
 
 package test.torture;
 
 import java.io.*;
 // ifdef J2SDK1.4
 // endif
 
 public class Torture extends DefaultHandler implements ParseExceptionListener,
 
     // variables that I read from the xml file header.
     protected static boolean debug = true;
 
     protected static long startTime;
 
     protected static int counter = 0;
 
     protected String testMessage;
 
     protected String encodedMessage;
 
     protected String testDescription;
 
     protected boolean debugFlag;
 
     protected boolean abortOnFail;
 
     protected String failureReason;
 
     protected String statusMessage;
 
     protected String exceptionHeader;
 
     protected String exceptionMessage;
 
     protected String exceptionClassName;
 
     protected boolean failureFlag;
 
     protected boolean successFlag;
 
     protected boolean outputParsedStructures;
 
     protected static boolean outputResults;
 
     private PrintStream testLogWriter;
 
     private Hashtable exceptionTable;
 
     private StringMsgParser stringParser;
 
     // These track which context the parser is currently parsing.
     private boolean messageContext;
 
     private boolean exceptionTextContext;
    private boolean sipHeaderContext;
    private boolean sipURLContext;
    private boolean testCaseContext;
    private boolean descriptionContext;
    private boolean expectExceptionContext;
    private boolean expectExtensionContext;
    private boolean exceptionMessageContext;
    private String testMessageType;
    private static final String XML_DOCTYPE_IDENTIFIER = "<?xml version='1.0' encoding='us-ascii'?>";
    class MyEntityResolver implements  EntityResolver {
        public InputSource resolveEntity(String publicIdString systemIdthrows SAXExceptionIOException {
            return new InputSourceTorture.class.getResourceAsStream("torture.dtd"));
        }
    }
    class TestLogWriter extends PrintStream {
        public void println(String stuff) {
            if ()
                super.println();
        }
        public TestLogWriter(FileOutputStream fos) {
            super(fos);
        }
    }
    class ExpectedException {
        protected boolean fired;
        protected String exceptionHeader;
        protected String exceptionMessage;
        protected Class exceptionClass;
        protected ExpectedException(String classNameString headerString) {
            try {
                 = Class.forName(className);
                 = headerString;
                 = false;
            } catch (ClassNotFoundException ex) {
                ex.printStackTrace();
                ..println(ex.getMessage());
                .close();
                System.exit(0);
            }
        }
    }
    private void setFailure() {
         = true;
    }
    public void handleException(ParseException exSIPMessage sipMessage,
            Class headerClassString headerTextString messageText)
            throws ParseException {
        String exceptionString = headerText;
        if () {
            ..println(exceptionString);
        }
        if ( == null) {
            formatFailureDiagnostic(ex,
                    "unexpected exception (no exceptions expected)");
            setFailure();
            if ()
                throw ex;
        } else {
            ExpectedException e = (ExpectedException
                    .get(headerText.trim());
            if (e == null) {
                // Check if there is an exception handler for
                // wildcard match.
                e = (ExpectedException.get(messageText.trim());
            }
            if (e == null) {
                formatFailureDiagnostic(ex"unexpected exception "
                        + ex.getMessage());
                setFailure();
                if ()
                    throw ex;
            } else if (!ex.getClass().equals(e.exceptionClass)) {
                setFailure();
                formatFailureDiagnostic(ex" got " + ex.getClass()
                        + " expecting " + e.exceptionClass.getName());
                if ()
                    throw ex;
            } else {
                e.exceptionMessage = headerText;
                e.fired = true;
                 = true;
            }
        }
    }
    public void characters(char[] chint startint length) {
        String str = new String(chstartlength);
        if (str.trim() == "")
            return;
        if () {
            if ( == null)
                 = str;
            else
                 += str;
        } else if () {
            if ( == null)
                 = str;
            else
                 += str;
        } else if () {
            if ( == null)
                 = str;
            else
                 += str;
        } else if () {
            if ( == null)
                 = str;
            else
                 += str;
        } else if () {
            if ( == null)
                 = str;
            else
                 += str;
        } else if () {
            if ( == null)
                 = str;
            else
                 += str;
        }
    }
    public void startElement(String namespaceURIString localString name,
            Attributes attrsthrows SAXException {
        if (name.compareTo() == 0) {
             = false;
             = false;
             = false;
             = false;
            String outputParsedString = attrs.getValue();
            if (outputParsedString != null
                    && outputParsedString.toLowerCase().compareTo() == 0) {
                 = true;
            }
            String debugString = attrs.getValue();
            if (debugString != null
                    && debugString.toLowerCase().compareTo() == 0) {
                 = true;
            }
            String abortString = attrs.getValue();
            if (abortString != null && abortString.compareTo() == 0) {
                 = true;
            } else
                 = false;
             = false;
            String outputResultsString = attrs.getValue();
            if (outputResultsString != null
                    && outputResultsString.compareTo() == 0) {
                 = true;
            } else
                 = false;
            if () {
                String testlog = attrs.getValue();
                if (testlog != null) {
                    try {
                        if ()
                             = new TestLogWriter(
                                    new FileOutputStream(testlogtrue));
                    } catch (IOException ex) {
                        ..println("Cannot open " + testlog
                                + " for write");
                        System.exit(0);
                    }
                } else {
                     = .;
                }
            }
            emitString();
            emitTag();
             = System.currentTimeMillis();
        } else if (name.compareTo() == 0) {
            // Starting a new test
            ++;
             = "";
             = "";
             = false;
             = false;
             = false;
             = false;
             = false;
             = false;
             = false;
             = false;
             = false;
             = null;
             = null;
             = null;
             = null;
             = new Hashtable();
        } else if (name.compareTo() == 0) {
             = ;
             = true;
        } else if (name.compareTo() == 0) {
             = ;
             = true;
        } else if (name.compareTo() == 0) {
             = ;
             = true;
        } else if (name.compareTo() == 0) {
             = true;
        } else if (name.compareTo() == 0) {
             = true;
             = attrs.getValue();
        } else if (name.compareTo() == 0) {
             = true;
        } else if (name.compareTo() == 0) {
             = true;
        } else
            throw new SAXException("Unrecognized tag " + name);
    }
    public void error(SAXParseException ethrows SAXParseException {
        throw e;
    }
    public void ignorableWhitespace(char buf[], int offsetint len)
            throws SAXException {
        // Ignore it
    }
    public void endElement(String namespaceURIString localString name)
            throws SAXException {
        if (name.compareTo() == 0) {
             = false;
             = new StringMsgParser();
            // stringParser.disableInputTracking();
            .setParseExceptionListener(this);
            SIPMessage sipMessage = null;
            SIPHeader sipHeader = null;
            SipUri sipURL = null;
            try {
                if (.equals()) {
                    sipMessage = .parseSIPMessage();
                     = sipMessage.encode();
                } else if (.equals()) {
                    sipHeader = .parseSIPHeader();
                     = sipHeader.encode();
                } else if (.equals()) {
                    sipURL = .parseSIPUrl(.trim());
                     = sipURL.encode();
                } else
                    throw new SAXException("Torture: Internal error");
            } catch (ParseException ex) {
                ex.printStackTrace();
                try {
                    handleException(exnullnew SipUri().getClass(),
                            null);
                } catch (ParseException ex1) {
                    ex1.printStackTrace();
                    ..println("Unexpected excception!");
                    System.exit(0);
                }
            } catch (Exception ex) {
                ex.printStackTrace();
                ..println("Unexpected excception!");
                System.exit(0);
            }
            Enumeration e = .elements();
            while (e.hasMoreElements()) {
                ExpectedException ex = (ExpectedExceptione.nextElement();
                if (!ex.fired) {
                    formatFailureDiagnostic(ex,
                            "Expected Exception not trapped ");
                } else {
                    formatStatusMessage(ex"Expected exception generated ");
                }
            }
            emitBeginTag();
            if (!) {
                emitAttribute("PASSED");
            } else {
                emitAttribute("FAILED");
            }
            emitEndTag();
            emitTaggedCData();
            if ( != null)
                emitTaggedCData();
            else
                emitTaggedCData();
            if () {
                emitTaggedData();
            }
            if () {
                emitTaggedData();
            }
            if () {
                if (sipMessage != null) {
                    emitTaggedCData(sipMessage.toString());
                } else if (sipHeader != null) {
                    emitTaggedCData(sipHeader.toString());
                } else if (sipURL != null) {
                    emitTaggedCData(sipURL.toString());
                }
            }
            emitEndTag();
            if ( && ) {
                if ( != null)
                    .close();
                ..println("Failed -- bailing out!");
                System.exit(0);
            }
        } else if (name.compareTo() == 0) {
             = false;
        } else if (name.compareTo() == 0) {
             = false;
        } else if (name.compareTo() == 0) {
             = false;
        } else if (name.compareTo() == 0) {
             = false;
            ExpectedException ex = new ExpectedException(,
                    );
            if ( != null) {
                this..put(.trim(), ex);
            } else {
                this..put(.trim(), ex);
            }
             = null;
        } else if (name.compareTo() == 0) {
             = false;
             = null;
        } else if (name.compareTo() == 0) {
             = false;
        } else if (name.compareTo() == 0) {
             = false;
        } else if (name.compareTo() == 0) {
            emitEndTag();
            if ()
                .close();
        }
    }
    protected void emitBeginTag(String tagName) {
        if ()
            .println( + tagName);
    }
    protected void emitTag(String tagName) {
        if ()
            .println( + tagName + );
    }
    protected void emitTaggedData(String tagString data) {
        if () {
            emitTag(tag);
            .println(data);
            emitEndTag(tag);
        }
    }
    /* A stand alone tag */
    protected void emitBeginEndTag(String tagName) {
        if () {
            .println( + tagName +  + );
        }
    }
    protected void emitEndTag() {
        if ()
            .println();
    }
    protected void emitEndTag(String tagName) {
        if ()
            .println( +  + tagName + );
    }
    protected void emitTaggedCData(String str) {
        if ()
            .println("<![CDATA[" + str + "]]>");
    }
    protected void emitTaggedCData(String tagString str) {
        if () {
            emitTag(tag);
            .println("<![CDATA[" + str + "]]>");
            emitEndTag(tag);
        }
    }
    protected void emitString(String str) {
        if ()
            .println(str);
    }
    protected void emitAttribute(String attrNameString attrval) {
        if ()
            .println(attrName +  + "\"" + attrval + "\"");
    }
    protected String emitStringAttribute(String attrNameString attrval) {
        return attrName +  + "\"" + attrval + "\"" + "\n";
    }
    protected String emitStringTag(String tag) {
        return "<" + tag + ">" + "\n";
    }
    protected String emitStringBeginTag(String tag) {
        return "<" + tag + "\n";
    }
    protected String emitStringEndTag() {
        return ">" + "\n";
    }
    protected String emitStringEndTag(String tag) {
        return "</" + tag + ">\n";
    }
    protected String emitStringCData(String msg) {
        return "<![CDATA[" + msg + "]]>\n";
    }
    protected String emitStringCData(String tagString msg) {
        return emitStringTag(tag) + emitStringCData(msg)
                + emitStringEndTag(tag);
    }
    protected void formatFailureDiagnostic(ParseException exString message) {
         += emitStringAttribute(message);
         += emitStringEndTag();
                .getName());
        // DataOutputStream dos = new DataOutputStream(bos);
        ByteArrayOutputStream bos = new ByteArrayOutputStream(4096);
        ex.printStackTrace(new PrintStream(bostrue));
         += emitStringCData(bos.toString());
         += emitStringCData(ex.getMessage());
    }
    protected void formatFailureDiagnostic(String hdrString reason) {
         += emitStringAttribute("BAD");
         += emitStringAttribute(reason);
         += emitStringEndTag();
         += emitStringCData(hdr);
    }
    protected void formatFailureDiagnostic(ExpectedException exString reason) {
         += emitStringAttribute("BAD");
         += emitStringAttribute(reason);
         += emitStringAttribute(ex.exceptionClass
                .getName());
         += emitStringEndTag();
         += emitStringCData(ex.exceptionHeader);
    }
    protected void formatStatusMessage(String hdrString reason) {
         += emitStringAttribute("OK");
         += emitStringAttribute(reason);
         += emitStringEndTag();
         += emitStringCData(hdr);
    }
    protected void formatStatusMessage(ExpectedException exString reason) {
         += emitStringAttribute("OK");
         += emitStringAttribute(reason);
         += emitStringAttribute(ex.exceptionClass
                .getName());
         += emitStringEndTag();
         += emitStringCData(ex.exceptionHeader);
    }
    public Torture () {
    }
    public void doTests() throws Exception {
        String fileName ;
        fileName = "torture.xml";
        /* The tests do not check for content length */
        StringMsgParser.setComputeContentLengthFromMessage(true);
        try {
            SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
            XMLReader saxParser = saxParserFactory.newSAXParser()
                    .getXMLReader();
            saxParser.setEntityResolver(new MyEntityResolver());
            saxParser.setContentHandler(this);
            saxParser
                    .setFeature("http://xml.org/sax/features/validation"true);
            saxParser.parse(new InputSource ( Torture.class.getResourceAsStream(fileName)));
            ..println("Elapsed time = "
                    + (System.currentTimeMillis() - ) / );
        } catch (Exception ex) {
            ex.printStackTrace();
            throw ex;
        }
    }
New to GrepCode? Check out our FAQ X