< prev index next >

test/javax/xml/jaxp/functional/org/xml/sax/ptests/DefaultHandlerTest.java

Print this page

        

*** 24,42 **** import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.IOException; - import java.nio.file.Files; - import java.nio.file.Path; - import java.nio.file.Paths; import javax.xml.parsers.ParserConfigurationException; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; import static jaxp.library.JAXPTestUtilities.compareWithGold; - import static jaxp.library.JAXPTestUtilities.failCleanup; - import static jaxp.library.JAXPTestUtilities.failUnexpected; import static org.testng.Assert.assertTrue; import org.testng.annotations.Test; import org.xml.sax.Attributes; import org.xml.sax.Locator; import org.xml.sax.SAXException; --- 24,38 ---- import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.IOException; import javax.xml.parsers.ParserConfigurationException; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; + import jaxp.library.JAXPFileBaseTest; import static jaxp.library.JAXPTestUtilities.compareWithGold; import static org.testng.Assert.assertTrue; import org.testng.annotations.Test; import org.xml.sax.Attributes; import org.xml.sax.Locator; import org.xml.sax.SAXException;
*** 48,97 **** /** * XMLReader parse XML with default handler that transverses XML and * print all visited node. Test verifies output is same as the golden file. */ ! public class DefaultHandlerTest { /** * Test default handler that transverses XML and print all visited node. */ @Test ! public void testDefaultHandler() { String outputFile = CLASS_DIR + "DefaultHandler.out"; String goldFile = GOLDEN_DIR + "DefaultHandlerGF.out"; String xmlFile = XML_DIR + "namespace1.xml"; - try { SAXParserFactory spf = SAXParserFactory.newInstance(); spf.setNamespaceAware(true); SAXParser saxparser = spf.newSAXParser(); MyDefaultHandler handler = new MyDefaultHandler(outputFile); File file = new File(xmlFile); String Absolutepath = file.getAbsolutePath(); String newAbsolutePath = Absolutepath; if (File.separatorChar == '\\') newAbsolutePath = Absolutepath.replace('\\', '/'); ! String uri = "file:///" + newAbsolutePath; ! saxparser.parse(uri, handler); ! } catch (IOException | ParserConfigurationException | SAXException ex) { ! failUnexpected(ex); ! } ! // Need close the output file before we compare it with golden file. ! try { assertTrue(compareWithGold(goldFile, outputFile)); ! } catch (IOException ex) { ! failUnexpected(ex); ! } finally { ! try { ! Path outputPath = Paths.get(outputFile); ! if(Files.exists(outputPath)) ! Files.delete(outputPath); ! } catch (IOException ex) { ! failCleanup(ex, outputFile); ! } ! } } } class MyDefaultHandler extends DefaultHandler { /** --- 44,85 ---- /** * XMLReader parse XML with default handler that transverses XML and * print all visited node. Test verifies output is same as the golden file. */ ! public class DefaultHandlerTest extends JAXPFileBaseTest { /** * Test default handler that transverses XML and print all visited node. + * + * @throws SAXException If any parse errors occur. + * @throws IOException if the file exists but is a directory rather than + * a regular file, does not exist but cannot be created, or cannot + * be opened for any other reason. + * @throws ParserConfigurationException if a DocumentBuilder cannot be + * created which satisfies the configuration requested. */ @Test ! public void testDefaultHandler() throws ParserConfigurationException, ! SAXException, IOException { String outputFile = CLASS_DIR + "DefaultHandler.out"; String goldFile = GOLDEN_DIR + "DefaultHandlerGF.out"; String xmlFile = XML_DIR + "namespace1.xml"; SAXParserFactory spf = SAXParserFactory.newInstance(); spf.setNamespaceAware(true); SAXParser saxparser = spf.newSAXParser(); MyDefaultHandler handler = new MyDefaultHandler(outputFile); File file = new File(xmlFile); String Absolutepath = file.getAbsolutePath(); String newAbsolutePath = Absolutepath; if (File.separatorChar == '\\') newAbsolutePath = Absolutepath.replace('\\', '/'); ! saxparser.parse("file:///" + newAbsolutePath, handler); ! assertTrue(compareWithGold(goldFile, outputFile)); ! } } class MyDefaultHandler extends DefaultHandler { /**
< prev index next >