< prev index next >
test/javax/xml/jaxp/functional/org/xml/sax/ptests/XMLFilterCBTest.java
Print this page
*** 24,41 ****
import java.io.BufferedWriter;
import java.io.FileInputStream;
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.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.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;
--- 24,37 ----
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParserFactory;
+ import jaxp.library.JAXPFileBaseTest;
import static jaxp.library.JAXPTestUtilities.compareWithGold;
import static org.testng.Assert.assertTrue;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;
*** 48,96 ****
/**
* Set parent of XMLFilter to XMLReader. Parsing on XML file will invoke XMLFilter
* to write to output file. Test verifies output is same as the golden file.
*/
! public class XMLFilterCBTest {
! public void testXMLFilterCB() {
String outputFile = CLASS_DIR + "XMLFilter.out";
String goldFile = GOLDEN_DIR + "XMLFilterGF.out";
String xmlFile = XML_DIR + "namespace1.xml";
! try (FileInputStream fis = new FileInputStream(xmlFile)){
SAXParserFactory spf = SAXParserFactory.newInstance();
spf.setNamespaceAware(true);
XMLReader xmlReader = spf.newSAXParser().getXMLReader();
-
- MyXMLFilter myXmlFilter = new MyXMLFilter(outputFile);
myXmlFilter.setParent(xmlReader);
! InputSource is = new InputSource(fis);
! myXmlFilter.parse(is);
! } catch( SAXException | IOException | ParserConfigurationException 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);
- }
- }
}
}
/**
* Writer XMLFiler which write all tags to output file when event happens.
*/
! class MyXMLFilter extends XMLFilterImpl{
/**
* FileWriter to write string to output file.
*/
private final BufferedWriter bWriter;
--- 44,87 ----
/**
* Set parent of XMLFilter to XMLReader. Parsing on XML file will invoke XMLFilter
* to write to output file. Test verifies output is same as the golden file.
*/
! public class XMLFilterCBTest extends JAXPFileBaseTest {
! /**
! * Test XMLFilter working with XML reader.
! *
! * @throws SAXException If there is a problem processing the document.
! * @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.
! */
! public void testXMLFilterCB() throws SAXException, IOException,
! ParserConfigurationException {
String outputFile = CLASS_DIR + "XMLFilter.out";
String goldFile = GOLDEN_DIR + "XMLFilterGF.out";
String xmlFile = XML_DIR + "namespace1.xml";
! try (FileInputStream fis = new FileInputStream(xmlFile);
! MyXMLFilter myXmlFilter = new MyXMLFilter(outputFile)){
SAXParserFactory spf = SAXParserFactory.newInstance();
spf.setNamespaceAware(true);
XMLReader xmlReader = spf.newSAXParser().getXMLReader();
myXmlFilter.setParent(xmlReader);
! myXmlFilter.parse(new InputSource(fis));
}
// Need close the output file before we compare it with golden file.
assertTrue(compareWithGold(goldFile, outputFile));
}
}
/**
* Writer XMLFiler which write all tags to output file when event happens.
*/
! class MyXMLFilter extends XMLFilterImpl implements AutoCloseable {
/**
* FileWriter to write string to output file.
*/
private final BufferedWriter bWriter;
*** 276,281 ****
--- 267,282 ----
bWriter.newLine();
} catch (IOException ex) {
throw new SAXException(ex);
}
}
+
+ /**
+ * Close writer handler.
+ * @throws IOException if any I/O error when close writer handler.
+ */
+ @Override
+ public void close() throws IOException {
+ if (bWriter != null)
+ bWriter.close();
+ }
}
< prev index next >