< prev index next >

test/javax/xml/jaxp/functional/test/auctionportal/AuctionItemRepository.java

Print this page




  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 package test.auctionportal;
  24 
  25 import static com.sun.org.apache.xerces.internal.impl.Constants.SP_ENTITY_EXPANSION_LIMIT;
  26 import static com.sun.org.apache.xerces.internal.impl.Constants.SP_MAX_OCCUR_LIMIT;
  27 import static com.sun.org.apache.xerces.internal.jaxp.JAXPConstants.JAXP_SCHEMA_LANGUAGE;
  28 import static com.sun.org.apache.xerces.internal.jaxp.JAXPConstants.JAXP_SCHEMA_SOURCE;
  29 import static org.testng.Assert.assertTrue;
  30 import java.io.File;
  31 import java.io.FileInputStream;
  32 import java.io.FileOutputStream;

  33 import java.io.IOException;
  34 import java.io.InputStream;
  35 import java.nio.file.Files;
  36 import java.nio.file.Path;
  37 import java.nio.file.Paths;
  38 import static javax.xml.XMLConstants.FEATURE_SECURE_PROCESSING;
  39 import static javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI;
  40 import javax.xml.parsers.DocumentBuilder;
  41 import javax.xml.parsers.DocumentBuilderFactory;
  42 import javax.xml.parsers.ParserConfigurationException;
  43 import javax.xml.parsers.SAXParser;
  44 import javax.xml.parsers.SAXParserFactory;
  45 import javax.xml.transform.TransformerException;
  46 import javax.xml.transform.TransformerFactory;
  47 import javax.xml.transform.dom.DOMSource;
  48 import javax.xml.transform.stream.StreamResult;

  49 import static jaxp.library.JAXPTestUtilities.compareDocumentWithGold;
  50 import static jaxp.library.JAXPTestUtilities.failCleanup;
  51 import static jaxp.library.JAXPTestUtilities.failUnexpected;
  52 import static org.testng.Assert.assertFalse;
  53 

  54 import org.testng.annotations.Test;
  55 import org.w3c.dom.Document;
  56 import org.xml.sax.SAXException;
  57 import org.xml.sax.SAXParseException;
  58 import static test.auctionportal.HiBidConstants.CLASS_DIR;
  59 import static test.auctionportal.HiBidConstants.GOLDEN_DIR;
  60 import static test.auctionportal.HiBidConstants.XML_DIR;
  61 
  62 /**
  63  * This is a test class for the Auction portal HiBid.com.
  64  */
  65 public class AuctionItemRepository {
  66     /**
  67      * XML file for parsing.
  68      */
  69     private final static String ENTITY_XML = XML_DIR + "entity.xml";
  70 
  71     /**
  72      * Feature name.
  73      */
  74     private final static String FEATURE_NAME = "http://xml.org/sax/features/namespace-prefixes";
  75 
  76     /**
  77      * Setting the EntityExpansion Limit to 128000 and checks if the XML
  78      * document that has more than two levels of entity expansion is parsed or
  79      * not. Previous system property was changed to jdk.xml.entityExpansionLimit
  80      * see http://docs.oracle.com/javase/tutorial/jaxp/limits/limits.html.







  81      */
  82     @Test
  83     public void testEntityExpansionSAXPos() {
  84         try {
  85             SAXParserFactory factory = SAXParserFactory.newInstance();
  86             // Secure processing will limit XML processing to conform to
  87             // implementation limits.
  88             factory.setFeature(FEATURE_SECURE_PROCESSING, true);
  89             // Set entityExpansionLimit as 2 should expect fatalError
  90             System.setProperty(SP_ENTITY_EXPANSION_LIMIT, String.valueOf(128000));
  91             SAXParser parser = factory.newSAXParser();
  92 
  93             MyErrorHandler fatalHandler = new MyErrorHandler();

  94             parser.parse(new File(ENTITY_XML), fatalHandler);
  95             assertFalse(fatalHandler.isAnyError());
  96         } catch (ParserConfigurationException | SAXException | IOException e) {
  97             failUnexpected(e);
  98         }
  99     }
 100     /**
 101      * Setting the EntityExpansion Limit to 2 and checks if the XML
 102      * document that has more than two levels of entity expansion is parsed or
 103      * not. Previous system property was changed to jdk.xml.entityExpansionLimit
 104      * see http://docs.oracle.com/javase/tutorial/jaxp/limits/limits.html.







 105      */
 106     @Test(expectedExceptions = SAXParseException.class)
 107     public void testEntityExpansionSAXNeg() throws SAXParseException {
 108         //
 109         try {
 110             SAXParserFactory factory = SAXParserFactory.newInstance();
 111             // Secure processing will limit XML processing to conform to
 112             // implementation limits.
 113             factory.setFeature(FEATURE_SECURE_PROCESSING, true);
 114             // Set entityExpansionLimit as 2 should expect SAXParseException
 115             System.setProperty(SP_ENTITY_EXPANSION_LIMIT, String.valueOf(2));
 116             SAXParser parser = factory.newSAXParser();
 117 

 118             MyErrorHandler fatalHandler = new MyErrorHandler();

 119             parser.parse(new File(ENTITY_XML), fatalHandler);
 120         } catch (SAXParseException e) {
 121             throw e;
 122         } catch (ParserConfigurationException | SAXException | IOException e) {
 123             failUnexpected(e);
 124         }
 125     }
 126 
 127     /**
 128      * Testing set MaxOccursLimit to 10000 in the secure processing enabled for
 129      * SAXParserFactory.







 130      */
 131     @Test
 132     public void testMaxOccurLimitPos() {

 133         String schema_file = XML_DIR + "toys.xsd";
 134         String xml_file = XML_DIR + "toys.xml";
 135 
 136         try (InputStream is = new FileInputStream(xml_file)) {
 137             SAXParserFactory factory = SAXParserFactory.newInstance();
 138             factory.setValidating(true);
 139             factory.setFeature(FEATURE_SECURE_PROCESSING, true);
 140             System.setProperty(SP_MAX_OCCUR_LIMIT, String.valueOf(10000));
 141             SAXParser parser = factory.newSAXParser();
 142             parser.setProperty(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA_NS_URI);

 143             parser.setProperty(JAXP_SCHEMA_SOURCE, new File(schema_file));

 144             MyErrorHandler eh = new MyErrorHandler();
 145             parser.parse(is, eh);
 146             assertFalse(eh.isAnyError());
 147         } catch (ParserConfigurationException | SAXException | IOException e) {
 148             failUnexpected(e);
 149         }
 150     }
 151 
 152     /**
 153      * Use a DocumentBuilder to create a DOM object and see if Secure Processing
 154      * feature affects the entity expansion.







 155      */
 156     @Test
 157     public void testEntityExpansionDOMPos()  {
 158         try {
 159             DocumentBuilderFactory dfactory = DocumentBuilderFactory.newInstance();
 160             dfactory.setFeature(FEATURE_SECURE_PROCESSING, true);
 161             System.setProperty(SP_ENTITY_EXPANSION_LIMIT, String.valueOf(10000));
 162             DocumentBuilder dBuilder = dfactory.newDocumentBuilder();
 163             MyErrorHandler eh = new MyErrorHandler();
 164             dBuilder.setErrorHandler(eh);

 165             dBuilder.parse(ENTITY_XML);
 166             assertFalse(eh.isAnyError());
 167         } catch (ParserConfigurationException | IOException | SAXException e) {
 168             failUnexpected(e);
 169         }
 170     }
 171 
 172     /**
 173      * Use a DocumentBuilder to create a DOM object and see how does the Secure
 174      * Processing feature and entityExpansionLimit value affects output.
 175      * Negative test that when entityExpansionLimit is too small.







 176      */
 177     @Test(expectedExceptions = SAXParseException.class)
 178     public void testEntityExpansionDOMNeg() throws SAXParseException {
 179         try {
 180             DocumentBuilderFactory dfactory = DocumentBuilderFactory.newInstance();
 181             dfactory.setFeature(FEATURE_SECURE_PROCESSING, true);
 182             System.setProperty(SP_ENTITY_EXPANSION_LIMIT, String.valueOf(2));
 183             DocumentBuilder dBuilder = dfactory.newDocumentBuilder();
 184             MyErrorHandler eh = new MyErrorHandler();
 185             dBuilder.setErrorHandler(eh);

 186             dBuilder.parse(ENTITY_XML);
 187         } catch (SAXParseException e) {
 188             throw e;
 189         } catch (ParserConfigurationException | IOException | SAXException e) {
 190             failUnexpected(e);
 191         }










 192     }
 193 
 194     /**
 195      * Test xi:include with a SAXParserFactory.
 196      */
 197     @Test
 198     public void testXIncludeSAXPos() {

















 199         String resultFile = CLASS_DIR + "doc_xinclude.out";
 200         String goldFile = GOLDEN_DIR + "doc_xincludeGold.xml";
 201         String xmlFile = XML_DIR + "doc_xinclude.xml";
 202 
 203         try {
 204             try(FileOutputStream fos = new FileOutputStream(resultFile)) {
 205                 XInclHandler xh = new XInclHandler(fos, null);
 206                 SAXParserFactory spf = SAXParserFactory.newInstance();
 207                 spf.setNamespaceAware(true);
 208                 spf.setXIncludeAware(true);
 209                 spf.setFeature(FEATURE_NAME, true);
 210                 spf.newSAXParser().parse(new File(xmlFile), xh);
 211             }
 212             assertTrue(compareDocumentWithGold(goldFile, resultFile));
 213         } catch (ParserConfigurationException | SAXException | IOException e) {
 214             failUnexpected(e);
 215         } finally {
 216             try {
 217                 Path resultPath = Paths.get(resultFile);
 218                 if (Files.exists(resultPath)) {
 219                     Files.delete(resultPath);
 220                 }
 221             } catch (IOException ex) {
 222                 failCleanup(ex, resultFile);
 223             }
 224         }
 225     }
 226 
 227     /**
 228      * Test the simple case of including a document using xi:include using a
 229      * DocumentBuilder.
 230      */
 231     @Test
 232     public void testXIncludeDOMPos() {










 233         String resultFile = CLASS_DIR + "doc_xincludeDOM.out";
 234         String goldFile = GOLDEN_DIR + "doc_xincludeGold.xml";
 235         String xmlFile = XML_DIR + "doc_xinclude.xml";
 236         try {
 237             try (FileOutputStream fos = new FileOutputStream(resultFile)) {
 238                 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
 239                 dbf.setXIncludeAware(true);
 240                 dbf.setNamespaceAware(true);
 241 
 242                 Document doc = dbf.newDocumentBuilder().parse(new File(xmlFile));
 243                 doc.setXmlStandalone(true);
 244 
 245                 TransformerFactory.newInstance().newTransformer().
 246                         transform(new DOMSource(doc), new StreamResult(fos));
 247             }
 248             assertTrue(compareDocumentWithGold(goldFile, resultFile));
 249         } catch (ParserConfigurationException | SAXException | IOException
 250                 | TransformerException e) {
 251             failUnexpected(e);
 252         } finally {
 253             try {
 254                 Path resultPath = Paths.get(resultFile);
 255                 if (Files.exists(resultPath)) {
 256                     Files.delete(resultPath);
 257                 }
 258             } catch (IOException ex) {
 259                 failCleanup(ex, resultFile);
 260             }
 261         }
 262     }
 263 
 264     /**
 265      * Test the simple case of including a document using xi:include within a
 266      * xi:fallback using a DocumentBuilder.
 267      */
 268     @Test
 269     public void testXIncludeFallbackDOMPos() {










 270         String resultFile = CLASS_DIR + "doc_fallbackDOM.out";
 271         String goldFile = GOLDEN_DIR + "doc_fallbackGold.xml";
 272         String xmlFile = XML_DIR + "doc_fallback.xml";
 273         try{
 274             try (FileOutputStream fos = new FileOutputStream(resultFile)) {
 275                 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
 276                 dbf.setXIncludeAware(true);
 277                 dbf.setNamespaceAware(true);
 278 
 279                 Document doc = dbf.newDocumentBuilder().parse(new File(xmlFile));
 280                 doc.setXmlStandalone(true);
 281                 TransformerFactory.newInstance().newTransformer()
 282                         .transform(new DOMSource(doc), new StreamResult(fos));
 283             }
 284             assertTrue(compareDocumentWithGold(goldFile, resultFile));
 285         } catch (ParserConfigurationException | SAXException | IOException
 286                 | TransformerException e) {
 287             failUnexpected(e);
 288         } finally {
 289             try {
 290                 Path resultPath = Paths.get(resultFile);
 291                 if (Files.exists(resultPath)) {
 292                     Files.delete(resultPath);
 293                 }
 294             } catch (IOException ex) {
 295                 failCleanup(ex, resultFile);
 296             }
 297         }
 298     }
 299 
 300     /**
 301      * Test for xi:fallback where the fall back text is parsed as text. This
 302      * test uses a nested xi:include for the fallback test.
 303      */
 304     @Test
 305     public void testXIncludeFallbackTextPos() {










 306         String resultFile = CLASS_DIR + "doc_fallback_text.out";
 307         String goldFile = GOLDEN_DIR + "doc_fallback_textGold.xml";
 308         String xmlFile = XML_DIR + "doc_fallback_text.xml";
 309 
 310         try{
 311             try (FileOutputStream fos = new FileOutputStream(resultFile)) {
 312                 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
 313                 dbf.setXIncludeAware(true);
 314                 dbf.setNamespaceAware(true);
 315 
 316                 Document doc = dbf.newDocumentBuilder().parse(new File(xmlFile));
 317                 doc.setXmlStandalone(true);
 318                 TransformerFactory.newInstance().newTransformer()
 319                         .transform(new DOMSource(doc), new StreamResult(fos));
 320             }
 321             assertTrue(compareDocumentWithGold(goldFile, resultFile));
 322         } catch (ParserConfigurationException | SAXException | IOException
 323                 | TransformerException e) {
 324             failUnexpected(e);
 325         } finally {
 326             try {
 327                 Path resultPath = Paths.get(resultFile);
 328                 if (Files.exists(resultPath)) {
 329                     Files.delete(resultPath);
 330                 }
 331             } catch (IOException ex) {
 332                 failCleanup(ex, resultFile);
 333             }
 334         }
 335     }
 336 
 337     /**
 338      * Test the XPointer element() framework with XInclude.
 339      */
 340     @Test
 341     public void testXpointerElementPos() {










 342         String resultFile = CLASS_DIR + "doc_xpointer_element.out";
 343         String goldFile = GOLDEN_DIR + "doc_xpointerGold.xml";
 344         String xmlFile = XML_DIR + "doc_xpointer_element.xml";
 345 
 346         try{
 347             try (FileOutputStream fos = new FileOutputStream(resultFile)) {
 348                 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
 349                 dbf.setXIncludeAware(true);
 350                 dbf.setNamespaceAware(true);
 351 
 352                 DocumentBuilder db = dbf.newDocumentBuilder();
 353 
 354                 TransformerFactory.newInstance().newTransformer()
 355                         .transform(new DOMSource(db.parse(new File(xmlFile))),
 356                                 new StreamResult(fos));
 357             }
 358             assertTrue(compareDocumentWithGold(goldFile, resultFile));
 359         } catch (ParserConfigurationException | SAXException | IOException
 360                 | TransformerException e) {
 361             failUnexpected(e);
 362         } finally {
 363             try {
 364                 Path resultPath = Paths.get(resultFile);
 365                 if (Files.exists(resultPath)) {
 366                     Files.delete(resultPath);
 367                 }
 368             } catch (IOException ex) {
 369                 failCleanup(ex, resultFile);
 370             }
 371         }
 372     }
 373 
 374     /**
 375      * Test the XPointer framework with a SAX object.
 376      */
 377     @Test
 378     public void testXPointerPos() {










 379         String resultFile = CLASS_DIR + "doc_xpointer.out";
 380         String goldFile = GOLDEN_DIR + "doc_xpointerGold.xml";
 381         String xmlFile = XML_DIR + "doc_xpointer.xml";
 382 
 383         try{
 384             try (FileOutputStream fos = new FileOutputStream(resultFile)) {
 385                 SAXParserFactory spf = SAXParserFactory.newInstance();
 386                 spf.setNamespaceAware(true);
 387                 spf.setXIncludeAware(true);
 388                 spf.setFeature(FEATURE_NAME, true);
 389                 // parse the file
 390                 spf.newSAXParser().parse(new File(xmlFile), new XInclHandler(fos, null));
 391             }
 392             assertTrue(compareDocumentWithGold(goldFile, resultFile));
 393         } catch (ParserConfigurationException | SAXException | IOException e) {
 394             failUnexpected(e);
 395         } finally {
 396             try {
 397                 Path resultPath = Paths.get(resultFile);
 398                 if (Files.exists(resultPath)) {
 399                     Files.delete(resultPath);
 400                 }
 401             } catch (IOException ex) {
 402                 failCleanup(ex, resultFile);
 403             }
 404         }
 405     }
 406 
 407     /**
 408      * Test if xi:include may reference the doc containing the include if the
 409      * parse type is text.
 410      */
 411     @Test
 412     public void testXIncludeLoopPos() {










 413         String resultFile = CLASS_DIR + "doc_xinc_loops.out";
 414         String goldFile = GOLDEN_DIR + "doc_xinc_loopGold.xml";
 415         String xmlFile = XML_DIR + "doc_xinc_loops.xml";
 416 
 417         try{
 418             try (FileOutputStream fos = new FileOutputStream(resultFile)) {
 419                 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
 420                 dbf.setXIncludeAware(true);
 421                 dbf.setNamespaceAware(true);
 422                 DocumentBuilder db = dbf.newDocumentBuilder();
 423                 Document doc = db.parse(new File(xmlFile));
 424                 doc.normalizeDocument();
 425                 doc.setXmlStandalone(true);
 426 
 427                 TransformerFactory.newInstance().newTransformer()
 428                         .transform(new DOMSource(doc), new StreamResult(fos));
 429             }
 430             assertTrue(compareDocumentWithGold(goldFile, resultFile));
 431         } catch (ParserConfigurationException | SAXException | IOException
 432                 | TransformerException e) {
 433             failUnexpected(e);
 434         } finally {
 435             try {
 436                 Path resultPath = Paths.get(resultFile);
 437                 if (Files.exists(resultPath)) {
 438                     Files.delete(resultPath);
 439                 }
 440             } catch (IOException ex) {
 441                 failCleanup(ex, resultFile);
 442             }
 443         }
 444     }
 445 
 446     /**
 447      * Test if two non nested xi:include elements can include the same document
 448      * with an xi:include statement.
 449      */
 450     @Test
 451     public void testXIncludeNestedPos() {










 452         String resultFile = CLASS_DIR + "schedule.out";
 453         String goldFile = GOLDEN_DIR + "scheduleGold.xml";
 454         String xmlFile = XML_DIR + "schedule.xml";
 455 
 456         try{
 457             try (FileOutputStream fos = new FileOutputStream(resultFile)) {
 458                 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
 459                 dbf.setXIncludeAware(true);
 460                 dbf.setNamespaceAware(true);
 461 
 462                 Document doc = dbf.newDocumentBuilder().parse(new File(xmlFile));
 463                 doc.setXmlStandalone(true);
 464                 TransformerFactory.newInstance().newTransformer()
 465                         .transform(new DOMSource(doc), new StreamResult(fos));
 466             }
 467             assertTrue(compareDocumentWithGold(goldFile, resultFile));
 468         } catch (ParserConfigurationException | SAXException | IOException
 469                 | TransformerException e) {
 470             failUnexpected(e);
 471         } finally {
 472             try {
 473                 Path resultPath = Paths.get(resultFile);
 474                 if (Files.exists(resultPath)) {
 475                     Files.delete(resultPath);
 476                 }
 477             } catch (IOException ex) {
 478                 failCleanup(ex, resultFile);
 479             }
 480         }
 481     }
 482 }


  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 package test.auctionportal;
  24 
  25 import static com.sun.org.apache.xerces.internal.impl.Constants.SP_ENTITY_EXPANSION_LIMIT;
  26 import static com.sun.org.apache.xerces.internal.impl.Constants.SP_MAX_OCCUR_LIMIT;
  27 import static com.sun.org.apache.xerces.internal.jaxp.JAXPConstants.JAXP_SCHEMA_LANGUAGE;
  28 import static com.sun.org.apache.xerces.internal.jaxp.JAXPConstants.JAXP_SCHEMA_SOURCE;
  29 import static org.testng.Assert.assertTrue;
  30 import java.io.File;
  31 import java.io.FileInputStream;
  32 import java.io.FileOutputStream;
  33 import java.io.FilePermission;
  34 import java.io.IOException;
  35 import java.io.InputStream;
  36 import java.io.UnsupportedEncodingException;


  37 import static javax.xml.XMLConstants.FEATURE_SECURE_PROCESSING;
  38 import static javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI;
  39 import javax.xml.parsers.DocumentBuilder;
  40 import javax.xml.parsers.DocumentBuilderFactory;
  41 import javax.xml.parsers.ParserConfigurationException;
  42 import javax.xml.parsers.SAXParser;
  43 import javax.xml.parsers.SAXParserFactory;
  44 import javax.xml.transform.TransformerException;
  45 import javax.xml.transform.TransformerFactory;
  46 import javax.xml.transform.dom.DOMSource;
  47 import javax.xml.transform.stream.StreamResult;
  48 import jaxp.library.JAXPFileBaseTest;
  49 import static jaxp.library.JAXPTestUtilities.compareDocumentWithGold;


  50 import static org.testng.Assert.assertFalse;
  51 import org.testng.annotations.AfterGroups;
  52 import org.testng.annotations.BeforeGroups;
  53 import org.testng.annotations.Test;
  54 import org.w3c.dom.Document;
  55 import org.xml.sax.SAXException;
  56 import org.xml.sax.SAXParseException;
  57 import static test.auctionportal.HiBidConstants.CLASS_DIR;
  58 import static test.auctionportal.HiBidConstants.GOLDEN_DIR;
  59 import static test.auctionportal.HiBidConstants.XML_DIR;
  60 
  61 /**
  62  * This is a test class for the Auction portal HiBid.com.
  63  */
  64 public class AuctionItemRepository extends JAXPFileBaseTest {
  65     /**
  66      * XML file for parsing.
  67      */
  68     private final static String ENTITY_XML = XML_DIR + "entity.xml";
  69 
  70     /**
  71      * Feature name.
  72      */
  73     private final static String FEATURE_NAME = "http://xml.org/sax/features/namespace-prefixes";
  74 
  75     /**
  76      * Setting the EntityExpansion Limit to 128000 and checks if the XML
  77      * document that has more than two levels of entity expansion is parsed or
  78      * not. Previous system property was changed to jdk.xml.entityExpansionLimit
  79      * see http://docs.oracle.com/javase/tutorial/jaxp/limits/limits.html.
  80      * 
  81      * @throws ParserConfigurationException if a DocumentBuilder cannot be 
  82      *         created which satisfies the configuration requested.
  83      * @throws SAXException If any parse errors occur.
  84      * @throws IOException if the file exists but is a directory rather than
  85      *         a regular file, does not exist but cannot be created, or cannot 
  86      *         be opened for any other reason.
  87      */
  88     @Test
  89     public void testEntityExpansionSAXPos() throws ParserConfigurationException,
  90             SAXException, IOException { 
  91         SAXParserFactory factory = SAXParserFactory.newInstance();
  92         // Secure processing will limit XML processing to conform to
  93         // implementation limits.
  94         factory.setFeature(FEATURE_SECURE_PROCESSING, true);
  95         // Set entityExpansionLimit as 2 should expect fatalError
  96         setSystemProperty(SP_ENTITY_EXPANSION_LIMIT, String.valueOf(128000));
  97         SAXParser parser = factory.newSAXParser();
  98 
  99         MyErrorHandler fatalHandler = new MyErrorHandler();
 100         setPermissions(new FilePermission(ENTITY_XML, "read"));
 101         parser.parse(new File(ENTITY_XML), fatalHandler);
 102         assertFalse(fatalHandler.isAnyError());



 103     }
 104     /**
 105      * Setting the EntityExpansion Limit to 2 and checks if the XML
 106      * document that has more than two levels of entity expansion is parsed or
 107      * not. Previous system property was changed to jdk.xml.entityExpansionLimit
 108      * see http://docs.oracle.com/javase/tutorial/jaxp/limits/limits.html.
 109      * 
 110      * @throws ParserConfigurationException if a DocumentBuilder cannot be 
 111      *         created which satisfies the configuration requested.
 112      * @throws SAXException If any parse errors occur.
 113      * @throws IOException if the file exists but is a directory rather than
 114      *         a regular file, does not exist but cannot be created, or cannot 
 115      *         be opened for any other reason.
 116      */
 117     @Test(expectedExceptions = SAXParseException.class)
 118     public void testEntityExpansionSAXNeg() throws ParserConfigurationException, 
 119             SAXException, IOException {

 120         SAXParserFactory factory = SAXParserFactory.newInstance();
 121         // Secure processing will limit XML processing to conform to
 122         // implementation limits.
 123         factory.setFeature(FEATURE_SECURE_PROCESSING, true);
 124         // Set entityExpansionLimit as 2 should expect SAXParseException.
 125         setSystemProperty(SP_ENTITY_EXPANSION_LIMIT, String.valueOf(2)); 

 126 
 127         SAXParser parser = factory.newSAXParser();
 128         MyErrorHandler fatalHandler = new MyErrorHandler();
 129         setPermissions(new FilePermission(ENTITY_XML, "read"));
 130         parser.parse(new File(ENTITY_XML), fatalHandler);





 131     }
 132 
 133     /**
 134      * Testing set MaxOccursLimit to 10000 in the secure processing enabled for
 135      * SAXParserFactory.
 136      * 
 137      * @throws ParserConfigurationException if a DocumentBuilder cannot be 
 138      *         created which satisfies the configuration requested.
 139      * @throws SAXException If any parse errors occur.
 140      * @throws IOException if the file exists but is a directory rather than
 141      *         a regular file, does not exist but cannot be created, or cannot 
 142      *         be opened for any other reason.
 143      */
 144     @Test
 145     public void testMaxOccurLimitPos() throws ParserConfigurationException, 
 146             SAXException, IOException {
 147         String schema_file = XML_DIR + "toys.xsd";
 148         String xml_file = XML_DIR + "toys.xml";


 149         SAXParserFactory factory = SAXParserFactory.newInstance();
 150         factory.setValidating(true);
 151         factory.setFeature(FEATURE_SECURE_PROCESSING, true);
 152         setSystemProperty(SP_MAX_OCCUR_LIMIT, String.valueOf(10000));
 153         SAXParser parser = factory.newSAXParser();
 154         parser.setProperty(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA_NS_URI);
 155         setPermissions(new FilePermission(XML_DIR + "-", "read"));
 156         parser.setProperty(JAXP_SCHEMA_SOURCE, new File(schema_file));
 157         try (InputStream is = new FileInputStream(xml_file)) {
 158             MyErrorHandler eh = new MyErrorHandler();
 159             parser.parse(is, eh);
 160             assertFalse(eh.isAnyError());


 161         }
 162     }
 163 
 164     /**
 165      * Use a DocumentBuilder to create a DOM object and see if Secure Processing
 166      * feature affects the entity expansion.
 167      * 
 168      * @throws ParserConfigurationException if a DocumentBuilder cannot be 
 169      *         created which satisfies the configuration requested.
 170      * @throws SAXException If any parse errors occur.
 171      * @throws IOException if the file exists but is a directory rather than
 172      *         a regular file, does not exist but cannot be created, or cannot 
 173      *         be opened for any other reason.
 174      */
 175     @Test
 176     public void testEntityExpansionDOMPos() throws ParserConfigurationException,
 177             SAXException, IOException  {
 178         DocumentBuilderFactory dfactory = DocumentBuilderFactory.newInstance();
 179         dfactory.setFeature(FEATURE_SECURE_PROCESSING, true);
 180         setSystemProperty(SP_ENTITY_EXPANSION_LIMIT, String.valueOf(10000));
 181         DocumentBuilder dBuilder = dfactory.newDocumentBuilder();
 182         MyErrorHandler eh = new MyErrorHandler();
 183         dBuilder.setErrorHandler(eh);
 184         setPermissions(new FilePermission(ENTITY_XML, "read"));
 185         dBuilder.parse(ENTITY_XML);
 186         assertFalse(eh.isAnyError());



 187     }
 188 
 189     /**
 190      * Use a DocumentBuilder to create a DOM object and see how does the Secure
 191      * Processing feature and entityExpansionLimit value affects output.
 192      * Negative test that when entityExpansionLimit is too small.
 193      * 
 194      * @throws ParserConfigurationException if a DocumentBuilder cannot be 
 195      *         created which satisfies the configuration requested.
 196      * @throws SAXException If any parse errors occur.
 197      * @throws IOException if the file exists but is a directory rather than
 198      *         a regular file, does not exist but cannot be created, or cannot 
 199      *         be opened for any other reason.
 200      */
 201     @Test(expectedExceptions = SAXParseException.class)
 202     public void testEntityExpansionDOMNeg() throws ParserConfigurationException,
 203             SAXException, IOException {
 204         DocumentBuilderFactory dfactory = DocumentBuilderFactory.newInstance();
 205         dfactory.setFeature(FEATURE_SECURE_PROCESSING, true);
 206         setSystemProperty(SP_ENTITY_EXPANSION_LIMIT, String.valueOf(2));
 207         DocumentBuilder dBuilder = dfactory.newDocumentBuilder();
 208         MyErrorHandler eh = new MyErrorHandler();
 209         dBuilder.setErrorHandler(eh);
 210         setPermissions(new FilePermission(ENTITY_XML, "read"));
 211         dBuilder.parse(ENTITY_XML);




 212     }
 213 
 214     
 215     /**
 216      * Save system property for restoring.
 217      */
 218     @BeforeGroups (groups = {"readWriteLocalFiles"})
 219     public void setFilePermissions() {
 220         setPermissions(new FilePermission(XML_DIR + "/-", "read"),
 221                 new FilePermission(GOLDEN_DIR + "/-", "read"),
 222                 new FilePermission(CLASS_DIR + "/-", "read, write"));
 223     }
 224     
 225     /**
 226      * Restore the system property.
 227      */
 228     @AfterGroups (groups = {"readWriteLocalFiles"})
 229     public void restoreFilePermissions() {
 230         setPermissions();
 231     }
 232     /**
 233      * Test xi:include with a SAXParserFactory.
 234      * 
 235      * @throws UnsupportedEncodingException if given encoding is an invalid
 236      *         encoding name.
 237      * @throws ParserConfigurationException if a DocumentBuilder cannot be 
 238      *         created which satisfies the configuration requested.
 239      * @throws SAXException If any parse errors occur.
 240      * @throws IOException if the file exists but is a directory rather than
 241      *         a regular file, does not exist but cannot be created, or cannot 
 242      *         be opened for any other reason.
 243      */
 244     @Test(groups = {"readWriteLocalFiles"})
 245     public void testXIncludeSAXPos() throws UnsupportedEncodingException, 
 246             ParserConfigurationException, SAXException, IOException {
 247         String resultFile = CLASS_DIR + "doc_xinclude.out";
 248         String goldFile = GOLDEN_DIR + "doc_xincludeGold.xml";
 249         String xmlFile = XML_DIR + "doc_xinclude.xml";
 250         

 251         try(FileOutputStream fos = new FileOutputStream(resultFile)) {
 252             XInclHandler xh = new XInclHandler(fos, null);
 253             SAXParserFactory spf = SAXParserFactory.newInstance();
 254             spf.setNamespaceAware(true);
 255             spf.setXIncludeAware(true);
 256             spf.setFeature(FEATURE_NAME, true);
 257             spf.newSAXParser().parse(new File(xmlFile), xh);
 258         }
 259         assertTrue(compareDocumentWithGold(goldFile, resultFile));












 260     }
 261 
 262     /**
 263      * Test the simple case of including a document using xi:include using a
 264      * DocumentBuilder.
 265      * 
 266      * @throws ParserConfigurationException if a DocumentBuilder cannot be 
 267      *         created which satisfies the configuration requested.
 268      * @throws SAXException If any parse errors occur.
 269      * @throws IOException if the file exists but is a directory rather than
 270      *         a regular file, does not exist but cannot be created, or cannot 
 271      *         be opened for any other reason.
 272      * @throws TransformerException If an unrecoverable error occurs
 273      *         during the course of the transformation.
 274      */
 275     @Test(groups = {"readWriteLocalFiles"})
 276     public void testXIncludeDOMPos() throws ParserConfigurationException, 
 277             SAXException, IOException, TransformerException {
 278         String resultFile = CLASS_DIR + "doc_xincludeDOM.out";
 279         String goldFile = GOLDEN_DIR + "doc_xincludeGold.xml";
 280         String xmlFile = XML_DIR + "doc_xinclude.xml";

 281         try (FileOutputStream fos = new FileOutputStream(resultFile)) {
 282             DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
 283             dbf.setXIncludeAware(true);
 284             dbf.setNamespaceAware(true);

 285             Document doc = dbf.newDocumentBuilder().parse(new File(xmlFile));
 286             doc.setXmlStandalone(true);

 287             TransformerFactory.newInstance().newTransformer().
 288                     transform(new DOMSource(doc), new StreamResult(fos));
 289         }
 290         assertTrue(compareDocumentWithGold(goldFile, resultFile));













 291     }
 292 
 293     /**
 294      * Test the simple case of including a document using xi:include within a
 295      * xi:fallback using a DocumentBuilder.
 296      * 
 297      * @throws ParserConfigurationException if a DocumentBuilder cannot be 
 298      *         created which satisfies the configuration requested.
 299      * @throws SAXException If any parse errors occur.
 300      * @throws IOException if the file exists but is a directory rather than
 301      *         a regular file, does not exist but cannot be created, or cannot 
 302      *         be opened for any other reason.
 303      * @throws TransformerException If an unrecoverable error occurs
 304      *         during the course of the transformation.
 305      */
 306     @Test(groups = {"readWriteLocalFiles"})
 307     public void testXIncludeFallbackDOMPos() throws ParserConfigurationException, 
 308             SAXException, IOException, TransformerException {
 309         String resultFile = CLASS_DIR + "doc_fallbackDOM.out";
 310         String goldFile = GOLDEN_DIR + "doc_fallbackGold.xml";
 311         String xmlFile = XML_DIR + "doc_fallback.xml";

 312         try (FileOutputStream fos = new FileOutputStream(resultFile)) {
 313             DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
 314             dbf.setXIncludeAware(true);
 315             dbf.setNamespaceAware(true);
 316 
 317             Document doc = dbf.newDocumentBuilder().parse(new File(xmlFile));
 318             doc.setXmlStandalone(true);
 319             TransformerFactory.newInstance().newTransformer()
 320                     .transform(new DOMSource(doc), new StreamResult(fos));
 321         }
 322         assertTrue(compareDocumentWithGold(goldFile, resultFile));













 323     }
 324 
 325     /**
 326      * Test for xi:fallback where the fall back text is parsed as text. This
 327      * test uses a nested xi:include for the fallback test.
 328      * 
 329      * @throws ParserConfigurationException if a DocumentBuilder cannot be 
 330      *         created which satisfies the configuration requested.
 331      * @throws SAXException If any parse errors occur.
 332      * @throws IOException if the file exists but is a directory rather than
 333      *         a regular file, does not exist but cannot be created, or cannot 
 334      *         be opened for any other reason.
 335      * @throws TransformerException If an unrecoverable error occurs
 336      *         during the course of the transformation.
 337      */
 338     @Test(groups = {"readWriteLocalFiles"})
 339     public void testXIncludeFallbackTextPos() throws ParserConfigurationException, 
 340             SAXException, IOException, TransformerException {
 341         String resultFile = CLASS_DIR + "doc_fallback_text.out";
 342         String goldFile = GOLDEN_DIR + "doc_fallback_textGold.xml";
 343         String xmlFile = XML_DIR + "doc_fallback_text.xml";


 344         try (FileOutputStream fos = new FileOutputStream(resultFile)) {
 345             DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
 346             dbf.setXIncludeAware(true);
 347             dbf.setNamespaceAware(true);
 348 
 349             Document doc = dbf.newDocumentBuilder().parse(new File(xmlFile));
 350             doc.setXmlStandalone(true);
 351             TransformerFactory.newInstance().newTransformer()
 352                     .transform(new DOMSource(doc), new StreamResult(fos));
 353         }
 354         assertTrue(compareDocumentWithGold(goldFile, resultFile));













 355     }
 356 
 357     /**
 358      * Test the XPointer element() framework with XInclude.
 359      * 
 360      * @throws ParserConfigurationException if a DocumentBuilder cannot be 
 361      *         created which satisfies the configuration requested.
 362      * @throws SAXException If any parse errors occur.
 363      * @throws IOException if the file exists but is a directory rather than
 364      *         a regular file, does not exist but cannot be created, or cannot 
 365      *         be opened for any other reason.
 366      * @throws TransformerException If an unrecoverable error occurs
 367      *         during the course of the transformation.
 368      */
 369     @Test(groups = {"readWriteLocalFiles"})
 370     public void testXpointerElementPos() throws ParserConfigurationException, 
 371             TransformerException, SAXException, IOException {
 372         String resultFile = CLASS_DIR + "doc_xpointer_element.out";
 373         String goldFile = GOLDEN_DIR + "doc_xpointerGold.xml";
 374         String xmlFile = XML_DIR + "doc_xpointer_element.xml";


 375         try (FileOutputStream fos = new FileOutputStream(resultFile)) {
 376             DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
 377             dbf.setXIncludeAware(true);
 378             dbf.setNamespaceAware(true);
 379 
 380             DocumentBuilder db = dbf.newDocumentBuilder();
 381 
 382             TransformerFactory.newInstance().newTransformer()
 383                     .transform(new DOMSource(db.parse(new File(xmlFile))),
 384                             new StreamResult(fos));
 385         }
 386         assertTrue(compareDocumentWithGold(goldFile, resultFile));













 387     }
 388 
 389     /**
 390      * Test the XPointer framework with a SAX object.
 391      * 
 392      * @throws ParserConfigurationException if a DocumentBuilder cannot be 
 393      *         created which satisfies the configuration requested.
 394      * @throws SAXException If any parse errors occur.
 395      * @throws IOException if the file exists but is a directory rather than
 396      *         a regular file, does not exist but cannot be created, or cannot 
 397      *         be opened for any other reason.
 398      * @throws TransformerException If an unrecoverable error occurs
 399      *         during the course of the transformation.
 400      */
 401     @Test(groups = {"readWriteLocalFiles"})
 402     public void testXPointerPos() throws ParserConfigurationException, 
 403             TransformerException, SAXException, IOException {
 404         String resultFile = CLASS_DIR + "doc_xpointer.out";
 405         String goldFile = GOLDEN_DIR + "doc_xpointerGold.xml";
 406         String xmlFile = XML_DIR + "doc_xpointer.xml";
 407   

 408         try (FileOutputStream fos = new FileOutputStream(resultFile)) {
 409             SAXParserFactory spf = SAXParserFactory.newInstance();
 410             spf.setNamespaceAware(true);
 411             spf.setXIncludeAware(true);
 412             spf.setFeature(FEATURE_NAME, true);
 413             // parse the file
 414             spf.newSAXParser().parse(new File(xmlFile), new XInclHandler(fos, null));
 415         }
 416         assertTrue(compareDocumentWithGold(goldFile, resultFile));












 417     }
 418 
 419     /**
 420      * Test if xi:include may reference the doc containing the include if the
 421      * parse type is text.
 422      * 
 423      * @throws ParserConfigurationException if a DocumentBuilder cannot be 
 424      *         created which satisfies the configuration requested.
 425      * @throws SAXException If any parse errors occur.
 426      * @throws IOException if the file exists but is a directory rather than
 427      *         a regular file, does not exist but cannot be created, or cannot 
 428      *         be opened for any other reason.
 429      * @throws TransformerException If an unrecoverable error occurs
 430      *         during the course of the transformation.
 431      */
 432     @Test(groups = {"readWriteLocalFiles"})
 433     public void testXIncludeLoopPos() throws ParserConfigurationException, 
 434             TransformerException, SAXException, IOException {
 435         String resultFile = CLASS_DIR + "doc_xinc_loops.out";
 436         String goldFile = GOLDEN_DIR + "doc_xinc_loopGold.xml";
 437         String xmlFile = XML_DIR + "doc_xinc_loops.xml";
 438 

 439         try (FileOutputStream fos = new FileOutputStream(resultFile)) {
 440             DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
 441             dbf.setXIncludeAware(true);
 442             dbf.setNamespaceAware(true);
 443             DocumentBuilder db = dbf.newDocumentBuilder();
 444             Document doc = db.parse(new File(xmlFile));
 445             doc.normalizeDocument();
 446             doc.setXmlStandalone(true);
 447 
 448             TransformerFactory.newInstance().newTransformer()
 449                     .transform(new DOMSource(doc), new StreamResult(fos));
 450         }
 451         assertTrue(compareDocumentWithGold(goldFile, resultFile));













 452     }
 453 
 454     /**
 455      * Test if two non nested xi:include elements can include the same document
 456      * with an xi:include statement.
 457      * 
 458      * @throws ParserConfigurationException if a DocumentBuilder cannot be 
 459      *         created which satisfies the configuration requested.
 460      * @throws SAXException If any parse errors occur.
 461      * @throws IOException if the file exists but is a directory rather than
 462      *         a regular file, does not exist but cannot be created, or cannot 
 463      *         be opened for any other reason.
 464      * @throws TransformerException If an unrecoverable error occurs
 465      *         during the course of the transformation.
 466      */
 467     @Test(groups = {"readWriteLocalFiles"})
 468     public void testXIncludeNestedPos() throws ParserConfigurationException, 
 469             TransformerException, SAXException, IOException {
 470         String resultFile = CLASS_DIR + "schedule.out";
 471         String goldFile = GOLDEN_DIR + "scheduleGold.xml";
 472         String xmlFile = XML_DIR + "schedule.xml";
 473         

 474         try (FileOutputStream fos = new FileOutputStream(resultFile)) {
 475             DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
 476             dbf.setXIncludeAware(true);
 477             dbf.setNamespaceAware(true);
 478 
 479             Document doc = dbf.newDocumentBuilder().parse(new File(xmlFile));
 480             doc.setXmlStandalone(true);
 481             TransformerFactory.newInstance().newTransformer()
 482                     .transform(new DOMSource(doc), new StreamResult(fos));
 483         }
 484         assertTrue(compareDocumentWithGold(goldFile, resultFile));













 485     }
 486 }
< prev index next >