< prev index next >

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

Print this page


   1 /*
   2  * Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  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 test.auctionportal.HiBidConstants.JAXP_SCHEMA_LANGUAGE;
  26 import static test.auctionportal.HiBidConstants.JAXP_SCHEMA_SOURCE;
  27 import static org.testng.Assert.assertEquals;
  28 import static org.testng.Assert.assertFalse;
  29 import static org.testng.Assert.assertTrue;





  30 import java.io.File;
  31 import java.io.FileInputStream;
  32 import java.io.InputStream;
  33 import java.math.BigInteger;
  34 import java.nio.file.Paths;
  35 import java.util.GregorianCalendar;
  36 import static javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI;
  37 import javax.xml.datatype.DatatypeConstants;
  38 import javax.xml.datatype.DatatypeFactory;
  39 import javax.xml.datatype.Duration;
  40 import javax.xml.parsers.DocumentBuilder;
  41 import javax.xml.parsers.DocumentBuilderFactory;
  42 import javax.xml.parsers.SAXParser;
  43 import javax.xml.parsers.SAXParserFactory;
  44 import javax.xml.transform.dom.DOMResult;
  45 import javax.xml.transform.dom.DOMSource;
  46 import javax.xml.validation.Schema;
  47 import javax.xml.validation.SchemaFactory;
  48 import javax.xml.validation.Validator;
  49 import jaxp.library.JAXPFileReadOnlyBaseTest;
  50 import static jaxp.library.JAXPTestUtilities.bomStream;
  51 import org.testng.annotations.Test;
  52 import org.w3c.dom.Attr;
  53 import org.w3c.dom.DOMConfiguration;
  54 import org.w3c.dom.Document;
  55 import org.w3c.dom.Element;
  56 import org.w3c.dom.NodeList;
  57 import org.w3c.dom.TypeInfo;
  58 import org.w3c.dom.bootstrap.DOMImplementationRegistry;
  59 import org.w3c.dom.ls.DOMImplementationLS;
  60 import org.w3c.dom.ls.LSSerializer;
  61 import static test.auctionportal.HiBidConstants.PORTAL_ACCOUNT_NS;
  62 import static test.auctionportal.HiBidConstants.XML_DIR;
  63 
  64 /**
  65  * This is the user controller  class for the Auction portal HiBid.com.
  66  */
  67 public class AuctionController extends JAXPFileReadOnlyBaseTest {

  68     /**
  69      * Check for DOMErrorHandler handling DOMError. Before fix of bug 4890927
  70      * DOMConfiguration.setParameter("well-formed",true) throws an exception.
  71      *
  72      * @throws Exception If any errors occur.
  73      */
  74     @Test(groups = {"readLocalFiles"})
  75     public void testCreateNewItem2Sell() throws Exception {
  76         String xmlFile = XML_DIR + "novelsInvalid.xml";
  77 
  78         Document document = DocumentBuilderFactory.newInstance()
  79                 .newDocumentBuilder().parse(xmlFile);
  80 
  81         document.getDomConfig().setParameter("well-formed", true);
  82 
  83         DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
  84         DOMImplementationLS impl = (DOMImplementationLS) registry.getDOMImplementation("LS");
  85         MyDOMOutput domOutput = new MyDOMOutput();
  86         domOutput.setByteStream(System.out);
  87         LSSerializer writer = impl.createLSSerializer();
  88         writer.write(document, domOutput);
  89     }
  90 
  91     /**
  92      * Check for DOMErrorHandler handling DOMError. Before fix of bug 4896132
  93      * test throws DOM Level 1 node error.
  94      *
  95      * @throws Exception If any errors occur.
  96      */
  97     @Test(groups = {"readLocalFiles"})
  98     public void testCreateNewItem2SellRetry() throws Exception  {
  99         String xmlFile = XML_DIR + "accountInfo.xml";
 100 
 101         DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
 102         dbf.setNamespaceAware(true);
 103         Document document = dbf.newDocumentBuilder().parse(xmlFile);
 104 
 105         DOMConfiguration domConfig = document.getDomConfig();
 106         MyDOMErrorHandler errHandler = new MyDOMErrorHandler();
 107         domConfig.setParameter("error-handler", errHandler);
 108 
 109         DOMImplementationLS impl =
 110              (DOMImplementationLS) DOMImplementationRegistry.newInstance()
 111                      .getDOMImplementation("LS");
 112         LSSerializer writer = impl.createLSSerializer();
 113         MyDOMOutput domoutput = new MyDOMOutput();
 114 
 115         domoutput.setByteStream(System.out);
 116         writer.write(document, domoutput);
 117 
 118         document.normalizeDocument();
 119         writer.write(document, domoutput);
 120         assertFalse(errHandler.isError());
 121     }
 122 
 123     /**
 124      * Check if setting the attribute to be of type ID works. This will affect
 125      * the Attr.isID method according to the spec.
 126      *
 127      * @throws Exception If any errors occur.
 128      */
 129     @Test(groups = {"readLocalFiles"})
 130     public void testCreateID() throws Exception {
 131         String xmlFile = XML_DIR + "accountInfo.xml";
 132 
 133         DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
 134         dbf.setNamespaceAware(true);
 135 
 136         Document document = dbf.newDocumentBuilder().parse(xmlFile);
 137         Element account = (Element)document
 138             .getElementsByTagNameNS(PORTAL_ACCOUNT_NS, "Account").item(0);
 139 
 140         account.setIdAttributeNS(PORTAL_ACCOUNT_NS, "accountID", true);
 141         Attr aID = account.getAttributeNodeNS(PORTAL_ACCOUNT_NS, "accountID");
 142         assertTrue(aID.isId());
 143     }
 144 
 145     /**
 146      * Check the user data on the node.
 147      *
 148      * @throws Exception If any errors occur.
 149      */
 150     @Test(groups = {"readLocalFiles"})
 151     public void testCheckingUserData() throws Exception {
 152         String xmlFile = XML_DIR + "accountInfo.xml";
 153 
 154         DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
 155         dbf.setNamespaceAware(true);
 156 
 157         DocumentBuilder docBuilder = dbf.newDocumentBuilder();
 158         Document document = docBuilder.parse(xmlFile);
 159 
 160         Element account = (Element)document.getElementsByTagNameNS(PORTAL_ACCOUNT_NS, "Account").item(0);
 161         assertEquals(account.getNodeName(), "acc:Account");
 162         Element firstName = (Element) document.getElementsByTagNameNS(PORTAL_ACCOUNT_NS, "FirstName").item(0);
 163         assertEquals(firstName.getNodeName(), "FirstName");
 164 
 165         Document doc1 = docBuilder.newDocument();
 166         Element someName = doc1.createElement("newelem");
 167 
 168         someName.setUserData("mykey", "dd",
 169             (operation, key,  data, src,  dst) ->  {
 170                 System.err.println("In UserDataHandler" + key);
 171                 System.out.println("In UserDataHandler");
 172             });
 173         Element impAccount = (Element)document.importNode(someName, true);
 174         assertEquals(impAccount.getNodeName(), "newelem");
 175         document.normalizeDocument();
 176         String data = (someName.getUserData("mykey")).toString();
 177         assertEquals(data, "dd");
 178     }
 179 
 180 
 181     /**
 182      * Check the UTF-16 XMLEncoding xml file.
 183      *
 184      * @throws Exception If any errors occur.
 185      * @see <a href="content/movies.xml">movies.xml</a>
 186      */
 187     @Test(groups = {"readLocalFiles"})
 188     public void testCheckingEncoding() throws Exception {
 189         // Note since movies.xml is UTF-16 encoding. We're not using stanard XML
 190         // file suffix.
 191         String xmlFile = XML_DIR + "movies.xml.data";
 192 
 193         try (InputStream source = bomStream("UTF-16", xmlFile)) {
 194             DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
 195             dbf.setNamespaceAware(true);
 196             Document document = dbf.newDocumentBuilder().parse(source);
 197             assertEquals(document.getXmlEncoding(), "UTF-16");
 198             assertEquals(document.getXmlStandalone(), true);
 199         }
 200     }
 201 
 202     /**
 203      * Check validation API features. A schema which is including in Bug 4909119
 204      * used to be testing for the functionalities.
 205      *
 206      * @throws Exception If any errors occur.
 207      * @see <a href="content/userDetails.xsd">userDetails.xsd</a>
 208      */
 209     @Test(groups = {"readLocalFiles"})
 210     public void testGetOwnerInfo() throws Exception {
 211         String schemaFile = XML_DIR + "userDetails.xsd";
 212         String xmlFile = XML_DIR + "userDetails.xml";
 213 
 214         try(FileInputStream fis = new FileInputStream(xmlFile)) {
 215             DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
 216             dbf.setNamespaceAware(true);
 217             dbf.setAttribute(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA_NS_URI);
 218 
 219             SchemaFactory schemaFactory = SchemaFactory.newInstance(W3C_XML_SCHEMA_NS_URI);
 220             Schema schema = schemaFactory.newSchema(Paths.get(schemaFile).toFile());
 221 
 222             Validator validator = schema.newValidator();
 223             MyErrorHandler eh = new MyErrorHandler();
 224             validator.setErrorHandler(eh);
 225 
 226             DocumentBuilder docBuilder = dbf.newDocumentBuilder();
 227             docBuilder.setErrorHandler(eh);
 228 
 229             Document document = docBuilder.parse(fis);
 230             DOMResult dResult = new DOMResult();
 231             DOMSource domSource = new DOMSource(document);
 232             validator.validate(domSource, dResult);
 233             assertFalse(eh.isAnyError());
 234         }
 235     }
 236 
 237     /**
 238      * Check grammar caching with imported schemas.
 239      *
 240      * @throws Exception If any errors occur.
 241      * @see <a href="content/coins.xsd">coins.xsd</a>
 242      * @see <a href="content/coinsImportMe.xsd">coinsImportMe.xsd</a>
 243      */
 244     @Test(groups = {"readLocalFiles"})
 245     public void testGetOwnerItemList() throws Exception {
 246         String xsdFile = XML_DIR + "coins.xsd";
 247         String xmlFile = XML_DIR + "coins.xml";
 248 
 249         try(FileInputStream fis = new FileInputStream(xmlFile)) {
 250             DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
 251             dbf.setNamespaceAware(true);
 252             dbf.setAttribute(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA_NS_URI);
 253             dbf.setValidating(false);
 254 
 255             SchemaFactory schemaFactory = SchemaFactory.newInstance(W3C_XML_SCHEMA_NS_URI);
 256             Schema schema = schemaFactory.newSchema(new File(((xsdFile))));
 257 
 258             MyErrorHandler eh = new MyErrorHandler();
 259             Validator validator = schema.newValidator();
 260             validator.setErrorHandler(eh);
 261 
 262             DocumentBuilder docBuilder = dbf.newDocumentBuilder();
 263             Document document = docBuilder.parse(fis);
 264             validator.validate(new DOMSource(document), new DOMResult());
 265             assertFalse(eh.isAnyError());
 266         }
 267     }
 268 
 269 
 270     /**
 271      * Check for the same imported schemas but will use SAXParserFactory and try
 272      * parsing using the SAXParser. SCHEMA_SOURCE attribute is using for this
 273      * test.
 274      *
 275      * @throws Exception If any errors occur.
 276      * @see <a href="content/coins.xsd">coins.xsd</a>
 277      * @see <a href="content/coinsImportMe.xsd">coinsImportMe.xsd</a>
 278      */
 279 
 280     @Test(groups = {"readLocalFiles"})
 281     public void testGetOwnerItemList1() throws Exception {
 282         String xsdFile = XML_DIR + "coins.xsd";
 283         String xmlFile = XML_DIR + "coins.xml";
 284         SAXParserFactory spf = SAXParserFactory.newInstance();
 285         spf.setNamespaceAware(true);
 286         spf.setValidating(true);
 287 
 288         SAXParser sp = spf.newSAXParser();
 289         sp.setProperty(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA_NS_URI);
 290         sp.setProperty(JAXP_SCHEMA_SOURCE, xsdFile);
 291 
 292         MyErrorHandler eh = new MyErrorHandler();
 293         sp.parse(new File(xmlFile), eh);
 294         assertFalse(eh.isAnyError());
 295     }
 296 
 297     /**
 298      * Check usage of javax.xml.datatype.Duration class.
 299      *
 300      * @throws Exception If any errors occur.
 301      */
 302     @Test(groups = {"readLocalFiles"})
 303     public void testGetItemDuration() throws Exception {
 304         String xmlFile = XML_DIR + "itemsDuration.xml";
 305 
 306         DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
 307         dbf.setNamespaceAware(true);
 308         Document document = dbf.newDocumentBuilder().parse(xmlFile);
 309 
 310         Element durationElement = (Element) document.getElementsByTagName("sellDuration").item(0);
 311 
 312         NodeList childList = durationElement.getChildNodes();
 313 
 314         for (int i = 0; i < childList.getLength(); i++) {
 315             System.out.println("child " + i + childList.item(i));
 316         }
 317 
 318         Duration duration = DatatypeFactory.newInstance().newDuration("P365D");
 319         Duration sellDuration = DatatypeFactory.newInstance().newDuration(childList.item(0).getNodeValue());
 320         assertFalse(sellDuration.isShorterThan(duration));
 321         assertFalse(sellDuration.isLongerThan(duration));
 322         assertEquals(sellDuration.getField(DatatypeConstants.DAYS), BigInteger.valueOf(365));
 323         assertEquals(sellDuration.normalizeWith(new GregorianCalendar(1999, 2, 22)), duration);
 324 
 325         Duration myDuration = sellDuration.add(duration);
 326         assertEquals(myDuration.normalizeWith(new GregorianCalendar(2003, 2, 22)),
 327                 DatatypeFactory.newInstance().newDuration("P730D"));
 328     }
 329 
 330     /**
 331      * Check usage of TypeInfo interface introduced in DOM L3.
 332      *
 333      * @throws Exception If any errors occur.
 334      */
 335     @Test(groups = {"readLocalFiles"})
 336     public void testGetTypeInfo() throws Exception {
 337         String xmlFile = XML_DIR + "accountInfo.xml";
 338 
 339         DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
 340         dbf.setNamespaceAware(true);
 341         dbf.setValidating(true);
 342         dbf.setAttribute(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA_NS_URI);
 343 
 344         DocumentBuilder docBuilder = dbf.newDocumentBuilder();
 345         docBuilder.setErrorHandler(new MyErrorHandler());
 346 
 347         Document document = docBuilder.parse(xmlFile);
 348         Element userId = (Element)document.getElementsByTagNameNS(PORTAL_ACCOUNT_NS, "UserID").item(0);
 349         TypeInfo typeInfo = userId.getSchemaTypeInfo();
 350         assertTrue(typeInfo.getTypeName().equals("nonNegativeInteger"));
 351         assertTrue(typeInfo.getTypeNamespace().equals(W3C_XML_SCHEMA_NS_URI));
 352 
 353         Element role = (Element)document.getElementsByTagNameNS(PORTAL_ACCOUNT_NS, "Role").item(0);
 354         TypeInfo roletypeInfo = role.getSchemaTypeInfo();
 355         assertTrue(roletypeInfo.getTypeName().equals("BuyOrSell"));
   1 /*
   2  * Copyright (c) 2003, 2016, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  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 javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI;
  26 import static jaxp.library.JAXPTestUtilities.bomStream;
  27 import static org.testng.Assert.assertEquals;
  28 import static org.testng.Assert.assertFalse;
  29 import static org.testng.Assert.assertTrue;
  30 import static test.auctionportal.HiBidConstants.JAXP_SCHEMA_LANGUAGE;
  31 import static test.auctionportal.HiBidConstants.JAXP_SCHEMA_SOURCE;
  32 import static test.auctionportal.HiBidConstants.PORTAL_ACCOUNT_NS;
  33 import static test.auctionportal.HiBidConstants.XML_DIR;
  34 
  35 import java.io.File;
  36 import java.io.FileInputStream;
  37 import java.io.InputStream;
  38 import java.math.BigInteger;
  39 import java.nio.file.Paths;
  40 import java.util.GregorianCalendar;
  41 
  42 import javax.xml.datatype.DatatypeConstants;
  43 import javax.xml.datatype.DatatypeFactory;
  44 import javax.xml.datatype.Duration;
  45 import javax.xml.parsers.DocumentBuilder;
  46 import javax.xml.parsers.DocumentBuilderFactory;
  47 import javax.xml.parsers.SAXParser;
  48 import javax.xml.parsers.SAXParserFactory;
  49 import javax.xml.transform.dom.DOMResult;
  50 import javax.xml.transform.dom.DOMSource;
  51 import javax.xml.validation.Schema;
  52 import javax.xml.validation.SchemaFactory;
  53 import javax.xml.validation.Validator;
  54 
  55 import org.testng.annotations.Listeners;
  56 import org.testng.annotations.Test;
  57 import org.w3c.dom.Attr;
  58 import org.w3c.dom.DOMConfiguration;
  59 import org.w3c.dom.Document;
  60 import org.w3c.dom.Element;
  61 import org.w3c.dom.NodeList;
  62 import org.w3c.dom.TypeInfo;
  63 import org.w3c.dom.bootstrap.DOMImplementationRegistry;
  64 import org.w3c.dom.ls.DOMImplementationLS;
  65 import org.w3c.dom.ls.LSSerializer;


  66 
  67 /**
  68  * This is the user controller  class for the Auction portal HiBid.com.
  69  */
  70 @Listeners({jaxp.library.FilePolicy.class})
  71 public class AuctionController {
  72     /**
  73      * Check for DOMErrorHandler handling DOMError. Before fix of bug 4890927
  74      * DOMConfiguration.setParameter("well-formed",true) throws an exception.
  75      *
  76      * @throws Exception If any errors occur.
  77      */
  78     @Test
  79     public void testCreateNewItem2Sell() throws Exception {
  80         String xmlFile = XML_DIR + "novelsInvalid.xml";
  81 
  82         Document document = DocumentBuilderFactory.newInstance()
  83                 .newDocumentBuilder().parse(xmlFile);
  84 
  85         document.getDomConfig().setParameter("well-formed", true);
  86 
  87         DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
  88         DOMImplementationLS impl = (DOMImplementationLS) registry.getDOMImplementation("LS");
  89         MyDOMOutput domOutput = new MyDOMOutput();
  90         domOutput.setByteStream(System.out);
  91         LSSerializer writer = impl.createLSSerializer();
  92         writer.write(document, domOutput);
  93     }
  94 
  95     /**
  96      * Check for DOMErrorHandler handling DOMError. Before fix of bug 4896132
  97      * test throws DOM Level 1 node error.
  98      *
  99      * @throws Exception If any errors occur.
 100      */
 101     @Test
 102     public void testCreateNewItem2SellRetry() throws Exception  {
 103         String xmlFile = XML_DIR + "accountInfo.xml";
 104 
 105         DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
 106         dbf.setNamespaceAware(true);
 107         Document document = dbf.newDocumentBuilder().parse(xmlFile);
 108 
 109         DOMConfiguration domConfig = document.getDomConfig();
 110         MyDOMErrorHandler errHandler = new MyDOMErrorHandler();
 111         domConfig.setParameter("error-handler", errHandler);
 112 
 113         DOMImplementationLS impl =
 114              (DOMImplementationLS) DOMImplementationRegistry.newInstance()
 115                      .getDOMImplementation("LS");
 116         LSSerializer writer = impl.createLSSerializer();
 117         MyDOMOutput domoutput = new MyDOMOutput();
 118 
 119         domoutput.setByteStream(System.out);
 120         writer.write(document, domoutput);
 121 
 122         document.normalizeDocument();
 123         writer.write(document, domoutput);
 124         assertFalse(errHandler.isError());
 125     }
 126 
 127     /**
 128      * Check if setting the attribute to be of type ID works. This will affect
 129      * the Attr.isID method according to the spec.
 130      *
 131      * @throws Exception If any errors occur.
 132      */
 133     @Test
 134     public void testCreateID() throws Exception {
 135         String xmlFile = XML_DIR + "accountInfo.xml";
 136 
 137         DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
 138         dbf.setNamespaceAware(true);
 139 
 140         Document document = dbf.newDocumentBuilder().parse(xmlFile);
 141         Element account = (Element)document
 142             .getElementsByTagNameNS(PORTAL_ACCOUNT_NS, "Account").item(0);
 143 
 144         account.setIdAttributeNS(PORTAL_ACCOUNT_NS, "accountID", true);
 145         Attr aID = account.getAttributeNodeNS(PORTAL_ACCOUNT_NS, "accountID");
 146         assertTrue(aID.isId());
 147     }
 148 
 149     /**
 150      * Check the user data on the node.
 151      *
 152      * @throws Exception If any errors occur.
 153      */
 154     @Test
 155     public void testCheckingUserData() throws Exception {
 156         String xmlFile = XML_DIR + "accountInfo.xml";
 157 
 158         DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
 159         dbf.setNamespaceAware(true);
 160 
 161         DocumentBuilder docBuilder = dbf.newDocumentBuilder();
 162         Document document = docBuilder.parse(xmlFile);
 163 
 164         Element account = (Element)document.getElementsByTagNameNS(PORTAL_ACCOUNT_NS, "Account").item(0);
 165         assertEquals(account.getNodeName(), "acc:Account");
 166         Element firstName = (Element) document.getElementsByTagNameNS(PORTAL_ACCOUNT_NS, "FirstName").item(0);
 167         assertEquals(firstName.getNodeName(), "FirstName");
 168 
 169         Document doc1 = docBuilder.newDocument();
 170         Element someName = doc1.createElement("newelem");
 171 
 172         someName.setUserData("mykey", "dd",
 173             (operation, key,  data, src,  dst) ->  {
 174                 System.err.println("In UserDataHandler" + key);
 175                 System.out.println("In UserDataHandler");
 176             });
 177         Element impAccount = (Element)document.importNode(someName, true);
 178         assertEquals(impAccount.getNodeName(), "newelem");
 179         document.normalizeDocument();
 180         String data = (someName.getUserData("mykey")).toString();
 181         assertEquals(data, "dd");
 182     }
 183 
 184 
 185     /**
 186      * Check the UTF-16 XMLEncoding xml file.
 187      *
 188      * @throws Exception If any errors occur.
 189      * @see <a href="content/movies.xml">movies.xml</a>
 190      */
 191     @Test
 192     public void testCheckingEncoding() throws Exception {
 193         // Note since movies.xml is UTF-16 encoding. We're not using stanard XML
 194         // file suffix.
 195         String xmlFile = XML_DIR + "movies.xml.data";
 196 
 197         try (InputStream source = bomStream("UTF-16", xmlFile)) {
 198             DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
 199             dbf.setNamespaceAware(true);
 200             Document document = dbf.newDocumentBuilder().parse(source);
 201             assertEquals(document.getXmlEncoding(), "UTF-16");
 202             assertEquals(document.getXmlStandalone(), true);
 203         }
 204     }
 205 
 206     /**
 207      * Check validation API features. A schema which is including in Bug 4909119
 208      * used to be testing for the functionalities.
 209      *
 210      * @throws Exception If any errors occur.
 211      * @see <a href="content/userDetails.xsd">userDetails.xsd</a>
 212      */
 213     @Test
 214     public void testGetOwnerInfo() throws Exception {
 215         String schemaFile = XML_DIR + "userDetails.xsd";
 216         String xmlFile = XML_DIR + "userDetails.xml";
 217 
 218         try(FileInputStream fis = new FileInputStream(xmlFile)) {
 219             DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
 220             dbf.setNamespaceAware(true);
 221             dbf.setAttribute(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA_NS_URI);
 222 
 223             SchemaFactory schemaFactory = SchemaFactory.newInstance(W3C_XML_SCHEMA_NS_URI);
 224             Schema schema = schemaFactory.newSchema(Paths.get(schemaFile).toFile());
 225 
 226             Validator validator = schema.newValidator();
 227             MyErrorHandler eh = new MyErrorHandler();
 228             validator.setErrorHandler(eh);
 229 
 230             DocumentBuilder docBuilder = dbf.newDocumentBuilder();
 231             docBuilder.setErrorHandler(eh);
 232 
 233             Document document = docBuilder.parse(fis);
 234             DOMResult dResult = new DOMResult();
 235             DOMSource domSource = new DOMSource(document);
 236             validator.validate(domSource, dResult);
 237             assertFalse(eh.isAnyError());
 238         }
 239     }
 240 
 241     /**
 242      * Check grammar caching with imported schemas.
 243      *
 244      * @throws Exception If any errors occur.
 245      * @see <a href="content/coins.xsd">coins.xsd</a>
 246      * @see <a href="content/coinsImportMe.xsd">coinsImportMe.xsd</a>
 247      */
 248     @Test
 249     public void testGetOwnerItemList() throws Exception {
 250         String xsdFile = XML_DIR + "coins.xsd";
 251         String xmlFile = XML_DIR + "coins.xml";
 252 
 253         try(FileInputStream fis = new FileInputStream(xmlFile)) {
 254             DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
 255             dbf.setNamespaceAware(true);
 256             dbf.setAttribute(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA_NS_URI);
 257             dbf.setValidating(false);
 258 
 259             SchemaFactory schemaFactory = SchemaFactory.newInstance(W3C_XML_SCHEMA_NS_URI);
 260             Schema schema = schemaFactory.newSchema(new File(((xsdFile))));
 261 
 262             MyErrorHandler eh = new MyErrorHandler();
 263             Validator validator = schema.newValidator();
 264             validator.setErrorHandler(eh);
 265 
 266             DocumentBuilder docBuilder = dbf.newDocumentBuilder();
 267             Document document = docBuilder.parse(fis);
 268             validator.validate(new DOMSource(document), new DOMResult());
 269             assertFalse(eh.isAnyError());
 270         }
 271     }
 272 
 273 
 274     /**
 275      * Check for the same imported schemas but will use SAXParserFactory and try
 276      * parsing using the SAXParser. SCHEMA_SOURCE attribute is using for this
 277      * test.
 278      *
 279      * @throws Exception If any errors occur.
 280      * @see <a href="content/coins.xsd">coins.xsd</a>
 281      * @see <a href="content/coinsImportMe.xsd">coinsImportMe.xsd</a>
 282      */
 283 
 284     @Test
 285     public void testGetOwnerItemList1() throws Exception {
 286         String xsdFile = XML_DIR + "coins.xsd";
 287         String xmlFile = XML_DIR + "coins.xml";
 288         SAXParserFactory spf = SAXParserFactory.newInstance();
 289         spf.setNamespaceAware(true);
 290         spf.setValidating(true);
 291 
 292         SAXParser sp = spf.newSAXParser();
 293         sp.setProperty(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA_NS_URI);
 294         sp.setProperty(JAXP_SCHEMA_SOURCE, xsdFile);
 295 
 296         MyErrorHandler eh = new MyErrorHandler();
 297         sp.parse(new File(xmlFile), eh);
 298         assertFalse(eh.isAnyError());
 299     }
 300 
 301     /**
 302      * Check usage of javax.xml.datatype.Duration class.
 303      *
 304      * @throws Exception If any errors occur.
 305      */
 306     @Test
 307     public void testGetItemDuration() throws Exception {
 308         String xmlFile = XML_DIR + "itemsDuration.xml";
 309 
 310         DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
 311         dbf.setNamespaceAware(true);
 312         Document document = dbf.newDocumentBuilder().parse(xmlFile);
 313 
 314         Element durationElement = (Element) document.getElementsByTagName("sellDuration").item(0);
 315 
 316         NodeList childList = durationElement.getChildNodes();
 317 
 318         for (int i = 0; i < childList.getLength(); i++) {
 319             System.out.println("child " + i + childList.item(i));
 320         }
 321 
 322         Duration duration = DatatypeFactory.newInstance().newDuration("P365D");
 323         Duration sellDuration = DatatypeFactory.newInstance().newDuration(childList.item(0).getNodeValue());
 324         assertFalse(sellDuration.isShorterThan(duration));
 325         assertFalse(sellDuration.isLongerThan(duration));
 326         assertEquals(sellDuration.getField(DatatypeConstants.DAYS), BigInteger.valueOf(365));
 327         assertEquals(sellDuration.normalizeWith(new GregorianCalendar(1999, 2, 22)), duration);
 328 
 329         Duration myDuration = sellDuration.add(duration);
 330         assertEquals(myDuration.normalizeWith(new GregorianCalendar(2003, 2, 22)),
 331                 DatatypeFactory.newInstance().newDuration("P730D"));
 332     }
 333 
 334     /**
 335      * Check usage of TypeInfo interface introduced in DOM L3.
 336      *
 337      * @throws Exception If any errors occur.
 338      */
 339     @Test
 340     public void testGetTypeInfo() throws Exception {
 341         String xmlFile = XML_DIR + "accountInfo.xml";
 342 
 343         DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
 344         dbf.setNamespaceAware(true);
 345         dbf.setValidating(true);
 346         dbf.setAttribute(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA_NS_URI);
 347 
 348         DocumentBuilder docBuilder = dbf.newDocumentBuilder();
 349         docBuilder.setErrorHandler(new MyErrorHandler());
 350 
 351         Document document = docBuilder.parse(xmlFile);
 352         Element userId = (Element)document.getElementsByTagNameNS(PORTAL_ACCOUNT_NS, "UserID").item(0);
 353         TypeInfo typeInfo = userId.getSchemaTypeInfo();
 354         assertTrue(typeInfo.getTypeName().equals("nonNegativeInteger"));
 355         assertTrue(typeInfo.getTypeNamespace().equals(W3C_XML_SCHEMA_NS_URI));
 356 
 357         Element role = (Element)document.getElementsByTagNameNS(PORTAL_ACCOUNT_NS, "Role").item(0);
 358         TypeInfo roletypeInfo = role.getSchemaTypeInfo();
 359         assertTrue(roletypeInfo.getTypeName().equals("BuyOrSell"));
< prev index next >