public boolean testCase2()

in java/src/org/apache/qetest/trax/stream/StreamSourceAPITest.java [216:342]


    public boolean testCase2()
    {
        reporter.testCaseInit("Basic functionality of StreamSources");

        TransformerFactory factory = null;
        String xslID = testFileInfo.inputName;
        String xmlID = testFileInfo.xmlName;
        try
        {
            factory = TransformerFactory.newInstance();
            factory.setErrorListener(new DefaultErrorHandler());
            // Create URLs for the filenames
            // What's the simplest way to do this?!? i.e. as a solution 
            //  to the general problem of having a String denoting a 
            //  path/filename, that may be absolute or relative,
            //  and may or may not exist, and to get a valid file: URL?
            reporter.logTraceMsg("Original names xslID=" + xslID + ", xmlID=" + xmlID);
            xslID = filenameToURI(xslID);
            xmlID = filenameToURI(xmlID);
            reporter.logTraceMsg("URL-ized names xslID=" + xslID + ", xmlID=" + xmlID);
        }
        catch (Throwable t)
        {
            reporter.checkFail("Problem creating factory; can't continue testcase");
            reporter.logThrowable(reporter.ERRORMSG, t,
                                  "Problem creating factory; can't continue testcase");
            return true;
        }
        try
        {
            // Verify we can do basic transforms with readers/streams
            reporter.logTraceMsg("Create stream sources and setSystemId separately");
            InputStream xslStream1 = new FileInputStream(testFileInfo.inputName);
            Source xslSource1 = new StreamSource(xslStream1);
            xslSource1.setSystemId(xslID);
            InputStream xmlStream1 = new FileInputStream(testFileInfo.xmlName);
            Source xmlSource1 = new StreamSource(xmlStream1);
            xmlSource1.setSystemId(xmlID);

            reporter.logTraceMsg("Create FileOutputStream to " + outNames.nextName());
            FileOutputStream fos1 = new FileOutputStream(outNames.currentName());
            Result result1 = new StreamResult(fos1);
            Templates templates1 = factory.newTemplates(xslSource1);
            Transformer transformer1 = templates1.newTransformer();
            transformer1.setErrorListener(new DefaultErrorHandler());
            reporter.logTraceMsg("about to transform to streams after setSystemId");
            transformer1.transform(xmlSource1, result1);
            fos1.close(); // must close ostreams we own
            int result = fileChecker.check(reporter, 
                              new File(outNames.currentName()), 
                              new File(testFileInfo.goldName), 
                              "transform to streams after setSystemId into " + outNames.currentName());
            if (result == reporter.FAIL_RESULT)
                reporter.logInfoMsg("transform to streams... failure reason:" + fileChecker.getExtendedInfo());
        }
        catch (Throwable t)
        {
            reporter.checkFail("Problem with transform-streams(1)");
            reporter.logThrowable(reporter.ERRORMSG, t, "Problem with transform-streams(1)");
        }
        try
        {
            reporter.logTraceMsg("Create stream sources with setSystemId in ctor");
            InputStream xslStream2 = new FileInputStream(testFileInfo.inputName);
            Source xslSource2 = new StreamSource(xslStream2, xslID);
            InputStream xmlStream2 = new FileInputStream(testFileInfo.xmlName);
            Source xmlSource2 = new StreamSource(xmlStream2, xmlID);
            FileOutputStream fos2 = new FileOutputStream(outNames.nextName());
            Result result2 = new StreamResult(fos2);

            reporter.logInfoMsg("Transform into " + outNames.currentName());
            Templates templates2 = factory.newTemplates(xslSource2);
            Transformer transformer2 = templates2.newTransformer();
            transformer2.setErrorListener(new DefaultErrorHandler());
            transformer2.transform(xmlSource2, result2);
            fos2.close(); // must close ostreams we own
            int result = fileChecker.check(reporter, 
                              new File(outNames.currentName()), 
                              new File(testFileInfo.goldName), 
                              "transform to streams after SystemId in ctor into " + outNames.currentName());
            if (result == reporter.FAIL_RESULT)
                reporter.logInfoMsg("transform to streams... failure reason:" + fileChecker.getExtendedInfo());
        }
        catch (Throwable t)
        {
            reporter.checkFail("Problem with transform-streams(2)");
            reporter.logThrowable(reporter.ERRORMSG, t, "Problem with transform-streams(2)");
        }

        try
        {
            // Do a transform without systemId set
            // Note: is affected by user.dir property; if we're 
            //  already in the correct place, this won't be different
            // But: most people will run from xml-xalan\test, so this should fail
            try
            {
                reporter.logStatusMsg("System.getProperty(user.dir) = " + System.getProperty("user.dir"));
            }
            catch (SecurityException e) // in case of Applet context
            {
                reporter.logTraceMsg("System.getProperty(user.dir) threw SecurityException");
            }
            reporter.logTraceMsg("Create stream sources without setSystemId set");
            InputStream xslStream3 = new FileInputStream(testFileInfo.inputName);
            Source xslSource3 = new StreamSource(xslStream3);
            InputStream xmlStream3 = new FileInputStream(testFileInfo.xmlName);
            Source xmlSource3 = new StreamSource(xmlStream3);
            FileOutputStream fos3 = new FileOutputStream(outNames.nextName());
            Result result3 = new StreamResult(fos3);

            Templates templates3 = factory.newTemplates(xslSource3);
            Transformer transformer3 = templates3.newTransformer();
            transformer3.setErrorListener(new DefaultErrorHandler());
            reporter.logStatusMsg("About to transform without systemID; probably throws exception");
            transformer3.transform(xmlSource3, result3);
            reporter.checkFail("The above transform should probably have thrown an exception; into " + outNames.currentName());
        }
        catch (Throwable t)
        {
            reporter.checkPass("Transforming with include/import and wrong SystemId throws exception; into " + outNames.currentName());
            reporter.logThrowable(reporter.ERRORMSG, t, "Transforming with include/import and wrong SystemId");
        }

        reporter.testCaseClose();
        return true;
    }