< prev index next >
test/javax/xml/jaxp/functional/javax/xml/transform/ptests/TransformerTest.java
Print this page
@@ -22,11 +22,11 @@
*/
package javax.xml.transform.ptests;
import java.io.File;
import java.io.FileInputStream;
-import java.io.FileNotFoundException;
+import java.io.FilePermission;
import java.io.IOException;
import java.util.Properties;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
@@ -37,89 +37,121 @@
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import static javax.xml.transform.ptests.TransformerTestConst.XML_DIR;
import javax.xml.transform.sax.SAXSource;
import javax.xml.transform.stream.StreamSource;
-import static jaxp.library.JAXPTestUtilities.failUnexpected;
+import jaxp.library.JAXPBaseTest;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNotNull;
import static org.testng.Assert.assertTrue;
+import org.testng.annotations.AfterGroups;
+import org.testng.annotations.BeforeGroups;
import org.testng.annotations.Test;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
/**
* Basic test cases for Transformer API
*/
-public class TransformerTest {
+public class TransformerTest extends JAXPBaseTest {
/**
* XSLT file serves every test method.
*/
private final static String TEST_XSL = XML_DIR + "cities.xsl";
+
+ /**
+ * Save system property for restoring.
+ */
+ @BeforeGroups (groups = {"readLocalFiles"})
+ public void setFilePermissions() {
+ setPermissions(new FilePermission(TEST_XSL, "read"));
+ }
+
/**
- * This tests if newTransformer(StreamSource) method returns Transformer
+ * Restore the system property.
*/
- @Test
- public void transformer01() {
- try {
+ @AfterGroups (groups = {"readLocalFiles"})
+ public void restoreFilePermissions() {
+ setPermissions();
+ }
+
+ /**
+ * This tests if newTransformer(StreamSource) method returns Transformer.
+ * @throws TransformerConfigurationException If for some reason the
+ * TransformerHandler can not be created.
+ */
+ @Test (groups = {"readLocalFiles"})
+ public void transformer01() throws TransformerConfigurationException {
TransformerFactory tfactory = TransformerFactory.newInstance();
StreamSource streamSource = new StreamSource(
new File(TEST_XSL));
Transformer transformer = tfactory.newTransformer(streamSource);
assertNotNull(transformer);
- } catch (TransformerConfigurationException ex){
- failUnexpected(ex);
- }
}
/**
- * This tests if newTransformer(SAXSource) method returns Transformer
- */
- @Test
- public void transformer02() {
- try {
+ * This tests if newTransformer(SAXSource) method returns Transformer.
+ * @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 TransformerConfigurationException If for some reason the
+ * TransformerHandler can not be created.
+ */
+ @Test (groups = {"readLocalFiles"})
+ public void transformer02() throws IOException, TransformerConfigurationException {
+ try (FileInputStream fis = new FileInputStream(TEST_XSL)) {
TransformerFactory tfactory = TransformerFactory.newInstance();
- InputSource is = new InputSource(
- new FileInputStream(TEST_XSL));
- SAXSource saxSource = new SAXSource(is);
+ SAXSource saxSource = new SAXSource(new InputSource(fis));
Transformer transformer = tfactory.newTransformer(saxSource);
assertNotNull(transformer);
- } catch (TransformerConfigurationException | FileNotFoundException ex){
- failUnexpected(ex);
}
}
/**
- * This tests if newTransformer(DOMSource) method returns Transformer
- */
- @Test
- public void transformer03() {
- try {
+ * This tests if newTransformer(DOMSource) method returns Transformer.
+ *
+ * @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 TransformerConfigurationException If for some reason the
+ * TransformerHandler can not be created.
+ * @throws ParserConfigurationException if a DocumentBuilder cannot be
+ * created which satisfies the configuration requested.
+ */
+ @Test (groups = {"readLocalFiles"})
+ public void transformer03() throws ParserConfigurationException, SAXException,
+ IOException, TransformerConfigurationException {
TransformerFactory tfactory = TransformerFactory.newInstance();
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
DocumentBuilder db = dbf.newDocumentBuilder();
Document document = db.parse(new File(TEST_XSL));
DOMSource domSource = new DOMSource(document);
Transformer transformer = tfactory.newTransformer(domSource);
assertNotNull(transformer);
- } catch (TransformerConfigurationException | IOException
- | ParserConfigurationException | SAXException ex){
- failUnexpected(ex);
- }
}
/**
- * This tests set/get ErrorListener methods of Transformer
- */
- @Test
- public void transformer04() {
- try {
+ * This tests set/get ErrorListener methods of Transformer.
+ *
+ * @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 TransformerConfigurationException If for some reason the
+ * TransformerHandler can not be created.
+ * @throws ParserConfigurationException if a DocumentBuilder cannot be
+ * created which satisfies the configuration requested.
+ */
+ @Test (groups = {"readLocalFiles"})
+ public void transformer04() throws ParserConfigurationException,
+ SAXException, IOException, TransformerConfigurationException {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
DocumentBuilder db = dbf.newDocumentBuilder();
Document document = db.parse(new File(TEST_XSL));
DOMSource domSource = new DOMSource(document);
@@ -127,22 +159,27 @@
Transformer transformer = TransformerFactory.newInstance()
.newTransformer(domSource);
transformer.setErrorListener(new MyErrorListener());
assertNotNull(transformer.getErrorListener());
assertTrue(transformer.getErrorListener() instanceof MyErrorListener);
- } catch (IOException | IllegalArgumentException | ParserConfigurationException
- | TransformerConfigurationException | SAXException ex){
- failUnexpected(ex);
- }
}
/**
- * This tests getOutputProperties() method of Transformer
- */
- @Test
- public void transformer05() {
- try {
+ * This tests getOutputProperties() method of Transformer.
+ *
+ * @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 TransformerConfigurationException If for some reason the
+ * TransformerHandler can not be created.
+ * @throws ParserConfigurationException if a DocumentBuilder cannot be
+ * created which satisfies the configuration requested.
+ */
+ @Test (groups = {"readLocalFiles"})
+ public void transformer05() throws ParserConfigurationException,
+ SAXException, IOException, TransformerConfigurationException {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
DocumentBuilder db = dbf.newDocumentBuilder();
Document document = db.parse(new File(TEST_XSL));
DOMSource domSource = new DOMSource(document);
@@ -155,36 +192,37 @@
assertEquals(prop.getProperty("method"), "xml");
assertEquals(prop.getProperty("encoding"), "UTF-8");
assertEquals(prop.getProperty("standalone"), "no");
assertEquals(prop.getProperty("version"), "1.0");
assertEquals(prop.getProperty("omit-xml-declaration"), "no");
- } catch (ParserConfigurationException | SAXException | IOException
- | TransformerConfigurationException ex){
- failUnexpected(ex);
- }
}
/**
- * This tests getOutputProperty() method of Transformer
- */
- @Test
- public void transformer06() {
- try {
+ * This tests getOutputProperty() method of Transformer.
+ *
+ * @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 TransformerConfigurationException If for some reason the
+ * TransformerHandler can not be created.
+ * @throws ParserConfigurationException if a DocumentBuilder cannot be
+ * created which satisfies the configuration requested.
+ */
+ @Test (groups = {"readLocalFiles"})
+ public void transformer06() throws ParserConfigurationException,
+ SAXException, IOException, TransformerConfigurationException {
TransformerFactory tfactory = TransformerFactory.newInstance();
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
DocumentBuilder db = dbf.newDocumentBuilder();
Document document = db.parse(new File(TEST_XSL));
DOMSource domSource = new DOMSource(document);
Transformer transformer = tfactory.newTransformer(domSource);
assertEquals(transformer.getOutputProperty("method"), "xml");
- } catch (ParserConfigurationException | SAXException | IOException
- | TransformerConfigurationException | IllegalArgumentException ex){
- failUnexpected(ex);
- }
}
}
/**
* Simple ErrorListener print out all exception.
< prev index next >