< prev index next >
test/javax/xml/jaxp/functional/org/xml/sax/ptests/XMLFilterCBTest.java
Print this page
@@ -24,18 +24,14 @@
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 jaxp.library.JAXPFileBaseTest;
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;
@@ -48,49 +44,44 @@
/**
* 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() {
+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)){
+ try (FileInputStream fis = new FileInputStream(xmlFile);
+ MyXMLFilter myXmlFilter = new MyXMLFilter(outputFile)){
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);
+ myXmlFilter.parse(new InputSource(fis));
}
// 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{
+class MyXMLFilter extends XMLFilterImpl implements AutoCloseable {
/**
* FileWriter to write string to output file.
*/
private final BufferedWriter bWriter;
@@ -276,6 +267,16 @@
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 >