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.USER_DIR;
  27 import static jaxp.library.JAXPTestUtilities.compareDocumentWithGold;
  28 import static org.testng.Assert.assertEquals;
  29 import static org.testng.Assert.assertFalse;
  30 import static org.testng.Assert.assertTrue;
  31 import static test.auctionportal.HiBidConstants.GOLDEN_DIR;
  32 import static test.auctionportal.HiBidConstants.JAXP_SCHEMA_LANGUAGE;
  33 import static test.auctionportal.HiBidConstants.PORTAL_ACCOUNT_NS;
  34 import static test.auctionportal.HiBidConstants.XML_DIR;
  35 
  36 import java.io.FileOutputStream;
  37 import java.nio.file.Files;
  38 import java.nio.file.Paths;
  39 import java.nio.file.StandardCopyOption;
  40 
  41 import javax.xml.parsers.DocumentBuilder;
  42 import javax.xml.parsers.DocumentBuilderFactory;
  43 
  44 import org.testng.annotations.Listeners;
  45 import org.testng.annotations.Test;
  46 import org.w3c.dom.Attr;
  47 import org.w3c.dom.Document;
  48 import org.w3c.dom.Element;
  49 import org.w3c.dom.NodeList;
  50 import org.w3c.dom.Text;
  51 import org.w3c.dom.bootstrap.DOMImplementationRegistry;
  52 import org.w3c.dom.ls.DOMImplementationLS;
  53 import org.w3c.dom.ls.LSParser;
  54 import org.w3c.dom.ls.LSSerializer;
  55 
  56 /**
  57  * This is the user controller class for the Auction portal HiBid.com.
  58  */
  59 @Listeners({jaxp.library.FilePolicy.class})
  60 public class UserController {
  61     /**
  62      * Checking when creating an XML document using DOM Level 2 validating
  63      * it without having a schema source or a schema location It must throw a
  64      * sax parse exception.
  65      *
  66      * @throws Exception If any errors occur.
  67      */
  68     @Test
  69     public void testCreateNewUser() throws Exception {
  70         String resultFile = USER_DIR + "accountInfoOut.xml";
  71         DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  72         dbf.setNamespaceAware(true);
  73         dbf.setValidating(true);
  74 
  75         DocumentBuilder docBuilder = dbf.newDocumentBuilder();
  76         MyErrorHandler eh = new MyErrorHandler();
  77         docBuilder.setErrorHandler(eh);
  78 
  79         Document document = docBuilder.newDocument();
  80 
  81         Element account = document.createElementNS(PORTAL_ACCOUNT_NS, "acc:Account");
  82         Attr accountID = document.createAttributeNS(PORTAL_ACCOUNT_NS, "acc:accountID");
  83         account.setAttributeNode(accountID);
  84 
  85         account.appendChild(document.createElement("FirstName"));
  86         account.appendChild(document.createElementNS(PORTAL_ACCOUNT_NS, "acc:LastName"));
  87         account.appendChild(document.createElement("UserID"));
  88 
  89         DOMImplementationLS impl
  90                 = (DOMImplementationLS) DOMImplementationRegistry
  91                         .newInstance().getDOMImplementation("LS");
  92         LSSerializer writer = impl.createLSSerializer();
  93         LSParser builder = impl.createLSParser(DOMImplementationLS.MODE_SYNCHRONOUS, null);
  94         try(FileOutputStream output = new FileOutputStream(resultFile)) {
  95             MyDOMOutput domOutput = new MyDOMOutput();
  96             domOutput.setByteStream(output);
  97             writer.write(account, domOutput);
  98             docBuilder.parse(resultFile);
  99         }
 100         assertTrue(eh.isAnyError());
 101     }
 102 
 103     /**
 104      * Checking conflicting namespaces and use renameNode and normalizeDocument.
 105      * @see <a href="content/accountInfo.xml">accountInfo.xml</a>
 106      *
 107      * @throws Exception If any errors occur.
 108      */
 109     @Test
 110     public void testAddUser() throws Exception {
 111         String resultFile = USER_DIR + "accountRole.out";
 112         String xmlFile = XML_DIR + "accountInfo.xml";
 113 
 114         // Copy schema for outputfile
 115         Files.copy(Paths.get(XML_DIR, "accountInfo.xsd"),
 116                 Paths.get(USER_DIR, "accountInfo.xsd"),
 117                 StandardCopyOption.REPLACE_EXISTING);
 118         MyErrorHandler eh = new MyErrorHandler();
 119         DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
 120 
 121         dbf.setAttribute(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA_NS_URI);
 122         dbf.setNamespaceAware(true);
 123         dbf.setValidating(true);
 124 
 125         DocumentBuilder docBuilder = dbf.newDocumentBuilder();
 126         docBuilder.setErrorHandler(eh);
 127 
 128         Document document = docBuilder.parse(xmlFile);
 129         Element sell = (Element) document.getElementsByTagNameNS(PORTAL_ACCOUNT_NS, "Sell").item(0);
 130         Element role = (Element) sell.getParentNode();
 131 
 132         Element buy = (Element) document.renameNode(sell, PORTAL_ACCOUNT_NS, "acc:Buy");
 133         role.appendChild(buy);
 134 
 135         DOMImplementationLS impl
 136                 = (DOMImplementationLS) DOMImplementationRegistry
 137                         .newInstance().getDOMImplementation("LS");
 138         LSSerializer writer = impl.createLSSerializer();
 139 
 140 
 141         try(FileOutputStream output = new FileOutputStream(resultFile)) {
 142             MyDOMOutput mydomoutput = new MyDOMOutput();
 143             mydomoutput.setByteStream(output);
 144             writer.write(document, mydomoutput);
 145         }
 146 
 147         docBuilder.parse(resultFile);
 148         assertFalse(eh.isAnyError());
 149     }
 150 
 151     /**
 152      * Checking Text content in XML file.
 153      * @see <a href="content/accountInfo.xml">accountInfo.xml</a>
 154      *
 155      * @throws Exception If any errors occur.
 156      */
 157     @Test
 158     public void testMoreUserInfo() throws Exception {
 159         String xmlFile = XML_DIR + "accountInfo.xml";
 160         DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
 161 
 162         dbf.setAttribute(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA_NS_URI);
 163         dbf.setNamespaceAware(true);
 164         dbf.setValidating(true);
 165 
 166         DocumentBuilder docBuilder = dbf.newDocumentBuilder();
 167         MyErrorHandler eh = new MyErrorHandler();
 168         docBuilder.setErrorHandler(eh);
 169 
 170         Document document = docBuilder.parse(xmlFile);
 171         Element account = (Element)document
 172                 .getElementsByTagNameNS(PORTAL_ACCOUNT_NS, "Account").item(0);
 173         String textContent = account.getTextContent();
 174         assertTrue(textContent.trim().regionMatches(0, "Rachel", 0, 6));
 175         assertEquals(textContent, "RachelGreen744");
 176 
 177         Attr accountID = account.getAttributeNodeNS(PORTAL_ACCOUNT_NS, "accountID");
 178         assertTrue(accountID.getTextContent().trim().equals("1"));
 179 
 180         assertFalse(eh.isAnyError());
 181     }
 182 
 183     /**
 184      * This will check if adoptNode works will adoptNode from
 185      * @see <a href="content/userInfo.xml">userInfo.xml</a>
 186      * @see <a href="content/accountInfo.xml">accountInfo.xml</a>. This is
 187      * adopting a node from the XML file which is validated by a DTD and
 188      * into an XML file which is validated by the schema This covers Row 5
 189      * for the table
 190      * http://javaweb.sfbay/~jsuttor/JSR206/jsr-206-html/ch03s05.html. Filed
 191      * bug 4893745 because there was a difference in behavior.
 192      *
 193      * @throws Exception If any errors occur.
 194      */
 195     @Test
 196     public void testCreateUserAccount() throws Exception {
 197         String userXmlFile = XML_DIR + "userInfo.xml";
 198         String accountXmlFile = XML_DIR + "accountInfo.xml";
 199         DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
 200         dbf.setNamespaceAware(true);
 201         dbf.setValidating(true);
 202 
 203         DocumentBuilder docBuilder = dbf.newDocumentBuilder();
 204         MyErrorHandler eh = new MyErrorHandler();
 205         docBuilder.setErrorHandler(eh);
 206 
 207         Document document = docBuilder.parse(userXmlFile);
 208         Element user = (Element) document.getElementsByTagName("FirstName").item(0);
 209         // Set schema after parsing userInfo.xml. Otherwise it will conflict
 210         // with DTD validation.
 211         dbf.setAttribute(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA_NS_URI);
 212         DocumentBuilder docBuilder1 = dbf.newDocumentBuilder();
 213         docBuilder1.setErrorHandler(eh);
 214         Document accDocument = docBuilder1.parse(accountXmlFile);
 215 
 216         Element firstName = (Element) accDocument
 217                 .getElementsByTagNameNS(PORTAL_ACCOUNT_NS, "FirstName").item(0);
 218         Element adoptedAccount = (Element) accDocument.adoptNode(user);
 219 
 220         Element parent = (Element) firstName.getParentNode();
 221         parent.replaceChild(adoptedAccount, firstName);
 222 
 223         DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
 224         DOMImplementationLS impl = (DOMImplementationLS) registry.getDOMImplementation("LS");
 225         LSSerializer writer = impl.createLSSerializer();
 226 
 227         MyDOMOutput mydomoutput = new MyDOMOutput();
 228         mydomoutput.setByteStream(System.out);
 229 
 230         writer.write(document, mydomoutput);
 231         writer.write(accDocument, mydomoutput);
 232 
 233         assertFalse(eh.isAnyError());
 234     }
 235 
 236     /**
 237      * Checking for Row 8 from the schema table when setting the schemaSource
 238      * without the schemaLanguage must report an error.
 239      *
 240      * @throws Exception If any errors occur.
 241      */
 242     @Test(expectedExceptions = IllegalArgumentException.class)
 243     public void testUserError() throws Exception {
 244         String xmlFile = XML_DIR + "userInfo.xml";
 245         String schema = "http://java.sun.com/xml/jaxp/properties/schemaSource";
 246         String schemaValue = "http://dummy.com/dummy.xsd";
 247 
 248         DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
 249         dbf.setNamespaceAware(true);
 250         dbf.setValidating(true);
 251         dbf.setAttribute(schema, schemaValue);
 252 
 253         DocumentBuilder docBuilder = dbf.newDocumentBuilder();
 254         MyErrorHandler eh = new MyErrorHandler();
 255         docBuilder.setErrorHandler(eh);
 256         docBuilder.parse(xmlFile);
 257         assertFalse(eh.isAnyError());
 258     }
 259 
 260     /**
 261      * Checking for namespace normalization.
 262      * @see <a href="content/screenName.xml">screenName.xml</a> has prefix of
 263      * userName is bound to "http://hibid.com/user" namespace normalization
 264      * will create a namespace of prefix us and attach userEmail.
 265      *
 266      * @throws Exception If any errors occur.
 267      */
 268     @Test
 269     public void testCheckScreenNameExists() throws Exception {
 270         String resultFile = USER_DIR + "screenName.out";
 271         String xmlFile = XML_DIR + "screenName.xml";
 272         String goldFile = GOLDEN_DIR + "screenNameGold.xml";
 273 
 274         String nsTagName = "http://hibid.com/screenName";
 275         String userNs = "http://hibid.com/user";
 276 
 277         try (FileOutputStream output = new FileOutputStream(resultFile)) {
 278             DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
 279             DOMImplementationLS impl = (DOMImplementationLS) registry.getDOMImplementation("LS");
 280             LSSerializer writer = impl.createLSSerializer();
 281             LSParser builder = impl.createLSParser(DOMImplementationLS.MODE_SYNCHRONOUS, null);
 282             Document document = builder.parseURI(xmlFile);
 283             NodeList nl = document.getElementsByTagNameNS(nsTagName, "screen-name");
 284             assertEquals(nl.getLength(), 1);
 285             Element screenName = (Element)nl.item(0);
 286             Element userEmail = document.createElementNS(userNs, "userEmail");
 287             assertTrue(userEmail.isDefaultNamespace(userNs));
 288 
 289             Text email = document.createTextNode("myid@hibid.com");
 290             userEmail.appendChild(email);
 291             screenName.appendChild(userEmail);
 292             document.normalizeDocument();
 293 
 294             MyDOMOutput domoutput = new MyDOMOutput();
 295             domoutput.setByteStream(output);
 296             writer.write(document, domoutput);
 297         }
 298         assertTrue(compareDocumentWithGold(goldFile, resultFile));
 299     }
 300 }